TensorFlow中的NaN

时间:2017-09-11 18:16:27

标签: python numpy tensorflow

我正在使用TensorFlow实现回归模型,但我总是为所有变量保持Nan值。以下是遇到Nan的代码。

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import xlrd

data_file = "data/fire_theft.xls"

book = xlrd.open_workbook(data_file, encoding_override="utf-8")
sheet = book.sheet_by_index(0)
data = np.asarray([sheet.row_values(i) for i in range(1, sheet.nrows)])
n_samples = sheet.nrows - 1

X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")

w = tf.Variable(0.0, name="weight_1")
u = tf.Variable(0.0, name="weight_2")
b = tf.Variable(0.0, name="bias")

Y_Predicted = tf.pow(X, 2.0) * w + X * u + b

loss = tf.square(Y - Y_Predicted, name="loss")
optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.0001).minimize(loss)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(10):
    for x,y in data:
        sess.run(optimizer, feed_dict={X: x, Y: y})
print(w.eval())

当我运行以下命令时:

print(loss.eval())

该程序抛出如下所示的异常:

Traceback (most recent call last):
File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-  packages/tensorflow/python/client/session.py", line 1327, in _do_call
return fn(*args)
File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1306, in _run_fn
status, run_metadata)
File "/usr/lib/python3.5/contextlib.py", line 66, in __exit__
next(self.gen)
File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-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 'X' with dtype float
 [[Node: X = 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 "/home/utkarsh/PycharmProjects/DiveIntoPython3/notes/quadratic_regression.py", line 32, in <module>
print(loss.eval())
File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 541, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 4085, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 895, in run
run_metadata_ptr)
File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1124, in _run
feed_dict_tensor, options, run_metadata)
File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
options, run_metadata)
File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-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 'X' with dtype float
 [[Node: X = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op 'X', defined at:
File "/home/utkarsh/PycharmProjects/DiveIntoPython3/notes/quadratic_regression.py", line 13, in <module>
X = tf.placeholder(tf.float32, name="X")
File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1548, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
 File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2094, in _placeholder
 name=name)
 File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
 File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/utkarsh/Documents/environments/tensorFlow/lib/python3.5/site-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 'X' with dtype float
 [[Node: X = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

我在这里遗漏了一些非常微不足道的东西。只需要一双不同的眼睛来调查。 感谢。

2 个答案:

答案 0 :(得分:0)

我在多层神经网络中遇到类似的问题,通过使用Xavier and Yoshua的方法初始化变量来修复:

graph = tf.Graph()
with graph.as_default():
    ...
    initializer = tf.contrib.layers.xavier_initializer()
    var1 = tf.Variable(initializer(var1_shape)
    ...

答案 1 :(得分:0)

堆栈跟踪显示InvalidArgumentError: You must feed a value for placeholder tensor 'X',这意味着它期望值为X,也可能是Y。 试试

loss.eval(feed_dict={X: x, Y: y})

此外,价值分布是什么?这些值是正确的类型吗? 您可以尝试规范化输入,也可以按照Saullo

的建议初始化变量