首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Tensorflow : LSTM模型中的形状错误应为shape=(None,None,90),找到shape=[90,1,78]

Tensorflow : LSTM模型中的形状错误应为shape=(None,None,90),找到shape=[90,1,78]
EN

Stack Overflow用户
提问于 2021-05-14 06:41:27
回答 1查看 943关注 0票数 0

我在这段代码中得到了一个形状错误,并且无法找出我做错了什么,LSTM模型中的形状错误预期为shape=(无,无,90),找到shape=90,1,78

尝试检查每个形状请帮助此问题来自编程作业Coursera (深度学习专门化课程5)

代码语言:javascript
运行
复制
def music_inference_model(LSTM_cell, densor, Ty=100):
    """
    Uses the trained "LSTM_cell" and "densor" from model() to generate a sequence of values.
    
    Arguments:
    LSTM_cell -- the trained "LSTM_cell" from model(), Keras layer object
    densor -- the trained "densor" from model(), Keras layer object
    Ty -- integer, number of time steps to generate
    
    Returns:
    inference_model -- Keras model instance
    """
    
    # Get the shape of input values
    n_values = densor.units
    # Get the number of the hidden state vector
    n_a = LSTM_cell.units
    
    # Define the input of your model with a shape 
    x0 = Input(shape=(1, n_values))
    
    
    # Define s0, initial hidden state for the decoder LSTM
    a0 = Input(shape=(n_a,), name='a0')
    c0 = Input(shape=(n_a,), name='c0')
    a = a0
    c = c0
    x = x0

    ### START CODE HERE ###
    # Step 1: Create an empty list of "outputs" to later store your predicted values (≈1 line)
    outputs = []
    
    # Step 2: Loop over Ty and generate a value at every time step
    for t in range(Ty):
        # Step 2.A: Perform one step of LSTM_cell (≈1 line)
        a, _, c = LSTM_cell(x, initial_state=[a, c])
        
        # Step 2.B: Apply Dense layer to the hidden state output of the LSTM_cell (≈1 line)
        out = densor(_)
        # Step 2.C: Append the prediction "out" to "outputs". out.shape = (None, 90) (≈1 line)
        outputs.append(out)
 
        # Step 2.D: 
        # Select the next value according to "out",
        # Set "x" to be the one-hot representation of the selected value
        # See instructions above.
        x = tf.math.argmax(out)
        x = tf.one_hot(indices=x, depth=78) 
        # Step 2.E: 
        # Use RepeatVector(1) to convert x into a tensor with shape=(None, 1, 90)
        x = RepeatVector(1)(x)
        
    # Step 3: Create model instance with the correct "inputs" and "outputs" (≈1 line)
    inference_model = Model(inputs=[x0, a0, c0], outputs=outputs)
    
    ### END CODE HERE ###
    
    return inference_model

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

我得到的错误

代码语言:javascript
运行
复制
ValueError                                Traceback (most recent call last)
<ipython-input-19-a33998d93c7b> in <module>
----> 1 inference_model = music_inference_model(LSTM_cell, densor, Ty = 50)

<ipython-input-18-ead9bae0b252> in music_inference_model(LSTM_cell, densor, Ty)
     38     for t in range(Ty):
     39         # Step 2.A: Perform one step of LSTM_cell (≈1 line)
---> 40         a, _, c = LSTM_cell(x, initial_state=[a, c])
     41 
     42         # Step 2.B: Apply Dense layer to the hidden state output of the LSTM_cell (≈1 line)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
    707       # Perform the call with temporarily replaced input_spec
    708       self.input_spec = full_input_spec
--> 709       output = super(RNN, self).__call__(full_input, **kwargs)
    710       # Remove the additional_specs from input spec and keep the rest. It is
    711       # important to keep since the input spec was populated by build(), and

/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)
    225                                ' is incompatible with layer ' + layer_name +
    226                                ': expected shape=' + str(spec.shape) +
--> 227                                ', found shape=' + str(shape))
    228 
    229 

ValueError: Input 0 is incompatible with layer lstm: expected shape=(None, None, 90), found shape=[90, 1, 78]
EN

回答 1

Stack Overflow用户

发布于 2021-05-21 15:27:56

尝试在Step2.B和Step2.D中修复代码,如下所示:

这是你的代码:

代码语言:javascript
运行
复制
# Step 2.B: Apply Dense layer to the hidden state output of the LSTM_cell 
out = densor(_)

更改为:

代码语言:javascript
运行
复制
out = densor(a)

这是你的代码:

代码语言:javascript
运行
复制
# Step 2.D: 
x = tf.math.argmax(out)
x = tf.one_hot(indices=x, depth=78)         

更改为:

代码语言:javascript
运行
复制
x = tf.math.argmax(out, axis = -1)
x = tf.one_hot(x, depth=n_values)    

确保重新启动内核,然后重新运行修复的代码!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67527054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档