바뀜

2,482 바이트 추가됨 ,  2022년 1월 20일 (목) 17:47
잔글
편집 요약 없음
3번째 줄: 3번째 줄:     
현재일을 포함한 50일 거래일의 데이터를 바탕으로 이후의 값을 예측. 일반적으로 OHLCV를 넣어준다.
 
현재일을 포함한 50일 거래일의 데이터를 바탕으로 이후의 값을 예측. 일반적으로 OHLCV를 넣어준다.
 +
 +
=== 요구사항 ===
 +
케라스와 텐서플로우, 판다스, 넘파이 따위의 라이브러리를 필요로 한다.
    
== 사용 ==
 
== 사용 ==
16번째 줄: 19번째 줄:  
import numpy as np
 
import numpy as np
 
import FinanceDataReader as fdr  # 주가데이터를 알려주는 라이브러리.
 
import FinanceDataReader as fdr  # 주가데이터를 알려주는 라이브러리.
form sklearn import preprocessing  # 사이킷런의 데이터 전처리 기능을 이용한다.
   
</syntaxhighlight>
 
</syntaxhighlight>
 
|-
 
|-
22번째 줄: 24번째 줄:  
|데이터를 불러와 50개씩 나누어 교육데이터를 준비한다.
 
|데이터를 불러와 50개씩 나누어 교육데이터를 준비한다.
 
|<syntaxhighlight lang="python">
 
|<syntaxhighlight lang="python">
 +
from sklearn import preprocessing  # 사이킷런의 데이터 전처리 기능을 이용한다.
 +
 
def call_dataset(ticker = '005930', stt = '2015-01-01', end = '2021-03-30', history_points = 50):
 
def call_dataset(ticker = '005930', stt = '2015-01-01', end = '2021-03-30', history_points = 50):
 
     data = fdr.DataReader(ticker, stt, end)
 
     data = fdr.DataReader(ticker, stt, end)
27번째 줄: 31번째 줄:  
     print('data: ', data.shape)
 
     print('data: ', data.shape)
 
     data = data.values # 값만 갖고온다
 
     data = data.values # 값만 갖고온다
     data_normalizer = preprocessing.MinMaxScaler() # 데이터를 0~1 범위로 점철되게 하는 함수 call
+
     data_normalizer = preprocessing.MinMaxScaler() # 데이터를 0~1 범위로 전처리 하는 함수 call
     data_normalized = data_normalizer.fit_transform(data) # 데이터를 0~1 범위로 점철되게 함수 수행
+
     data_normalized = data_normalizer.fit_transform(data) # 데이터를 0~1 범위로 전처리
 
     print('data_normalized: ', data_normalized.shape)
 
     print('data_normalized: ', data_normalized.shape)
 
     # using the last {history_points} open close high low volume data points, predict the next open value
 
     # using the last {history_points} open close high low volume data points, predict the next open value
91번째 줄: 95번째 줄:     
import keras
 
import keras
from keras.models import Model
  −
from keras.layers import Dense, Dropout, LSTM, Input, Activation
  −
from keras import optimizers
   
import numpy as np
 
import numpy as np
 
np.random.seed(4)
 
np.random.seed(4)
124번째 줄: 125번째 줄:  
|
 
|
 
|<syntaxhighlight lang="python">
 
|<syntaxhighlight lang="python">
 +
from keras.models import Model
 +
from keras.layers import Dense, Dropout, LSTM, Input, Activation
 +
from keras import optimizers
 +
 
# model architecture
 
# model architecture
 
lstm_input = Input(shape=(history_points, 5), name='lstm_input')
 
lstm_input = Input(shape=(history_points, 5), name='lstm_input')
174번째 줄: 179번째 줄:  
</syntaxhighlight>
 
</syntaxhighlight>
 
|}
 
|}
 +
 +
= 성능을 개선하기 위한 노력 =
 +
어떻게 하면 성능이 더욱 개선될까?
 +
 +
상황에 따라, 데이터 형태에 따라 하이퍼파라미터는 바뀌기 마련이지만, 이에 대한 연구가 있어 간략하게 써 본다.<ref name=":0">Journal of Digital Convergence. LSTM을 이용한 주가예측 모델의 학습방법에 따른 성능분석(정종진, 김지연. 2020)</ref>
 +
 +
그리고 향상의 노력을 이곳에 담아본다.
 +
 +
== 데이터 전처리 ==
 +
거래량 특정값에 있어 거래량 수치 자체를 사용하는 것보다 거래량의 변화량을 사용한 경우가 평균제곱오차에서 성능이 더 좋았다.
 +
 +
하나의 데이터를 사용하기보다 OHLCV 모두를 입력으로 넣는 편이 더 잘 작동한다. 그리고 인덱스를 넣어주면 더 좋았다.(더 상위기업만 포함한 것이)<ref>2015 IEEE International Conference on Big Data. A LSTM-based method for stock returns prediction : A case study of China stock market. Kai Chen, Yi Zhou, Fangyan Dai(2015)</ref>
 +
 +
== 가중치 초기화 방법 ==
 +
{| class="wikitable"
 +
!과정
 +
!설명
 +
!적합한 방법
 +
|-
 +
|kernel initializer
 +
|
 +
|Xavier normal
 +
|-
 +
|recurrent initializer
 +
|
 +
|random normal
 +
|-
 +
|bias initializer
 +
|
 +
|he uniform
 +
|}
 +
 +
== 정규화 방법 ==
 +
{| class="wikitable"
 +
!과정
 +
!설명
 +
!적합한 방법
 +
|-
 +
|kernel
 +
|activity regularizer가 L2(0.1)일 때.
 +
|L2(0.003)
 +
|}
 +
 +
== 활성화 함수 ==
 +
{| class="wikitable"
 +
!과정
 +
!설명
 +
!적합한 방법
 +
|-
 +
|기본 활성화 함수
 +
|
 +
|elu, softsign, tanh
 +
|-
 +
|recurrent활성화 함수
 +
|
 +
|elu, softsign, tanh, hard sigmoid
 +
|}
 +
위 둘의 조합으로는 케라스의 기본 설정인 tanh와 hard sigmoid가 가장 성능이 좋았다고 한다.
 +
 +
== 성능평가 ==
 +
 +
=== loss ===
 +
회귀에 대한 문제는 일반적으로 MSE가 사용된다.<ref name=":1">International Journal of Machine Learrning and Cybernetics(2020). Study on the prediction of stock price based on the associated network model of LSTM. Guangyu Ding, Lianxi Qin.</ref>
 +
 +
=== metrics ===
 +
평균제곱근오차(RMSE)를 사용한다. 모델의 예측값과 실제 환경에서 관찰되는 값의 차리를 다룰 때 흔히 사용되는 평가방법이라 한다.
 +
 +
== 최적화 함수 ==
 +
활성화 함수가 tanh일 땐 Adam이 가장 좋았다고 한다.<ref name=":0" /><ref name=":1" />
 +
 +
= 각주 =
 
[[분류:LSTM]]
 
[[분류:LSTM]]
 +
<references />
 +
[[분류:Keras]]