这是我第一次创建LSTM RNN。我对生成的“训练损失和验证损失”图的含义感到困惑。这是图形的图像:
Training and Validation Loss Graph
以下是训练数据的示例:
lat long trip_id mode_cat
datetime id
2011-08-27 06:13:01 20 39.979973 116.305745 1 1
2011-08-27 06:13:02 20 39.979957 116.305688 1 1
2011-08-27 06:13:03 20 39.979960 116.305693 1 1
2011-08-27 06:13:04 20 39.979970 116.305717 1 1
2011-08-27 06:13:05 20 39.979985 116.305732 1 1
日期时间和ID(user_id)设置为索引。
以下是用于创建移动窗口和LSTM的代码:
def moving_window(dataset_x, dataset_y, past_history):
data, labels = [], []
for i in range(past_history, len(dataset_x)):
indices = range(i-past_history, i)
data.append(dataset_x[indices])
labels.append(dataset_y[i])
return np.array(data), np.array(labels)
past_history = 60
x_train_single, y_train_single = moving_window(dataset_train_x, dataset_train_y, past_history)
x_test_single, y_test_single = moving_window(dataset_test_x, dataset_test_y, past_history)
buffer_size = len(x_train_single)//10
batch_size = 256
train_data_single = tf.data.Dataset.from_tensor_slices((x_train_single, y_train_single))
train_data_single = train_data_single.cache().shuffle(buffer_size).batch(batch_size).repeat()
test_data_single = tf.data.Dataset.from_tensor_slices((x_test_single, y_test_single))
test_data_single = test_data_single.batch(batch_size).repeat()
single_step_model = tf.keras.models.Sequential()
single_step_model.add(tf.keras.layers.LSTM(32,
input_shape=x_train_single.shape[-2:]))
single_step_model.add(tf.keras.layers.Dense(1))
single_step_model.compile(optimizer=tf.keras.optimizers.RMSprop(), loss='mae')
evaluation_interval = len(x_train_single)//batch_size
epochs = 10
single_step_history = single_step_model.fit(train_data_single, epochs=epochs,
steps_per_epoch=evaluation_interval,
validation_data=test_data_single,
validation_steps=50)