前提・実現したいこと
①時系列データから②いくつかのトレーニング期間とラベル期間を指定して学習③アンサンブルする
このようなモデルを作成したく、コードを書いています。
参考はこちらを参照させていただいています。
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
# -*- coding: utf-8 -*- import os import datetime import IPython import IPython.display import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns import tensorflow as tf mpl.rcParams['figure.figsize'] = (12,9) mpl.rcParams['axes.grid'] = False import requests from PIL import Image from io import BytesIO def image(url): req = requests.get(url) return Image.open(BytesIO(req.content)) import pandas_datareader.data as pdr df = pdr.DataReader('aapl', 'yahoo', '2018/01/01') df['Date Time'] = df.index df.reset_index(inplace=True) date_time = pd.to_datetime(df.pop('Date Time'), format='%d.%m.%Y %H:%M:%S') df.drop(columns='Date', inplace=True) # converting it to seconds: timestamp_s = date_time.map(datetime.datetime.timestamp) # del print day = 24*60*60 year = (365.2425)*day df['Day sin'] = np.sin(timestamp_s * (2*np.pi/day)) df['Day cos'] = np.cos(timestamp_s * (2*np.pi/day)) df['Year sin'] = np.sin(timestamp_s * (2*np.pi/year)) df['Year cos'] = np.cos(timestamp_s * (2*np.pi/year)) plt.plot(np.array(df['Day sin'])) # plt.plot(np.array(df['Day cos'])) plt.xlabel('Time [h]') plt.title('Time of day signal') """## Split data""" column_indices = {name:i for i, name in enumerate(df.columns)} n = len(df) train_df = df[0:int(n*0.7)] val_df = df[int(n*0.7):int(n*0.9)] test_df = df[int(n*0.9):] num_features = df.shape[1] train_mean = train_df.mean() train_std = train_df.std() train_df = (train_df - train_mean) / train_std val_df = (val_df - train_mean) / train_std test_df = (test_df - train_mean) / train_std df_std = (df - train_mean) / train_std df_std = df_std.melt(var_name='Column', value_name='Normalized') """# Data windowing""" class WindowGenerator(): def __init__(self, input_width, label_width, shift, train_df=train_df, val_df=val_df, test_df=test_df, label_columns=None): self.train_df = train_df self.val_df = val_df self.test_df =test_df # for column indices of 'Label' self.label_columns = label_columns if label_columns is not None: self.label_columns_indices = {name:i for i,name in enumerate(label_columns)} self.column_indices = {name:i for i,name in enumerate(label_columns)} # for window params self.input_width = input_width self.label_width = label_width self.shift = shift self.total_window_size = input_width + shift self.input_slice = slice(0, input_width) self.input_indices = np.arange(self.total_window_size)[self.input_slice] self.label_start = self.total_window_size - self.label_width self.label_slice = slice(self.label_start, None) self.label_indices = np.arange(self.total_window_size)[self.label_slice] def __repr__(self): return '\n'.join([ f'Total window size: {self.total_window_size}', f'Input indices: {self.input_indices}', f'Label indices: {self.label_indices}', f'Label column name(s): {self.label_columns}' ]) # 2 samples w1 = WindowGenerator(input_width=24, label_width=1, shift=1, label_columns=['Adj Close']) w1 w2 = WindowGenerator(input_width=6, label_width=1, shift=1, label_columns=['Adj Close']) w2 def split_window(self, features): inputs = features[:, self.input_slice, :] labels = features[:, self.label_slice, :] if self.label_columns is not None: labels = tf.stack( [labels[:, :, self.column_indices[name]] for name in self.label_columns], axis=-1) inputs.set_shape([None, self.input_width, None]) labels.set_shape([None, self.label_width, None]) return inputs, labels WindowGenerator.split_window = split_window # stack 3 slices,, exam_window = tf.stack([ np.array(train_df[:w2.total_window_size]), np.array(train_df[100:100+w2.total_window_size]), np.array(train_df[200:200+w2.total_window_size]) ]) exam_inputs, exam_labels = w2.split_window(exam_window) print('All shapes: (batch, time, features)') print(f'Window shape: {exam_window.shape}') print(f'Inputs shape: {exam_inputs.shape}') print(f'Labels shape: {exam_labels.shape}') w2.example = exam_inputs, exam_labels def plot(self, model=None, plot_col='Adj Close', max_subplot=3): inputs, labels = self.example plt.figure(figsize=(12,8)) plot_col_index = self.column_indices[plot_col] max_n = min(max_subplot, len(inputs)) for n in range(max_n): plt.subplot(3, 1, n+1) plt.ylabel(f'{plot_col} [normed]') plt.plot(self.input_indices, inputs[n, :, plot_col_index], label='Inputs', marker='.', zorder=-10) if self.label_columns: label_col_index = self.label_columns_indices.get(plot_col, None) else: label_col_index = plot_col_index if label_col_index is None: continue plt.scatter(self.label_indices, labels[n, :, label_col_index], edgecolors='k', label='Labels', c='#2ca02c', s=64) if model is not None: predictions = model(inputs) plt.scatter(self.label_indices, predictions[n, :, label_col_index], marker='X', edgecolors='k', label='Predictions', c='#ff7f0e', s=64) if n==0: plt.legend() plt.xlabel('Time [h]') WindowGenerator.plot = plot def make_dataset(self, data): data = np.array(data, dtype=np.float32) ds = tf.keras.preprocessing.timeseries_dataset_from_array( data=data, targets=None, sequence_length=self.total_window_size, sequence_stride=1, shuffle=True, batch_size=32, ) ds = ds.map(self.split_window) return ds WindowGenerator.make_dataset = make_dataset @property def train(self): return self.make_dataset(self.train_df) @property def val(self): return self.make_dataset(self.val_df) @property def test(self): return self.make_dataset(self.test_df) WindowGenerator.train = train WindowGenerator.val = val WindowGenerator.test = test for exam_inputs, exam_labels in w2.train.take(1): print(f'Inputs shape (batch, time, features): {exam_inputs.shape}') print(f'Labels shape (batch, time, features): {exam_labels.shape}') # by Dense MAX_EPOCHS = 20 def compile_and_fit(model, window, patience=2): early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=patience, mode='min') model.compile(loss=tf.losses.MeanSquaredError(), optimizer=tf.optimizers.Adam(), metrics=[tf.metrics.MeanAbsoluteError()]) history = model.fit(window.train, epochs=MAX_EPOCHS, validation_data=window.val, callbacks=[early_stopping]) return history dense = tf.keras.Sequential([ tf.keras.layers.Dense(units=64, activation='relu'), tf.keras.layers.Dense(units=64, activation='relu'), tf.keras.layers.Dense(units=1) ]) CONV_WIDTH = 20 conv_window = WindowGenerator( input_width=CONV_WIDTH, label_width=1, shift=1, label_columns=['Adj Close'] ) multi_step_dense = tf.keras.Sequential([ # Shape: (time, features) => (time*features) tf.keras.layers.Flatten(), tf.keras.layers.Dense(units=32, activation='relu'), tf.keras.layers.Dense(units=32, activation='relu'), tf.keras.layers.Dense(units=1), # Add back the time dimension. # Shape: (outputs) => (1, outputs) tf.keras.layers.Reshape([1, -1]), ]) history = compile_and_fit(multi_step_dense, conv_window) # nan event '''====== Epoch 1/20 17/17 [==============================] - 1s 34ms/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan Epoch 2/20 17/17 [==============================] - 0s 12ms/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan [ ] ======='''
試したこと
これらを修正するために
こちら 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 のようなアンサンブルにできる
モデル生成器をを完成させたいです。
まだ回答がついていません
会員登録して回答してみよう