Go together

If you want to go fast, go alone. If you want to go far, go together.

핸즈온 머신러닝 2판

LSTM autoencoder

NowChan 2022. 2. 4. 13:42

예시 데이터 생성

 

def temporalize(X, y, lookback):
  output_X = []
  output_y = []
  for i in range(len(X)-lookback-1): # i=0 ~ 4, when lookback=3
    t = []
    for j in range(1, lookback+1):   # j=1 ~ 3, when lookback=3
      t.append(X[[i+j+1], :]) 
    output_X.append(t) # i=0, t.append X[2] ~ X[4]
                       # i=1, t.append X[3] ~ X[5]
    output_y.append(y[i+lookback+1]) # i=0, y = y[4]
                                     # i=1, y = y[5]
  return output_X, output_y
timeseries = np.array([[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
                       [0.1**3, 0.2**3, 0.3**3, 0.4**3, 0.5**3, 0.6**3, 0.7**3, 0.8**3,
                       0.9**3]]).transpose()

timesteps = timeseries.shape[0]
n_features = timeseries.shape[1]
timeseries # shape=(9, 2)
'''
array([[0.1  , 0.001],
       [0.2  , 0.008],
       [0.3  , 0.027],
       [0.4  , 0.064],
       [0.5  , 0.125],
       [0.6  , 0.216],
       [0.7  , 0.343],
       [0.8  , 0.512],
       [0.9  , 0.729]])
'''
timesteps = 3
X, y = temporalize(X=timeseries, y=np.zeros(len(timeseries)), lookback = timesteps)

n_features = 2
X = np.array(X)
X = X.reshape(X.shape[0], timesteps, n_features)

# X.shape = (5, 3, 2)
'''
X =
[[[0.3   0.027]
  [0.4   0.064]
  [0.5   0.125]]

 [[0.4   0.064]
  [0.5   0.125]
  [0.6   0.216]]

 [[0.5   0.125]
  [0.6   0.216]
  [0.7   0.343]]

 [[0.6   0.216]
  [0.7   0.343]
  [0.8   0.512]]

 [[0.7   0.343]
  [0.8   0.512]
  [0.9   0.729]]]
'''

'''
np.zeros(len(timeseries)) =
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
'''

 

 

LSTM Autoencoder 구조 이해하기

model = keras.models.Sequential()
model.add(keras.layers.LSTM(128, activation='relu', input_shape=(timesteps, n_features),
          return_sequences=True))
model.add(keras.layers.LSTM(64, activation='relu', return_sequences=False))
model.add(keras.layers.RepeatVector(timesteps))
model.add(keras.layers.LSTM(64, activation='relu', return_sequences=True))
model.add(keras.layers.LSTM(128, activation='relu', return_sequences=True))
model.add(keras.layers.TimeDistributed(keras.layers.Dense(n_features)))
model.compile(optimizer='adam', loss='mse')
model.summary()

'''
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm_4 (LSTM)               (None, 3, 128)            67072     
                                                                 
 lstm_5 (LSTM)               (None, 64)                49408     
                                                                 
 repeat_vector_1 (RepeatVect  (None, 3, 64)            0         
 or)                                                             
                                                                 
 lstm_6 (LSTM)               (None, 3, 64)             33024     
                                                                 
 lstm_7 (LSTM)               (None, 3, 128)            98816     
                                                                 
 time_distributed_1 (TimeDis  (None, 3, 2)             258       
 tributed)                                                       
                                                                 
=================================================================
Total params: 248,578
Trainable params: 248,578
Non-trainable params: 0
_________________________________________________________________
'''
# fit the model
model.fit(X, X, epochs=300, batch_size=5, verbose=0)

# reconstruction
yhat = model.predict(X, verbose=0)
print('---Predicted---')
print(np.round(yhat, 3))
print('---Actual---')
print(np.round(X, 3))
'''
---Predicted---
[[[0.321 0.04 ]
  [0.413 0.064]
  [0.497 0.124]]

 [[0.39  0.068]
  [0.494 0.122]
  [0.595 0.214]]

 [[0.492 0.12 ]
  [0.595 0.215]
  [0.702 0.341]]

 [[0.603 0.209]
  [0.698 0.348]
  [0.798 0.509]]

 [[0.697 0.343]
  [0.797 0.508]
  [0.894 0.725]]]
  
---Actual---
[[[0.3   0.027]
  [0.4   0.064]
  [0.5   0.125]]

 [[0.4   0.064]
  [0.5   0.125]
  [0.6   0.216]]

 [[0.5   0.125]
  [0.6   0.216]
  [0.7   0.343]]

 [[0.6   0.216]
  [0.7   0.343]
  [0.8   0.512]]

 [[0.7   0.343]
  [0.8   0.512]
  [0.9   0.729]]]
'''

모델 구조, 출처: https://skyeong.net/316

 

위 다이어그램은 한 데이터 샘플에 대한 데이터 흐름입니다. ([[0.4, 0.5, 0.6], [0.064, 0.125, 0.216]])

 

return_sequences 설정 차이 , 출처: https://skyeong.net/316

RepeatVector(timesteps)를 사용해 벡터를 timesteps 만큼 복제합니다.

 

 

출처: https://skyeong.net/316