我修改了 model.compile()功能,添加了 mape 指标,以找出平均绝对百分比误差。运行代码后,每个时代的 mape 变得如此巨大,将其视为百分比指标。我错过了一些明显的东西还是输出正确? 输出如下:
Epoch 91/100
0s - loss: 0.0103 - mean_absolute_percentage_error: 1764997.4502
Epoch 92/100
0s - loss: 0.0103 - mean_absolute_percentage_error: 1765653.4924
Epoch 93/100
0s - loss: 0.0102 - mean_absolute_percentage_error: 1766505.5107
Epoch 94/100
0s - loss: 0.0102 - mean_absolute_percentage_error: 1766814.5450
Epoch 95/100
0s - loss: 0.0102 - mean_absolute_percentage_error: 1767510.8146
Epoch 96/100
0s - loss: 0.0101 - mean_absolute_percentage_error: 1767686.9054
Epoch 97/100
0s - loss: 0.0101 - mean_absolute_percentage_error: 1767076.2169
Epoch 98/100
0s - loss: 0.0100 - mean_absolute_percentage_error: 1767014.8481
Epoch 99/100
0s - loss: 0.0100 - mean_absolute_percentage_error: 1766592.8125
Epoch 100/100
0s - loss: 0.0100 - mean_absolute_percentage_error: 1766348.6332
我运行的代码(省略了预测部分)如下:
import numpy
from numpy import array
import matplotlib.pyplot as plt
from pandas import read_csv
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import 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 numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
dataframe = read_csv('airlinepassdata.csv', usecols=[1], engine='python', skipfooter=3)
dataset = dataframe.values
#dataset = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
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.67)
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 = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.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))
model.compile(loss='mse', optimizer='adam', metrics=['mape'])
model.fit(trainX, trainY, nb_epoch=100, batch_size=50, verbose=2)
答案 0 :(得分:1)
我通过在调用编译器之前用keras.backend.set_epsilon(1)
将模糊因子epsilon设置为一个来解决此问题。
提示在源代码中
def mean_absolute_percentage_error(y_true, y_pred):
diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),
K.epsilon(),
None))
return 100. * K.mean(diff, axis=-1)
意味着,由于某种未知的原因,训练集的MAPE计算中的K.abs(y_true)
项低于模糊默认值(1e-7),因此它使用该默认值代替,因此数量巨大