Error mapping one_hot_matrix over y_test and y_train

In the Course2 Week ungraded block which maps one_hot_matrix over y_test and y_train I get a series of errors culiminating in " TypeError: expected string or bytes-like object". There are no previous failing blocks. I tried casting to tf.uint8 (since things currently seem to be in64), but that didn’t work.
Below is the full error trace (using the original, unmodified code)

TypeError Traceback (most recent call last)
----> 1 new_y_test =
2 new_y_train =

/opt/conda/lib/python3.7/site-packages/tensorflow/python/data/ops/ in map(self, map_func, num_parallel_calls, deterministic)
1693 “”"
1694 if num_parallel_calls is None:
→ 1695 return MapDataset(self, map_func, preserve_cardinality=True)
1696 else:
1697 return ParallelMapDataset(

/opt/conda/lib/python3.7/site-packages/tensorflow/python/data/ops/ in init(self, input_dataset, map_func, use_inter_op_parallelism, preserve_cardinality, use_legacy_function)
4043 self._transformation_name(),
4044 dataset=input_dataset,
→ 4045 use_legacy_function=use_legacy_function)
4046 variant_tensor = gen_dataset_ops.map_dataset(
4047 input_dataset._variant_tensor, # pylint: disable=protected-access

/opt/conda/lib/python3.7/site-packages/tensorflow/python/data/ops/ in init(self, func, transformation_name, dataset, input_classes, input_shapes, input_types, input_structure, add_to_graph, use_legacy_function, defun_kwargs)
3369 with tracking.resource_tracker_scope(resource_tracker):
3370 # TODO(b/141462134): Switch to using garbage collection.
→ 3371 self._function = wrapper_fn.get_concrete_function()
3372 if add_to_graph:
3373 self._function.add_to_graph(ops.get_default_graph())

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/ in get_concrete_function(self, *args, **kwargs)
2937 “”"
2938 graph_function = self._get_concrete_function_garbage_collected(
→ 2939 *args, **kwargs)
2940 graph_function._garbage_collector.release() # pylint: disable=protected-access
2941 return graph_function

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/ in _get_concrete_function_garbage_collected(self, *args, **kwargs)
2904 args, kwargs = None, None
2905 with self._lock:
→ 2906 graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
2907 seen_names = set()
2908 captured = object_identity.ObjectIdentitySet(

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/ in _maybe_define_function(self, args, kwargs)
3212 self._function_cache.missed.add(call_context_key)
→ 3213 graph_function = self._create_graph_function(args, kwargs)
3214 self._function_cache.primary[cache_key] = graph_function
3215 return graph_function, args, kwargs

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/ in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3073 arg_names=arg_names,
3074 override_flat_arg_shapes=override_flat_arg_shapes,
→ 3075 capture_by_value=self._capture_by_value),
3076 self._function_attributes,
3077 function_spec=self.function_spec,

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
984 _, original_func = tf_decorator.unwrap(python_func)
→ 986 func_outputs = python_func(*func_args, **func_kwargs)
988 # invariant: func_outputs contains only Tensors, CompositeTensors,

/opt/conda/lib/python3.7/site-packages/tensorflow/python/data/ops/ in wrapper_fn(*args)
3362 attributes=defun_kwargs)
3363 def wrapper_fn(*args): # pylint: disable=missing-docstring
→ 3364 ret = _wrapper_helper(*args)
3365 ret = structure.to_tensor_list(self._output_structure, ret)
3366 return [ops.convert_to_tensor(t) for t in ret]

/opt/conda/lib/python3.7/site-packages/tensorflow/python/data/ops/ in _wrapper_helper(*args)
3297 nested_args = (nested_args,)
→ 3299 ret = autograph.tf_convert(func, ag_ctx)(*nested_args)
3300 # If func returns a list of tensors, nest.flatten() and
3301 # ops.convert_to_tensor() would conspire to attempt to stack

/opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/impl/ in wrapper(*args, **kwargs)
256 except Exception as e: # pylint:disable=broad-except
257 if hasattr(e, ‘ag_error_metadata’):
→ 258 raise e.ag_error_metadata.to_exception(e)
259 else:
260 raise

TypeError: in user code:

<ipython-input-18-e07ad63c3728>:16 one_hot_matrix  *
    one_hot = tf.reshape(tf.one_hot(label, depth, axis = 0), depth, -1)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/util/ wrapper  **
    return target(*args, **kwargs)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/ reshape
    result = gen_array_ops.reshape(tensor, shape, name)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/ reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ _apply_op_helper
    with g.as_default(), ops.name_scope(name) as scope:
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ __enter__
    return self._name_scope.__enter__()
/opt/conda/lib/python3.7/ __enter__
    return next(self.gen)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ name_scope
    if not _VALID_OP_NAME_REGEX.match(name):

TypeError: expected string or bytes-like object


Well, that pre-written code is calling one of your functions, right? So that should give a pretty big clue where to look for the problem. Most likely this means something is wrong with your one_hot_matrix implementation, even if it passes the local tests. What is the type of the output? Are you sure you didn’t hard-code the dimensions in a way that happens to pass the unit test for that function?

Here’s what my output from the test cell for one_hot_matrix looks like:

Test 1: tf.Tensor([0. 1. 0. 0.], shape=(4,), dtype=float32)
Test 2: tf.Tensor([0. 0. 1. 0.], shape=(4,), dtype=float32)
All test passed

What do you get?

Hi Paul,

As I said previously, all previous blocks passed including that one. Here’s a copy of the output:

Test 1: tf.Tensor([0. 1. 0. 0.], shape=(4,), dtype=float32)
Test 2: tf.Tensor([0. 0. 1. 0.], shape=(4,), dtype=float32)
All test passed


Interesting. Well I do know of at least two mistakes one can make which pass that test and give exactly that same output, but fail later tests and the grader. But when I tried those two mistakes, neither of them give the specific error trace that you show. So I think it’s worth taking a look at your code to advantage of your creativity to improve the tests here. Please check your DMs!


I was getting a different error in this block despite passing local tests earlier. Got it to pass by playing around with the syntax of the second argument in the reshape function as suggested by this comment. I’m not sure what the problem was. My implementation did not have hardcoded dimensions to deal with the local example only, so I found it odd. Anyways, hopefully this helps.


1 Like

Thanks, Lucas. A similar approach worked for me too.