尝试使用Tensorflow在加利福尼亚住房数据集中运行线性回归。使用占位符时,出现“ InvalidArgumentError:您必须使用dtype float和shape [?,1]
输入占位符张量y的值
[[{{node y}}]]“。代码在下面,请帮助!
我假设出于某种原因,我的y_batch或x_batch不是float32。所以我添加了一个tf.cast(...,tf.float32)。那并没有解决问题。还添加了一个tf.reset_default_graph。
# Construction
tf.reset_default_graph()
x = tf.placeholder(dtype = tf.float32, shape = (None, n + 1), name = "x")
y = tf.placeholder(dtype = tf.float32, shape = (None, 1), name = "y")
theta = tf.Variable(tf.random_uniform(shape=[n + 1, 1], minval=-1.0, maxval=1.0), dtype = tf.float32, name = "theta")
y_pred = tf.matmul(x, theta, name = "predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")
grad = tf.gradients(mse, [theta])[0]
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(mse)
init = tf.global_variables_initializer()
n_epochs = 1000
batch_size = 100
n_batches = np.int(np.ceil(m/ batch_size))
a = 0
b = 0
def fetch_batch(epoch, batch_index, batch_size):
a = batch_size * batch_index
b = a + batch_size
x_batch = tf.cast(scaled_housing_data_bias[a:b,:], tf.float32)
y_batch = tf.cast(housing_target[a:b,:], tf.float32)
return x_batch, y_batch
# dtype and shape checking
a, b = fetch_batch(1, 3, 100)
print(a.shape)
print(b.shape)
print(a.dtype)
print(b.dtype)
# (100, 9)
# (100, 1)
# <dtype: 'float32'>
# <dtype: 'float32'>
# execution
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
rmse = mse.eval()
if epoch_num % 100 == 0:
print("\n Epoch: ", epoch_num, "\t MSE: ", rmse)
for batch_index in range(n_batches):
x_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
sess.run(training_op, feed_dict = {x: x_batch, y: y_batch})
best_theta = theta.eval()
print("Best_theta: \n", best_theta)
print("\n Final rmse: ", rmse)
InvalidArgumentError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1355 try:
-> 1356 return fn(*args)
1357 except errors.OpError as e:
8 frames
InvalidArgumentError: You must feed a value for placeholder tensor 'y' with dtype float and shape [?,1]
[[{{node y}}]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1368 pass
1369 message = error_interpolation.interpolate(message, self._graph)
-> 1370 raise type(e)(node_def, op, message)
1371
1372 def _extend_graph(self):
InvalidArgumentError: You must feed a value for placeholder tensor 'y' with dtype float and shape [?,1]
[[node y (defined at <ipython-input-16-ccae920efe43>:3) ]]
Original stack trace for 'y':
File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.6/dist-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
File "/usr/local/lib/python3.6/dist-packages/tornado/ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
self._handle_recv()
File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
self._run_callback(callback, msg)
File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
callback(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/usr/local/lib/python3.6/dist-packages/ipykernel/ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.6/dist-packages/ipykernel/zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
if self.run_code(code, result):
File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-16-ccae920efe43>", line 3, in <module>
y = tf.placeholder(dtype = tf.float32, shape = (None, 1), name = "y")
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py", line 2143, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 6262, in placeholder
"Placeholder", dtype=dtype, shape=shape, name=name)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3616, in create_op
op_def=op_def)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 2005, in __init__
self._traceback = tf_stack.extract_stack()