如何配置Keras模型,以便训练数组可以比预测数组大?

时间:2018-10-11 19:39:40

标签: python machine-learning keras

我有一个Keras模型,该模型以输入形状(1,时间,纬度,经度,通道)的5维地理时空数据作为输入。

我想用比通常用于预测输入的时间更多的时间步长训练模型。

例如,假设我的训练数据集有1000个时间步长。训练完模型后,我将要使用它对包含100个时间步长的数据集进行预测。

如何以一种在时间维度上具有这种灵活性的方式配置/训练我的模型?

我当前的模型定义:

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

当我具有与训练输入数据集相同维度的输入数据集时,使用上述模型进行预测就没有问题,但是当我使用具有不同时间步长的输入数据集时,即在数组具有除了时间维度外,其他形状相同。例如,我可以使用形状为(1,720,12,23,4)(720个时间步长)的输入数组训练模型,但是如果我用{输入数组,其形状为(1、360、12、23、4)(360个时间步长)。

感谢您的见解/建议。

0 个答案:

没有答案