如何在Keras ConvLSTM层中处理输入数据

时间:2018-10-12 05:16:58

标签: python keras deep-learning

我有一个14512天的网格数据集,网格大小为25 *53。预测时间为14天。我尝试将它们放入Keras ConvLSTM层,但结果似乎不正确。如何处理输入数据?

预处理数据: 数据的形状为(14512,25,53)

fh = Dataset('slp.nc', mode='r')
length = len(fh.variables['time'][:])
lat = len(fh.variables['lat'][:])
lon = len(fh.variables['lon'][:])
data = [[[0 for _ in range(0, lon)] for _ in range(0, lat)] for _ in range(0, length)]
for i in range(0, length):
    item = fh.variables['slp'][i]
    for j in range(0, lat):
        for k in range(0, lon):
            data[i][j][k] = item[j][k]
data = pd.DataFrame(data)
data.drop([8665], inplace=True)

然后定义模型:

model = Sequential()
model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3), input_shape=(None, 25, 53, 1), padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3), padding='same',   return_sequences=True))
model.add(BatchNormalization())
model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3), padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3), padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(Conv3D(filters=1, kernel_size=(3, 3, 3), activation='sigmoid', padding='same', data_format='channels_last'))
model.compile(loss='binary_crossentropy', optimizer='adadelta')

然后生成输入数据并进行训练:

train_x = np.zeros((t_day, 14, 25, 53, 1),dtype = np.float)
train_y = np.zeros((t_day, 14, 25, 53, 1),dtype = np.float)
for i in range(t_day):
    for j in range(0, 14):
        for a in range(25):
            for b in range(53):
                if i + j > 13:
                    train_x[i][j][a][b][0] = data[i-14+j][a][b]
                if i + j < 13:
                    train_x[i][j][a][b][0] = 0
                train_y[i][j][a][b][0] = data[i+14+j][a][b]
model.fit(train_x, train_y, epochs=100, batch_size=128, validation_split=0.05, verbose=1, shuffle=False)

但是损失函数似乎不正确:

Epoch 1/100

10/11400 [..............................] - ETA: 2:56:36 - loss: -3.7733
20/11400 [..............................] - ETA: 2:18:04 - loss: -110.7391
30/11400 [..............................] - ETA: 2:04:56 - loss: -322.3076
40/11400 [..............................] - ETA: 1:58:30 - loss: -554.6822
50/11400 [..............................] - ETA: 1:54:23 - loss: -746.9508
60/11400 [..............................] - ETA: 1:51:40 - loss: -932.8895
70/11400 [..............................] - ETA: 1:49:53 - loss: -1141.2996
80/11400 [..............................] - ETA: 1:48:23 - loss: -1368.9223
90/11400 [..............................] - ETA: 1:47:08 - loss: -1579.1898
100/11400 [..............................] - ETA: 1:46:08 - loss: -1773.8168
110/11400 [..............................] - ETA: 1:45:18 - loss: -1990.5375
120/11400 [..............................] - ETA: 1:44:37 - loss: -2189.3257
130/11400 [..............................] - ETA: 1:44:01 - loss: -2364.1422
140/11400 [..............................] - ETA: 1:43:36 - loss: -2579.9297
150/11400 [..............................] - ETA: 1:43:05 - loss: -2818.1180
160/11400 [..............................] - ETA: 1:42:40 - loss: -3001.0507
170/11400 [..............................] - ETA: 1:42:17 - loss: -3173.8332
180/11400 [..............................] - ETA: 1:41:55 - loss: -3358.1284
190/11400 [..............................] - ETA: 1:41:38 - loss: -3541.8308
200/11400 [..............................] - ETA: 1:41:20 - loss: -3733.1575
210/11400 [..............................] - ETA: 1:41:09 - loss: -3941.8186
220/11400 [..............................] - ETA: 1:40:54 - loss: -4164.8925
230/11400 [..............................] - ETA: 1:40:41 - loss: -4422.5382

1 个答案:

答案 0 :(得分:0)

您确实解释了模型的目的。从代码中,我认为这是一个回归任务。因此,您不应使用用于分类的“ binary_crossentropy”损失函数。您应该使用损失函数进行回归,例如mean_squared_error或mean_absolute_error。您可以参考Keras document的页面。