# .map in Exercise 3 - one\_hot\_matrix, gives error, Course 2 Week 3

**URL:** <https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333>\
**Category:** Improving Deep Neural Networks: Hyperparameter tun\
**Tags:** week-module-3, coursera-platform\
**Created:** [March 13, 2024, 9:32pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333 "2024-03-13T21:32:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amir\_Amir](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Amir\_Amir](https://community.deeplearning.ai/u/Amir_Amir)\
**Post date:** [March 13, 2024, 9:32pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333/1 "2024-03-13T21:32:38Z")

</div>

In the 2.3 - Using One Hot Encodings, Exercise 3, there are 2 predefined codes below:  
new\_y\_test = y\_test.map(one\_hot\_matrix)  
new\_y\_train = y\_train.map(one\_hot\_matrix)

but when I run, it generates large error lines below, while all my upper codes passed successfully. The problem is new\_y\_test will be needed downstream so I cannot passed exercise 6! The error is:

* * *

ValueError Traceback (most recent call last)  
 in   
----\> 1 new\_y\_test = y\_test.map(one\_hot\_matrix)  
2 new\_y\_train = y\_train.map(one\_hot\_matrix)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset\_ops.py 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/dataset\_ops.py 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/dataset\_ops.py 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/function.py 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/function.py 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/function.py in \_maybe\_define\_function(self, args, kwargs)  
3211  
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/function.py 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/func\_graph.py 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)  
985  
 → 986 func\_outputs = python\_func(\*func\_args, \*\*func\_kwargs)  
987  
988 # invariant: `func_outputs` contains only Tensors, CompositeTensors,

/opt/conda/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset\_ops.py 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/dataset\_ops.py in \_wrapper\_helper(\*args)  
3297 nested\_args = (nested\_args,)  
3298  
 → 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/api.py 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

ValueError: in user code:

```
<ipython-input-20-54cb3adeecf2>:17 one_hot_matrix *
    one_hot = tf.reshape(one_hot,C)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py:201 wrapper **
    return target(*args, **kwargs)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py:195 reshape
    result = gen_array_ops.reshape(tensor, shape, name)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py:8234 reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:744 _apply_op_helper
    attrs=attr_protos, op_def=op_def)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py:593 _create_op_internal
    compute_device)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:3485 _create_op_internal
    op_def=op_def)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:1975 __init__
    control_input_ops, op_def)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:1815 _create_c_op
    raise ValueError(str(e))

ValueError: Shape must be rank 1 but is rank 0 for '{{node Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32](one_hot, Reshape/shape)' with input shapes: [6], [].

```

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [March 13, 2024, 9:37pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333/2 "2024-03-13T21:37:18Z")

</div>

Are you running the notebook on some platform other than Coursera Labs?

---

<div class="post-metadata">

**Author:** ![Amir\_Amir](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Amir\_Amir](https://community.deeplearning.ai/u/Amir_Amir)\
**Post date:** [March 13, 2024, 9:38pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333/3 "2024-03-13T21:38:59Z")

</div>

No, only Coursera platform.

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [March 13, 2024, 9:50pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333/4 "2024-03-13T21:50:35Z")

</div>

Please post a screen capture image that shows your code for the one\_hot\_matrix() function.

I’ll delete it afterward to clear the Code of Conduct.

---

<div class="post-metadata">

**Author:** ![Amir\_Amir](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Amir\_Amir](https://community.deeplearning.ai/u/Amir_Amir)\
**Post date:** [March 13, 2024, 10:19pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333/5 "2024-03-13T22:19:34Z")

</div>

{image removed}

---

<div class="post-metadata">

**Author:** ![Amir\_Amir](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Amir\_Amir](https://community.deeplearning.ai/u/Amir_Amir)\
**Post date:** [March 13, 2024, 10:20pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333/6 "2024-03-13T22:20:03Z")

</div>

It says “all tests passed”

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [March 13, 2024, 10:39pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333/7 "2024-03-13T22:39:06Z")

</div>

That only means it passed one set of tests. That doesn’t prove it is perfect.

It’s failing later on when it’s used in other functions.

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [March 13, 2024, 10:40pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333/8 "2024-03-13T22:40:45Z")

</div>

Notice the shape parameter should be [C,]. Your code just has ‘C’.

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [March 13, 2024, 10:42pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333/10 "2024-03-13T22:42:42Z")

</div>

Note that throughout the course, when you see “All tests passed”, what that really means is “Some tests passed.”

---

<div class="post-metadata">

**Author:** ![Amir\_Amir](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Amir\_Amir](https://community.deeplearning.ai/u/Amir_Amir)\
**Post date:** [March 13, 2024, 10:43pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333/11 "2024-03-13T22:43:58Z")

</div>

It worked. Thanks for your help.

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [March 13, 2024, 11:21pm UTC](https://community.deeplearning.ai/t/map-in-exercise-3-one-hot-matrix-gives-error-course-2-week-3/590333/12 "2024-03-13T23:21:44Z")

</div>

No problem.

I think your original code would have worked if you used “shape=[C,]” in the second line.
