2017-09-11 93 views
1

我用TensorFlow實施迴歸模型,但我始終保持讓楠值的所有變量。下面是Nan的代碼。楠TensorFlow

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"]()]] 

我在這裏失去了一些東西很瑣碎。只需要一雙不同的眼睛來看看。 謝謝。

+0

有沒有一些問題的答案能幫你嗎? –

回答

0

我有類似的問題,在一個多層神經網絡,這是固定使用的Xavier and Yoshua方法初始化變量:

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

堆棧跟蹤說InvalidArgumentError: You must feed a value for placeholder tensor 'X',這意味着它期待的X值,也許也是Y。 試用

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

另外,值的分佈是什麼?這些值是正確的類型嗎? 你可以去嘗試標準化的輸入,也初始化變量所推薦的Saullo