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

kjthegm6  于 2022-12-23  发布在  其他
关注(0)|答案(2)|浏览(177)

我得到这个奇怪的错误输出形状的lstm层。我已经尝试了几件事,但不确定我在哪里做错了。
这个问题来自深度学习专业化课程
“”“定义音乐推理模型(LSTM单元,密度,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.

'''

hs1ihplo

hs1ihplo1#

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

# 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
e37o9pze

e37o9pze2#

我被同一个问题困了甚至几个小时,终于找到了解决它的方法。
将此行中的代码更改为:

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

相关问题