I'm modifying an old code, by adding attention layer to a model. But I'm not able to figure out how to stack the layers with correct input size.
The actual input data would be (200,189,1).
//I'm trying something like this
def mocap_model(optimizer='SGD'):
model = Sequential()
model.add(Conv2D(32, 3, strides=(2, 2), padding ='same', input_shape=(200, 189, 1)))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Conv2D(64, 3, strides=(2, 2), padding ='same'))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Conv2D(64, 3, strides=(2, 2), padding ='same'))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Conv2D(128, 3, strides=(2, 2), padding ='same'))
model.add(Dropout(0.2))
model.add(Flatten())
return model
cnn = mocap_model()
main_input = Input(shape=(200, 189, 1))
rnn = Sequential()
rnn = LSTM(256, return_sequences=True, input_shape=(200,189))
model = TimeDistributed(cnn)(main_input)
model = rnn(model)
att_in=LSTM(256,return_sequences=True,dropout=0.3,recurrent_dropout=0.2)(model)
att_out=attention()(att_in)
output3=Dense(256,activation='relu',trainable=True)(att_out)
output4=Dense(4,activation='softmax',trainable=True)(output3)
model=Model(main_input,output4)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
But I get this error:
----> 8 model = TimeDistributed(cnn)(main_input)
ValueError: Input 0 of layer sequential_40 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 189, 1)
Problem with Input shape. tf.keras.layers.TimeDistributed
expects batch size as input. Expects inputs: Input tensor of shape (batch, time, ...)
. In the main_input add batch_size
main_input = Input(shape=(10, 200, 189, 1))
Working sample code
import tensorflow as tf
cnn = tf.keras.Sequential()
cnn.add(tf.keras.layers.Conv2D(64, 1, 1, input_shape=(200, 189, 1)))
cnn.add(tf.keras.layers.Flatten())
cnn.output_shape
main_input = tf.keras.Input(shape=(10, 200, 189, 1))
outputs = tf.keras.layers.TimeDistributed(cnn)(main_input)
outputs.shape
Output
TensorShape([None, 10, 2419200])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.