Studying TF. I made an example with adding data through placeholder, and it does not work. As I understand it, fetch_batch creates batchy as a string and cannot feed them via feed_dict. But why and how to fix do not understand. I would be very grateful for the help.
import tensorflow as tf import numpy as np from sklearn.datasets import fetch_california_housing from sklearn.preprocessing import StandardScaler housing = fetch_california_housing() m, n = housing.data.shape learning_rate = 0.1 n_epochs = 1000 scaler = StandardScaler() scaled_housing_data = scaler.fit_transform(housing.data) scaled_housing_data_plus_bias = np.c_[np.ones((m, 1)), scaled_housing_data] #X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X") #y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y") X = tf.placeholder(tf.float32, shape=(None, n+1), name="X") y = tf.placeholder(tf.float32, shape=(None, 1), name="y") def fetch_batch (batch_index, batch_size, epoch): np.random.seed (epoch * batch_size + batch_size) indices = np.random.randint (m, size=batch_size) X_batch = scaled_housing_data_plus_bias [indices] y_batch = housing.target.reshape(-1, 1) [indices] return X_batch, y_batch batch_size = 100 theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name="theta") y_pred = tf.matmul(X, theta, name="prediction") erorr = y_pred - y mse = tf.reduce_mean(tf.square(erorr), name="mse") #gradients = 2/m * tf.matmul(tf.transpose(X), erorr, name="gradients") #training_op = tf.assign(theta, theta - learning_rate * gradients) optimizer = tf.train.GradientDescentOptimizer (learning_rate=learning_rate) training_op = optimizer.minimize(mse) n_batches = int(np.ceil(m / batch_size)) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for epoch in range(n_epochs): for batch_index in range(n_batches): X_batch, y_batch = fetch_batch (batch_index, batch_size, epoch) sess.run(training_op, feed_dict={X:X_batch, y:y_batch}) if epoch % 100 == 0 : print("Epoch:", epoch, "MSE:", mse.eval()) but it doesn't work at all
--------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last) /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 1333 try: -> 1334 return fn(*args) 1335 except errors.OpError as e: /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata) 1318 return self._call_tf_sessionrun( -> 1319 options, feed_dict, fetch_list, target_list, run_metadata) 1320 /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata) 1406 self._session, options, feed_dict, fetch_list, target_list, -> 1407 run_metadata) 1408 InvalidArgumentError: You must feed a value for placeholder tensor 'X_11' with dtype float and shape [?,9] [[{{node X_11}} = Placeholder[dtype=DT_FLOAT, shape=[?,9], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]] [[{{node mse_12/_7}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_15_mse_12", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]] During handling of the above exception, another exception occurred: InvalidArgumentError Traceback (most recent call last) <ipython-input-19-46a547b04ceb> in <module>() 56 sess.run(training_op, feed_dict={X:X_batch, y:y_batch}) 57 if epoch % 100 == 0 : ---> 58 print("Epoch:", epoch, "MSE:", mse.eval()) 59 60 /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in eval(self, feed_dict, session) 711 712 """ --> 713 return _eval_using_default_session(self, feed_dict, self.graph, session) 714 715 /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _eval_using_default_session(tensors, feed_dict, graph, session) 5155 "the tensor's graph is different from the session's " 5156 "graph.") -> 5157 return session.run(tensors, feed_dict) 5158 5159 /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 927 try: 928 result = self._run(None, fetches, feed_dict, options_ptr, --> 929 run_metadata_ptr) 930 if run_metadata: 931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 1150 if final_fetches or final_targets or (handle and feed_dict_tensor): 1151 results = self._do_run(handle, final_targets, final_fetches, -> 1152 feed_dict_tensor, options, run_metadata) 1153 else: 1154 results = [] /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 1326 if handle is None: 1327 return self._do_call(_run_fn, feeds, fetches, targets, options, -> 1328 run_metadata) 1329 else: 1330 return self._do_call(_prun_fn, handle, feeds, fetches) /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 1346 pass 1347 message = error_interpolation.interpolate(message, self._graph) -> 1348 raise type(e)(node_def, op, message) 1349 1350 def _extend_graph(self): InvalidArgumentError: You must feed a value for placeholder tensor 'X_11' with dtype float and shape [?,9] [[node X_11 (defined at <ipython-input-19-46a547b04ceb>:23) = Placeholder[dtype=DT_FLOAT, shape=[?,9], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]] [[{{node mse_12/_7}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_15_mse_12", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]] Caused by op 'X_11', defined at: 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-19-46a547b04ceb>", line 23, in <module> X = tf.placeholder(tf.float32, shape=(None, n+1), name="X") File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py", line 1747, 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 5206, 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 787, in _apply_op_helper op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py", line 488, in new_func return func(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3274, in create_op op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 1770, in __init__ self._traceback = tf_stack.extract_stack() InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'X_11' with dtype float and shape [?,9] [[node X_11 (defined at <ipython-input-19-46a547b04ceb>:23) = Placeholder[dtype=DT_FLOAT, shape=[?,9], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]] [[{{node mse_12/_7}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_15_mse_12", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]