Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: Shape error in LSTM, The layer "lstm" has multiple inbound nodes, with different output shapes

I am getting this weird error about output shape for lstm layer. I have tried several things but not sure where I am doing mistake.

This question is from deep learning specialization from courser

''' def music_inference_model(LSTM_cell, densor, Ty=100):

    n_values = densor.units
    n_a = LSTM_cell.units
    
    x0 = Input(shape=(1, n_values))


    a0 = Input(shape=(n_a,), name='a0')
    c0 = Input(shape=(n_a,), name='c0')
    a = a0
    c = c0
    x = x0

    outputs = []

    for t in range(Ty):
        a, _, c = LSTM_cell(x, initial_state=[a, c])
        out = densor(a)
        outputs.append(out)
        x = tf.math.argmax(out)
        x = tf.one_hot(x,  depth=n_values)
        x = RepeatVector(1)(x)
    
    inference_model = Model([x0,a0,c0],outputs)



    return inference_model

inference_model = music_inference_model(LSTM_cell, densor, Ty = 50)

inference_summary = summary(inference_model) 
comparator(inference_summary, music_inference_model_out)

'''

But I am getting this error.

'''

AttributeError                            Traceback (most recent call last)
<ipython-input-21-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)
     34     result = []
     35     for layer in model.layers:
---> 36         descriptors = [layer.__class__.__name__, layer.output_shape,             layer.count_params()]
     37         if (type(layer) == Conv2D):
     38             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.

'''

like image 911
user9664490 Avatar asked Oct 23 '25 11:10

user9664490


1 Answers

Adding to the previous answer, it may be necessary to reload previous cells, plus the model itself to make it work properly:

# Reload this cell:
n_values = 90 # number of music values
reshaper = Reshape((1, n_values))                  # Used in Step 2.B of djmodel(), below
LSTM_cell = LSTM(n_a, return_state = True)         # Used in Step 2.C
densor = Dense(n_values, activation='softmax')     # Used in Step 2.D
like image 84
Toshiharutf Avatar answered Oct 25 '25 01:10

Toshiharutf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!