Advanced_RNN

Jacob Kim·2024년 1월 31일

Naver Project Week3

목록 보기
4/10
import tensorflow as tf
tf.__version__
# 2.15.0
layers = tf.keras.layers

RNN 테스트를 위한 더미데이터 생성

inputs = tf.random.normal([2, 5, 4])
inputs
#<tf.Tensor: shape=(2, 5, 4), dtype=float32, numpy=
#array([[[ 4.4453326e-01,  1.0075720e-01, -9.5329976e-01,  #1.1139495e+00],
#        [ 8.4952074e-01, -8.2276110e-04, -4.4115674e-01,  #2.1292589e+00],
#        [-4.7433725e-01,  2.6830766e-01, -1.1578703e+00,  9.8682511e-#01],
#        [ 9.8028284e-01,  6.5015936e-01,  1.4324926e+00,  #1.0221030e+00],
#        [ 9.6877623e-01,  7.0859128e-01,  1.5271817e+00, -8.8737763e-#02]],
#
#       [[-1.1165879e+00,  1.3280001e-01, -1.0021292e+00,  1.5935566e-#01],
#        [-1.5056019e+00, -4.3323570e-01,  5.5768734e-01, -7.4184048e-#01],
#        [ 2.1857807e-01,  2.6606172e-01,  9.8774773e-01, -1.4808702e-#01],
#        [ 2.1850944e+00,  1.5791602e-01, -8.1345099e-01, -4.5798057e-#01],
#        [ 4.2589566e-01,  1.5710378e+00,  2.9278207e-01,  8.3634877e-#01]]],
#      dtype=float32)>
  • tf.keras를 이용한 다양한 RNN layer를 구현해보자

Multi-Layer RNN

model = tf.keras.Sequential()
model.add(layers.GRU(1, return_sequences=False)) # 2, 1을 입력값을 받을 수 없다.
model(inputs)
#<tf.Tensor: shape=(2, 1), dtype=float32, numpy=
#array([[-0.61818504],
#       [-0.83722234]], dtype=float32)>
model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 gru (GRU)                   (2, 1)                    21        
                                                                 
=================================================================
Total params: 21 (84.00 Byte)
Trainable params: 21 (84.00 Byte)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

Bi-directional RNN

model = tf.keras.Sequential()
model.add(layers.Bidirectional(layers.LSTM(1, return_sequences=True)))
model.add(layers.Bidirectional(layers.LSTM(1, return_sequences=True)))
model.add(layers.Bidirectional(layers.LSTM(1)))
model(inputs)
#<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
#array([[-0.03389528, -0.00798441],
#       [-0.02343381,  0.00245169]], dtype=float32)>
model.summary()
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 bidirectional (Bidirection  (2, 5, 2)                 48        
 al)                                                             
                                                                 
 bidirectional_1 (Bidirecti  (2, 5, 2)                 32        
 onal)                                                           
                                                                 
 bidirectional_2 (Bidirecti  (2, 2)                    32        
 onal)                                                           
                                                                 
=================================================================
Total params: 112 (448.00 Byte)
Trainable params: 112 (448.00 Byte)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
profile
AI, Information and Communication, Electronics, Computer Science, Bio, Algorithms

0개의 댓글