尝试计算Tensorflow线性回归模型的R Squared时遇到错误

时间:2017-10-05 09:36:55

标签: tensorflow

我有一个简单的单变量线性回归模型,我使用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方法之间的区别是什么?

任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

您已将 X 定义为代码中的占位符。占位符通常为空,除非您为其指定默认值或使用 feed_dict 为其提供值。

例如,尝试使用:

tf_session.run(R_squared, feed_dict={X: 1})

您显然可以将 1 替换为您想要的任何其他值 - 您也可以使用任何Python变量。

关于eval和run之间的区别,请参阅this question

答案 1 :(得分:0)

我强烈建议您不要使用配方来计算!我发现的示例无法产生一致的结果,尤其是只有一个目标变量时。这让我头疼不已!

正确的做法是使用tensorflow_addons.metrics.RQsquare()。 Tensorflow附加组件为on PyPi here,文档为part of Tensorflow here。您要做的只是将y_shape设置为输出的形状,对于单个输出变量,通常将其设置为(1,)