同时从图中请求多个值

Requesting multiple values from graph at same time

在下面的代码中,l2 令人惊讶地 returns 与 l1 相同的值,但是由于在 l2 之前的列表中请求优化器,我预计损失是训练后的新损失。我不能同时从图中请求多个值并期望一致的输出吗?

import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.float32, shape=[None, 10])
y = tf.placeholder(tf.float32, shape=[None, 2])

weight = tf.Variable(tf.random_uniform((10, 2), dtype=tf.float32))

loss = tf.nn.sigmoid_cross_entropy_with_logits(tf.matmul(x, weight), y)

optimizer = tf.train.AdamOptimizer(0.1).minimize(loss)

with tf.Session() as sess:
    tf.initialize_all_variables().run()

    X = np.random.rand(1, 10)
    Y = np.array([[0, 1]])

    # Evaluate loss before running training step
    l1 = sess.run([loss], feed_dict={x: X, y: Y})[0][0][0]
    print(l1) # 3.32393

    # Running the training step
    _, l2 = sess.run([optimizer, loss], feed_dict={x: X, y: Y})
    print(l2[0][0]) # 3.32393 -- didn't change?

    # Evaluate loss again after training step as sanity check
    l3 = sess.run([loss], feed_dict={x: X, y: Y})[0][0][0]
    print(l3) # 2.71041

否 - 您在列表中请求它们的顺序对评估顺序没有影响。对于优化器等具有副作用的操作,如果要保证特定的顺序,则需要使用 with_dependencies 或类似的控制流结构来强制执行。通常,忽略副作用,TensorFlow 将通过在计算后立即从图中抓取节点来为您提供 return 结果 - 显然,损失是在 之前 计算的优化器,因为优化器需要损失作为其输入之一。 (请记住 'loss' 不是变量;它是张量;因此它实际上不受优化器步骤的影响。)

sess.run([loss, optimizer], ...)

sess.run([optimizer, loss], ...)

是等价的。

作为 Dave , the order of arguments to Session.run() has no effect on the order of evaluation, and the loss tensor in your example does not have a dependency on the optimizer op. To add a dependency, you could use tf.control_dependencies() 在获取损失之前添加对优化器 运行 的显式依赖:

with tf.control_dependencies([optimizer]):
    loss_after_optimizer = tf.identity(loss)

_, l2 = sess.run([optimizer, loss_after_optimizer], feed_dict={x: X, y: Y})

我用session.run的三种方式测试了在tensorflow中实现的逻辑回归:

  1. 一起

    res1, res2, res3 = sess.run([op1, op2, op3])

  2. 分别

    res1 = sess.run(op1)

    res2 = sess.run(op2)

    res3 = sess.run(op3)

  3. 具有依赖关系

    with tf.control_dependencies([op1]):

    op2_after = tf.identity(op1)

    op3_after = tf.identity(op1)

    res1,res2,res3 = session.run([op1, op2_after, op3_after])

设置批量大小为10000,结果为:

1: 0.05+ secs < 2: 0.11+ secs < 3: 0.25+ secs

1 和 3 之间的主要区别只是一个小批量。使用 3 而不是 1 可能不值得。

这里是测试代码(是别人写的LR例子。。。)

这是data

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Jun  2 13:38:14 2017

@author: inse7en
"""

from __future__ import print_function
import numpy as np
import tensorflow as tf
from six.moves import cPickle as pickle
import time

pickle_file = '/Users/inse7en/Downloads/notMNIST.pickle'
with open(pickle_file, 'rb') as f:
  save = pickle.load(f)
  train_dataset = save['train_dataset']
  train_labels = save['train_labels']
  valid_dataset = save['valid_dataset']
  valid_labels = save['valid_labels']
  test_dataset = save['test_dataset']
  test_labels = save['test_labels']
  del save  # hint to help gc free up memory
  print('Training set', train_dataset.shape, train_labels.shape)
  print('Validation set', valid_dataset.shape, valid_labels.shape)
  print('Test set', test_dataset.shape, test_labels.shape)


image_size = 28
num_labels = 10

def reformat(dataset, labels):
  dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)
  # Map 2 to [0.0, 1.0, 0.0 ...], 3 to [0.0, 0.0, 1.0 ...]
  labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
  return dataset, labels
train_dataset, train_labels = reformat(train_dataset, train_labels)
valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)
test_dataset, test_labels = reformat(test_dataset, test_labels)
print('Training set', train_dataset.shape, train_labels.shape)
print('Validation set', valid_dataset.shape, valid_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)

# This is to expedite the process
train_subset = 10000
# This is a good beta value to start with
beta = 0.01

graph = tf.Graph()
with graph.as_default():
    # Input data.
    # They're all constants.
    tf_train_dataset = tf.constant(train_dataset[:train_subset, :])
    tf_train_labels = tf.constant(train_labels[:train_subset])
    tf_valid_dataset = tf.constant(valid_dataset)
    tf_test_dataset = tf.constant(test_dataset)

    # Variables
    # They are variables we want to update and optimize.
    weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels]))
    biases = tf.Variable(tf.zeros([num_labels]))

    # Training computation.
    logits = tf.matmul(tf_train_dataset, weights) + biases
    # Original loss function
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))
    # Loss function using L2 Regularization
    regularizer = tf.nn.l2_loss(weights)
    loss = tf.reduce_mean(loss + beta * regularizer)

    # Optimizer.
    optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

    # Predictions for the training, validation, and test data.
    train_prediction = tf.nn.softmax(logits)
    valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases)
    test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases)

    num_steps = 50


    def accuracy(predictions, labels):
        return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
                / predictions.shape[0])


    with tf.Session(graph=graph) as session:
        # This is a one-time operation which ensures the parameters get initialized as
        # we described in the graph: random weights for the matrix, zeros for the
        # biases.
        tf.initialize_all_variables().run()
        print('Initialized')
        for step in range(num_steps):
            # Run the computations. We tell .run() that we want to run the optimizer,
            # and get the loss value and the training predictions returned as numpy
            # arrays.
            #_, l, predictions = session.run([optimizer, loss, train_prediction])

            start_time = time.time()
            with tf.control_dependencies([optimizer]):
                loss_after_optimizer = tf.identity(loss)
                predictions_after = tf.identity(train_prediction)
                regularizers_after = tf.identity(regularizer)


            _, l, predictions,regularizers = session.run([optimizer, loss_after_optimizer, predictions_after, regularizers_after])

            print("--- with dependencies: %s seconds ---" % (time.time() - start_time))
            #start_time = time.time()
            #opt = session.run(optimizer)
            #l = session.run(loss)
            #predictions = session.run(train_prediction)
            #regularizers = session.run(regularizer)

            #print("--- run separately: %s seconds ---" % (time.time() - start_time))

            #start_time = time.time()
            #_, l, predictions,regularizers = session.run([optimizer, loss, train_prediction, regularizer])

            #print("--- all together: %s seconds ---" % (time.time() - start_time))

            #if (step % 100 == 0):
                #print('Loss at step {}: {}'.format(step, l))
                #print('Training accuracy: {:.1f}'.format(accuracy(predictions,
                                                                  #train_labels[:train_subset, :])))
                # Calling .eval() on valid_prediction is basically like calling run(), but
                # just to get that one numpy array. Note that it recomputes all its graph
                # dependencies.

                # You don't have to do .eval above because we already ran the session for the
                # train_prediction
                #print('Validation accuracy: {:.1f}'.format(accuracy(valid_prediction.eval(),
                                                                    #valid_labels)))
        #print('Test accuracy: {:.1f}'.format(accuracy(test_prediction.eval(), test_labels)))
        #print(regularizer)