RNN LSTM basic

RNN model
- ์ˆœํ™˜์‹ ๊ฒฝ๋ง ๋ชจ๋ธ

* NotImplementedError ์˜ค๋ฅ˜ํ•ด๊ฒฐ๋ฒ• : array_ops.py ํŒŒ์ผ ๊ต์ฒด(data ํด๋”) -> restart
C:\\UsersITWILLanaconda3envs\tensorflow\Lib\site-packages\tensorflow\python\ops
C:\ProgramData\Anaconda3\envs\tensorflow\Lib\site-packages\tensorflow\python\ops
array_ops.py ํŒŒ์ผ ๊ต์ฒด


import tensorflow as tf #seed value import numpy as np #ndarray from tensorflow.keras import Sequential #model from tensorflow.keras.layers import SimpleRNN, Dense #RNN layer tf.random.set_seed(123) #seed๊ฐ’ ์ง€์ •


many-to-one : word(4๊ฐœ) -> ์ถœ๋ ฅ(1๊ฐœ)

X = [[[0.0], [0.1], [0.2], [0.3]], [[0.1], [0.2], [0.3], [0.4]], [[0.2], [0.3], [0.4], [0.5]], [[0.3], [0.4], [0.5], [0.6]], [[0.4], [0.5], [0.6], [0.7]], [[0.5], [0.6], [0.7], [0.8]]] Y = [0.4, 0.5, 0.6, 0.7, 0.8, 0.9] X = np.array(X, dtype=np.float32) Y = np.array(Y, dtype=np.float32) X.shape #(6, 4, 1) - (batch_size, time_steps, features) input_shape = (4, 1) model = Sequential()


RNN layer ์ถ”๊ฐ€

model.add(SimpleRNN(units=30, input_shape=input_shape, activation='tanh'))


DNN layer ์ถ”๊ฐ€

model.add(Dense(units=1)) #์ถœ๋ ฅ : ํšŒ๊ท€๋ชจ๋ธ


model ํ•™์Šตํ™˜๊ฒฝ

model.compile(optimizer='adam', loss='mse', metrics=['mae'])


model training

model.fit(X, Y, epochs=100, verbose=1)


model prediction

y_pred = model.predict(X) print(y_pred)\





timeSeries RNN

- ์‹œ๊ณ„์—ด๋ฐ์ดํ„ฐ + RNN model = ์‹œ๊ณ„์—ด๋ถ„์„

import pandas as pd #csv file read import matplotlib.pyplot as plt #์‹œ๊ณ„์—ด ์‹œ๊ฐํ™” import numpy as np #ndarray import tensorflow as tf #seed ๊ฐ’ from tensorflow.keras import Sequential #model from tensorflow.keras.layers import SimpleRNN, Dense #RNN layer tf.random.set_seed(12) #seed๊ฐ’ ์ง€์ •



1. csv file read : ์ฃผ์‹๋ฐ์ดํ„ฐ

path = r'C:\ITWILL\5_Tensorflow\workspace\chap08_TextVectorizing_RNN\data' timeSeries = pd.read_csv(path + '/timeSeries.csv') timeSeries.info()

RangeIndex: 100 entries, 0 to 99
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 no 100 non-null int64
1 data 100 non-null float64

data = timeSeries['data'] print(data) #์ฃผ๊ฐ€ ์ •๊ทœํ™” ์ž๋ฃŒ plt.plot(data, 'r--', label='time Series') plt.legend() plt.show()




2. RNN ์ ํ•ฉํ•œ dataset ์ƒ์„ฑ

x_data = [] for i in range(len(data)-10) : #0~89 for j in range(10) : #0~9 x_data.append(data[i+j]) #90 * 10 = 900 len(x_data) #900


list -> array

x_data = np.array(x_data) x_data.shape #(900,) ''' 0~9 : 0+0~9 1~10 : 1+0~9 2~11 : 2+0~9 : 89~98 : 89+0~9 ''' y_data = [] for i in range(len(data)-10) : #0 ~ 89 y_data.append(data[i+10]) #90


list -> array

y_data = np.array(y_data) y_data.shape #(90,) ''' 10 11 12 : 99 '''


train(700)/val(200) split

x_train = x_data[:700].reshape(70,10,1) x_val = x_data[700:].reshape(-1,10,1) x_train.shape #(70, 10, 1) - (batch size, time steps, features) x_val.shape #(20, 10, 1)


train(70)/val(20) split

y_train = y_data[:70].reshape(70,1) y_val = y_data[70:].reshape(20,1)




3. model ์ƒ์„ฑ

model = Sequential() input_shape = (10, 1)


RNN layer ์ถ”๊ฐ€

model.add(SimpleRNN(units=8, input_shape=input_shape, activation ='tanh'))


DNN layer ์ถ”๊ฐ€

model.add(Dense(units=1)) #์ถœ๋ ฅ - ํšŒ๊ท€๋ชจ๋ธ - mse, mae


model ํ•™์Šตํ™˜๊ฒฝ

model.compile(optimizer='sgd', loss='mse', metrics=['mae'])


model ํ•™์Šต

model.fit(x=x_train, y=y_train, epochs=400, verbose=1)


model ์˜ˆ์ธก

y_pred = model.predict(x_val)


y_true vs y_pred

plt.plot(y_val, 'y--', label='real value') plt.plot(y_pred, 'r--', label='predicted value') plt.legend(loc='best') plt.show()

+ Recent posts