我是TensorFlow的新手。我搞不懂tf的区别。占位符和tf.Variable。在我看来,tf。占位符用于输入数据,tf。变量用于存储数据的状态。这就是我所知道的一切。
谁能给我详细解释一下他们的不同之处吗?特别是,什么时候使用tf。变量和何时使用tf.placeholder?
我是TensorFlow的新手。我搞不懂tf的区别。占位符和tf.Variable。在我看来,tf。占位符用于输入数据,tf。变量用于存储数据的状态。这就是我所知道的一切。
谁能给我详细解释一下他们的不同之处吗?特别是,什么时候使用tf。变量和何时使用tf.placeholder?
当前回答
简而言之,使用tf。变量为可训练变量,如权重(W)和偏差(B)为您的模型。
weights = tf.Variable(
tf.truncated_normal([IMAGE_PIXELS, hidden1_units],
stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))), name='weights')
biases = tf.Variable(tf.zeros([hidden1_units]), name='biases')
特遣部队。占位符用于提供实际的训练示例。
images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, IMAGE_PIXELS))
labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size))
这是你在训练中输入训练示例的方式:
for step in xrange(FLAGS.max_steps):
feed_dict = {
images_placeholder: images_feed,
labels_placeholder: labels_feed,
}
_, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)
你的助教。变量将被训练(修改)作为这个训练的结果。
详见https://www.tensorflow.org/versions/r0.7/tutorials/mnist/tf/index.html。(例子摘自网页。)
其他回答
博士TL;
变量
为了学习参数 价值观可以从培训中获得 初始值是必需的(通常是随机的)
占位符
为数据分配存储(例如在馈送期间用于图像像素数据) 初始值不是必需的(但可以设置,参见tf.placeholder_with_default)
占位符:
A placeholder is simply a variable that we will assign data to at a later date. It allows us to create our operations and build our computation graph, without needing the data. In TensorFlow terminology, we then feed data into the graph through these placeholders. Initial values are not required but can have default values with tf.placeholder_with_default) We have to provide value at runtime like : a = tf.placeholder(tf.int16) // initialize placeholder value b = tf.placeholder(tf.int16) // initialize placeholder value use it using session like : sess.run(add, feed_dict={a: 2, b: 3}) // this value we have to assign at runtime
变量:
TensorFlow变量是表示共享的最佳方式, 由程序操纵的持久状态。 变量是通过tf操作的。变量类。一个特遣部队。变量 表示一个张量,其值可以通过对其运行操作来改变。
例如:tf。变量("欢迎来到tensorflow!! ")
除了其他人的答案,他们在Tensoflow网站上的MNIST教程中也解释得很好:
We describe these interacting operations by manipulating symbolic variables. Let's create one: x = tf.placeholder(tf.float32, [None, 784]), x isn't a specific value. It's a placeholder, a value that we'll input when we ask TensorFlow to run a computation. We want to be able to input any number of MNIST images, each flattened into a 784-dimensional vector. We represent this as a 2-D tensor of floating-point numbers, with a shape [None, 784]. (Here None means that a dimension can be of any length.) We also need the weights and biases for our model. We could imagine treating these like additional inputs, but TensorFlow has an even better way to handle it: Variable. A Variable is a modifiable tensor that lives in TensorFlow's graph of interacting operations. It can be used and even modified by the computation. For machine learning applications, one generally has the model parameters be Variables. W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) We create these Variables by giving tf.Variable the initial value of the Variable: in this case, we initialize both W and b as tensors full of zeros. Since we are going to learn W and b, it doesn't matter very much what they initially are.
对于TF V1:
常数是有初始值的,在计算中不会发生变化; 变量具有初值,在计算中可以变化;(对于参数来说很好) 占位符没有初始值,在计算中不会改变。(非常适合像预测实例这样的输入)
对于TF V2,同样,但他们试图隐藏占位符(图形模式不是首选)。
示例代码片段:
import numpy as np
import tensorflow as tf
### Model parameters ###
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
### Model input and output ###
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
### loss ###
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
### optimizer ###
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
### training data ###
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
### training loop ###
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
sess.run(train, {x:x_train, y:y_train})
顾名思义,占位符是稍后提供一个值的承诺。
变量只是训练参数(W(矩阵),b(偏差),与您在日常编程中使用的正常变量相同,培训师在每次运行/步骤中更新/修改。
虽然占位符不需要任何初始值,当你创建x和y时,TF不分配任何内存,相反,当你在sesss .run()中使用feed_dict提供占位符时,TensorFlow将为它们分配适当大小的内存(x和y) -这种不受约束的特性允许我们提供任何大小和形状的数据。
简而言之:
Variable -是一个你希望训练器(例如GradientDescentOptimizer)在每一步之后更新的参数。
占位符演示-
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b # + provides a shortcut for tf.add(a, b)
执行:
print(sess.run(adder_node, {a: 3, b:4.5}))
print(sess.run(adder_node, {a: [1,3], b: [2, 4]}))
结果是输出
7.5
[ 3. 7.]
在第一种情况下,3和4.5将分别传递给a和b,然后传递给adder_node输出7。在第二种情况下,有一个提要列表,第一步1和2将被添加,接下来的3和4 (a和b)。
相关阅读:
特遣部队。占位符doc。 特遣部队。变量doc。 变量VS占位符。