我是Tensorflow的新手。我正在编写代码以使用tensorflow在python中执行简单的多层Perceptron代码。在python中执行代码时,出现“ InvalidArgumentError”。请在下面找到我在python中编写的代码以及该代码生成的错误。
我还使用UBUNTU 19.04 OS,Python 3.7.4,TensorFlow版本1.14(GPU版本)和Conda版本4.7.11
预先感谢
这是我编写的代码。我正在使用numpy随机生成数据。
import tensorflow as tf
import numpy as np
np.random.seed(0)
# setting my data for experiment
x1 = np.random.uniform(-100,100, 10000)
x2 = np.random.uniform(-100,100, 10000)
x3 = np.random.uniform(-100,100, 10000)
x_data = np.append(x1.reshape((10000,1)),x2.reshape((10000,1)),axis = 1)
x_data = np.append(x_data,x3.reshape((10000,1)),axis = 1)
y_data = (8*x1 - 3*x2 + x3 + np.random.uniform(-5,5,10000)).reshape((10000,1))
del x1,x2,x3
# Tensorflow code
X = tf.placeholder(dtype = tf.float64, shape = (10, 3), name = 'X')
y = tf.placeholder(dtype = tf.float64, shape = (10,1), name = 'y')
W1 = tf.Variable(np.random.uniform(-0.5,0.5, size = (3,5)), name = 'W1')
b1 = tf.Variable(np.random.uniform(-0.5,0.5, size = (1,5)), name = 'b1')
h1 = tf.add(tf.matmul(X,W1) , b1)
W2 = tf.Variable(np.random.uniform(-0.5,0.5, size = (5,1)), name = 'W2')
b2 = tf.Variable(np.random.uniform(-0.5,0.5, size = (1,1)), name = 'b2')
predict = tf.add(tf.matmul(h1,W2) , b2)
loss = tf.reduce_sum(tf.square(y - predict))
optimizer = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
total = 10000
k = 10
for i in range(total//k):
feed_dict = {X : x_data[i*k:(i+1)*k], y : y_data[i*k: (i+1)*k] }
sess.run(optimizer, feed_dict)
print(sess.run(loss))
Traceback (most recent call last):
File "<ipython-input-1-859d9ba5b0e9>", line 1, in <module>
runfile('/home/devesh/Project Work/Code/Tensorflow/MLP_1.py', wdir='/home/devesh/Project Work/Code/Tensorflow')
File "/home/devesh/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "/home/devesh/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/devesh/Project Work/Code/Tensorflow/MLP_1.py", line 42, in <module>
print(sess.run(loss))
File "/home/devesh/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 950, in run
run_metadata_ptr)
File "/home/devesh/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1173, in _run
feed_dict_tensor, options, run_metadata)
File "/home/devesh/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1350, in _do_run
run_metadata)
File "/home/devesh/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1370, in _do_call
raise type(e)(node_def, op, message)
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: You must feed a value for placeholder tensor 'y' with dtype double and shape [10,1]
[[node y (defined at /home/devesh/Project Work/Code/Tensorflow/MLP_1.py:18) ]]
[[Sum/_5]]
(1) Invalid argument: You must feed a value for placeholder tensor 'y' with dtype double and shape [10,1]
[[node y (defined at /home/devesh/Project Work/Code/Tensorflow/MLP_1.py:18) ]]
0 successful operations.
0 derived errors ignored.