【发布时间】:2018-07-18 17:16:08
【问题描述】:
下面是小Tensorflow代码
# coding: utf-8
# In[27]:
import tensorflow as tf
# In[28]:
# Model parameters
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# In[29]:
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
# In[30]:
y = tf.placeholder(tf.float32)
# In[31]:
# loss
loss = tf.reduce_sum(tf.square(linear_model - y))
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# In[32]:
# training data
x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]
# In[33]:
# training loop
init = tf.global_variables_initializer()
# In[34]:
with tf.Session() as sess:
sess.run(init)
for i in range(1000):
sess.run(train, {x: x_train, y: y_train})
# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))
# In[ ]:
在for循环中我们有以下代码
with tf.Session() as sess:
sess.run(init)
for i in range(1000):
sess.run(train, {x: x_train, y: y_train})
# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))
我的问题是当我们运行 sess.run(train, {x: x_train, y: y_train}) 时,loss 也会被计算出来,那么为什么我们需要在想要检索如下损失值时传递 feed_dict 呢?谁能帮我理解这个?
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
【问题讨论】:
标签: tensorflow machine-learning deep-learning