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:
But I would like the output to be as follows:
CodePudding user response:
You should replace,
tf.reshape(x, [1, 4, 25]) with keras.layers.Reshape([4, 25])(x)