无法绘制 MAPE 和 MSE 的训练和测试值?
Can't plot training and testing values of MAPE and MSE?
我正在研究预测风速的代码。
起初,我使用 print(history.history.keys()) 来打印损失、val_loss、mape 和 val_mean_absolute_percentage_error 值,但是,它只显示 dict_keys(['loss', 'mape'])。
然后,由于它没有 val_loss 和 val_mean_absolute_percentage_error 值,它会显示 KeyError: ‘val_mean_absolute_percentage_error’
你能帮帮我吗?
这是我的代码:
from __future__ import print_function
from sklearn.metrics import mean_absolute_error
import math
import numpy as np
import matplotlib.pyplot as plt
from pandas import read_csv
from keras.models import Sequential
from keras.layers import Dense, LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return np.array(dataX), np.array(dataY)
# fix random seed for reproducibility
np.random.seed(7)
# load the dataset
dataframe = read_csv(‘OND_Q4.csv’, usecols=[7], engine=’python’, header=3)
dataset = dataframe.values
print(dataframe.head)
dataset = dataset.astype(‘float32′)
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.7) # Use 70% of data to train
test_size = len(dataset) – train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
#compile model
model.compile(loss=’mean_squared_error’, optimizer=’adam’,metrics=[‘mape’])
history=model.fit(trainX, trainY, epochs=5, batch_size=1, verbose=2)
# list all data in history
print(history.history.keys())
train_MAPE = history.history[‘mape’]
valid_MAPE = history.history[‘val_mean_absolute_percentage_error’]
train_MSE = history.history[‘loss’]
valid_MSE = history.history[‘val_loss’]
谢谢
您需要在model.fit()
中定义验证集
您可以使用 validation_split=0.2
(在 0 和 1 之间浮动。训练数据的一部分用作验证数据。)
例如history=model.fit(trainX, trainY, epochs=5, validation_split=0.2, batch_size=1, verbose=2)
或者您可以使用 validation_data=
(用于评估损失的数据和每个时期结束时的任何模型指标。模型不会在该数据上进行训练。validation_data 将覆盖validation_split. validation_data 可以是: - Numpy 数组或张量的元组 (x_val, y_val) - 元组 (x_val, y_val, val_sample_weights) 的 Numpy 数组 - 数据集或数据集迭代器
我正在研究预测风速的代码。 起初,我使用 print(history.history.keys()) 来打印损失、val_loss、mape 和 val_mean_absolute_percentage_error 值,但是,它只显示 dict_keys(['loss', 'mape'])。 然后,由于它没有 val_loss 和 val_mean_absolute_percentage_error 值,它会显示 KeyError: ‘val_mean_absolute_percentage_error’
你能帮帮我吗?
这是我的代码:
from __future__ import print_function
from sklearn.metrics import mean_absolute_error
import math
import numpy as np
import matplotlib.pyplot as plt
from pandas import read_csv
from keras.models import Sequential
from keras.layers import Dense, LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return np.array(dataX), np.array(dataY)
# fix random seed for reproducibility
np.random.seed(7)
# load the dataset
dataframe = read_csv(‘OND_Q4.csv’, usecols=[7], engine=’python’, header=3)
dataset = dataframe.values
print(dataframe.head)
dataset = dataset.astype(‘float32′)
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.7) # Use 70% of data to train
test_size = len(dataset) – train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
#compile model
model.compile(loss=’mean_squared_error’, optimizer=’adam’,metrics=[‘mape’])
history=model.fit(trainX, trainY, epochs=5, batch_size=1, verbose=2)
# list all data in history
print(history.history.keys())
train_MAPE = history.history[‘mape’]
valid_MAPE = history.history[‘val_mean_absolute_percentage_error’]
train_MSE = history.history[‘loss’]
valid_MSE = history.history[‘val_loss’]
谢谢
您需要在model.fit()
您可以使用 validation_split=0.2
(在 0 和 1 之间浮动。训练数据的一部分用作验证数据。)
例如history=model.fit(trainX, trainY, epochs=5, validation_split=0.2, batch_size=1, verbose=2)
或者您可以使用 validation_data=
(用于评估损失的数据和每个时期结束时的任何模型指标。模型不会在该数据上进行训练。validation_data 将覆盖validation_split. validation_data 可以是: - Numpy 数组或张量的元组 (x_val, y_val) - 元组 (x_val, y_val, val_sample_weights) 的 Numpy 数组 - 数据集或数据集迭代器