tensorflow初心者です。
RNNで来月の航空会社の乗客数を予測するというサイトでご紹介されていることを参考にコードを作成しましたが、結果として
このような形でプロットされてしまいました。(本来であれば緑線と予測値である赤線が青線に近いようにプロットされます)
初心者のため申し訳ありませんがサイトでご紹介されているような緑線、赤線を青線に追従するようにプロットするにはどのようにすれば良いのかご教示のほど宜しく願い致します。
紹介されているコードと同じですが
python
1import numpy as np 2import pandas as pd 3import tflearn 4import matplotlib.pyplot as plt 5 6dataframe = pd.read_csv('international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3) 7dataset = dataframe.values 8dataset = dataset.astype('float32') 9 10dataset -= np.min(np.abs(dataset)) 11dataset /= np.max(np.abs(dataset)) 12 13 14def create_dataset(dataset, steps_of_history, steps_in_future): 15 X, Y = [], [] 16 for i in range(0, len(dataset)-steps_of_history, steps_in_future): 17 X.append(dataset[i:i+steps_of_history]) 18 Y.append(dataset[i + steps_of_history]) 19 X = np.reshape(np.array(X), [-1, steps_of_history, 1]) 20 Y = np.reshape(np.array(Y), [-1, 1]) 21 return X, Y 22 23 24def split_data(x, y, test_size=0.1): 25 pos = round(len(x) * (1 - test_size)) 26 trainX, trainY = x[:pos], y[:pos] 27 testX, testY = x[pos:], y[pos:] 28 return trainX, trainY, testX, testY 29 30steps_of_history = 1 31steps_in_future = 1 32 33X, Y = create_dataset(dataset, steps_of_history, steps_in_future) 34trainX, trainY, testX, testY = split_data(X, Y, 0.33) 35 36# TFLearnでLSTMネットワークを構築 37net = tflearn.input_data(shape=[None, steps_of_history, 1]) 38net = tflearn.lstm(net, n_units=6) 39net = tflearn.fully_connected(net, 1, activation='linear') 40net = tflearn.regression(net, optimizer='adam', learning_rate=0.001, 41 loss='mean_square') 42 43model = tflearn.DNN(net, tensorboard_verbose=0) 44model.fit(trainX, trainY, validation_set=0.1, batch_size=1, n_epoch=150) 45 46model = tflearn.DNN(net, tensorboard_verbose=0) 47 48train_predict = model.predict(trainX) 49test_predict = model.predict(testX) 50 51# 可視化する 52train_predict_plot = np.empty_like(dataset) 53train_predict_plot[:, :] = np.nan 54train_predict_plot[steps_of_history:len(train_predict)+steps_of_history, :] = \ 55 train_predict 56 57test_predict_plot = np.empty_like(dataset) 58test_predict_plot[:, :] = np.nan 59test_predict_plot[len(train_predict)+steps_of_history:len(dataset), :] = \ 60 test_predict 61 62plt.figure(figsize=(8, 8)) 63plt.title('History={} Future={}'.format(steps_of_history, steps_in_future)) 64plt.plot(dataset) 65plt.plot(train_predict_plot) 66plt.plot(test_predict_plot) 67plt.savefig('passenger.png') 68plt.show()
のようにコードを作成し、実行しました。
環境:
Windows7
python35
tensorflow 1.2.1
tflearn 0.3.2
ぱっとグラフを見た感じ訓練がうまくできていないようなのですが、model.fitの後訓練データはちゃんと出ていますか?
処理が終わった際の訓練データはこのように表示されてます。(長いので数行のみ出します)
Training Step: 12899 | total loss: 0.00224 | time: 0.234s | Adam | epoch: 150 | loss: 0.00224 -- iter: 85/86 Training Step: 12900 | total loss: 0.00204 | time: 1.250s | Adam | epoch: 150 | loss: 0.00204 | val_loss: 0.00177 -- iter: 86/86
回答1件
あなたの回答
tips
プレビュー