简体   繁体   English

Keras 密集输入大小错误,展平返回(无,无)

[英]Keras dense input size error, flatten returns (None, None)

I'm trying to implement this model to generate midi music but I'm getting an error我正在尝试实现此模型以生成 MIDI 音乐,但出现错误

The last dimension of the inputs to `Dense` should be defined. Found `None`.

Here's my code这是我的代码

model = Sequential()
model.add(Bidirectional(LSTM(512, return_sequences=True), input_shape=(network_input.shape[1], network_input.shape[2])))
model.add(SeqSelfAttention(attention_activation='sigmoid'))
model.add(Dropout(0.3))
model.add( LSTM(512, return_sequences=True))
model.add(Dropout(0.3))
model.add(Flatten())
model.summary()
model.add(Dense(note_variants_count))
model.compile(loss='categorical_crossentropy', optimizer='adam')

And here's the summary before the dense layer这是密集层之前的总结


Model: "sequential_17"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
bidirectional_19 (Bidirectio (None, 100, 1024)         2105344   
_________________________________________________________________
seq_self_attention_20 (SeqSe (None, None, 1024)        65601     
_________________________________________________________________
dropout_38 (Dropout)         (None, None, 1024)        0         
_________________________________________________________________
lstm_40 (LSTM)               (None, None, 512)         3147776   
_________________________________________________________________
dropout_39 (Dropout)         (None, None, 512)         0         
_________________________________________________________________
flatten_14 (Flatten)         (None, None)              0         
=================================================================
Total params: 5,318,721
Trainable params: 5,318,721
Non-trainable params: 0
_________________________________________________________________

I think that the Flatten layer is causing the problem but I have no idea why it's returning a (None, None) shape.我认为 Flatten 层导致了问题,但我不知道为什么它返回 (None, None) 形状。

您必须为 Flatten 层提供除批次维度之外的所有维度。

Using RNN and 'return_sequence'使用 RNN 和“return_sequence”

For RNNs like LSTM, there is an option to either return the whole sequence or just the results.对于像 LSTM 这样的 RNN,可以选择返回整个序列或仅返回结果。 In this case you want just the results.在这种情况下,您只需要结果。 Changing just this one line只改变这一行

model.add(Dropout(0.3))
model.add(LSTM(512, return_sequences=False)) # <== change to False
model.add(Dropout(0.3))
model.add(Flatten())
model.summary()

Returns the following network shape from summary从摘要中返回以下网络形状

_________________________________________________________________
lstm_4 (LSTM)                (None, 512)               3147776   
_________________________________________________________________
dropout_3 (Dropout)          (None, 512)               0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 512)               0         
=================================================================

Notice the reduction of rank of the output tensor from Rank 3 to Rank 2. This is because this output is just that, the output, and not the whole sequence under consideration with all the hidden states.注意输出张量的秩从 Rank 3 降低到 Rank 2。这是因为这个输出只是那个输出,而不是考虑到所有隐藏状态的整个序列。

暂无
暂无

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

相关问题 (None,None,1) 输入在 keras 中如何工作? - How is the (None,None,1) input working in keras? Keras:计算模型输出与输入返回的导数[无] - Keras: calculating derivatives of model output wrt input returns [None] Tensorflow keras model_load 错误:ValueError:应定义“密集”输入的最后一个维度。 发现“无” - Tensorflow keras model_load error: ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None` Keras 张量流错误:ValueError:应定义“密集”输入的最后一个维度。 找到`无` - Keras Tensor Flow Error : ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None` 检查输入时出错:期望flatten_input具有3维,但数组的形状为(无,100、100、1) - Error when checking input: expected flatten_input to have 3 dimensions, but got array with shape (None, 100, 100, 1) Keras序列-ValueError:检查目标时出错:预期density_3具有形状(None,45),但具有形状的数组(2868700,1) - Keras Sequential - ValueError: Error when checking target: expected dense_3 to have shape (None, 45) but got array with shape (2868700, 1) 使用 Keras 将 (None, None, 2) 减少到 (None,2) - Reduce (None, None, 2) to (None,2) using Keras keras:形状(无,1)和(无,3)不兼容 - keras: Shapes (None, 1) and (None, 3) are incompatible 使用Keras的顺序密集网络上的输入尺寸误差 - Input dimension error on sequential dense network with Keras Keras模型密集输入形状投掷误差 - Keras Model Dense Input Shape Throwing Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM