C3W3_Assignment 3 - Error thrown just prior to end of first epoch

When trying to train the model (under a number of different architectures), I’m getting the following error just prior to the first epoch completing:

UnimplementedError: Cast string to float is not supported
[[node binary_crossentropy/Cast
(defined at /opt/conda/lib/python3.8/site-packages/keras/losses.py:1797)
]] [Op:__inference_test_function_64273]

Errors may have originated from an input operation.
Input Source operations connected to node binary_crossentropy/Cast:
In[0] ExpandDims (defined at /opt/conda/lib/python3.8/site-packages/keras/engine/compile_utils.py:677)

The cells at the start of the assignment are showing the expected outputs. Anyone else run into this issue and have suggestions on things I may be able to try to resolve?

Hello Raymond,

Can you share a screenshot of the image of your error without sharing any codes.

Kindly look at your model compile statement where your function call from strong to float is creating this error.

Labels should be numeric.

Hello Deepti,
The stack trace is pretty long. But here it is:
Epoch 1/20
4493/4500 [============================>.] - ETA: 0s - loss: 0.5779 - accuracy: 0.6899

UnimplementedError Traceback (most recent call last)
in
5
6 # Train the model and save the training history
----> 7 history = model.fit(train_pad_trunc_seq, train_labels, epochs=20, validation_data=(val_pad_trunc_seq, val_labels))

/opt/conda/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.traceback)
—> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb

/opt/conda/lib/python3.8/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
56 try:
57 ctx.ensure_initialized()
—> 58 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
59 inputs, attrs, num_outputs)
60 except core._NotOkStatusException as e:

UnimplementedError: Cast string to float is not supported
[[node binary_crossentropy/Cast
(defined at /opt/conda/lib/python3.8/site-packages/keras/losses.py:1797)
]] [Op:__inference_test_function_19035]

Errors may have originated from an input operation.
Input Source operations connected to node binary_crossentropy/Cast:
In[0] ExpandDims (defined at /opt/conda/lib/python3.8/site-packages/keras/engine/compile_utils.py:677)

Operation defined at: (most recent call last)

File “/opt/conda/lib/python3.8/runpy.py”, line 194, in _run_module_as_main
return _run_code(code, main_globals, None,

File “/opt/conda/lib/python3.8/runpy.py”, line 87, in _run_code
exec(code, run_globals)

File “/opt/conda/lib/python3.8/site-packages/ipykernel_launcher.py”, line 16, in
app.launch_new_instance()

File “/opt/conda/lib/python3.8/site-packages/traitlets/config/application.py”, line 845, in launch_instance
app.start()

File “/opt/conda/lib/python3.8/site-packages/ipykernel/kernelapp.py”, line 612, in start
self.io_loop.start()

File “/opt/conda/lib/python3.8/site-packages/tornado/platform/asyncio.py”, line 199, in start
self.asyncio_loop.run_forever()

File “/opt/conda/lib/python3.8/asyncio/base_events.py”, line 570, in run_forever
self._run_once()

File “/opt/conda/lib/python3.8/asyncio/base_events.py”, line 1859, in _run_once
handle._run()

File “/opt/conda/lib/python3.8/asyncio/events.py”, line 81, in _run
self._context.run(self._callback, *self._args)

File “/opt/conda/lib/python3.8/site-packages/tornado/ioloop.py”, line 688, in
lambda f: self._run_callback(functools.partial(callback, future))

File “/opt/conda/lib/python3.8/site-packages/tornado/ioloop.py”, line 741, in _run_callback
ret = callback()

File “/opt/conda/lib/python3.8/site-packages/tornado/gen.py”, line 814, in inner
self.ctx_run(self.run)

File “/opt/conda/lib/python3.8/site-packages/tornado/gen.py”, line 775, in run
yielded = self.gen.send(value)

File “/opt/conda/lib/python3.8/site-packages/ipykernel/kernelbase.py”, line 374, in dispatch_queue
yield self.process_one()

File “/opt/conda/lib/python3.8/site-packages/tornado/gen.py”, line 250, in wrapper
runner = Runner(ctx_run, result, future, yielded)

File “/opt/conda/lib/python3.8/site-packages/tornado/gen.py”, line 741, in init
self.ctx_run(self.run)

File “/opt/conda/lib/python3.8/site-packages/tornado/gen.py”, line 775, in run
yielded = self.gen.send(value)

File “/opt/conda/lib/python3.8/site-packages/ipykernel/kernelbase.py”, line 358, in process_one
yield gen.maybe_future(dispatch(*args))

File “/opt/conda/lib/python3.8/site-packages/tornado/gen.py”, line 234, in wrapper
yielded = ctx_run(next, result)

File “/opt/conda/lib/python3.8/site-packages/ipykernel/kernelbase.py”, line 261, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))

File “/opt/conda/lib/python3.8/site-packages/tornado/gen.py”, line 234, in wrapper
yielded = ctx_run(next, result)

File “/opt/conda/lib/python3.8/site-packages/ipykernel/kernelbase.py”, line 536, in execute_request
self.do_execute(

File “/opt/conda/lib/python3.8/site-packages/tornado/gen.py”, line 234, in wrapper
yielded = ctx_run(next, result)

File “/opt/conda/lib/python3.8/site-packages/ipykernel/ipkernel.py”, line 302, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)

File “/opt/conda/lib/python3.8/site-packages/ipykernel/zmqshell.py”, line 539, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)

File “/opt/conda/lib/python3.8/site-packages/IPython/core/interactiveshell.py”, line 2894, in run_cell
result = self._run_cell(

File “/opt/conda/lib/python3.8/site-packages/IPython/core/interactiveshell.py”, line 2940, in _run_cell
return runner(coro)

File “/opt/conda/lib/python3.8/site-packages/IPython/core/async_helpers.py”, line 68, in _pseudo_sync_runner
coro.send(None)

File “/opt/conda/lib/python3.8/site-packages/IPython/core/interactiveshell.py”, line 3165, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,

File “/opt/conda/lib/python3.8/site-packages/IPython/core/interactiveshell.py”, line 3357, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):

File “/opt/conda/lib/python3.8/site-packages/IPython/core/interactiveshell.py”, line 3437, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File “”, line 7, in
history = model.fit(train_pad_trunc_seq, train_labels, epochs=20, validation_data=(val_pad_trunc_seq, val_labels))

File “/opt/conda/lib/python3.8/site-packages/keras/utils/traceback_utils.py”, line 64, in error_handler
return fn(*args, **kwargs)

File “/opt/conda/lib/python3.8/site-packages/keras/engine/training.py”, line 1252, in fit
val_logs = self.evaluate(

File “/opt/conda/lib/python3.8/site-packages/keras/utils/traceback_utils.py”, line 64, in error_handler
return fn(*args, **kwargs)

File “/opt/conda/lib/python3.8/site-packages/keras/engine/training.py”, line 1537, in evaluate
tmp_logs = self.test_function(iterator)

File “/opt/conda/lib/python3.8/site-packages/keras/engine/training.py”, line 1366, in test_function
return step_function(self, iterator)

File “/opt/conda/lib/python3.8/site-packages/keras/engine/training.py”, line 1356, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))

File “/opt/conda/lib/python3.8/site-packages/keras/engine/training.py”, line 1349, in run_step
outputs = model.test_step(data)

File “/opt/conda/lib/python3.8/site-packages/keras/engine/training.py”, line 1305, in test_step
self.compiled_loss(

File “/opt/conda/lib/python3.8/site-packages/keras/engine/compile_utils.py”, line 201, in call
loss_value = loss_obj(y_t, y_p, sample_weight=sw)

File “/opt/conda/lib/python3.8/site-packages/keras/losses.py”, line 141, in call
losses = call_fn(y_true, y_pred)

File “/opt/conda/lib/python3.8/site-packages/keras/losses.py”, line 245, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)

File “/opt/conda/lib/python3.8/site-packages/keras/losses.py”, line 1797, in binary_crossentropy
y_true = tf.cast(y_true, y_pred.dtype)

The labels are of type int.

Apparently not. See here:

UnimplementedError: Cast string to float is not supported
[[node binary_crossentropy/Cast
(defined at /opt/conda/lib/python3.8/site-packages/keras/losses.py:1797)
]] [Op:__inference_test_function_19035]

and here:

File “/opt/conda/lib/python3.8/site-packages/keras/losses.py”, line 1797, in binary_crossentropy
y_true = tf.cast(y_true, y_pred.dtype)

If this doesn’t help, please click my name and message your notebook as an attachment.

1 Like

@balaji.ambresh Yep. You are correct. I accidentally set validation_labels to be sentences split on the training size instead of the labels with the appropriate split.

Oof. Thank you and Happy New Year!

1 Like

got exactly the same error, thanks for the answer mate.