I have a question regarding the input shape for Dense layers in Keras. I have received a code that uses Dense layers to solve a timeseries prediction problem.
The input data, x_train
array has shape (5829, 18)
. After this, there's this piece of code:
# Reshaping the input data
max_batch_size = x_train.shape[0]
timesteps = x_train.shape[1]
input_dim = 1
x_train = x_train.reshape(max_batch_size, timesteps, input_dim)
The code above, was used to reshape the input data to train a Conv1D
in Keras. But it is still being used to train this following MLP model:
model = Sequential(name='MLP')
model.add(InputLayer(input_shape=(x_train.shape[1]), name='Input_Layer'))
model.add(Dense(units=2*x_train.shape[1], activation='tanh', name='Dense_1'))
model.add(Dense(units=2*y_train.shape[1], activation='tanh', name='Dense_2'))
model.add(Dense(units=y_train.shape[1], activation='tanh', name='Output_Layer'))
model.compile(optimizer='adam', loss="mse")
model.summary()
It is wrong to do that reshape for the MLP model? Could it lead to wrong results? Or it does not matter?
The reshape in itself won't cause any problem: Dense layers don't assume any structural information. If you look at many image classifier networks you'll see they have several convolutional layers, then a dense layer, with the tensors generally flattened beforehand. Each neuron in the dense layer is equally connected to ALL* neurons in the previous layers, and the weights are trained to fit the output. Unlike a convolutional layer, no assumption is made about structure.
However, quite apart from the dense layer, your reshape does seem slightly strange: It seems strange that the max batch size is 5829 and there are 18 time steps. It seems far more likely that you have 5829 time steps and 18 features per step. But obviously this is just a guess and I don't know your data.
* At least in a standard neural network.