【发布时间】:2018-03-16 21:07:03
【问题描述】:
我有一个使用 Tensorflow 编写的简单单变量线性回归模型。
我正在尝试计算此模型的决定系数(R 平方)。
我将R_squared 声明为tf.Variable(我也尝试将其声明为占位符,并将其声明为普通的python 变量)。
R_squared = tf.variable(0,name = 'R_squared')
prediction = tf.add(tf.multiply(X,W),b)
training_cost = tf.reduce_sum(tf.pow(prediction-Y,2))/(2 * n_samples)
unexplained_cost = tf.reduce_sum(tf.square(tf.subtract(Y,prediction)))
R_squared = tf.subtract(1.0, tf.divide(unexplained_cost, training_cost))
稍后在运行优化器后的代码中,我尝试打印出来
R_squared。
print ('R squared = ', tf_session.run(R_squared))
但我总是遇到同样的错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 1327, in _do_call
return fn(*args)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 1306, in _run_fn
status, run_metadata)
File "/usr/lib/python3.4/contextlib.py", line 66, in __exit__
next(self.gen)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./linear_regression.py", line 126, in <module>
print ('R squared = ', tf_session.run(R_squared))
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 895, in run
run_metadata_ptr)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 1124, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 1321, in _do_run
options, run_metadata)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 1340, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'Placeholder', defined at:
File "./linear_regression.py", line 78, in <module>
X = tf.placeholder('float')
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/array_ops.py", line 1548, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 2094, in _placeholder
name=name)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
我也尝试打印出R_squared.eval(),但我仍然遇到同样的错误。
另外,在张量对象上调用eval 方法与将其传递给会话run 方法有什么区别?
任何帮助表示赞赏。
【问题讨论】:
标签: tensorflow