前提・実現したいこと
①時系列データから②いくつかのトレーニング期間とラベル期間を指定して学習③アンサンブルする
このようなモデルを作成したく、コードを書いています。
参考はこちらを参照させていただいています。
https://www.tensorflow.org/tutorials/structured_data/time_series
loss計算や評価計算がnanになってしまうため、学習させるコードを確認したく
質問させていただきました。
tensorflow version : 2.4.1
keras version : 2.4.3
発生している問題・エラーメッセージ
参考先で与えられているデータ
zip_path = tf.keras.utils.get_file( origin='https://storage.googleapis.com/tensorflow/tf-keras- # 文字数の関係で一部省略
では発生しなかったのですが
私が取得したデータ
import pandas_datareader.data as pdr df = pdr.DataReader('aapl', 'yahoo', '2018/01/01') # 一部省略
では
モデル内で採用する損失関数や評価指標
tensorflow.keras.Sequential.Dense(losses=MeanSquaredError(),metrics=MeanAbsoluteError())
が欠損値nan
になってしまいます。
該当のソースコード
Python
1 2# -*- coding: utf-8 -*- 3 4import os 5import datetime 6 7import IPython 8import IPython.display 9import matplotlib as mpl 10import matplotlib.pyplot as plt 11import numpy as np 12import pandas as pd 13import seaborn as sns 14import tensorflow as tf 15 16mpl.rcParams['figure.figsize'] = (12,9) 17mpl.rcParams['axes.grid'] = False 18 19import requests 20from PIL import Image 21from io import BytesIO 22 23def image(url): 24 req = requests.get(url) 25 return Image.open(BytesIO(req.content)) 26 27import pandas_datareader.data as pdr 28 29df = pdr.DataReader('aapl', 'yahoo', '2018/01/01') 30df['Date Time'] = df.index 31df.reset_index(inplace=True) 32date_time = pd.to_datetime(df.pop('Date Time'), format='%d.%m.%Y %H:%M:%S') 33df.drop(columns='Date', inplace=True) 34 35 36# converting it to seconds: 37timestamp_s = date_time.map(datetime.datetime.timestamp) 38 39# del print 40 41day = 24*60*60 42year = (365.2425)*day 43 44df['Day sin'] = np.sin(timestamp_s * (2*np.pi/day)) 45df['Day cos'] = np.cos(timestamp_s * (2*np.pi/day)) 46df['Year sin'] = np.sin(timestamp_s * (2*np.pi/year)) 47df['Year cos'] = np.cos(timestamp_s * (2*np.pi/year)) 48 49plt.plot(np.array(df['Day sin'])) 50# plt.plot(np.array(df['Day cos'])) 51plt.xlabel('Time [h]') 52plt.title('Time of day signal') 53 54 55 56 57 58"""## Split data""" 59 60column_indices = {name:i for i, name in enumerate(df.columns)} 61 62n = len(df) 63train_df = df[0:int(n*0.7)] 64val_df = df[int(n*0.7):int(n*0.9)] 65test_df = df[int(n*0.9):] 66num_features = df.shape[1] 67 68train_mean = train_df.mean() 69train_std = train_df.std() 70 71train_df = (train_df - train_mean) / train_std 72val_df = (val_df - train_mean) / train_std 73test_df = (test_df - train_mean) / train_std 74 75df_std = (df - train_mean) / train_std 76df_std = df_std.melt(var_name='Column', value_name='Normalized') 77 78 79 80 81 82"""# Data windowing""" 83 84class WindowGenerator(): 85 def __init__(self, input_width, label_width, shift, 86 train_df=train_df, val_df=val_df, test_df=test_df, 87 label_columns=None): 88 self.train_df = train_df 89 self.val_df = val_df 90 self.test_df =test_df 91 92 # for column indices of 'Label' 93 self.label_columns = label_columns 94 if label_columns is not None: 95 self.label_columns_indices = {name:i for i,name in enumerate(label_columns)} 96 self.column_indices = {name:i for i,name in enumerate(label_columns)} 97 98 # for window params 99 self.input_width = input_width 100 self.label_width = label_width 101 self.shift = shift 102 self.total_window_size = input_width + shift 103 self.input_slice = slice(0, input_width) 104 self.input_indices = np.arange(self.total_window_size)[self.input_slice] 105 self.label_start = self.total_window_size - self.label_width 106 self.label_slice = slice(self.label_start, None) 107 self.label_indices = np.arange(self.total_window_size)[self.label_slice] 108 109 110 def __repr__(self): 111 return '\n'.join([ 112 f'Total window size: {self.total_window_size}', 113 f'Input indices: {self.input_indices}', 114 f'Label indices: {self.label_indices}', 115 f'Label column name(s): {self.label_columns}' 116 ]) 117 118 119# 2 samples 120w1 = WindowGenerator(input_width=24, label_width=1, shift=1, label_columns=['Adj Close']) 121w1 122 123w2 = WindowGenerator(input_width=6, label_width=1, shift=1, label_columns=['Adj Close']) 124w2 125 126 127 128def split_window(self, features): 129 inputs = features[:, self.input_slice, :] 130 labels = features[:, self.label_slice, :] 131 if self.label_columns is not None: 132 labels = tf.stack( 133 [labels[:, :, self.column_indices[name]] for name in self.label_columns], 134 axis=-1) 135 inputs.set_shape([None, self.input_width, None]) 136 labels.set_shape([None, self.label_width, None]) 137 138 return inputs, labels 139 140WindowGenerator.split_window = split_window 141 142 143 144 145# stack 3 slices,, 146 147exam_window = tf.stack([ 148 np.array(train_df[:w2.total_window_size]), 149 np.array(train_df[100:100+w2.total_window_size]), 150 np.array(train_df[200:200+w2.total_window_size]) 151]) 152 153exam_inputs, exam_labels = w2.split_window(exam_window) 154 155print('All shapes: (batch, time, features)') 156print(f'Window shape: {exam_window.shape}') 157print(f'Inputs shape: {exam_inputs.shape}') 158print(f'Labels shape: {exam_labels.shape}') 159 160w2.example = exam_inputs, exam_labels 161 162def plot(self, model=None, plot_col='Adj Close', max_subplot=3): 163 inputs, labels = self.example 164 plt.figure(figsize=(12,8)) 165 plot_col_index = self.column_indices[plot_col] 166 max_n = min(max_subplot, len(inputs)) 167 for n in range(max_n): 168 plt.subplot(3, 1, n+1) 169 plt.ylabel(f'{plot_col} [normed]') 170 plt.plot(self.input_indices, inputs[n, :, plot_col_index], 171 label='Inputs', marker='.', zorder=-10) 172 173 if self.label_columns: 174 label_col_index = self.label_columns_indices.get(plot_col, None) 175 else: 176 label_col_index = plot_col_index 177 178 if label_col_index is None: 179 continue 180 181 plt.scatter(self.label_indices, labels[n, :, label_col_index], 182 edgecolors='k', label='Labels', c='#2ca02c', s=64) 183 184 if model is not None: 185 predictions = model(inputs) 186 plt.scatter(self.label_indices, predictions[n, :, label_col_index], 187 marker='X', edgecolors='k', label='Predictions', c='#ff7f0e', s=64) 188 189 if n==0: 190 plt.legend() 191 192 plt.xlabel('Time [h]') 193 194 195WindowGenerator.plot = plot 196 197 198 199 200 201 202def make_dataset(self, data): 203 data = np.array(data, dtype=np.float32) 204 ds = tf.keras.preprocessing.timeseries_dataset_from_array( 205 data=data, 206 targets=None, 207 sequence_length=self.total_window_size, 208 sequence_stride=1, 209 shuffle=True, 210 batch_size=32, 211 ) 212 213 ds = ds.map(self.split_window) 214 215 return ds 216 217 218WindowGenerator.make_dataset = make_dataset 219 220 221 222@property 223def train(self): 224 return self.make_dataset(self.train_df) 225 226@property 227def val(self): 228 return self.make_dataset(self.val_df) 229 230@property 231def test(self): 232 return self.make_dataset(self.test_df) 233 234WindowGenerator.train = train 235WindowGenerator.val = val 236WindowGenerator.test = test 237 238 239 240 241 242for exam_inputs, exam_labels in w2.train.take(1): 243 print(f'Inputs shape (batch, time, features): {exam_inputs.shape}') 244 print(f'Labels shape (batch, time, features): {exam_labels.shape}') 245 246 247 248 249# by Dense 250 251MAX_EPOCHS = 20 252 253def compile_and_fit(model, window, patience=2): 254 early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', 255 patience=patience, 256 mode='min') 257 model.compile(loss=tf.losses.MeanSquaredError(), 258 optimizer=tf.optimizers.Adam(), 259 metrics=[tf.metrics.MeanAbsoluteError()]) 260 261 history = model.fit(window.train, epochs=MAX_EPOCHS, 262 validation_data=window.val, 263 callbacks=[early_stopping]) 264 265 return history 266 267dense = tf.keras.Sequential([ 268 tf.keras.layers.Dense(units=64, activation='relu'), 269 tf.keras.layers.Dense(units=64, activation='relu'), 270 tf.keras.layers.Dense(units=1) 271 272]) 273 274 275 276CONV_WIDTH = 20 277conv_window = WindowGenerator( 278 input_width=CONV_WIDTH, 279 label_width=1, 280 shift=1, 281 label_columns=['Adj Close'] 282) 283 284 285 286 287multi_step_dense = tf.keras.Sequential([ 288 # Shape: (time, features) => (time*features) 289 tf.keras.layers.Flatten(), 290 tf.keras.layers.Dense(units=32, activation='relu'), 291 tf.keras.layers.Dense(units=32, activation='relu'), 292 tf.keras.layers.Dense(units=1), 293 # Add back the time dimension. 294 # Shape: (outputs) => (1, outputs) 295 tf.keras.layers.Reshape([1, -1]), 296]) 297 298 299history = compile_and_fit(multi_step_dense, conv_window) 300 301# nan event 302'''====== 303Epoch 1/20 30417/17 [==============================] - 1s 34ms/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan 305Epoch 2/20 30617/17 [==============================] - 0s 12ms/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan 307[ ] 308=======''' 309
試したこと
これらを修正するために
こちら https://teratail.com/questions/299561 や
こちら https://teratail.com/questions/295771 を参考にさせていただき
前処理方法から着手してみましたが
変化がなく困っております。
私が取得したデータについて、なぜ欠損値になってしまうのでしょうか。
学習できるようにするにはどの視点からコードを修正したらよいでしょうか。
教えていただけると幸いです。
補足情報(FW/ツールのバージョンなど)
Google Colab
chrome webbrowser
<参考>
1.[TensorFlow時系列分析]
https://www.tensorflow.org/tutorials/structured_data/time_series
2.[tensorflow.keras _ 損失関数や評価指標メトリックについて]
https://keras.io/api/metrics/
<ゴール>
最終的にはこちら https://teratail.com/questions/299561 のようなアンサンブルにできる
モデル生成器をを完成させたいです。
回答1件
あなたの回答
tips
プレビュー