Home > Software design >  Keep the batch dimension of Conv1D Variable
Keep the batch dimension of Conv1D Variable

Time:02-02

None in TensorFlow/keras refers to variable dimension. I would like to build a Conv1D layer where the dimension of the batch is variable as (None, 4, 20), please. The input to the Conv1D layer has to be 3D, but the previous layer has only a 1D vector of size 100, so I did is to use TensorFlow.reshape(input, (1, 4, 25)) then I flattened the output of Conv1D back as the input to the next layer after Conv1D should be (None, 100).

import tensorflow as tf
import tensorflow.keras.layers 
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, LSTM, Attention, Flatten, Dense, Dropout, Lambda, Bidirectional, Conv1D, GlobalMaxPooling1D

def initialize_base_network():
    #passing input to layers
    input = Input(shape=(100,), name="base_input")
    x = Flatten(name="flatten_input")(input)
    x = Dropout(0.2, name="first_dropout")(x)
    x = Attention(use_scale=True)([x,x])
    x = tf.reshape(x, [1, 4, 25])
    x = Conv1D(filters=16, kernel_size=3, activation='relu')(x)
    x = Flatten(name="flatten_input2", input_shape=(None,32))(x)
    x = Dense(128, activation='relu', name="first_base_dense")(x)
    x = Dense(128, activation='relu', name="second_base_dense")(x)
    x = Dropout(0.1, name="second_dropout")(x)
    x = Dense(128, activation='relu', name="third_base_dense")(x)
    
    #return the model
    return Model(inputs=input, outputs=x)

base_network = initialize_base_network()
#plot_model(base_network, show_shapes=True, show_layer_names=True, to_file='base-model.png')
base_network.summary()

Output:

enter image description here

But I would like the output to be as follows:

enter image description here

CodePudding user response:

You should replace,

tf.reshape(x, [1, 4, 25]) with keras.layers.Reshape([4, 25])(x)
  • Related