train_X = train_x.reshape((train_x.shape[0], n_hours, n_features)) test_X = test_x.reshape((test_x.shape[0], n_hours, n_features)) model = Sequential() model.add(LSTM(20, input_shape=(train_X.shape[1], train_X.shape[2]), return_sequences=True, kernel_regularizer=regularizers.l2(0.005), recurrent_regularizer=regularizers.l2(0.005))) model.add(LSTM(20, kernel_regularizer=regularizers.l2(0.005), recurrent_regularizer=regularizers.l2(0.005))) model.add(Dense(1)) model.compile(loss='mae', optimizer='adam') history = model.fit(train_X, train_y, epochs=500, batch_size=2 ** 8, validation_data=(test_X, test_y)) plt.plot(history.history['loss'], label='train') plt.plot(history.history['val_loss'], label='test') plt.legend() plt.show() # make the prediction,为了在原始数据的维度上计算损失,需要将数据转化为原来的范围再计算损失 yHat = model.predict(test_X) y = model.predict(train_X) test_X = test_X.reshape((test_X.shape[0], n_hours * n_features))怎么求训练集rmse
时间: 2023-06-18 14:06:55 浏览: 156
可以使用以下代码计算训练集的 RMSE:
```
from sklearn.metrics import mean_squared_error
import numpy as np
# 将预测结果转换为原始范围
yHat = scaler.inverse_transform(yHat)
train_y = scaler.inverse_transform(train_y)
# 计算 RMSE
rmse = np.sqrt(mean_squared_error(train_y, y))
print('Train RMSE: %.3f' % rmse)
```
其中,`scaler` 是对数据进行标准化或归一化处理时使用的对象,需要根据具体情况进行定义和初始化。
相关问题
values = reframed.values n_train_hours = 365 * 24 train = values[:n_train_hours, :] test = values[n_train_hours:, :] # 分离出特征集与标签 train_X, train_y = train[:, :-1], train[:, -1] test_X, test_y = test[:, :-1], test[:, -1] # 转换成3维数组 [样本数, 时间步 ,特征数] train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1])) test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1])) print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
这段代码是将数据集划分为训练集和测试集,并将其转换成神经网络模型需要的3维数组形式。其中,n_train_hours变量表示训练集的时间跨度,train和test分别表示训练集和测试集的特征和标签。train_X和test_X表示训练集和测试集的特征集,train_y和test_y表示训练集和测试集的标签。最后,将训练集和测试集的特征集转换成3维数组形式,使其可以被适配到神经网络模型中。
# 划分训练集和测试集,70% 作为训练集 train_size = int(len(data_X) * 0.7) test_size = len(data_X) - train_size train_X = data_X[:train_size] train_Y = data_Y[:train_size] test_X = data_X[train_size:] test_Y = data_Y[train_size:] train_X = train_X.reshape(-1, 1, 2) train_Y = train_Y.reshape(-1, 1, 1) test_X = test_X.reshape(-1, 1, 2) train_x = torch.from_numpy(train_X) train_y = torch.from_numpy(train_Y) test_x = torch.from_numpy(test_X)
这段代码是在将数据集划分为训练集和测试集后,将其进行了reshape操作,然后将numpy数组转化为了PyTorch tensor。其中train_X和test_X是输入特征,train_Y和test_Y是对应的标签。reshape操作将二维的输入特征变成了三维的tensor,第一个维度表示样本数量,第二个维度表示每个样本只有一个特征,第三个维度表示每个特征有两个值。同样地,对于标签train_Y和test_Y,reshape操作将其从二维变成了三维tensor,第一个维度表示样本数量,第二个维度表示每个样本只有一个标签,第三个维度表示每个标签只有一个值。最后,将numpy数组转化为PyTorch tensor,可以方便地用于模型的训练和评估。
阅读全文