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.
ํ์ํ ๋ชจ๋์ 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')
# 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)
# 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)
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
# ์ ๊ทํ ์ฝ๋
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
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)
์ด์ 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()
optimizer = tf.keras.optimizers.Adam(0.0001)
model.compile(optimizer=optimizer, loss=tf.keras.losses.Huber(), metrics=['mae'])
checkpoint_path = 'model/my_checkpoint.ckpt'
checkpoint = ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True,
save_best_only=True,
monitor='val_mae',
verbose=1)
model.fit(train_set,
validation_data=(valid_set),
epochs=100,
callbacks=[checkpoint])
ํ์ต์ด ์๋ฃ๋ ํ์๋ ๋ฐ๋์ load_weights
๋ฅผ ํด์ฃผ์ด์ผ ํฉ๋๋ค.
๊ทธ๋ ์ง ์์ผ๋ฉด, ์ด์ฌํ ModelCheckpoint๋ฅผ ๋ง๋ ์๋ฏธ๊ฐ ์์ต๋๋ค.
model.load_weights(checkpoint_path)