0

I want to design a BiLstm-Alexnet Model. I wrote the below code for defining the model architecture:

model= Sequential()
model.add(Reshape((450,2,2),input_shape=(900,1,2)))
model.add(Conv2D(456, kernel_size=(11,1), strides=(1,1), padding="same", activation="relu", kernel_initializer="he_normal",
                kernel_regularizer=l2(weight), bias_regularizer=l2(weight),input_shape=(900,1,2)))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(3,1)))
    model.add(Conv2D(256, kernel_size=(5,1), strides=1, padding="same", activation="relu", kernel_initializer="he_normal",
                kernel_regularizer=l2(weight), bias_regularizer=l2(weight)))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(3,1)))   
    model.add(Conv2D(384, kernel_size=(3,1), strides=(1,1), padding="same", activation="relu", kernel_initializer="he_normal",
                kernel_regularizer=l2(weight), bias_regularizer=l2(weight)))
    model.add(BatchNormalization())
    model.add(Conv2D(384, kernel_size=(3,1), strides=1, padding="same", activation="relu", kernel_initializer="he_normal",
                kernel_regularizer=l2(weight), bias_regularizer=l2(weight)))
    model.add(BatchNormalization())
    model.add(Conv2D(256, kernel_size=(3,1), strides=1, padding="same", activation="relu", kernel_initializer="he_normal",
                kernel_regularizer=l2(weight), bias_regularizer=l2(weight)))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(3,1),strides=(2,1))) 

    model.add(Permute((2,1,3)))
    #.add(Embedding(max_features, embedding_size, input_length=maxlen*max_charlen))
    model.add(Reshape((2,4*256))) 

    model.add(Bidirectional(LSTM(64, return_sequences=True)))
    model.add(Flatten())
    model.add(Dense(18, activation="relu"))

    model.add(Dense(2, activation="softmax"))
    return model

When I try to compile the model, I am getting the following error:

ValueError: Exception encountered when calling layer "reshape_5" (type Reshape).

total size of new array must be unchanged, input_shape = [2, 24, 256], output_shape = [2, 1024]

Call arguments received by layer "reshape_5" (type Reshape):
  • inputs=tf.Tensor(shape=(None, 2, 24, 256), dtype=float32)

Which layer is called reshape_5? Why I am receiving this error? How can I fix my Error?

Gugu72
  • 2,052
  • 13
  • 35
  • Please format your code and the error messages properly. It's hard to read, and therefore difficult to help you. – Krokomot May 19 '23 at 22:01

1 Answers1

0

I fixed it by changing the input of second model size. In part 2 of this combinational model the output of first network is a 2 * 24 * 256 tensor. I had to change model.add(Reshape((2, 4 * 256))) to
model.add(Reshape((2, 24 * 256)))

Daraan
  • 1,797
  • 13
  • 24