Keras: How to define input shape for 1st DENSE layer?

Viewed 133

I am new to deep learning & keras.

Refer to below code.

  1. I want to confirm the terminology. Before processing to create batches by the TimeSeries Generator, there are 10 samples. After processing by the Generator, am I correct to say there are 8 samples in 1 batch?

  2. I don't understand why yhat differs when I define the 1st layer input shape as 'input_shape' vs 'input_dim'. yhat should only be (1,1) - a single value.

  3. If instead, I use a simple RNN layer as my 1st layer, what should the inputs shape be?

Thank you

# univariate one step problem with mlp
from numpy import array
from keras.models import Sequential
from keras.layers import Dense
from keras.preprocessing.sequence import TimeseriesGenerator

# define dataset
series = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # 10 samples before processing by the Generator

# define generator
timestep = 2
generator = TimeseriesGenerator(series, series, length=timestep, batch_size=8)


# number of batch
print('Batches: %d' % len(generator))
# OUT --> Batches: 1

# print each batch
for i in range(len(generator)):
   x, y = generator[i]
   print('%s => %s' % (x, y))

#OUT:
[[1 2]
 [2 3]
 [3 4]
 [4 5]
 [5 6]
 [6 7]
 [7 8]
 [8 9]] => [ 3  4  5  6  7  8  9 10]
   
#After processing by the Generator, there are 8 samples in 1 batch. 

x, y = generator[0]
print(x.shape)
    
# define model
model = Sequential()

#TensorFlow assumes the first dimension is the batch_size which can have any size so you don't need to define it. The 2nd D is the number of time steps. The 3rd D is the number of features

#1st LAYER with input shape defined by input_shape
#model.add(Dense(100, activation='relu', input_shape= (timestep,1)))

#1st LAYER with input shape defined by input_dim
model.add(Dense(100, activation='relu', input_dim=timestep))

model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')

# fit model
model.fit_generator(generator, steps_per_epoch=1, epochs=200, verbose=0)
# make a one step prediction out of sample
x_input = array([9, 10]).reshape((1, timestep))
print(x_input.shape)

yhat = model.predict(x_input, verbose=0)
print(yhat)

# OUT: [[9.3066435, 10.239568]] if 1st layer's shape is input_shape
# OUT: [[11.545249]] if 1st layer's shape is input_dim
0 Answers
Related