質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Q&A

解決済

1回答

9559閲覧

tf.keras.denseで、loss計算や評価計算がnanになってしまうため、学習させるコードを確認したい

Por

総合スコア40

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

0グッド

0クリップ

投稿2021/02/05 04:56

編集2021/02/05 13:07

前提・実現したいこと

①時系列データから②いくつかのトレーニング期間とラベル期間を指定して学習③アンサンブルする
このようなモデルを作成したく、コードを書いています。

参考はこちらを参照させていただいています。
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 のようなアンサンブルにできる
モデル生成器をを完成させたいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

toast-uz

2021/02/05 10:44 編集

実行してみましたが、特にエラーが出ませんでした。以下実行結果です。tensorflow 2.4.1 date_time 0 2018-01-02 1 2018-01-03 2 2018-01-04 3 2018-01-05 4 2018-01-08 ... 774 2021-01-29 775 2021-02-01 776 2021-02-02 777 2021-02-03 778 2021-02-04 Name: Date Time, Length: 779, dtype: datetime64[ns] timestamp_s 0 1.514819e+09 1 1.514905e+09 2 1.514992e+09 3 1.515078e+09 4 1.515337e+09 ... 774 1.611846e+09 775 1.612105e+09 776 1.612192e+09 777 1.612278e+09 778 1.612364e+09 Name: Date Time, Length: 779, dtype: float64 2021-02-05 19:40:33.325196: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set 2021-02-05 19:40:33.325562: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. All shapes: (batch, time, features) Window shape: (3, 7, 10) Inputs shape: (3, 6, 10) Labels shape: (3, 1, 1) 2021-02-05 19:40:33.449907: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2) Inputs shape (batch, time, features): (32, 6, 10) Labels shape (batch, time, features): (32, 1, 1) Epoch 1/20 17/17 [==============================] - 1s 33ms/step - loss: 1.0240 - mean_absolute_error: 0.6482 - val_loss: 1.3410 - val_mean_absolute_error: 1.0566 Epoch 2/20 17/17 [==============================] - 0s 6ms/step - loss: 0.1235 - mean_absolute_error: 0.2698 - val_loss: 2.0220 - val_mean_absolute_error: 1.2964 Epoch 3/20 17/17 [==============================] - 0s 5ms/step - loss: 0.0547 - mean_absolute_error: 0.1808 - val_loss: 1.8565 - val_mean_absolute_error: 1.2506
Por

2021/02/05 11:55

ありがとうございます。 datetimeやdfのLength:779、shapeを拝見させていただく限り、間違いなくinputしたいデータで試していただいており、 また関数の計算もされているようです。 確認と教えていただき、ありがとうございます。 私のほうで 1. tensorflowのバージョン確認を改めて 2. 確認させていただきます。 また差し支えなければ、ご質問させてください。
Por

2021/02/05 13:12 編集

toast_uz様 教えていただきありがとうございました。 tfのバージョン、toast_uz様と同じでした。 また(念の為)Colabのノートブックに影響を受けているのか、とも思い 一つのセルで実行しましたが、おそらくまた欠損になり学習されていないようです。 ===== All shapes: (batch, time, features) Window shape: (3, 7, 10) Inputs shape: (3, 6, 10) Labels shape: (3, 1, 1) Inputs shape (batch, time, features): (32, 6, 10) Labels shape (batch, time, features): (32, 1, 1) Epoch 1/20 17/17 [==============================] - 1s 20ms/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 ======\nEpoch 1/20\n17/17 [==============================] - 1s 34ms/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan\nEpoch 2/20\n17/17 [==============================] - 0s 12ms/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan\n[ ]\n======= ===== またお時間ありましたら、ご教授いただけますと幸いです。
toast-uz

2021/02/05 13:44

私の方は、colabではなく、ローカルのPython3.8で実行しています。
toast-uz

2021/02/05 14:00 編集

確かにcolabだとnan - val_mean_absolute_errorとかとか出ますね。何でしょう・・。
Por

2021/02/05 22:07 編集

試験いただきまして、感謝申し上げます。 わかりました。ColabのPythonは3.6.9だったはずですので 私の方で次に ・3.8のColab ・3.8のローカル を確認します。 一旦3.8で動くか確認してみます。
guest

回答1

0

ベストアンサー

標準化操作の時に、標準偏差=0と計算された列についてゼロ除算が発生したのが影響して、学習エラーになっていたようです。以下のようにゼロ除算を防止することで、Google Colab上でエラーが無くなるのを確認しました。

Python

1train_df = (train_df - train_mean) / np.maximum(train_std, 1e-12) 2val_df = (val_df - train_mean) / np.maximum(train_std, 1e-12) 3test_df = (test_df - train_mean) / np.maximum(train_std, 1e-12) 4 5df_std = (df - train_mean) / np.maximum(train_std, 1e-12)

なお、私の環境では、環境差によってか分かりませんが、データが微妙に異なっており、ゼロ除算がたまたま発生しなかったようです。

投稿2021/02/06 00:17

編集2021/02/06 00:32
toast-uz

総合スコア3266

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Por

2021/02/06 02:29

私のほうでも学習されていることが確認できました。 今朝からずっとColab上のPythonバージョンの変更に悩まされていたので、とても助かりました。 このような形で感謝の気持ちを伝えられるかわかりませんが、とにかくありがとうございます。心から感謝申し上げます。 引き続きよろしくお願いいたします。
toast-uz

2021/02/06 02:42

よかったです。 「Colab上のPythonバージョンの変更」って、私も調べてみましたが、わずかに英文の質問応答のやりとりがあるだけで、できるのかどうかも謎ですね。これはこれで、良質問になりそうなネタです。笑
Por

2021/02/06 05:11

結果できていないですが、共有します。 やりたかったのは、toast-uz様のアドバイス受けてPythonのバージョンアップとパッケージ周りをまとめて新しくすることでした。 1. colab上のapt-getでのインストールでは変化なし←ただしパス変更まではやっていない 2. conda(私はminiconda)を導入するとconda内のpythonバージョンにまで上がっている、ただパスを確認する限りconda環境へのパスへ変化(試みたが)せず、断念 3. pyenvはよくわからなかった、その他Python本体のバージョン管理ができるライブラリも未試験です そうですか。 あげてみてもいいかもしれませんね、ありがとうございます。
Por

2021/02/06 05:13 編集

ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問