https://blog.csdn.net/lanchunhui/article/details/61712830
https://www.cnblogs.com/silence-tommy/p/7029561.html
二者的主要區別在於:
-
tf.Variable:主要在於一些可訓練變量(trainable variables),比如模型的權重(weights,W)或者偏執值(bias);
- 聲明時,必須提供初始值;
- 名稱的真實含義,在於變量,也即在真實訓練時,其值是會改變的,自然事先需要指定初始值;
weights = tf.Variable( tf.truncated_normal([IMAGE_PIXELS, hidden1_units], stddev=1./math.sqrt(float(IMAGE_PIXELS)), name='weights') ) biases = tf.Variable(tf.zeros([hidden1_units]), name='biases')- 1
- 2
- 3
- 4
- 5
-
tf.placeholder:用於得到傳遞進來的真實的訓練樣本:
- 不必指定初始值,可在運行時,通過 Session.run 的函數的 feed_dict 參數指定;
- 這也是其命名的原因所在,僅僅作為一種占位符;
images_placeholder = tf.placeholder(tf.float32, shape=[batch_size, IMAGE_PIXELS]) labels_placeholder = tf.placeholder(tf.int32, shape=[batch_size])- 1
- 2
如下則是二者真實的使用場景:
for step in range(FLAGS.max_steps): feed_dict = { images_placeholder = images_feed, labels_placeholder = labels_feed } _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)
- 1
- 2
- 3
- 4
- 5
- 6
當執行這些操作時,tf.Variable 的值將會改變,也即被修改,這也是其名稱的來源(variable,變量)。
What’s the difference between tf.placeholder and tf.Variable
