์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- ์๋๋ก์ด๋์คํ๋์ค
- ๊นํ
- ๋์
- AI
- ์ํ์ฝ๋ฉ
- ์๊ณ ๋ฆฌ์ฆ
- native
- ๋ฐ์ดํฐ์๊ฐํ
- cs231n
- Git
- ์ธํ๋ฐ
- ํ์ดํ๋
- react
- c++
- ๋จธ์ ๋ฌ๋
- Titanic
- ์๋ฒ ๋ฉ
- nlp
- ๋ฐ์ดํฐ๋ถ์
- ๋ฐฑ์ค
- ์ ํ๋์ํ
- ๊ฒฐ์ ํธ๋ฆฌ
- ํ๊ตญ์ด์๋ฒ ๋ฉ
- ๋ค์ดํฐ๋ธ
- linearalgebra
- ๋ฆฌ์กํธ
- ๋ฐ์ดํฐ
- Kaggle
- ๋ถ์
- ๋ฅ๋ฌ๋
- Today
- Total
yeon's ๐ฉ๐ป๐ป
RNN (many-to-one stacking) ๋ณธ๋ฌธ
https://engineer-mole.tistory.com/25
[python/Tensorflow2.0] RNN(Recurrent Neural Network) ; many to one stacking
1. Stacking์ด๋? โCNN์์ convolution layer๋ฅผ ์ฌ๋ฌ ๊ฐ๋ฅผ ์ผ๋ฏ ,RNN๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฌ๋ฌ ๊ฐ๋ฅผ ์์ ์ ์๋ค. ์ด๋ฅผ multi layered RNN ๋๋ stacked RNN์ด๋ผ๊ณ ์๊ธฐํ๋ค. CNN์์ convolution layer๋ฅผ ์ฌ๋ฌ ๊ฐ ์..
engineer-mole.tistory.com
์ ๋ธ๋ก๊ทธ๋ฅผ ํ์ฌํ๋ฉฐ ๊ณต๋ถ
* ๋ชจ๋ ํ ์คํธ์ ์ด๋ฏธ์ง์ ์ถ์ฒ๋ ์ ๋ธ๋ก๊ทธ์ ๋๋ค.
(๋๋ถ์ด ํด๋น ํฌ์คํธ์ ์ ๊ธ์ ๊น์ฑํ ๊ต์๋์ '๋ชจ๋๋ฅผ ์ํ ๋ฅ๋ฌ๋'์ ๋ฐํ์ผ๋ก ์ ์๋์์์ ๋ช ์ํฉ๋๋ค.
https://www.boostcourse.org/ai212/lecture/43752?isDesc=false)
1. Stacking์ด๋?
CNN์์ convolution layer๋ฅผ ์ฌ๋ฌ ๊ฐ ์ผ๋ฏ, RNN๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฌ๋ฌ ๊ฐ๋ฅผ ์์ ์ ์๋ค.
-> ์ด๋ฅผ multi layered RNN ๋๋ stacked RNN ์ด๋ผ๊ณ ์๊ธฐํ๋ค.
CNN์์ convolution layer๋ฅผ ์ฌ๋ฌ ๊ฐ ์์์ ๋, input ์ด๋ฏธ์ง์ ๊ฐ๊น์ด convolution layer์ edge์ ๊ฐ์ ๊ธ๋ก๋ฒํ feature์ ๋ฝ์ ์ ์๊ณ , output์ ๊ฐ๊น์ด convolution layer์ ์ข ๋ abstractํ feature์ ๋ฝ์ ์ ์๋ฏ์ด,
RNN์์๋ stacked RNN์ ํ์ฉํด ๋น์ทํ ํจ๊ณผ๋ฅผ ์ป์ ์ ์๋ค.
- ์์ฐ์ด ์ฒ๋ฆฌ ๋ถ์ผ์ stacked RNN ๊ด๋ จ ์ฌ๋ฌ ๋ ผ๋ฌธ์์ input์ ๊ฐ๊น์ด RNN์ hidden states๊ฐ sementic information(์๋ฏธ์ ์ ๋ณด)๋ณด๋ค syntatic information(๋ฌธ๋ฒ์ ์ ๋ณด)์ ์๋์ ์ผ๋ก ๋ ์ ์ธ์ฝ๋ฉ ํ๊ณ ์์ผ๋ฉฐ,
- ๋ฐ๋๋ก output์ ๊ฐ๊น์ด RNN์ hidden states๋ sementic information์ syntatic information๋ณด๋ค ๋์ฑ ์ ์ธ์ฝ๋ฉ ํ๊ณ ์์์ ์ค์ฆ์ ์ผ๋ก ํ์ ํ๊ธฐ ๋๋ฌธ์ ์์ฐ์ด ์ฒ๋ฆฌ์ stacked RNN์ด ๋ค์ํ๊ฒ ํ์ฉ๋๊ณ ์๋ค.
2. Stacked RNN ๊ตฌํ
์ด์ ์ many-to-one ๊ตฌ์กฐ๋ฅผ ํ์ฉํ๋ ๋ฐฉ์๊ณผ ํฌ๊ฒ ๋ค๋ฅด์ง ์์ผ๋ฉฐ ์ฐจ์ด์ ์ RNN์ ์ฌ๋ฌ ๊ฐ ํ์ฉํ๋ stacked RNN์ many-to-one์ผ๋ก ์ฌ์ฉํ๋ค๋ ์
- ์ํ์ค๋ฅผ tokenization ํ ๋ค embedding layer์ ๊ฑฐ์ณ ์ด๋ค numeric vector๋ก ํํ๋ ๊ฐ ํ ํฐ์ stacked RNN์ด ์์๋๋ก ์ฝ์ด๋ค์ธ๋ค.
- stacked RNN์ ๊ตฌ์ฑํ๊ณ ์๋ RNN ์ค t๋ฒ์งธ ์์ ์ ํ ํฐ(๋ง์ง๋ง ์ค ๋นจ๊ฐ์)๊ณผ t-1 ์์ ์ hidden states(์ด๋ก์)๋ฅผ ๋ฐ์์ t๋ฒ์งธ ์์ ์ hidden states(์ด๋ก์)๋ฅผ ์์ฑํ๋ค.
- ๋ ๋ฒ์งธ RNN์ t๋ฒ์งธ ์์ ์ ์ฒซ ๋ฒ์งธ rnn์ hidden states์ t-1๋ฒ์งธ ์์ ์ ๋ ๋ฒ์งธ rnn์ hidden states๋ฅผ ๋ฐ์์ t๋ฒ์งธ ์์ ์ hidden states๋ฅผ ์์ฑํ๋ค.
- ์ด์ ๊ฐ์ ๋ฐฉ์์ rnn์ ๋ช ๊ฐ staking ํ๋๋์ ์๊ด์์ด ๋์ผํ๊ฒ ์ ์ฉ๋๋ค.
- ๋ง์ง๋ง ํ ํฐ์ ์ฝ์์ ๋ ๋์จ ์ถ๋ ฅ๊ณผ ์ ๋ต์ loss๋ฅผ ๊ณ์ฐํ๊ณ , ์ด loss๋ก stacked RNN์ back propagation์ ํตํด ํ์ตํ๋ค.
- ์ด๋ฒ์๋ ๋ฌธ์ฅ์ ๋ถ๋ฅํด๋ณด์
(1) Importing Libraries
# setup
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import Sequential, Model
from tensorflow.keras.preprocessing.sequence import pad_sequences
%matplotlib inline
print(tf.__version__)
(2) Preparing Dataset
# example data
sentences = ['What I cannot create, I do not understand.',
'Intellecuals solve problems, geniuses prevent them',
'A person who never made a mistake never tied anything new.',
'The same equations have the same solutions.']
y_data = [1, 0, 0, 1]
- y_data์์ 1์ richard feynman์ด ํ๋ ๋ง, 0์ albert eisntein์ด ํ๋ ๋ง ..
- ์์ ๋ฐ์ดํฐ๋ก ์ฃผ์ด์ง ๊ฐ sentence๋ฅผ ์บ๋ฆญํฐ์ ์ํ์ค๋ก ๊ฐ์ฃผํ๊ณ ๋ฌธ์ ๋ฅผ ํ ๊ฒ์ด๋ฉฐ,
- ์ด๋ฅผ ์ํด (์ง๋ ๋ฒ ์ฒ๋ผ) ํ ํฐ์ด ์ด๋ฌํ ์บ๋ฆญํฐ๋ฅผ integer index๋ก ๋งตํํ๊ณ ์๋ ํ ํฐ์ ๋์ ๋๋ฆฌ๋ฅผ ๋ง๋ค์ด์ผ ํจ
# creating a token dictionary
char_set = ['<pad>'] + sorted(list(set(''.join(sentences))))
idx2char = {idx: char for idx, char in enumerate(char_set)}
char2idx = {char: idx for idx, char in enumerate(char_set)}
print(char_set)
print(idx2char)
print(char2idx)
# converting sequence of token to sequence of indices
x_data = list(map(lambda sentence: [char2idx.get(char) for char in sentence], sentences))
x_data_len = list(map(lambda sentence: len(sentence), sentences))
print(x_data)
print(x_data_len)
print(y_data)
- ์ด์ ๋ณด๋ค ์ํ์ค์ ๊ธธ์ด๊ฐ ํจ์ฌ ๊ธธ์ด์ก๋ค.
- ์ด๋ ๊ฒ ๊ธธ์ด๊ฐ ๊ธด ์ํ์ค๋ฅผ ๋ค๋ฃฐ ๋์๋ ๋จ์ RNN๋ณด๋ค๋ Long Short - Term Memory Network(LSTM) ๋๋ Gated Recurrent Unit(GRU) ๋ฑ์ ํ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
- ์ฌ๊ธฐ์๋ stacked RNN ๊ตฌ์กฐ๋ก ์์ฑํ์๋ค.
# padding the sequence of indices
max_sequence = 58
x_data = pad_sequences(sequences=x_data, maxlen=max_sequence, padding='post', truncating='post')
# checking data
print(x_data)
print(x_data_len)
print(y_data)
- sentence์ sequence๊ฐ ๋ค๋ฅธ ๋ฌธ์ ๋ฅผ ํด๊ฒฐ
(3) Creating Model
# creating simple rnn for "many to one" classification without dropout
num_classes = 2
hidden_dims = [10, 10]
input_dim = len(char2idx)
output_dim = len(char2idx)
one_hot = np.eye(len(char2idx))
model = Sequential()
model.add(layers.Embedding(input_dim=input_dim, output_dim=output_dim,
trainable=False, mask_zero=True, input_length=max_sequence,
embeddings_initializer=keras.initializers.Constant(one_hot)))
model.add(layers.SimpleRNN(units=hidden_dims[0], return_sequences=True))
model.add(layers.TimeDistributed(layers.Dropout(rate=.2)))
model.add(layers.SimpleRNN(units=hidden_dims[1]))
model.add(layers.Dropout(rate=.2))
model.add(layers.Dense(units=num_classes))
- ์ด์ ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก mask_zero=True ์ต์ ์ ํตํด ์ํ์ค ์ค 0์ผ๋ก padding๋ ๋ถ๋ถ์ ์ฐ์ฐ์ ํฌํจํ์ง ์์ ์ ์๋ค.
trainable=False ์ต์ ์ผ๋ก one-hot vector๋ฅผ trainingํ์ง ์์ ์ ์๋ค.
- return_sequences=True: ๋ ๋ฒ์งธ rnn์ด ํ์ํ ํํ,
์ฆ (data dimension, max sequences, input dimension)์ ํํ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฆฌํดํ๋ค.
- TimeDistributed์ Dropout์ ์ด์ฉํ๋ ์ด์ ๋
stacked RNN์ด shallow RNN์ ๋นํด ๋ชจ๋ธ์ capacity๊ฐ ๋์ ๊ตฌ์กฐ์ด๋ฏ๋ก overfitting๋ ๊ฐ๋ฅ์ฑ์ด ํฌ๊ธฐ ๋๋ฌธ.
- ๋ฐ๋ผ์ rnn์ด ๊ฐ ํ ํฐ์ ์ฒ๋ฆฌํ hidden states์ dropout์ ๊ฑธ์ด overfitting์ ๋ฐฉ์งํด์ค๋ค.
(๋ ๋ฒ์งธ layer์์๋ ๋ง์ฐฌ๊ฐ์ง)
model.summary()
(4) Training model
# creating loss function
def loss_fn(model, x, y, training):
return tf.reduce_mean(tf.keras.losses.sparse_categorical_crossentropy(
y_true=y, y_pred=model(x, training), from_logits=True))
# creating an optimizer
lr = .01
epochs = 30
batch_size = 2
opt = tf.keras.optimizers.Adam(learning_rate=lr)
# generating data pipeline
tr_dataset = tf.data.Dataset.from_tensor_slices((x_data, y_data))
tr_dataset = tr_dataset.shuffle(buffer_size=4)
tr_dataset = tr_dataset.batch(batch_size=batch_size)
print(tr_dataset)
- ์ฐ๋ฆฌ๊ฐ ์ค๊ณํ stacked RNN ๊ตฌ์กฐ๋ Dropout์ ํ์ฉํ๋ค.
- Dropout์ trainingํ ๋ ํ์ฉํ๋ inference ๋จ๊ณ์์๋ ํ์ฉํ์ง ์์ผ๋ฏ๋ก
์ด๋ฅผ controlํ๊ธฐ ์ํด loss function์ training argument๋ฅผ ๋์ด ์ด๋ฅผ ์ปจํธ๋กคํ๋ค.
# training
tr_loss_hist = []
for epoch in range(epochs):
avg_tr_loss = 0
tr_step = 0
for x_mb, y_mb in tr_dataset:
with tf.GradientTape() as tape:
tr_loss = loss_fn(model, x=x_mb, y=y_mb, training=True)
grads = tape.gradient(target=tr_loss, sources=model.variables)
opt.apply_gradients(grads_and_vars=zip(grads, model.variables))
avg_tr_loss += tr_loss
tr_step += 1
else:
avg_tr_loss /= tr_step
tr_loss_hist.append(avg_tr_loss)
if (epoch + 1) % 5 == 0:
print('epoch: {:3}, tr_loss: {:.3f}'.format(epoch+1, avg_tr_loss.numpy()))
(5) Checking performance
yhat = model.predict(x_data)
yhat = np.argmax(yhat, axis=-1)
print('acc: {:.2%}'.format(np.mean(yhat == y_data)))
plt.plot(tr_loss_hist)
'Computer ๐ป > Deep Learning' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Support Vector Machine (์ํฌํธ ๋ฒกํฐ ๋จธ์ ) (0) | 2022.01.10 |
---|---|
[cs231n] 3. Loss Functions and Optimization | Linear Classification, Loss Function, Softmax, Optimization, Generalization, Regularization, Cross-validation, Gradient Descent ๋ฑ (0) | 2022.01.10 |
RNN (many-to-one) (0) | 2021.10.29 |
RNN in TensorFlow(ํ ์ํ๋ก์ฐ) (0) | 2021.10.28 |
RNN์ด๋? (2) (0) | 2021.10.28 |