2017-01-01 66 views
2

我要通過tensorflow教程,並不斷收到此錯誤:Tensorflow:您必須養活佔位符張量「佔位」的值與D型浴液但價值是float]

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float 
    [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 

這裏是回溯:

Traceback (most recent call last): 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 972, in _do_call 
    return fn(*args) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 954, in _run_fn 
    status, run_metadata) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/contextlib.py", line 66, in __exit__ 
    next(self.gen) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/framework/errors.py", line 463, in raise_exception_on_not_ok_status 
    pywrap_tensorflow.TF_GetCode(status)) 
tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float 
    [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "neural_network.py", line 48, in <module> 
    print(sess.run(loss), feed_dict={xs:x_data, ys:y_data}) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 717, in run 
    run_metadata_ptr) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 915, in _run 
    feed_dict_string, options, run_metadata) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 965, in _do_run 
    target_list, options, run_metadata) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 985, in _do_call 
    raise type(e)(node_def, op, message) 
tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float 
    [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 

Caused by op 'Placeholder', defined at: 
    File "neural_network.py", line 21, in <module> 
    xs = tf.placeholder(tf.float32,[None,1]) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1332, in placeholder 
    name=name) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1748, in _placeholder 
    name=name) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op 
    op_def=op_def) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2380, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1298, in __init__ 
    self._traceback = _extract_stack() 

這裏是我的代碼:

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

def add_layer(inputs, in_size, out_size,activation_function=None): 
    Weights = tf.Variable(tf.random_normal([in_size, out_size])) 
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1) 
    Wx_plus_b = tf.matmul(inputs, Weights) + biases 
    if activation_function is None: 
     outputs = Wx_plus_b 
    else: 
     outputs = activation_function(Wx_plus_b) 
    return outputs 

# Make up some data 
x_data = np.linspace(-1,1,300, dtype=np.float32)[:,np.newaxis] 
noise = np.random.normal(0,0.05,x_data.shape).astype(np.float32) 
y_data = np.square(x_data) - 0.5 + noise 

# define placeholder for inputs to network 
xs = tf.placeholder(tf.float32,[None,1]) 
ys = tf.placeholder(tf.float32,[None,1]) 

# add hidden layer 
lay1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu) 

#add output layer 
prediction = add_layer(lay1, 10, 1, activation_function=None) 

# the error between prediction and real data 
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1])) 
### if we have [[3,4],[5,6],[7,8]] and reduction_indices is 1 
### then we are taking f(3,4) f(5,6) and f(7,8) 
### if reduction_indices is 0, then we are taking 
### f(3,5,7) and f(4,6,8) 
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) 

# important step 
init = tf.initialize_all_variables() 

with tf.Session() as sess: 
    sess.run(init) 
    for i in range(1000): 
     # training 
     sess.run(train_step, feed_dict={xs:x_data, ys:y_data}) 
     if i% 50 == 0: 
      # to see the step improvement 
      print(sess.run(loss), feed_dict={xs:x_data, ys:y_data}) 

正如你可以在這裏看到,無論是x_data和y_da ta是花車,所以我不明白爲什麼我得到這個錯誤。

當我在sess.run(train_step, feed_dict={xs:x_data, ys:y_data})之前添加打印語句print(type(x_data[0][0]),type(y_data[0][0]))時。我得到這個:<class 'numpy.float32'> <class 'numpy.float32'>所以很清楚,他們都是花車

我在這裏錯過了什麼?

+0

你使用TensorFlow r0.12? – martianwars

回答

2

您在print()聲明中有錯誤。幸運的是,我使用TensorFlow r0.11作爲SyntaxError

更換,

print(sess.run(loss), feed_dict={xs:x_data, ys:y_data}) 

用,

print(sess.run(loss, feed_dict={xs:x_data, ys:y_data})) 
相關問題