Week 1 - Assignment 3

Hi everyone

I am not able to get the music_inference_model(LSTM_cell, densor, Ty=100) work as it is supposed to. I had worked on it for a couple of hours, but I could not find a way around it that everything goes safe and sound. To be more specific, I need guidance regarding the proper implementation of the part referred to as 2.D. This is the error that I stuck with:

AttributeError: The layer “lstm” has multiple inbound nodes, with different output shapes. Hence the notion of “output shape” is ill-defined for the layer. Use get_output_shape_at(node_index) instead.

I do not post my code snippet, as I think it is against the moral code, however, let me know if it is required.

Looking forward to hearing from you soon.

Amirhossein

6 Likes

In the loop you are asked to update the cell state (initialized with a0 and c0) for every time period. That gives you the x and the updated a and c values inside the LSTM cell. Then the state variable that is passed between the LSTM cells at different time periods is also fed to the densor. I think if you do this your output should be correct.

1 Like

Hi laacdm, thanks for your reply, however, I don’t think the problem is what you addressed. As I mentioned earlier, it seems that my problem lies in the implementation of the 2.D, which consists of two substeps; using the tf.math.argmax() function to get the index of the maximum value of the x and consecutively, converting the x to an one_hot representation, using the tf.one_hot(). I believe I am not using the tf.one_hot() function correctly and I tried to find more supplementary information regarding using it, but I found them somehow complicated. Please let me know if you have a fairly straightforward explanation regarding this function and the way it is supposed to be used.

Looking forward to hearing from you soon.

Amirhossein Berenji

1 Like

If your tf.math.argmax() part is correct then the instructions tell you to use the results of it as one of the arguments in the tf.one_hot() function the other argument should be the shape of input values. After that you have still to add one more layer. As a hint look at which of the keras layers imported at the beginning of the notebook you have not used up to this point and its respective documentation.

1 Like

Dear laccdm thanks for your answer.

I passed the output of the tf.math.argmax() as the indices argument of the tf.one_hot() and I took the n_values declared earlier in the same cell as the depth argument of the tf.one_hot() function, but I don’t know what is the problem. The error is generated by the execution of the first unit test, after the music_inference_model() function, when the inference_summary = summary(inference_model) is being executed.

Looking forward to hearing from you soon.

Amirhossein Berenji

1 Like

There is one more layer after the one hot encoding please read carefully my comment above.

1 Like

Dear laccdm, I have read both instructions and your comment carefully and I believe you are addressing the use of the RepeatVector(), which is the 2.E step of this exercise and I had taken care of that before.

I was wondering if you could check my code, as it is almost two days now that I am stuck with this error.

Thanks for all you are going through to help me.

Amirhossein Berenji

1 Like

Send me your code privately so I check it and tell you what is wrong.

1 Like

Thanks dear laacdm, I sent it to you privately.

1 Like

You have to give to the argmax function the axis to reduce across (which is the last dimension). If you print the shape of a you will see that the second dimension is None and so you want to drop it.

16 Likes

Thanks a lot dear laacdm, I managed to set the axis parameter of the argmax function correctly and now it is working well.

I do appreciate all the troubles you have gone through to help me overcome the problem. Thank you so much.

3 Likes

No troubles at all :+1:

1 Like

Thank you anyway. You helped a lot.
By the way, I just submitted the assignment and it was accepted. :relaxed: :relieved:

1 Like

Hi,

Seems i have the similar issue:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-80-c395f100af16> in <module>
      1 # UNIT TEST
----> 2 inference_summary = summary(inference_model)
      3 comparator(inference_summary, music_inference_model_out)

~/work/W1A3/test_utils.py in summary(model)
     33     result = []
     34     for layer in model.layers:
---> 35         descriptors = [layer.__class__.__name__, layer.output_shape, layer.count_params()]
     36         if (type(layer) == Conv2D):
     37             descriptors.append(layer.padding)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in output_shape(self)
   2190                            'ill-defined for the layer. '
   2191                            'Use `get_output_shape_at(node_index)` '
-> 2192                            'instead.' % self.name)
   2193 
   2194   @property

AttributeError: The layer "lstm" has multiple inbound nodes, with different output shapes. Hence the notion of "output shape" is ill-defined for the layer. Use `get_output_shape_at(node_index)` instead.

… but shapes looks well:

x.shape after argmax: (None,)
x.shape after one_hot: (None, 90)
x.shape after RepeatVector: (None, 1, 90)

Do you have any idea where i can check for an error?
Thank you!

1 Like

Hi Leszek. My mistake was not using the tf.argmax() properly, you may consider checking that line. You can also read the comments left by laacdm, especially the ones that are written almost two days ago, as they are sufficient intitutive.

Hope you overcome this soon.

1 Like

I have the same problem as Leszek and I am using the last axis in the argmax function. x shapes are as follows:
x.shape after argmax: (None,)
x.shape after one_hot: (None, 90)
x.shape after RepeatVector: (None, 1, 90)

1 Like

After making change to argmax function, try re-run all the cells above. this may solve the “different output shapes” error.

6 Likes

great! thank you. restarting the kernel and re-running all cells solved the problem.

5 Likes

I had the same problems. All recommendations here are very useful. The last one, restarting the kernel and running again all cells, is necessary after many changes. Thanks.

8 Likes

I also had similar problems…

Thanks to @laacdm for the guidance

Also restarting the kernel after making changes is important at least in my case…

I don’t know the exact reasons but it may occur because of Keras storing values internal variables for faster calculations

1 Like