前提・実現したいこと
アメダスのデータから降水量、気温、湿度、気圧のパラメータを使って、降水量を予測するプログラムです。
AccracyとLossの推移を可視化したいのですが、下のようなエラーが発生してしまいます。
発生している問題・エラーメッセージ
KeyError Traceback (most recent call last) ~/gen/LSTM_amedas.py in <module> 56 57 # 学習過程の可視化 ---> 58 acc = history.history['acc'] 59 val_acc = history.history['val_acc'] 60 loss = history.history['loss'] KeyError: 'acc'
該当のソースコード
python
1import os 2import numpy as np 3import matplotlib.pyplot as plt 4import pandas as pd 5from keras.models import Sequential 6from keras.layers import Dense, Activation 7from keras.layers.recurrent import LSTM 8from keras import optimizers 9from keras.callbacks import ModelCheckpoint 10from keras.layers import Dense, SimpleRNN, GRU, LSTM 11from keras import metrics 12from sklearn.preprocessing import MinMaxScaler 13from sklearn.metrics import mean_squared_error 14from keras.utils import np_utils 15 16 17df = pd.read_csv('../amedas_1980_2020.csv', skiprows=[0,1,2,3,4], header=None, usecols=[1,4,8,11], engine='python') 18dataset = df.values 19dataset = dataset.astype('float32') 20 21scaler = MinMaxScaler(feature_range=(0, 1)) 22dataset = scaler.fit_transform(dataset) 23 24train_size = int(len(dataset) * 0.67) 25test_size = len(dataset) - train_size 26train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:] 27 28def create_dataset(dataset, look_back=1): 29 dataX, dataY = [], [] 30 for i in range(len(dataset)-look_back-1): 31 xset = [] 32 for j in range(dataset.shape[1]): 33 a = dataset[i:(i+look_back), j] 34 xset.append(a) 35 dataY.append(dataset[i + look_back, 1]) 36 dataX.append(xset) 37 return np.array(dataX), np.array(dataY) 38 39# reshape into X=t and Y=t+1 40look_back = 12 41trainX, trainY = create_dataset(train, look_back) 42testX, testY = create_dataset(test, look_back) 43print(testX.shape) 44print(testX[0]) 45print(testY) 46 47# reshape input to be [samples, time steps(number of variables), features] *convert time series into column 48trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], trainX.shape[2])) 49testX = np.reshape(testX, (testX.shape[0], testX.shape[1], testX.shape[2])) 50 51model = Sequential() 52model.add(LSTM(32, input_shape=(testX.shape[1], look_back))) 53model.add(Dense(1)) 54model.compile(loss='mean_squared_error', optimizer='adam',metrics=['accuracy']) 55history = model.fit(trainX, trainY, epochs=100, batch_size=10, verbose=1, validation_data=(testX,testY)) 56 57# 学習過程の可視化 58acc = history.history['acc'] 59val_acc = history.history['val_acc'] 60loss = history.history['loss'] 61val_loss = history.history['val_loss'] 62epochs = range(len(acc)) 63 64plt.plot(epochs,acc,'bo',label='Training acc') 65plt.plot(epochs,val_acc,'b',label='Validation acc') 66plt.title('Training and validation accracy') 67plt.legend() 68 69plt.figure() 70 71plt.plot(epochs,loss,'bo',label='Training loss') 72plt.plot(epochs,val_loss,'b',label='Validation loss') 73plt.title('Training and Validation loss') 74plt.legend() 75 76score = model.evaluate(testX, testY, verbose=0) 77print('Test loss:', score[0]) 78print('Test accuracy:', score[1]) 79 80trainPredict = model.predict(trainX) 81testPredict = model.predict(testX) 82 83x1 = np.arange(0,485) 84x2 = np.arange(12,323) 85x3 = np.arange(336,484) 86 87plt.figure() 88 89plt.plot(x1,dataset[:,1],label='data') 90plt.plot(x2,trainPredict,label='Train Predict') 91plt.plot(x3,testPredict,label='Test Predict') 92plt.legend(fontsize=18) 93 94plt.show() 95
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/02 01:23