前提・実現したいこと
よろしくお願いいたします。
インスタンスへのメンバー追加とクラスへのメンバー追加の違い
メンバー追加の正しい方法
を確認したいと思っております。
ご教授いただけますと幸いです。
発生している問題・エラーメッセージ
AttributeError: 'WindowGenerator' object has no attribute 'example'
該当のソースコード
Python3.6.9
1# -*- coding: utf-8 -*- 2 3import os 4import datetime 5 6import IPython 7import IPython.display 8import matplotlib as mpl 9import matplotlib.pyplot as plt 10import numpy as np 11import pandas as pd 12import seaborn as sns 13import tensorflow as tf 14 15mpl.rcParams['figure.figsize'] = (12,9) 16mpl.rcParams['axes.grid'] = False 17 18import requests 19from PIL import Image 20from io import BytesIO 21 22def image(url): 23 req = requests.get(url) 24 return Image.open(BytesIO(req.content)) 25 26import pandas_datareader.data as pdr 27 28df = pdr.DataReader('aapl', 'yahoo', '2018/01/01') 29df['Date Time'] = df.index 30df.reset_index(inplace=True) 31date_time = pd.to_datetime(df.pop('Date Time'), format='%d.%m.%Y %H:%M:%S') 32df.drop(columns='Date', inplace=True) 33 34 35# converting it to seconds: 36timestamp_s = date_time.map(datetime.datetime.timestamp) 37 38# del print 39 40day = 24*60*60 41year = (365.2425)*day 42 43df['Day sin'] = np.sin(timestamp_s * (2*np.pi/day)) 44df['Day cos'] = np.cos(timestamp_s * (2*np.pi/day)) 45df['Year sin'] = np.sin(timestamp_s * (2*np.pi/year)) 46df['Year cos'] = np.cos(timestamp_s * (2*np.pi/year)) 47 48# plt.plot(np.array(df['Day sin'])) 49# # plt.plot(np.array(df['Day cos'])) 50# plt.xlabel('Time [h]') 51# plt.title('Time of day signal') 52 53 54 55 56 57"""## Split data""" 58 59column_indices = {name:i for i, name in enumerate(df.columns)} 60 61n = len(df) 62train_df = df[0:int(n*0.7)] 63val_df = df[int(n*0.7):int(n*0.9)] 64test_df = df[int(n*0.9):] 65num_features = df.shape[1] 66 67train_mean = train_df.mean() 68train_std = train_df.std() 69 70train_df = (train_df - train_mean) / np.maximum(train_std, 1e-12) 71val_df = (val_df - train_mean) / np.maximum(train_std, 1e-12) 72test_df = (test_df - train_mean) / np.maximum(train_std, 1e-12) 73 74 75df_std = (df - train_mean) / np.maximum(train_std, 1e-12) 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 162 163 164def plot(self, model=None, plot_col='Adj Close', max_subplot=3): 165 inputs, labels = self.example 166 plt.figure(figsize=(12,8)) 167 plot_col_index = self.column_indices[plot_col] 168 max_n = min(max_subplot, len(inputs)) 169 for n in range(max_n): 170 plt.subplot(3, 1, n+1) 171 plt.ylabel(f'{plot_col} [normed]') 172 plt.plot(self.input_indices, inputs[n, :, plot_col_index], 173 label='Inputs', marker='.', zorder=-10) 174 175 if self.label_columns: 176 label_col_index = self.label_columns_indices.get(plot_col, None) 177 else: 178 label_col_index = plot_col_index 179 180 if label_col_index is None: 181 continue 182 183 plt.scatter(self.label_indices, labels[n, :, label_col_index], 184 edgecolors='k', label='Labels', c='#2ca02c', s=64) 185 186 if model is not None: 187 predictions = model(inputs) 188 plt.scatter(self.label_indices, predictions[n, :, label_col_index], 189 marker='X', edgecolors='k', label='Predictions', c='#ff7f0e', s=64) 190 191 if n==0: 192 plt.legend() 193 194 plt.xlabel('Time [h]') 195 196 197WindowGenerator.plot = plot 198 199 200 201 202 203 204def make_dataset(self, data): 205 data = np.array(data, dtype=np.float32) 206 ds = tf.keras.preprocessing.timeseries_dataset_from_array( 207 data=data, 208 targets=None, 209 sequence_length=self.total_window_size, 210 sequence_stride=1, 211 shuffle=True, 212 batch_size=32, 213 ) 214 215 ds = ds.map(self.split_window) 216 217 return ds 218 219 220WindowGenerator.make_dataset = make_dataset 221 222 223 224@property 225def train(self): 226 return self.make_dataset(self.train_df) 227 228@property 229def val(self): 230 return self.make_dataset(self.val_df) 231 232@property 233def test(self): 234 return self.make_dataset(self.test_df) 235 236WindowGenerator.train = train 237WindowGenerator.val = val 238WindowGenerator.test = test 239 240 241 242 243 244for exam_inputs, exam_labels in w2.train.take(1): 245 print(f'Inputs shape (batch, time, features): {exam_inputs.shape}') 246 print(f'Labels shape (batch, time, features): {exam_labels.shape}') 247 248 249 250 251# by Dense 252 253MAX_EPOCHS = 20 254 255def compile_and_fit(model, window, patience=2): 256 early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', 257 patience=patience, 258 mode='min') 259 model.compile(loss=tf.losses.MeanSquaredError(), 260 optimizer=tf.optimizers.Adam(), 261 metrics=[tf.metrics.MeanAbsoluteError()]) 262 263 history = model.fit(window.train, epochs=MAX_EPOCHS, 264 validation_data=window.val, 265 callbacks=[early_stopping]) 266 267 return history 268 269dense = tf.keras.Sequential([ 270 tf.keras.layers.Dense(units=64, activation='relu'), 271 tf.keras.layers.Dense(units=64, activation='relu'), 272 tf.keras.layers.Dense(units=1) 273 274]) 275 276 277 278CONV_WIDTH = 20 279conv_window = WindowGenerator( 280 input_width=CONV_WIDTH, 281 label_width=1, 282 shift=1, 283 label_columns=['Adj Close'] 284) 285 286 287 288 289multi_step_dense = tf.keras.Sequential([ 290 # Shape: (time, features) => (time*features) 291 tf.keras.layers.Flatten(), 292 tf.keras.layers.Dense(units=32, activation='relu'), 293 tf.keras.layers.Dense(units=32, activation='relu'), 294 tf.keras.layers.Dense(units=1), 295 # Add back the time dimension. 296 # Shape: (outputs) => (1, outputs) 297 tf.keras.layers.Reshape([1, -1]), 298]) 299 300 301history = compile_and_fit(multi_step_dense, conv_window) 302 303 304 305conv_window.plot() 306# AttributeError: 'WindowGenerator' object has no attribute 'example' 307 308
試したこと
インスタンスから組み込み関数dirで、メンバーを抽出できることを調べ
上記'example'がメンバーとして表示されていることを確認しました。
WindowGeneratorクラスからw2インスタンスを作成
w2.exampleと記述し、WindowGeneratorクラスへメンバーが追加できる
と認識しておりますが、、
どこか間違っているのでしょうか。
こちらのコードを参考
https://www.tensorflow.org/tutorials/structured_data/time_series
にさせていただき、何度か確認いたしましたが
こちらは
WindowGeneratorクラスへではなく、インスタンスへexampleメンバーを追加することで
その後のplot()メソッドも実行できておりました。
その違いがわからずとなっており
ご教授いただけますと幸いです。
補足情報(FW/ツールのバージョンなど)
python3.6.9
Google Colab
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/06 12:23
2021/02/06 12:39
2021/02/06 23:48
2021/02/07 02:46
2021/02/07 06:02
2021/02/08 06:05
2021/02/10 07:26