[Survey] Seq2Seq 자료 모으기

2020. 4. 14. 19:17분석 Python/구현 및 자료

728x90

Tensorflow에서 seq2seq를 하고 싶은데, 생각보다 자료가 많지 않은 것 같아서, 검색하면서 찾은 것들
하다 보니 오류 투성이라 Pytorch쪽으로 넘어가고 싶다는 생각이 100번 정도 든 듯 (아니면 Tensorflow 2?...)

multi step

single step에서 multi step까지 예측

keras multi step

keras multi step (kaggle)

4 Strategies for multi step time series forcasting

Seq2Seq

lstm 먼가 쉽게 설명한 것 처럼 보이는 코드

seq2seq 설명

seq2seq 설명 자료

seq2seq attention 한국어 설명

attention-seq2seq 그림 설명

keras_seq2seq_single_prediction

stock prediction github

tf-seq2seq github

seq2seq 관련 코드

custom-seq2seq model for machine trnaslation

seq2seq 추론 학습 잘 정리된 코드

seq2seq decoder 사용법

dynamic_decode 작동하는 코드

dynamic_decode 작동하는 코드 추론 학습 부분 코드

dynamic_decode 돌아가진 않지만 관련된 코드

seq2seq 간단 코드

attention based seq2seq

attention based seq2seq 2

tensorflow seq2seq attention 포함 nmt 코드

Seq2Seq 학습 추론 잘 설명한 그림있는 미디엄 사이트

Attention

attention mechanism 그림 설명 자료

seq2seq부터 self attention까지의 part 1 미디엄 자료

seq2seq self attention part 2 자료

tensorflow v2 transformer 공식 자료

Stock Forecasting with Transformer Architecture & Attention Mechanism

Stock Prediction multi-Head Attention (Kaggle 자료)

medium attention mechanism 한국어 자료

seq2seq 제목 추출

설정하는 이유 

> RNN에서 현시점의 hidden state는 이전 hidden state와 현 시점 입력값에 의해 결정됨. 
하지만 첫 시점의 hidden state는 이전 hidden state가 없기 때문에 별도의 초기 state가 필요함.
하지만 저 초기값도 test 할 때는 어떻게 해야 할지?? 그냥 초기값이 0을 넣는 게 맞는 것인지 의문
아니면 먼가 학습된 것을 넣어야 하는 것인지?

  • 아래 참고 사이트 중 한 실험에서는 제로 보단 학습된 초기 state를 사용하는 것이 좋다고 함.
    • 개인적으로 초기값을 넣는 것보다는 학습된 초기 state를 넣는 게 더 데이터가 읽기에 좋을 것이라고 생각함. 
    • 예를 들어, 주가 같은 경우 batch size마다 자를 때 initial_state를 꼭 0이나 noise로 줄 필요 없이 현재 학습하고 있는 배치 이전에 배치 정보를 넣어주면 그 initial_state 이전 batch의 먼가 정보를 담고 있지 않을까 하는 생각도 들긴 함.
 drop = tf.contrib.rnn.DropoutWrapper(
 rnn_cells, output_keep_prob = self.keep_prob)
 ## version 1
 self.encoder_hidden_layer = tf.placeholder(tf.float32, (None, num_layers * 2 * size_layer))
 ## version 2
 self.encoder_hidden_layer =\
 tf.random_normal(shape=(self.batch_size, drop.state_size),
 mean=0.0)
 _, last_state = tf.nn.dynamic_rnn(
 drop, self.X, 
 initial_state = self.encoder_hidden_layer,
 dtype = tf.float32
 )
 
 ## 참고 사이트
https://wdprogrammer.tistory.com/34
https://r2rt.com/non-zero-initial-states-for-recurrent-neural-networks.html

 

만약 Time Series 데이터를 사용할 경우 순차적으로 데이터를 넣으면 그 이전 스테이트도 중요한 정보를 가지고 있다고 이론적인 근거는 없지만 직관적으로 생각하고 있다.
그래서 아래 그림처럼도 이전 hidden state를 넣어서 좋은 역할을 할 수 있는지 테스트해보고 있다.

 

tf.contrib.seq2seq.dynamic_decode 작동한 코드(겨우 찾음 ㅠㅠ) 다른 것은 에러가 남
핵심은 encoder cell과 decoder cell 약간 동일한 포맷이어야 됨. (아직 완벽히 작동을 하지 않는 코드)

def lstm_cell(size_layer):
    return tf.nn.rnn_cell.LSTMCell(size_layer,
                                   state_is_tuple = False,
                                   activation= tf.nn.tanh)
size_layer = 32
num_layers= 2                            
rnn_cells = tf.nn.rnn_cell.MultiRNNCell(
        [lstm_cell(size_layer) for _ in range(num_layers)],
        state_is_tuple = False
    )
decoder_cell = tf.nn.rnn_cell.MultiRNNCell(
        [lstm_cell(size_layer) for _ in range(num_layers)],
        state_is_tuple = False
)

### 똑같은 layer 그리고 state_is_tuple = True로 해야함!
size_layer = 32
encoder_cell = tf.nn.rnn_cell.BasicLSTMCell(size_layer, state_is_tuple=True)
decoder_cell = tf.nn.rnn_cell.BasicLSTMCell(size_layer, state_is_tuple=True)


X = tf.placeholder(tf.float32, ( TIME_STEPS , None, size))
DecX = tf.placeholder(tf.float32, (TIME_STEPS , None , size))
encoder_cell = tf.nn.rnn_cell.BasicLSTMCell(size_layer, state_is_tuple=True)
enc_outputs , enc_last_state = tf.nn.dynamic_rnn(
encoder_cell,X, time_major=True, dtype=tf.float32)
size_layer = 32
projection_layer = layers_core.Dense(15, use_bias=False)
helper = tf.contrib.seq2seq.TrainingHelper(DecX, batch_size, time_major=True)
decoder_cell = tf.nn.rnn_cell.BasicLSTMCell(size_layer)
initial_state = enc_last_state
decoder = tf.contrib.seq2seq.BasicDecoder(
    decoder_cell, helper, initial_state,
    output_layer=projection_layer)
final_outputs, _final_state, _final_sequence_lengths = tf.contrib.seq2seq.dynamic_decode(decoder)

https://gist.github.com/higepon/eb81ba0f6663a57ff1908442ce753084

 

 

tf.nn.dynamic_rnn 아웃풋 설명

  1. Output 1, let's call it h, has all outputs at each time steps (i.e. h_1, h_2, etc),
  2. Output 2, final_state, has two elements: the cell_state, and the last output for each element of the batch (as long as you input the sequence length to dynamic_rnn).
h, final_state= tf.dynamic_rnn( ..., sequence_length=[batch_size_vector], ... )
the last state for each element in the batch is:

final_state.h
Note that this includes the case when the length of the sequence is different for each element of the batch, as we are using the sequence_length argument.

https://stackoverflow.com/questions/36817596/get-last-output-of-dynamic-rnn-in-tensorflow

 

Get last output of dynamic_rnn in tensorflow?

I am using dynamic_rnn to process MNIST data: # LSTM Cell lstm = rnn_cell.LSTMCell(num_units=200, forget_bias=1.0, initializer=tf.random_normal) #

stackoverflow.com

Seq2Seq 훈련, 추론 차이를 잘 보여주는 그림

Inference

Inference

Training

Training

 

728x90