1
我有一個簡單的單變量線性迴歸模型,我使用Tensorflow編寫。獲取嘗試計算R平方的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
方法有什麼區別?
任何幫助表示讚賞。