繁体   English   中英

Tensorflow:LSTM 中的形状错误,“lstm”层有多个入站节点,输出形状不同

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

我收到关于 lstm 层输出形状的奇怪错误。 我尝试了几件事,但不确定我在哪里做错了。

本题来自courser的deep learning specialization

''' 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)

'''

但是我收到了这个错误。

'''

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.

'''

添加到以前的答案,可能需要重新加载以前的单元格,加上模型本身才能使其正常工作:

# 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

我在同一个问题上被困了几个小时,终于找到了解决方法。

将此行中的代码更改为:

x = tf.math.argmax(out, axis = -1)   # the axis is crucial!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM