๐ŸงฉTensorflow Certification ์ทจ๋“ํ•˜๊ธฐ - part 11. ์‹ค์ „ ( Weekly US Retail)

vincaยท2023๋…„ 1์›” 3์ผ
0

๐ŸŒ• AI/DL -Tenserflow Certification

๋ชฉ๋ก ๋ณด๊ธฐ
11/11
post-thumbnail

Weekly US Retail

  • Sequence (์‹œ๊ณ„์—ด) ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ

Build and train a neural network to predict the time indexed variable of the univariate US diesel prices (On - Highway) All types for the period of 1994 - 2021.
Using a window of past 10 observations of 1 feature , train the model to predict the next 10 observations of that feature.
HINT: If you follow all the rules mentioned above and throughout this
question while training your neural network, there is a possibility that a
validation MAE of approximately 0.02 or less on the normalized validation
dataset
may fetch you top marks.

Solution

์ˆœ์„œ ์š”์•ฝ

  1. import: ํ•„์š”ํ•œ ๋ชจ๋“ˆ import
  2. ์ „์ฒ˜๋ฆฌ: ํ•™์Šต์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  3. ๋ชจ๋ธ๋ง(model): ๋ชจ๋ธ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  4. ์ปดํŒŒ์ผ(compile): ๋ชจ๋ธ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  5. ํ•™์Šต (fit): ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค.

1. import ํ•˜๊ธฐ

ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ import ํ•ฉ๋‹ˆ๋‹ค.

import urllib
import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import Dense, Conv1D, LSTM, Bidirectional
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint

# ์•„๋ž˜ 2์ค„ ์ฝ”๋“œ๋Š” ๋„ฃ์ง€ ๋ง์•„ ์ฃผ์„ธ์š”!!!
url = 'https://www.dropbox.com/s/eduk281didil1km/Weekly_U.S.Diesel_Retail_Prices.csv?dl=1'
urllib.request.urlretrieve(url, 'Weekly_U.S.Diesel_Retail_Prices.csv')

2.1 ์ „์ฒ˜๋ฆฌ (Normalization)

# This function normalizes the dataset using min max scaling.
# DO NOT CHANGE THIS CODE
def normalize_series(data, min, max):
    data = data - min
    data = data / max
    return data
download_and_extract_data()
df = pd.read_csv('household_power_consumption.csv', sep=',', infer_datetime_format=True, index_col='datetime', header=0)
df.head(10)

2.2 ์ „์ฒ˜๋ฆฌ (Windowed Dataset ์ƒ์„ฑ)

# DO NOT CHANGE THIS.
def windowed_dataset(series, batch_size, n_past=10, n_future=10, shift=1):
    ds = tf.data.Dataset.from_tensor_slices(series)
    ds = ds.window(size=n_past + n_future, shift=shift, drop_remainder=True)
    ds = ds.flat_map(lambda w: w.batch(n_past + n_future))
    ds = ds.map(lambda w: (w[:n_past], w[n_past:]))
    return ds.batch(batch_size).prefetch(1)

2.3 ์ „์ฒ˜๋ฆฌ (Dataset ๋กœ๋“œ)

df = pd.read_csv('Weekly_U.S.Diesel_Retail_Prices.csv', infer_datetime_format=True, index_col='Week of', header=0)
df.head(20)

# ํŠน์„ฑ ์ •์˜
N_FEATURES = len(df.columns)
N_FEATURES

2.4 ์ „์ฒ˜๋ฆฌ (์ •๊ทœํ™” ๋ฐ ๋ฐ์ดํ„ฐ ๋ถ„ํ• )

# ์ •๊ทœํ™” ์ฝ”๋“œ
data = df.values
data = normalize_series(data, data.min(axis=0), data.max(axis=0))

# ๋ฐ์ดํ„ฐ ๋ถ„ํ• 
SPLIT_TIME = int(len(data) * 0.8) # DO NOT CHANGE THIS
x_train = data[:SPLIT_TIME]
x_valid = data[SPLIT_TIME:]
BATCH_SIZE = 32  # ๋ฐฐ์น˜์‚ฌ์ด์ฆˆ
N_PAST = 10      # ๊ณผ๊ฑฐ ๋ฐ์ดํ„ฐ (X)
N_FUTURE = 10    # ๋ฏธ๋ž˜ ๋ฐ์ดํ„ฐ (Y)
SHIFT = 1        # SHIFT

2.5 ์ „์ฒ˜๋ฆฌ (train / validation set ๊ตฌ์„ฑ)

train_set = windowed_dataset(series=x_train, batch_size=BATCH_SIZE,
                             n_past=N_PAST, n_future=N_FUTURE,
                             shift=SHIFT)

valid_set = windowed_dataset(series=x_valid, batch_size=BATCH_SIZE,
                             n_past=N_PAST, n_future=N_FUTURE,
                             shift=SHIFT)

3. ๋ชจ๋ธ ์ •์˜ (Sequential)

์ด์ œ Modeling์„ ํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค.

model = tf.keras.models.Sequential([
    Conv1D(filters=32, kernel_size=5, padding='causal', activation='relu', input_shape=[N_PAST, 1]),
    Bidirectional(LSTM(32, return_sequences=True)),
    Bidirectional(LSTM(32, return_sequences=True)),
    Dense(32, activation='relu'),
    Dense(16, activation='relu'),
    Dense(N_FEATURES)
])

๋ชจ๋ธ ๊ฒฐ๊ณผ ์š”์•ฝ

model.summary()

4. ์ปดํŒŒ์ผ (compile)

optimizer = tf.keras.optimizers.Adam(0.0001)
model.compile(optimizer=optimizer, loss=tf.keras.losses.Huber(), metrics=['mae'])

ModelCheckpoint: ์ฒดํฌํฌ์ธํŠธ ์ƒ์„ฑ

checkpoint_path = 'model/my_checkpoint.ckpt'
checkpoint = ModelCheckpoint(filepath=checkpoint_path,
                             save_weights_only=True,
                             save_best_only=True,
                             monitor='val_mae',
                             verbose=1)

5. ํ•™์Šต (fit)

model.fit(train_set,
          validation_data=(valid_set),
          epochs=100,
          callbacks=[checkpoint])

ํ•™์Šต ์™„๋ฃŒ ํ›„ Load Weights (ModelCheckpoint)

ํ•™์Šต์ด ์™„๋ฃŒ๋œ ํ›„์—๋Š” ๋ฐ˜๋“œ์‹œ load_weights๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด, ์—ด์‹ฌํžˆ ModelCheckpoint๋ฅผ ๋งŒ๋“  ์˜๋ฏธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

model.load_weights(checkpoint_path)
profile
๋ถ‰์€ ๋ฐฐ ์˜ค์ƒ‰ ๋”ฑ๋‹ค๊ตฌ๋ฆฌ ๊ฐœ๋ฐœ์ž ๐ŸฆƒCloud & DevOps

0๊ฐœ์˜ ๋Œ“๊ธ€