Tensoflow:形状必须至少为 3 级,但对于 '{{node bidirectional/forward_lstm/lstm_cell_1/BiasAdd}} 为 2 级

问题描述 投票:0回答:0

我正在尝试在 tensorflow keras 中使用形状为 (2,128,128,64) (

channels_first
) 的多模式 3D 图像构建模型。代码如下:

tf.keras.backend.set_image_data_format('channels_first')   

inputs = keras.Input((2, 128, 128, 64))

x = layers.Conv3D(filters=16, kernel_size=3, activation='relu' , name='conv1')(inputs) #32
x = layers.Conv3D(filters=32, kernel_size=3, activation="relu",name='conv2')(x) #64
x = layers.MaxPool3D(pool_size=2)(x)
x = GlobalAveragePooling3D()(x)
x = Dense(1024,activation='relu')(x)
x = Dropout(0.3)(x)

dense2 = Dense(2, activation='softmax')(x) 

x = RepeatVector(2)(x)
LSTM1 = Bidirectional(LSTM(64,activation='relu',return_sequences=True,dropout=0.2,recurrent_dropout=0.2))(x)
LSTM1 = Bidirectional(LSTM(32, activation='relu',return_sequences=True,dropout=0.2,recurrent_dropout=0.2))(LSTM1)
dense4 = TimeDistributed(Dense(1024, activation='relu'))(LSTM1)
dense1 = TimeDistributed(Dense(3, activation='softmax'))(dense4) 
model = Model(inputs, outputs=[dense1,dense2])

我在 LSTM 层中收到以下值错误。

ValueError: Exception encountered when calling layer "lstm_cell_1" (type LSTMCell).

Shape must be at least rank 3 but is rank 2 for '{{node bidirectional/forward_lstm/lstm_cell_1/BiasAdd}} = BiasAdd[T=DT_FLOAT, data_format="NCHW"](bidirectional/forward_lstm/lstm_cell_1/MatMul, bidirectional/forward_lstm/lstm_cell_1/split_1)' with input shapes: [?,64], [64].

Call arguments received:
  • inputs=tf.Tensor(shape=(None, 1024), dtype=float32)
  • states=('tf.Tensor(shape=(None, 64), dtype=float32)', 'tf.Tensor(shape=(None, 64), dtype=float32)')
  • training=False

我看到这篇文章 ValueError: Shape must be at least rank 3 but is rank 2 for '{{node BiasAdd}} = BiasAdd[T=DT_FLOAT, data_format="NCHW"](add, bias)' 输入形状: 建议使用“channels_last”格式,但是有没有办法训练模型保持“channels_first”格式?

python tensorflow keras deep-learning lstm
© www.soinside.com 2019 - 2024. All rights reserved.