Develop LSTM Models for Time Series Forecasting | Kaggle
# 일변량 데이터 준비
from numpy import array
#일변량 시쿼스 샘플 데이터를 나눈다.
def split_sequence(sequence, n_steps):
X, y = list(), list()#출력
for i in range(len(sequence)):#입력받은 시퀀스의 길이 만큼 반복시킨다.0~8
#마지막 인덱스 계산
end_ix = i + n_steps
#인덱스 오류 안나게 브레이크
if end_ix > len(sequence)-1:
break
#X,y에 인풋과 어웃풋 부분을 추가한다.
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
#입력에 사용할 시퀀스를 정의한다.
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
#시간 단계 설정
n_steps = 3
#샘플 데이터를 나누어준다
X, y = split_sequence(raw_seq, n_steps)
#출력
for i in range(len(X)):
print(X[i], y[i])
#vanilla LSTM 각장 단순한 형태의 순환신경망(장기 의존성때문에 짧은 시퀀스에만 효과적)
#한개의 lstm은닉층이 단층이며 1개의 출력층이 예측하는데 사용
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
# split a univariate sequence into samples
def split_sequence(sequence, n_steps):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the sequence
if end_ix > len(sequence)-1:
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
#인풋 시퀀스
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
#타임 스텝은 3
n_steps = 3
#샘플 데이터를 나눈다
X, y = split_sequence(raw_seq, n_steps)
# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))#3차원으로 (차원,행,열)X가 3차원으로 1개의 열로
print(X)
#모델링
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))#활성화 함수는 relu, 전달 형태는 3x1
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')#손실함수는 평균제곱오차, 옵티마이저는 경사하강법의 단점을 개선하고 adaGrad와 모멘텀 방식을 합친 adam사용
# 훈련, verbose는 0이면 훈련과정이 출력 안되며 1이면 출력
model.fit(X, y, epochs=200, verbose=0)
# 예측
x_input = array([70, 80, 90])
x_input = x_input.reshape((1, n_steps, n_features))
yhat = model.predict(x_input, verbose=1)#예측
print(yhat)#103으로 예측하였다.
원래 lstm 모델은 1개의 은닉lstm층과 표준 순전파 출력층으로 구성되어있다.
Stack lstm은 확장한 것이다 이 모델을, 다층 은닉 lstm층으로 where여러 기억셀을 가지고 있는.
여기에서는 너는 스택lstm에대해서 알아볼 것 이다.
이 튜토리얼을 완료하면 너는 이것들을 알 수 있다.
-심층 신경망의 장점
-스택 lstm의 rnn구조
개요
1.왜 깊이를 증가시켜야하나?
2.스택lstm구조
1.왜 깊이를 증가시키나?
스택lstm은 은닉층을 더 깊게 만든 것이다, 더 정확한 딥러닝 기술을 얻을 수 있다.
일반적으로 신경망의 깊이는 성공에 기여한다, 넓은 도전 예측문제에 대한.
추가적으로 은닉층은 추간될수있다, 다층 퍼셉트론 신경망을 더 깊게하기 위한. 추가로 숨겨진 층들은 이해된다, 이전 층에서 학습된 것과 새로운 층에서 나타내는 것들을 높은층으로 끌어올리기 위해서
예를 들어, 선에서 도형, 객체에 까지.
충분히 큰 단층 은닉층 다층 퍼셉트론은 사용될수있다, 대부분의 기능들을 근사화하는데. 깊이를 증가시키는 네트워크는 제공한다, 대체 솔루션을 , 더 적은 뉴런과 더 빠른 훈련을 필요로하는.
궁국적으로 깊이를 추가하는 것은 표현 최적화중 하나다.
스택 lstm구조
lstm에서도 같은 장점을 이용할 수 있다.
lstm이 시퀀스 데이터에서 작동하는 것은, 의미한다, 시간이 지나면서 입력 관측치의 추상화 레벨이 추가된 층에 추가된다고(이 문장 잘 해석 안됨).
스택lstm 혹은 심층 lstm은 도입되었다 Graves에 의해서. 그들이 음성 인식문제에 , 벤치마크의 어려운 표준문제를 beating하고.
같은 연구에서, 그들은 네트워크의 킾이가 메모리 셀 수보다 더 중요하다는 것을 발견했다.
스택형 lstm은 지금 시퀀스 문제에 도전하기에 적합한 기술이다. 스택형 lstm은 다층lstm으로 구성된 것으로 정의할 수 있다.
위의 lstm은 시퀀스를 출력으로 제공한다, 1개의 출력값이 아니라. 특히 입력 시간 단계당 하나의 출력이 아니라, 모든 입력 단계의 대한 하나의 출력이다.
#https://machinelearningmastery.com/stacked-long-short-term-memory-networks/
#위의 lstm은 lstm층이 단층이라면 stacked lstm은 여러층을 쌓을 수 있다.
#기본 3차원 입력이며 출력은 2차원이며 출력할때 시퀀스의 끝에 출력한다.
# univariate stacked lstm example
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
# split a univariate sequence
def split_sequence(sequence, n_steps):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the sequence
if end_ix > len(sequence)-1:
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
# define input sequence
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# choose a number of time steps
n_steps = 3
# split into samples
X, y = split_sequence(raw_seq, n_steps)
# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))
#위의 모델과 비교하면 add(lstm)이 늘어난 것을 보고 단층에서 다층으로 lstm층이 늘어남을 볼 수 있다.
model = Sequential()
model.add(LSTM(50, activation='relu', return_sequences=True, input_shape=(n_steps, n_features)))
model.add(LSTM(50, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=200, verbose=0)
# demonstrate prediction
x_input = array([70, 80, 90])
x_input = x_input.reshape((1, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
회귀를 위한 lstm과 양방향lstm
lstm은 1997년에 처음 제안된 모델이다. lstm은 RNN이며 양방향 lstm은 이 모델을 확장한 것이다. 이 특징은 네트워크가 미래의 셀 처리에 사용할 정보들을 저장할 수 있다는 것이다. 우리는 lstm을 두개의 주요 벡터를 가진 메모리풀이 있는 rnn이라고 생각할 수 있다.
1.단기기억상태:출력을 현재 시간 단계로 유지
2.장기 상태:저장,읽기를 거부한다, 의미있는 것들을 오랜기간 네트워크를 지나갈때 동안
이러한 읽기,저장,쓰기의 결정은 활성화함수를 기반으로 한다. 그림1에서 볼 수 있듯이.
출력은 활성화함수로부터 0부터 1사이이다.
망각과 출력 게이트는 결정한다, 들어오는 새로운 정보를 버릴지 유지할지. lstm블럭 메모리와 출력 게이트의 조건은 모델 결정을 생산한다. 그 출력은 다시 네트워크에 전달된다, 다시 반복 시퀀스를 만드는 입력으로
양방향 lstm에서는 싱글 모델을 훈련하는 대신에, 2가지를 도입한다.
첫번째 모델은 공급된 시퀀스를 학습하고 두번째 모델은 이 시퀀스의 역을 학습한다.
두개의 모델이 훈련되고 나서부터, 우리는 이것을을 결합할 메커니즘을 만들 필요가 있다. 이것을 흔히 병합단계라고 한다. 병합은 다음 펑션중 1가지를 사용할 수 있다
-합
-곱
-평균
-결합
#양뱡향lstm은 훈련시에 시퀀스의 역방향도 사용한다.https://towardsdatascience.com/lstm-and-bidirectional-lstm-for-regression-4fddf910c655
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Bidirectional
# split a univariate sequence
def split_sequence(sequence, n_steps):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the sequence
if end_ix > len(sequence)-1:
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
# define input sequence
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# choose a number of time steps
n_steps = 3
# split into samples
X, y = split_sequence(raw_seq, n_steps)
# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))
# define model
model = Sequential()
model.add(Bidirectional(LSTM(50, activation='relu'), input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=200, verbose=0)
# demonstrate prediction
x_input = array([70, 80, 90])
x_input = x_input.reshape((1, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
#cnn은 2차원 이미지 데이터를 처리하는데 개발된 것이다. cnn은 일변량 시계열 데이터같은 1차원 시퀀스에서 자동적으로 추출하고 학습하는데 효과적이다.
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import TimeDistributed
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
# split a univariate sequence into samples
def split_sequence(sequence, n_steps):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the sequence
if end_ix > len(sequence)-1:
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
# define input sequence
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# choose a number of time steps
n_steps = 4
# split into samples
X, y = split_sequence(raw_seq, n_steps)
# reshape from [samples, timesteps] into [samples, subsequences, timesteps, features]
n_features = 1
n_seq = 2
n_steps = 2
X = X.reshape((X.shape[0], n_seq, n_steps, n_features))#4차원으로 리쉐잎한다.
# define model
model = Sequential()
model.add(TimeDistributed(Conv1D(filters=64, kernel_size=1, activation='relu'), input_shape=(None, n_steps, n_features)))#1D합성곱층 추가하며 (필터 수 , 커널의 크기,활성화함수,입력 형태)
model.add(TimeDistributed(MaxPooling1D(pool_size=2)))#맥스풀링이며 풀링윈도우 크기설정
model.add(TimeDistributed(Flatten()))#1차원 배열 데이터로 바꾸어준다.
model.add(LSTM(50, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=500, verbose=0)
# demonstrate prediction
x_input = array([60, 70, 80, 90])
x_input = x_input.reshape((1, n_seq, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
#위의 lstm과 관련 있으며, 입력의 컨볼루션이 직접 lstm에 들어간다. 2차원 데이터를 읽기위해 개발되었지만, 일변량 시계열 예측에 적용될 수 있다.
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import ConvLSTM2D
# split a univariate sequence into samples
def split_sequence(sequence, n_steps):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the sequence
if end_ix > len(sequence)-1:
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
# define input sequence
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# choose a number of time steps
n_steps = 4
# split into samples
X, y = split_sequence(raw_seq, n_steps)
# reshape from [samples, timesteps] into [samples, timesteps, rows, columns, features]
n_features = 1
n_seq = 2
n_steps = 2
X = X.reshape((X.shape[0], n_seq, 1, n_steps, n_features))
# define model
model = Sequential()
model.add(ConvLSTM2D(filters=64, kernel_size=(1,2), activation='relu', input_shape=(n_seq, 1, n_steps, n_features)))
model.add(Flatten())
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=500, verbose=0)
# demonstrate prediction
x_input = array([60, 70, 80, 90])
x_input = x_input.reshape((1, n_seq, 1, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
# 다변량
from numpy import array
from numpy import hstack
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
# split a multivariate sequence into samples
def split_sequences(sequences, n_steps):
X, y = list(), list()
for i in range(len(sequences)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the dataset
if end_ix > len(sequences):
break
# gather input and output parts of the pattern
seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1, -1]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
#인풋 시퀀스
in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90])
in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95])
out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))])#위의 인풋 시퀀스의 각각 인덱스를 더해서 저장
#입력 시퀀스들과 출력 시퀀스를 2차원으로 만든다 행x열, 9x1
in_seq1 = in_seq1.reshape((len(in_seq1), 1))
in_seq2 = in_seq2.reshape((len(in_seq2), 1))
out_seq = out_seq.reshape((len(out_seq), 1))
#수평으로 데이터 셋들을 붙힌다
dataset = hstack((in_seq1, in_seq2, out_seq))
# choose a number of time steps
n_steps = 3
# convert into input/output
X, y = split_sequences(dataset, n_steps)
# the dataset knows the number of features, e.g. 2
n_features = X.shape[2]
# define model
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
#훈련
model.fit(X, y, epochs=200, verbose=0)
x_input = array([[80, 85], [90, 95], [100, 105]])
x_input = x_input.reshape((1, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
#일변량 다단계 벡터-아웃풋 스택lstm
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
# split a univariate sequence into samples
def split_sequence(sequence, n_steps_in, n_steps_out):#출력 변수도 추가된다.
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps_in
out_end_ix = end_ix + n_steps_out
# check if we are beyond the sequence
if out_end_ix > len(sequence):
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix:out_end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
#입력 시퀀스
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
#다변량이니 n_steps_out추가
n_steps_in, n_steps_out = 3, 2
# split into samples
X, y = split_sequence(raw_seq, n_steps_in, n_steps_out)
# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))
# define model
model = Sequential()
model.add(LSTM(100, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features)))
model.add(LSTM(100, activation='relu'))
model.add(Dense(n_steps_out))#위에서 설정한 만큼 출력층을 나오게하는 dense층 추가
model.compile(optimizer='adam', loss='mse')
#훈련
model.fit(X, y, epochs=50, verbose=0)
# demonstrate prediction
x_input = array([70, 80, 90])
x_input = x_input.reshape((1, n_steps_in, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
#일변량 다단계 인코더-디코더 lstm
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import RepeatVector
from keras.layers import TimeDistributed
# split a univariate sequence into samples
def split_sequence(sequence, n_steps_in, n_steps_out):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps_in
out_end_ix = end_ix + n_steps_out
# check if we are beyond the sequence
if out_end_ix > len(sequence):
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix:out_end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
#입력 시퀀스
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# choose a number of time steps
n_steps_in, n_steps_out = 3, 2
# split into samples
X, y = split_sequence(raw_seq, n_steps_in, n_steps_out)
# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))
y = y.reshape((y.shape[0], y.shape[1], n_features))
# define model
model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(n_steps_in, n_features)))
model.add(RepeatVector(n_steps_out))
model.add(LSTM(100, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=100, verbose=0)
# demonstrate prediction
x_input = array([70, 80, 90])
x_input = x_input.reshape((1, n_steps_in, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
# 다변량 단단계 스택 lstm
from numpy import array
from numpy import hstack
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
# 다변량 데이터들을 나눈다
def split_sequences(sequences, n_steps_in, n_steps_out):#이것은 다단계 시계열 예측이니 n_steps_out이 추가된다.
X, y = list(), list()
for i in range(len(sequences)):
# find the end of this pattern
end_ix = i + n_steps_in
out_end_ix = end_ix + n_steps_out-1
# check if we are beyond the dataset
if out_end_ix > len(sequences):
break
# gather input and output parts of the pattern
seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1:out_end_ix, -1]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
#입력 시퀀스
in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90])
in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95])
out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))])
#시퀀스들을 리쉐잎한다.
in_seq1 = in_seq1.reshape((len(in_seq1), 1))
in_seq2 = in_seq2.reshape((len(in_seq2), 1))
out_seq = out_seq.reshape((len(out_seq), 1))
#수평
dataset = hstack((in_seq1, in_seq2, out_seq))
# 입력 스텝은 3, 출력은 2로 설정
n_steps_in, n_steps_out = 3, 2
# covert into input/output
X, y = split_sequences(dataset, n_steps_in, n_steps_out)
# the dataset knows the number of features, e.g. 2
n_features = X.shape[2]
# define model
model = Sequential()
model.add(LSTM(100, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features)))
model.add(LSTM(100, activation='relu'))
model.add(Dense(n_steps_out))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=200, verbose=0)
# demonstrate prediction
x_input = array([[70, 75], [80, 85], [90, 95]])
x_input = x_input.reshape((1, n_steps_in, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
'di' 카테고리의 다른 글
| MLM블로그 Crash Course in Recurrent Neural Networks for Deep Learning-2 (1) | 2023.03.05 |
|---|---|
| MLM블로그 Crash Course in Recurrent Neural Networks for Deep Learning-1 (0) | 2023.03.04 |
| 코드리뷰 MLM블로그 Demonstration of Memory with a Long Short-Term Memory Network in Python -2 (0) | 2023.03.03 |
| 코드리뷰 MLM블로그 Demonstration of Memory with a Long Short-Term Memory Network in Python -1 (0) | 2023.03.02 |
| 코드리뷰lstm (0) | 2023.02.24 |