Help: Improvise_a_Jazz_Solo_with_an_LSTM_Network_v4

I am stuck on this exercise from last 2 weeks and missed the deadline twice.

In the function def music_inference_model, I am getting this error.


TypeError Traceback (most recent call last)
in
1 ### YOU CANNOT EDIT THIS CELL
----> 2 inference_model = music_inference_model(LSTM_cell, densor, Ty = 50)

in music_inference_model(LSTM_cell, densor, Ty)
51
52 x = tf.math.argmax(outputs)
—> 53 x = tf.one_hot(x)
54 # Step 2.E:
55 # Use RepeatVector(1) to convert x into a tensor with shape=(None, 1, 90)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
199 “”“Call target, and fall back on dispatchers if there is a TypeError.”“”
200 try:
→ 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a

TypeError: one_hot() missing 1 required positional argument: ‘depth’

I am unable to solve this error. I guess the problem is somewhere in Step 2.D and Step 2.E.

Any help is appreciated.

Thanks.

You need to pass two arguments: ‘x’ and the depth.

The error message tells you you’re missing the “depth = …” argument.

Here is the TF documentation for one_hot():

I am unsure what should be the depth.

It’s the number of possible values (choices) for the musical notes, right? The representation was explained in the earlier parts of the notebook.

I figured that out but getting this error now.


ValueError Traceback (most recent call last)
in
1 ### YOU CANNOT EDIT THIS CELL
----> 2 inference_model = music_inference_model(LSTM_cell, densor, Ty = 50)

in music_inference_model(LSTM_cell, densor, Ty)
54 # Step 2.E:
55 # Use RepeatVector(1) to convert x into a tensor with shape=(None, 1, 90)
—> 56 x = RepeatVector(1)(x)
57
58 # Step 3: Create model instance with the correct “inputs” and “outputs” (≈1 line)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in call(self, *args, **kwargs)
924 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
925 return self._functional_construction_call(inputs, args, kwargs,
→ 926 input_list)
927
928 # Maintains info about the Layer.call stack.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
1090 # TODO(reedwm): We should assert input compatibility after the inputs
1091 # are casted, not before.
→ 1092 input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
1093 graph = backend.get_graph()
1094 # Use self._name_scope() to avoid auto-incrementing the name.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
178 ‘expected ndim=’ + str(spec.ndim) + ‘, found ndim=’ +
179 str(ndim) + '. Full shape received: ’ +
→ 180 str(x.shape.as_list()))
181 if spec.max_ndim is not None:
182 ndim = x.shape.ndims

ValueError: Input 0 of layer repeat_vector is incompatible with the layer: expected ndim=2, found ndim=3. Full shape received: [None, 90, 90]

That says that the output of your one_hot function is the wrong shape. It has 3 dimensions, but it should have 2. I added

print(x.shape)

after the call to tf.one_hot and before the call to RepeatVector. This is what I see with code that passes:

(90, 90)

What do you see in that case?

It shows (None, 90, 90)

Ok, that is why it fails. Now you need to figure out why that happened. One possibility is that the input to the one hot function is the wrong shape. I added more print statements to my code to show the shapes at each stage of the computation:

out.shape before arg_max (None, 90)
x.shape before one_hot (90,)
x.shape after one_hot (90, 90)
x.shape after RepeatVector (90, 1, 90)

Where do yours differ? Then you need to figure out why. This is how debugging works: one step at a time. :nerd_face:

The problem lies in argmax and I think i am doing wrong something here that I cant understand. because

out.shape before argmax = (None, 90)
x.shape before one_hot = (1, 90)

Are you sure you specified the axis on the argmax? And that you used TensorFlow argmax, not numpy argmax? With axis = -1 on the tf.argmax, I get this:

out.shape before arg_max (None, 90)
x.shape before one_hot (None,)
x.shape after one_hot (None, 90)
x.shape after RepeatVector (None, 1, 90)

So I think what I showed earlier was actually incorrect. The “None” is the “samples” dimension there. I’m guessing maybe you used numpy argmax and specified keepdims.

1 Like

I figured it out. Thank you sir :slight_smile:

1 Like