Number of indices mismatch, using Keras LSTM - python

I am trying to build a text classifier using LSTM:
model = Sequential()
model.add(Embedding(vocabulary_dim, 150, input_length=max_length))
model.add(LSTM(150, return_sequences=False))
model.add(Dense(1, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=5, batch_size=128)
Where the parameter vocabulary_dim is calculated by:
temp_list = [element for element in sentence_pad]
vocabulary_dim = len(np.unique(temp_list))
When I run model.fit(...) I get this error:
InvalidArgumentError: indices[3,0] = 25501 is not in [0, 19355)
[[Node: embedding_2/Gather = Gather[Tindices=DT_INT32, Tparams=DT_FLOAT, validate_indices=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_2/embeddings/read, _arg_embedding_2_input_0_2)]]
Caused by op 'embedding_2/Gather', defined at:
File "C:\Users\Simone\Anaconda3\envs\keras\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\ipykernel\__main__.py", line 3, in <module>
app.launch_new_instance()
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\ipykernel\kernelapp.py", line 474, in start
ioloop.IOLoop.instance().start()
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\tornado\ioloop.py", line 887, in start
handler_func(fd_obj, events)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\ipykernel\kernelbase.py", line 276, in dispatcher
return self.dispatch_shell(stream, msg)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\ipykernel\kernelbase.py", line 228, in dispatch_shell
handler(stream, idents, msg)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\ipykernel\kernelbase.py", line 390, in execute_request
user_expressions, allow_stdin)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\ipykernel\zmqshell.py", line 501, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\IPython\core\interactiveshell.py", line 2821, in run_ast_nodes
if self.run_code(code, result):
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-14-42e99436b8e4>", line 3, in <module>
model.add(Embedding(dim_vocabolario, 150, input_length=lunghezza_massima_periodo))
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\keras\models.py", line 422, in add
layer(x)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\keras\engine\topology.py", line 554, in __call__
output = self.call(inputs, **kwargs)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\keras\layers\embeddings.py", line 119, in call
out = K.gather(self.embeddings, inputs)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\keras\backend\tensorflow_backend.py", line 966, in gather
return tf.gather(reference, indices)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\tensorflow\python\ops\array_ops.py", line 2486, in gather
params, indices, validate_indices=validate_indices, name=name)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1833, in gather
validate_indices=validate_indices, name=name)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\tensorflow\python\framework\ops.py", line 2956, in create_op
op_def=op_def)
File "C:\Users\Simone\Anaconda3\envs\keras\lib\site-packages\tensorflow\python\framework\ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): indices[3,0] = 25501 is not in [0, 19355)
[[Node: embedding_2/Gather = Gather[Tindices=DT_INT32, Tparams=DT_FLOAT, validate_indices=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_2/embeddings/read, _arg_embedding_2_input_0_2)]]
I suppose that there is something wrong with how I have calculated vocabulary_dimension, but I 'm not sure about this.

Yes, the problem is in the calculation of vocabulary_dim. From the documentation:
input_dim: int > 0. Size of the vocabulary, i.e. maximum integer index + 1.
In your case, it seems there are gaps in the indices so the length doesn't correspond to the maximum integer index. You should instead:
temp_list = [element for element in sentence_pad]
vocabulary_dim = np.max(np.unique(temp_list)) + 1
Note: this vocabulary dim should be calculated across your entire dataset (including test set) so every word has a unique integer index.

The error probably comes because you mapped your words using temp_list, whereas, the mapping should have been done on set(temp_list)
temp_list = [element for element in sentence_pad]
temp_list = set(temp_list)
vocabulary_dim = len(temp_list)
Now, using this temp_list map the words in your dataframe to its index, something like,
word2idx = {v:i for i,v in enumerate(temp_list)}
For example, if your data is something like,
df
words
0 credits
1 pandas
2 good
X_train = df['words'].map(word2idx)

Related

How to create tf.data.Dataset for one image input and multiple outputs neural network?

I have a 1 input 7 output neural network.
The input is an image and all the 7 outputs are different categories.
My images are stored in image directory.
And, labels are stored in a CSV file.
I have created a list where x_train_path is a path to the image directory and all y_train_... are list of labels, like this.
x_train_path = [x for x in df_train.filepath]
y_train_gender = [x for x in df_train.gender]
y_train_masterCategory = [x for x in df_train.masterCategory]
y_train_subCategory = [x for x in df_train.subCategory]
y_train_articleType = [x for x in df_train.articleType]
y_train_baseColour = [x for x in df_train.baseColour]
y_train_season = [x for x in df_train.season]
y_train_usage = [x for x in df_train.usage]
I wanted to create a tf.data.Dataset() so I am doing the following:
def load_and_preprocess_image(path):
image_string = tf.compat.as_str_any(path)
image_string = tf.io.read_file(path)
img = tf.io.decode_png(image_string, channels = 3)
return tf.image.resize(img, [500, 500])
def generator():
for img, lable1, lable2, lable3, lable4, lable5, lable6, lable7 in zip(x_train_path, y_train_gender, y_train_masterCategory, y_train_subCategory, y_train_articleType, y_train_baseColour, y_train_season, y_train_usage):
image = load_and_preprocess_image(path = img)
yield image, {"gender_output":lable1,
"master_category_output":lable2,
"sub_category_output":lable3,
"article_type_output":lable4,
"base_color_output":lable5,
"season_output":lable6,
"usage_output":lable7}
dataset = tf.data.Dataset.from_generator(generator, output_types = (tf.float64, {"gender_output":tf.int64,
"master_category_output":tf.int64,
"sub_category_output":tf.int64,
"article_type_output":tf.int64,
"base_color_output":tf.int64,
"season_output":tf.int64,
"usage_output":tf.int64}))
dataset = dataset.batch(64)
and, I have compiled the model like this:
model.compile(optimizer = 'adam',
loss = {"gender_output" : "categorical_crossentropy",
"master_category_output" : "categorical_crossentropy",
"sub_category_output" : "categorical_crossentropy",
"article_type_output" : "categorical_crossentropy",
"base_color_output" : "categorical_crossentropy",
"season_output" : "categorical_crossentropy",
"usage_output" : "categorical_crossentropy"}
)
but when I am fitting the model using model.fit(dataset, epochs = 1), it is giving me this error:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-29-4dae7d62ef06> in <module>
----> 1 model.fit(dataset, epochs = 1)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 ctx.ensure_initialized()
54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
InvalidArgumentError: Graph execution error:
Detected at node 'categorical_crossentropy_5/mul' defined at (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
app.start()
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 612, in start
self.io_loop.start()
File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
self._run_once()
File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once
handle._run()
File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.7/dist-packages/tornado/ioloop.py", line 758, in _run_callback
ret = callback()
File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 1233, in inner
self.run()
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 1147, in run
yielded = self.gen.send(value)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 365, in process_one
yield gen.maybe_future(dispatch(*args))
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 545, in execute_request
user_expressions, allow_stdin,
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2855, in run_cell
raw_cell, store_history, silent, shell_futures)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
return runner(coro)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
coro.send(None)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3058, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-29-4dae7d62ef06>", line 1, in <module>
model.fit(dataset, epochs = 1)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1409, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 890, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 949, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 139, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 243, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1788, in categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5153, in categorical_crossentropy
return -tf.reduce_sum(target * tf.math.log(output), axis)
Node: 'categorical_crossentropy_5/mul'
Detected at node 'categorical_crossentropy_5/mul' defined at (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
app.start()
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 612, in start
self.io_loop.start()
File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
self._run_once()
File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once
handle._run()
File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.7/dist-packages/tornado/ioloop.py", line 758, in _run_callback
ret = callback()
File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 1233, in inner
self.run()
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 1147, in run
yielded = self.gen.send(value)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 365, in process_one
yield gen.maybe_future(dispatch(*args))
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 545, in execute_request
user_expressions, allow_stdin,
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2855, in run_cell
raw_cell, store_history, silent, shell_futures)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
return runner(coro)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
coro.send(None)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3058, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-29-4dae7d62ef06>", line 1, in <module>
model.fit(dataset, epochs = 1)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1409, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 890, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 949, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 139, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 243, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1788, in categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5153, in categorical_crossentropy
return -tf.reduce_sum(target * tf.math.log(output), axis)
Node: 'categorical_crossentropy_5/mul'
2 root error(s) found.
(0) INVALID_ARGUMENT: required broadcastable shapes
[[{{node categorical_crossentropy_5/mul}}]]
[[categorical_crossentropy_3/cond/then/_33/categorical_crossentropy_3/cond/cond/pivot_t/_366/_405]]
(1) INVALID_ARGUMENT: required broadcastable shapes
[[{{node categorical_crossentropy_5/mul}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_94694]
Can anyone help?

UnimplementedError: Graph execution error: Detected at node 'huber_loss/Cast' defined at (most recent call last): How can I solve this?

When trying to run some one code for counting leaves, I got this problem at the training step. I use gg colab to run this code take much time but still do not know how to solve this. Anyone can help me ?
You can see my code here:
https://github.com/ThanhHung2112/Machine_learning/blob/main/Counting_leaves.ipynb
Thiss is the error:
UnimplementedError: Graph execution error:
Detected at node 'huber_loss/Cast' defined at (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
app.start()
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 612, in start
self.io_loop.start()
File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
self._run_once()
File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once
handle._run()
File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.7/dist-packages/tornado/ioloop.py", line 758, in _run_callback
ret = callback()
File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 1233, in inner
self.run()
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 1147, in run
yielded = self.gen.send(value)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 365, in process_one
yield gen.maybe_future(dispatch(*args))
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 545, in execute_request
user_expressions, allow_stdin,
File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2855, in run_cell
raw_cell, store_history, silent, shell_futures)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
return runner(coro)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
coro.send(None)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3058, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-68-3176974e15d1>", line 8, in <module>
validation_data = val_dataset
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1384, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1694, in huber
y_true = tf.cast(y_true, dtype=backend.floatx())
Node: 'huber_loss/Cast'
2 root error(s) found.
(0) UNIMPLEMENTED: Cast string to float is not supported
[[{{node huber_loss/Cast}}]]
(1) CANCELLED: Function was cancelled before it was started
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_52546]
Make sure that the training variables are in the correct shape as the model input layer.

I have a graph execution error in python. How can I solve this?

I'm trying to create a graph for the outputs:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import wrapt
from sklearn.model_selection import train_test_split
from keras import models
from keras import layers
from tensorflow.keras.utils import to_categorical
from sklearn.metrics import confusion_matrix
# read data
data=pd.read_csv('adult-stretch.data', header=None)
# convert to arrays
x=data.iloc[:, :4].to_numpy()
t=data[4].replace(['T','F'],[0,1])
t=t.to_numpy()
# split the dataset
xTrain, xTest, tTrain, tTest = train_test_split(x, t, test_size=0.2, random_state=3)
# convert to categorical (hot encoding)
tTrainHot = to_categorical(tTrain)
tTestHot = to_categorical(tTest)
# create network
net=models.Sequential()
#net.add(layers.Dense(5,activation="relu", input_shape=(4,)))
net.add(layers.Dense(5,activation="relu", input_shape=(np.size(x,1),)))
#net.add(layers.Dense(3,activation="sigmoid"))
net.add(layers.Dense(np.size(tTrainHot, 1),activation="sigmoid"))
print(net.summary()) #see the network structure
# set network train parameters
net.compile(loss='categorical_crossentropy', metrics='accuracy')
#train the network
history = net.fit(xTrain, tTrainHot, epochs=300, validation_split=0.2)
plt.close('all')
plt.plot(history.history['loss'],label='Training Loss',linewidth=3)
plt.plot(history.history['val_loss'],label='Validation Loss',linewidth=3)
plt.legend()
plt.grid('on')
plt.title('Loss')
plt.figure()
plt.plot(history.history['accuracy'],label='Training Accuracy',linewidth=3)
plt.plot(history.history['val_accuracy'],label='Validation Accuracy',linewidth=3)
plt.legend()
plt.grid('on')
plt.title('Accuracy')
lossTest, accTest = net.evaluate(xTest, tTestHot)
print('accTest=', accTest)
# real output (hot-encoded)
yTestHot = net.predict(xTest)
# convert yTest to label vector
yTest = np.argmax(yTestHot, axis=1)
print(confusion_matrix(tTest, yTest))
and i just keep getting this graph execution error:
Detected at node 'sequential_23/Cast' defined at (most recent call last):
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/spyder_kernels/console/__main__.py", line 23, in <module>
start.main()
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/spyder_kernels/console/start.py", line 328, in main
kernel.start()
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelapp.py", line 677, in start
self.io_loop.start()
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 199, in start
self.asyncio_loop.run_forever()
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/asyncio/base_events.py", line 596, in run_forever
self._run_once()
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/asyncio/base_events.py", line 1890, in _run_once
handle._run()
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 457, in dispatch_queue
await self.process_one()
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 446, in process_one
await dispatch(*args)
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 353, in dispatch_shell
await result
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 648, in execute_request
reply_content = await reply_content
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/ipkernel.py", line 353, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 2901, in run_cell
result = self._run_cell(
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 2947, in _run_cell
return runner(coro)
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
coro.send(None)
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3172, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3364, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3444, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "/var/folders/v6/jqcvfqns1gn7zy2m3pcvzrqm0000gn/T/ipykernel_702/4012300658.py", line 1, in <module>
runfile('/Users/ispasdinu-ioan/Downloads/Lab/Lab3/nnga3_ex1.py', wdir='/Users/ispasdinu-ioan/Downloads/Lab/Lab3')
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/spyder_kernels/customize/spydercustomize.py", line 577, in runfile
exec_code(file_code, filename, ns_globals, ns_locals,
File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/spyder_kernels/customize/spydercustomize.py", line 465, in exec_code
exec(compiled, ns_globals, ns_locals)
File "/Users/ispasdinu-ioan/Downloads/Lab/Lab3/nnga3_ex1.py", line 40, in <module>
history = net.fit(xTrain, tTrainHot, epochs=300, validation_split=0.2)
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/training.py", line 1384, in fit
tmp_logs = self.train_function(iterator)
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/training.py", line 1021, in train_function
return step_function(self, iterator)
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/training.py", line 1010, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/training.py", line 1000, in run_step
outputs = model.train_step(data)
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/base_layer.py", line 1096, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/sequential.py", line 374, in call
return super(Sequential, self).call(inputs, training=training, mask=mask)
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/functional.py", line 451, in call
return self._run_internal_graph(
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/functional.py", line 571, in _run_internal_graph
y = self._conform_to_reference_input(y, ref_input=x)
File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/functional.py", line 671, in _conform_to_reference_input
tensor = tf.cast(tensor, dtype=ref_input.dtype)
Node: 'sequential_23/Cast'
Cast string to float is not supported
[[{{node sequential_23/Cast}}]] [Op:__inference_train_function_10601]
As ML models train on numerics, you are passing input of type object to the neural network. Changing the input to numerics will help. You can use pd.get_dummies() to one-hot encode the categorical features as follows:
data1=pd.get_dummies(data[0])
data2=pd.get_dummies(data[1])
data3=pd.get_dummies(data[2])
data4=pd.get_dummies(data[3])
#you can concatenate the one-hot encoded values by using pd.concat
data = pd.concat([data1,data2,data3,data4,data[4]], axis='columns')
After one-hot encoding the dataset looks like this
Please find the working code here. Thank you.

Tensorflow iterator Out of Range

I have two dataset in TFRecords, one holds around 20,000 entries, other hold 1.2 million.
This code perfectly work when I use TFRecord with 20,000 entries but give out of range error when I use 1.2 million .
def parse(serialized):
features = \
{
'train/image': tf.FixedLenFeature([], tf.string),
'train/label': tf.FixedLenFeature([], tf.int64)
}
parsed_example = tf.parse_single_example(serialized=serialized,
features=features)
image_raw = parsed_example['train/image']
image = tf.decode_raw(image_raw, tf.uint8)
image = tf.cast(image, tf.float32)
label = parsed_example['train/label']
return image, label
def input_fn(filenames, train, batch_size=32, buffer_size=2048):
dataset = tf.data.TFRecordDataset(filenames=filenames)
dataset = dataset.map(parse)
if train:
dataset = dataset.shuffle(buffer_size=buffer_size)
num_repeat = None
else:
num_repeat = 1
dataset = dataset.repeat(num_repeat)
dataset = dataset.batch(batch_size)
iterator = dataset.make_one_shot_iterator()
images_batch, labels_batch = iterator.get_next()
x = {'image':images_batch}
y = labels_batch
return x, y
x,y = input_fn('train.tfrecords',False)
print(x)
with tf.Session() as sess:
for i in range(10):
print(sess.run(x))
The error is coming is this:
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1374, in _do_call
raise type(e)(node_def, op, message)
OutOfRangeError: End of sequence
[[Node: IteratorGetNext_2 = IteratorGetNext[output_shapes=[[?,?], [?]], output_types=[DT_FLOAT, DT_INT64], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator_2)]]
Caused by op 'IteratorGetNext_2', defined at:
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 268, in <module>
main()
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 264, in main
kernel.start()
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 478, in start
self.io_loop.start()
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2728, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2856, in run_ast_nodes
if self.run_code(code, result):
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2910, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-23a4ed6f3a2e>", line 1, in <module>
runfile('C:/Users/kakus/Desktop/landmark/tfrecord_test_outputv2.py', wdir='C:/Users/kakus/Desktop/landmark')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/kakus/Desktop/landmark/tfrecord_test_outputv2.py", line 84, in <module>
x,y = input_fn('train.tfrecords',False)
File "C:/Users/kakus/Desktop/landmark/tfrecord_test_outputv2.py", line 76, in input_fn
images_batch, labels_batch = iterator.get_next()
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\iterator_ops.py", line 330, in get_next
name=name)), self._output_types,
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_dataset_ops.py", line 895, in iterator_get_next
output_shapes=output_shapes, name=name)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3271, in create_op
op_def=op_def)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1650, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
OutOfRangeError (see above for traceback): End of sequence
[[Node: IteratorGetNext_2 = IteratorGetNext[output_shapes=[[?,?], [?]], output_types=[DT_FLOAT, DT_INT64], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator_2)]]
dataset.repeat(num_epochs) will repeat the data for the number of epochs specified. In the train mode you have specified it to be none, change it to the number of epochs you want to train the dataset.

Race Condition when reading from generator

I am running a python program on Python2.7. Wherein I am trying to print the value of a generator using the following approach.
y = m.predict(input_fn=lambda:input_fn(df_predict), as_iterable=True)
#output of this is a generator
print(type(y))
print(list(y))
The following error is displayed:
Traceback (most recent call last):
File "tensorflow/examples/wide_n_deep_model_predictions_v3.0.py", line 176, in <module>
tf.app.run()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 43, in run
sys.exit(main(sys.argv[:1] + flags_passthrough))
File "tensorflow/examples/wide_n_deep_model_predictions_v3.0.py", line 171, in main
train_and_eval()
File "tensorflow/examples/wide_n_deep_model_predictions_v3.0.py", line 167, in train_and_eval
print (list(y))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.py", line 328, in _as_iterable
for pred in preds:
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 909, in _infer_model_as_iterable
restore_checkpoint_path=checkpoint_path):
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/graph_actions.py", line 867, in run_feeds_iter
yield session.run(output_dict, f)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 766, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 964, in _run
feed_dict_string, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1014, in _do_run
target_list, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1034, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: WhereOp: Race condition between counting the number of true elements and writing them. When counting, saw 2645 elements; but when writing their indices, saw 19 elements.
[[Node: linear/linear/ip/ip_weights/Where = Where[_device="/job:localhost/replica:0/task:0/cpu:0"](linear/linear/ip/ip_weights/GreaterEqual/_409)]]
Caused by op u'linear/linear/ip/ip_weights/Where', defined at:
File "tensorflow/examples/wide_n_deep_model_predictions_v3.0.py", line 176, in <module>
tf.app.run()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 43, in run
sys.exit(main(sys.argv[:1] + flags_passthrough))
File "tensorflow/examples/wide_n_deep_model_predictions_v3.0.py", line 171, in main
train_and_eval()
File "tensorflow/examples/wide_n_deep_model_predictions_v3.0.py", line 163, in train_and_eval
y = m.predict(input_fn=lambda:input_fn(df_predict), as_iterable=True)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py", line 245, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.py", line 747, in predict
as_iterable=as_iterable)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py", line 191, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 477, in predict
as_iterable=as_iterable)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 847, in _infer_model
infer_ops = self._get_predict_ops(features)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1113, in _get_predict_ops
return self._call_model_fn(features, labels, ModeKeys.INFER)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1026, in _call_model_fn
params=self.params)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.py", line 504, in _dnn_linear_combined_model_fn
scope=scope)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.py", line 535, in weighted_sum_from_feature_columns
weight_collections)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.py", line 332, in _create_embedding_lookup
name=column.name + '_weights')
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/embedding_ops.py", line 123, in safe_embedding_lookup_sparse
sparse_ids, sparse_weights = _prune_invalid_ids(sparse_ids, sparse_weights)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/embedding_ops.py", line 169, in _prune_invalid_ids
sparse_ids = sparse_ops.sparse_retain(sparse_ids, is_id_valid)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/sparse_ops.py", line 880, in sparse_retain
where_true = array_ops.reshape(array_ops.where(to_retain), [-1])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 2501, in where
return gen_array_ops.where(input=condition, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3821, in where
result = _op_def_lib.apply_op("Where", input=input, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2259, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1130, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): WhereOp: Race condition between counting the number of true elements and writing them. When counting, saw 2645 elements; but when writing their indices, saw 19 elements.
[[Node: linear/linear/ip/ip_weights/Where = Where[_device="/job:localhost/replica:0/task:0/cpu:0"](linear/linear/ip/ip_weights/GreaterEqual/_409)]]
On running print ('Predictions: {}'.format(str(y))) the output is Predictions: <generator object _as_iterable at 0x7f1883235550>
Can someone guide me how can I print the generator?
I think you need to do something like this
# Print out predictions
y = regressor.predict(input_fn=lambda: input_fn(prediction_set))
# .predict() returns an iterator; convert to a list and print predictions
predictions = list(itertools.islice(y, 6))
print("Predictions: {}".format(str(predictions)))
I found it from the boston tutorial: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/input_fn/boston.py

Categories

Resources