現在の状況
現在、ディープラーニングを用いてAPPLE社の株式予測のプログラムを書いています。その途中で以下のエラーが発生してしまいました。
Python
1ValueError: Input 0 of layer sequential_10 is incompatible with the layer: expected axis -1 of input shape to have value 20 but received input with shape [None, 19]
調べても解決できなかったのでこれがどのようなエラーでどこを直せば解決できるのか分かれば教えていただきたいです。以下のコードはリンクを参考にプログラムを書きました。
以下コード
Python
1import pandas as pd 2import numpy as np 3from sklearn.model_selection import train_test_split 4from keras.callbacks import EarlyStopping 5import matplotlib.pyplot as plt 6from keras.layers import Dense, LSTM, Dropout, Flatten 7from keras.models import Model 8from pandas_datareader import data 9from datetime import date 10from keras import models 11%matplotlib inline 12pd.core.common.is_list_like = pd.api.types.is_list_like 13 14#2000年から今日までのAPPLEの株式を読み込む 15start = '2000-04-01' 16end = date.today() 17df = data.DataReader('AAPL', 'yahoo', start, end) 18 19#Adj Closeの値を用いる 20price = df['Adj Close'] 21stc_date = df.index 22 23#model_1の設計 24model_1 = models.Sequential() 25model_1.add(Dense(5, activation='relu', input_shape=(20,))) 26model_1.add(Dropout(0.5)) 27model_1.add(Dense(1, activation='linear')) 28model_1.summary() 29model_1.compile(optimizer='adam', 30 loss='mse', 31 metrics=['mae']) 32 33#model_2の設計 34model_2 = models.Sequential() 35model_2.add(LSTM(10, 36 dropout=0.2, 37 recurrent_dropout=0.2, 38 input_shape=(20,1))) 39model_2.add(Dense(5, activation='relu')) 40model_2.add(Dropout(0.5)) 41model_2.add(Dense(1, activation='linear')) 42model_2.summary() 43model_2.compile(optimizer='adam', 44 loss='mse', 45 metrics=['mae']) 46 47#入力用、出力用テンソルを出力する関数 48def getInputLabel(price, period=20): 49 period = period 50 input_tensor = [] 51 label_tensor = [] 52 for i in range(0, len(price) - period, 1): 53 input_tensor.append(price.iloc[i:i + period -1]) 54 label_tensor.append(price.iloc[i + period -1]) 55 input_tensor = np.array(input_tensor) 56 label_tensor = np.array(label_tensor) 57 return input_tensor, label_tensor 58 59#Topixデータを標準化 60tmp = price - price.mean() 61tmp = tmp/price.std() 62input_tensor, label_tensor = getInputLabel(price = tmp) 63 64#トレーニングデータとテストデータに分類 65X_train, X_test, y_train, y_test = train_test_split(input_tensor, label_tensor, test_size=0.2, random_state=100, shuffle=False) 66 67#過学習防止 68earlystopping = EarlyStopping(monitor='loss', patience=5) 69 70#model_1学習 71model_1.fit(X_train, y_train, batch_size=10, epochs=50, callbacks=[earlystopping]) 72 73#model_1学習をした際のエラーメッセージ 74 75ValueError: in user code: 76 77 /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function * 78 return step_function(self, iterator) 79 /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function ** 80 outputs = model.distribute_strategy.run(run_step, args=(data,)) 81 /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run 82 return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) 83 /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica 84 return self._call_for_each_replica(fn, args, kwargs) 85 /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica 86 return fn(*args, **kwargs) 87 /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step ** 88 outputs = model.train_step(data) 89 /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:747 train_step 90 y_pred = self(x, training=True) 91 /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:976 __call__ 92 self.name) 93 /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:216 assert_input_compatibility 94 ' but received input with shape ' + str(shape)) 95 96 ValueError: Input 0 of layer sequential_10 is incompatible with the layer: expected axis -1 of input shape to have value 20 but received input with shape [None, 19]
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。