为什么测试集上的 MSE 非常低并且似乎没有进化(增加时代后不增加)
why MSE on test set is very low and doesn't seem to evolve (not increasing after increasing epochs)
我正在研究使用 LSTM 预测股票价值的问题。
我的工作基于以下 project 。
我使用总长度为 12075 的数据集(股票价格的时间序列),我将其分为训练集和测试集(几乎 10%)。它与 link 项目中使用的相同。
train_data.shape
(11000,)
test_data.shape
(1075,)
在我们的模型中,我们首先在多对多 lstm 模型上对其进行训练,其中我们提供 N 个输入序列(股票价格)和 N 个标签序列(通过对 train_data 分为 N 段作为输入和标签被采样为输入的以下值序列)。
然后我们开始分别预测每个值,并在下次将其作为输入提供,直到我们达到 num_predictions 个预测。
损失只是预测值和实际值之间的均方误差。
最后的预测好像还不错。但是,我只是不明白为什么训练误差会急剧下降,而测试误差总是非常非常低(尽管它一直在下降很少)。我知道通常情况下,由于过度拟合,测试错误也应该在经过一些时期后开始增加。我用更简单的代码和不同的数据集进行了测试,我遇到了相对相似的 MSE 图。
这是我的鬃毛环:
for ep in range(epochs):
# ========================= Training =====================================
for step in range(num_batches):
u_data, u_labels = data_gen.unroll_batches()
feed_dict = {}
for ui,(dat,lbl) in enumerate(zip(u_data,u_labels)):
feed_dict[train_inputs[ui]] = dat.reshape(-1,1)
feed_dict[train_outputs[ui]] = lbl.reshape(-1,1)
feed_dict.update({tf_learning_rate: 0.0001, tf_min_learning_rate:0.000001})
_, l = session.run([optimizer, loss], feed_dict=feed_dict)
average_loss += l
# ============================ Validation ==============================
if (ep+1) % valid_summary == 0:
average_loss = average_loss/(valid_summary*num_batches)
# The average loss
if (ep+1)%valid_summary==0:
print('Average loss at step %d: %f' % (ep+1, average_loss))
train_mse_ot.append(average_loss)
average_loss = 0 # reset loss
predictions_seq = []
mse_test_loss_seq = []
# ===================== Updating State and Making Predicitons ========================
for w_i in test_points_seq:
mse_test_loss = 0.0
our_predictions = []
if (ep+1)-valid_summary==0:
# Only calculate x_axis values in the first validation epoch
x_axis=[]
# Feed in the recent past behavior of stock prices
# to make predictions from that point onwards
for tr_i in range(w_i-num_unrollings+1,w_i-1):
current_price = all_mid_data[tr_i]
feed_dict[sample_inputs] = np.array(current_price).reshape(1,1)
_ = session.run(sample_prediction,feed_dict=feed_dict)
feed_dict = {}
current_price = all_mid_data[w_i-1]
feed_dict[sample_inputs] = np.array(current_price).reshape(1,1)
# Make predictions for this many steps
# Each prediction uses previous prediciton as it's current input
for pred_i in range(n_predict_once):
pred = session.run(sample_prediction,feed_dict=feed_dict)
our_predictions.append(np.asscalar(pred))
feed_dict[sample_inputs] = np.asarray(pred).reshape(-1,1)
if (ep+1)-valid_summary==0:
# Only calculate x_axis values in the first validation epoch
x_axis.append(w_i+pred_i)
mse_test_loss += 0.5*(pred-all_mid_data[w_i+pred_i])**2
session.run(reset_sample_states)
predictions_seq.append(np.array(our_predictions))
mse_test_loss /= n_predict_once
mse_test_loss_seq.append(mse_test_loss)
if (ep+1)-valid_summary==0:
x_axis_seq.append(x_axis)
current_test_mse = np.mean(mse_test_loss_seq)
# Learning rate decay logic
if len(test_mse_ot)>0 and current_test_mse > min(test_mse_ot):
loss_nondecrease_count += 1
else:
loss_nondecrease_count = 0
if loss_nondecrease_count > loss_nondecrease_threshold :
session.run(inc_gstep)
loss_nondecrease_count = 0
print('\tDecreasing learning rate by 0.5')
test_mse_ot.append(current_test_mse)
#print('\tTest MSE: %.5f'%np.mean(mse_test_loss_seq))
print('\tTest MSE: %.5f' % current_test_mse)
predictions_over_time.append(predictions_seq)
print('\tFinished Predictions')
epochs_evolution.append(ep+1)
这正常吗?我应该只增加测试集的大小吗?有没有做错什么?关于如何 test/investigate 有什么想法吗?
上述训练和测试 MSE 差异背后的原因是我们计算的不是同一件事。在训练期间,MSE 是训练数据中每个样本随时间步长的误差总和的平均值,因此它很大。在测试期间,我们进行 N=50 次预测并计算预测值与实际值之间的平均误差。这个平均值总是很小,在上图中几乎保持不变。
我正在研究使用 LSTM 预测股票价值的问题。
我的工作基于以下 project 。 我使用总长度为 12075 的数据集(股票价格的时间序列),我将其分为训练集和测试集(几乎 10%)。它与 link 项目中使用的相同。
train_data.shape (11000,)
test_data.shape
(1075,)
在我们的模型中,我们首先在多对多 lstm 模型上对其进行训练,其中我们提供 N 个输入序列(股票价格)和 N 个标签序列(通过对 train_data 分为 N 段作为输入和标签被采样为输入的以下值序列)。
然后我们开始分别预测每个值,并在下次将其作为输入提供,直到我们达到 num_predictions 个预测。
损失只是预测值和实际值之间的均方误差。
最后的预测好像还不错。但是,我只是不明白为什么训练误差会急剧下降,而测试误差总是非常非常低(尽管它一直在下降很少)。我知道通常情况下,由于过度拟合,测试错误也应该在经过一些时期后开始增加。我用更简单的代码和不同的数据集进行了测试,我遇到了相对相似的 MSE 图。
这是我的鬃毛环:
for ep in range(epochs):
# ========================= Training =====================================
for step in range(num_batches):
u_data, u_labels = data_gen.unroll_batches()
feed_dict = {}
for ui,(dat,lbl) in enumerate(zip(u_data,u_labels)):
feed_dict[train_inputs[ui]] = dat.reshape(-1,1)
feed_dict[train_outputs[ui]] = lbl.reshape(-1,1)
feed_dict.update({tf_learning_rate: 0.0001, tf_min_learning_rate:0.000001})
_, l = session.run([optimizer, loss], feed_dict=feed_dict)
average_loss += l
# ============================ Validation ==============================
if (ep+1) % valid_summary == 0:
average_loss = average_loss/(valid_summary*num_batches)
# The average loss
if (ep+1)%valid_summary==0:
print('Average loss at step %d: %f' % (ep+1, average_loss))
train_mse_ot.append(average_loss)
average_loss = 0 # reset loss
predictions_seq = []
mse_test_loss_seq = []
# ===================== Updating State and Making Predicitons ========================
for w_i in test_points_seq:
mse_test_loss = 0.0
our_predictions = []
if (ep+1)-valid_summary==0:
# Only calculate x_axis values in the first validation epoch
x_axis=[]
# Feed in the recent past behavior of stock prices
# to make predictions from that point onwards
for tr_i in range(w_i-num_unrollings+1,w_i-1):
current_price = all_mid_data[tr_i]
feed_dict[sample_inputs] = np.array(current_price).reshape(1,1)
_ = session.run(sample_prediction,feed_dict=feed_dict)
feed_dict = {}
current_price = all_mid_data[w_i-1]
feed_dict[sample_inputs] = np.array(current_price).reshape(1,1)
# Make predictions for this many steps
# Each prediction uses previous prediciton as it's current input
for pred_i in range(n_predict_once):
pred = session.run(sample_prediction,feed_dict=feed_dict)
our_predictions.append(np.asscalar(pred))
feed_dict[sample_inputs] = np.asarray(pred).reshape(-1,1)
if (ep+1)-valid_summary==0:
# Only calculate x_axis values in the first validation epoch
x_axis.append(w_i+pred_i)
mse_test_loss += 0.5*(pred-all_mid_data[w_i+pred_i])**2
session.run(reset_sample_states)
predictions_seq.append(np.array(our_predictions))
mse_test_loss /= n_predict_once
mse_test_loss_seq.append(mse_test_loss)
if (ep+1)-valid_summary==0:
x_axis_seq.append(x_axis)
current_test_mse = np.mean(mse_test_loss_seq)
# Learning rate decay logic
if len(test_mse_ot)>0 and current_test_mse > min(test_mse_ot):
loss_nondecrease_count += 1
else:
loss_nondecrease_count = 0
if loss_nondecrease_count > loss_nondecrease_threshold :
session.run(inc_gstep)
loss_nondecrease_count = 0
print('\tDecreasing learning rate by 0.5')
test_mse_ot.append(current_test_mse)
#print('\tTest MSE: %.5f'%np.mean(mse_test_loss_seq))
print('\tTest MSE: %.5f' % current_test_mse)
predictions_over_time.append(predictions_seq)
print('\tFinished Predictions')
epochs_evolution.append(ep+1)
这正常吗?我应该只增加测试集的大小吗?有没有做错什么?关于如何 test/investigate 有什么想法吗?
上述训练和测试 MSE 差异背后的原因是我们计算的不是同一件事。在训练期间,MSE 是训练数据中每个样本随时间步长的误差总和的平均值,因此它很大。在测试期间,我们进行 N=50 次预测并计算预测值与实际值之间的平均误差。这个平均值总是很小,在上图中几乎保持不变。