How to configure Keras model so training array can be larger than prediction arrays?
I have a Keras model that takes as input a 5-D array of geospatial-temporal data with an input shape (1, times, lats, lons, channels).
I want to train the model using more time steps than I'll typically use for the prediction input.
For example let's say my training dataset has 1000 time steps. After I've trained my model I will want to use it to make predictions on datasets that contain 100 timesteps.
How do I configure/train my model in a way that will allow for this flexibility in the time dimension?
My current model definition:
def define_model_cnn(num_times, num_lats, num_lons, num_features, num_labels): """ Define a model using convolutional neural network layers. Input data is expected to have shape (1, times, lats, lons, features) and output data will have shape (1, times, lats, lons, labels). :param num_times: the number of times in the input's time dimension :param num_lats: the number of lats in the input's lat dimension :param num_lons: the number of lons in the input's lon dimension :param num_features: the number of features (input attributes) in the input's channel dimension :param num_labels: the number of labels (output attributes) in the output's channel dimension :return: a Keras neural network model that uses convolutional layers """ # define the model cnn_model = Sequential() # add an initial 3-D convolutional layer cnn_model.add(Conv3D(filters=32, kernel_size=(3, 3, 3), activation="relu", data_format="channels_last", input_shape=(num_times, num_lats, num_lons, num_features), padding='same')) # add a fully-connected hidden layer with twice the number of neurons as input attributes (features) cnn_model.add(Dense(num_features * 2, activation='relu')) # output layer uses no activation function since we are interested # in predicting numerical values directly without transform cnn_model.add(Dense(num_labels)) # compile the model using the ADAM optimization algorithm and a mean squared error loss function cnn_model.compile(optimizer='adam', loss='mse') return cnn_model
I have no trouble using the above model for prediction when I have an input dataset with the same dimensions as the training input dataset, but it complains when I use an input dataset that has a different number of timesteps, i.e. the array has the same shape other than in the time dimension. For example I can train the model with an input array with shape (1, 720, 12, 23, 4) (720 time steps) but I get an error complaining about an unexpected input shape if I call
predict() with an input array with shape (1, 360, 12, 23, 4) (360 time steps).
Thanks for any insight/suggestions.