본문 바로가기

Project 🖥/2022 캡스톤👩🏻‍💻

[캡스톤] 4월 3주차 trial and error

1. biLSTM + attention

이전에 fasttext+CNN모델에서 validation acc 0.89까지 나와서 정성적 평가까지 해보았더니 기사의 제목과 본문의 키워드는 유사하지만 뉘앙스가 다른 가짜뉴스들을 제대로 분류하지 못하고 있었다. 0.52정도의 score로 가짜뉴스를 분류하고 있었기 때문.

문맥을 잘 반영하는 모델로 바꾸어야겠다는 생각에 LSTM과 CNN에서 문맥을 잘 반영하려면 어떻게 해야할까를 찾다가 attention mechanism을 활용하기로 했다!

우선 body의 문맥을 잘 반영하지 못하니까 body에 biLSTM을 쓰고 attention까지 해서 context vector를 잘 뽑아서 title+context vector로 신경망에 넣어보자👊

 

2. 모델 저장 error

서브클래싱으로 모델을 작성하면 모델 저장 방식이 기존의 sequential api나 functional api로 작성하는 방식과 다른 거 같다.

그래서 계속 오류가 났엇는데 model.compile할 때 모델 저장 콜백을 빼주고 진행하니까 문제없이 코드가 돌아갔다.

 

3. 구조를 짜는데에 어려움을 느끼고 있음... return_sequences = True

  • 우선은 LSTM()에서 return_sequences=False가 기본값이다.
  • 그러면 어떤 값이 return이 될까? last hidden state가 최종 output이다.
  • return_sequences=True는 모든 time step의 states를 리턴해주어서 sequences를 리턴해준다고 본다.
    마지막 time step의 결과만 필요할 때(ex. 분류, 감성분석)는 false그대로 사용하고 어텐션을 사용하거나 speech recognition처럼 전체 시퀀스가 필요할 때(모든 time step의 states)는 True해서 사용하기~!
  • 세개의 값을 return하는데,lstm1 : 모든 time step의 hidden state
    state_h : 마지막 time step의 hidden state
    state_c : 마지막 time step의 cell state
lstm1, state_h, state_c = LSTM(1, return_sequences=True, return_sequences=True)(inputs1)
  • cell state와 hidden state의 차이는 LSTM의 구조와 식을 이해하고 있으면 된다!

 

Difference Between Return Sequences and Return States for LSTMs in Keras

 

Difference Between Return Sequences and Return States for LSTMs in Keras

The Keras deep learning library provides an implementation of the Long Short-Term Memory, or LSTM, recurrent neural network. As part […]

machinelearningmastery.com