这是我第一次尝试创建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
Datetime和Id(用户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)
如果训练损失和验证损失之间的差距较大,则表示模型拟合过度;如果训练损失较大,则表示模型拟合不足。 如果您的培训损失和验证损失重叠或彼此接近,则表示您的模型现在适于预测
这里的模型安装过度了
相关问题 更多 >
编程相关推荐