前提・実現したいこと
日経株価の学習予測をしています。エラーが出て解決策が分からない状況です。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "C:\Users\gakusyuuyosoku1.py", line 114, in <module> model.fit(X_train, Y_train, File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py", line 1158, in fit tmp_logs = self.train_function(iterator) File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 889, in __call__ result = self._call(*args, **kwds) File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 950, in _call return self._stateless_fn(*args, **kwds) File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 3023, in __call__ return graph_function._call_flat( File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 1960, in _call_flat return self._build_call_outputs(self._inference_function.call( File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 591, in call outputs = execute.execute( File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\execute.py", line 59, in quick_execute tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, tensorflow.python.framework.errors_impl.UnimplementedError: Cast string to float is not supported [[node mean_squared_error/Cast (defined at \AppData\Local\Programs\Python\Python39\lib\site-packages\keras\losses.py:1201) ]] [Op:__inference_train_function_3684] Errors may have originated from an input operation. Input Source operations connected to node mean_squared_error/Cast: IteratorGetNext (defined at \AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py:819) Function call stack: train_function
ソースコード
python
1import numpy as np 2import pandas as pd 3from keras.models import Sequential 4from keras.layers.core import Dense, Activation 5from keras.layers.recurrent import LSTM 6from keras.optimizers import Adam 7from sklearn.model_selection import train_test_split 8 9owarine = [] 10data = [] 11data2 = [] 12data3 = [] 13target = [] 14kairi = [] 15jyousyou = [] 16maxlen = 60 17day1 = [] 18day2 = [] 19day3 = [] 20owarine = [] 21 22def normalization(a): 23 b = [] 24 for i in a: 25 b.append((i-min(a))/(max(a)-min(a))) 26 return b 27 28file=pd.read_csv('^nkx_d.csv',delimiter=',') 29for i in range(len(file)): 30 day1.append(file['Date'][i]) 31 owarine.append(file['Close'][i]) 32 33with open("kairiritu.txt","r",encoding="utf-8") as f: 34 next(f) 35 for line in f: 36 data2 = line.split(' ') 37 day2.append(str(data2[0])) 38 kairi.append(float(data2[1])) 39 40with open("jousyoudo.txt","r",encoding="utf-8") as f: 41 next(f) 42 for line in f: 43 data3 = line.split(' ') 44 day3.append(str(data3[0])) 45 jyousyou.append(str(data3[1])) 46''' 47日付合わせ 48''' 49for i in range(len(owarine)-1,0,-1): 50 if(day1[i] == day3[-1]): 51 del day1[i+1:len(day1)] 52 del owarine[i+1:len(owarine)] 53 break 54for i in range(len(kairi)-1,0,-1): 55 if(day2[i] == day3[-1]): 56 del day2[i+1:len(day2)] 57 del kairi[i+1:len(kairi)] 58 break 59if(len(day2) > len(day3)): 60 for i in range(0,len(owarine)): 61 if(day1[i] == day3[0]): 62 del day1[0:i] 63 del owarine[0:i] 64 break 65 for i in range(0,len(kairi)-1): 66 if(day2[i] == day3[0]): 67 del day2[0:i] 68 del kairi[0:i] 69 break 70else: 71 for i in range(0,len(owarine)): 72 if(day1[i] == day2[0]): 73 del day1[0:i] 74 del owarine[0:i] 75 break 76 for i in range(0,len(jyousyou)): 77 if(day3[i] == day2[0]): 78 del day3[0:i] 79 del jyousyou[0:i] 80 break 81for i in range(0, len(kairi)-maxlen): 82 data.append(normalization(kairi[i: i + maxlen])) 83 target.append(jyousyou[i + maxlen]) 84X = np.array(data).reshape(len(data), maxlen, 1) 85Y = np.array(target).reshape(len(data), 1) 86N_train = int(len(data) * 0.5) 87N_validation = len(data) - N_train 88X_train, X_validation, Y_train, Y_validation = train_test_split(X, Y, test_size=N_validation,shuffle=False) 89 90''' 91モデル設定 92''' 93n_in = len(X[0][0]) 94n_hidden = 60 95n_out = len(Y[0]) 96def weight_variable(shape, name=None): 97 return np.random.normal(scale=.01, size=shape) 98model = Sequential() 99model.add(LSTM(n_hidden, 100 kernel_initializer="random_uniform", 101 input_shape=(maxlen, n_in))) 102model.add(Dense(n_hidden, kernel_initializer="random_uniform")) 103model.add(Activation('sigmoid')) 104model.add(Dense(n_out, kernel_initializer="random_uniform")) 105model.add(Activation('sigmoid')) 106optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999) 107model.compile(loss='mean_squared_error', 108 optimizer=optimizer) 109''' 110モデル学習 111''' 112epochs = 100 113batch_size = 1000 114model.fit(X_train, Y_train, 115 batch_size=batch_size, 116 epochs=epochs, 117 validation_split = 0.25) 118y_ = model.predict(X_validation) 119yosoku = [] 120for i in range (0,len(y_)): 121 if (y_[i]>=0.5): 122 yosoku.append('1') 123 else: 124 yosoku.append('0') 125#学習の予測データ表示 126b = open("yosoku.txt","w",encoding="utf-8") 127b.write("日付 上昇度" + "\n") 128print(len(yosoku)) 129for s in range(len(yosoku)): 130 b.write(str(day3[(len(jyousyou))-(len(yosoku))+s])+" "+str(yosoku[s])+"\n")
補足情報
Python 3.9.6
Keras 2.4.3
numpy 1.19.5
pandas 1.3.0
tensorflow 2.5.0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。