<jupyter_start><jupyter_text>1. 데이터 정의하기  sine 데이터 정의<jupyter_code>x=np.linspace(0, 6 * np.pi, 200)
y=np.sin(x)

plt.rcParams['figure.figsize'] = [20, 5]

print(len(x), len(y))
plt.plot(x, y, color='r')
plt.title("SINE WAVE")
plt.show()<jupyter_output>200 200<jupyter_text>2. 데이터 준비하기  데이터셋 생성 함수<jupyter_code>def seq2dataset(seq, window_size):
    dataset = []
    
    for i in range(len(seq) - window_size):
        subset = seq[i:(i + window_size + 1)]
        dataset.append([item for item in subset])
    return np.array(dataset)<jupyter_output><empty_output><jupyter_text>생성<jupyter_code>n_steps = 5
n_inputs = 1

dataset = seq2dataset(seq=y, window_size=n_steps)

print("dataset.shape: {0}".format(dataset.shape))<jupyter_output>dataset.shape: (195, 6)<jupyter_text>입력(X)과 출력(Y) 변수로 분리하기<jupyter_code>x_train = dataset[:, 0: n_steps]
y_train = dataset[:, n_steps]

print("x_train: {0}".format(x_train.shape))
print("y_train: {0}".format(y_train.shape))<jupyter_output>x_train: (195, 5)
y_train: (195,)<jupyter_text>입력을 (샘플 수, 타입스텝, 특성 수)로 형태 변환<jupyter_code>x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], n_inputs))
print("After pre-processing")
print("x_train: {0}".format(x_train.shape))
print("y_train: {0}".format(y_train.shape))<jupyter_output>After pre-processing
x_train: (195, 5, 1)
y_train: (195,)<jupyter_text>3. 모델 구성하기<jupyter_code>model = Sequential()
model.add(LSTM(
    units=10,
    kernel_initializer='glorot_normal',
    bias_initializer='zero',
    batch_input_shape=(1, n_steps, n_inputs), 
    stateful=True
))

model.add(Dense(
    units=1, 
    kernel_initializer='glorot_normal',
    bias_initializer='zero',
    activation="linear"
))<jupyter_output><empty_output><jupyter_text>4. 모델 학습과정 설정하기<jupyter_code>model.compile(
    loss='mse',
    optimizer='adam'
)<jupyter_output><empty_output><jupyter_text>5. 모델 학습시키기  손실 이력 클래스 정의<jupyter_code>class LossHistory(keras.callbacks.Callback):
    def init(self):
        self.epoch = 0
        self.losses = []

    def on_epoch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))
        
        if self.epoch % 100 == 0:
            print("epoch: {0} - loss: {1:8.6f}".format(self.epoch, logs.get('loss')))
            
        self.epoch += 1<jupyter_output><empty_output><jupyter_text>학습<jupyter_code>num_epochs = 1500
history = LossHistory()  # 손실 이력 객체 생성

history.init()

for epoch_idx in range(num_epochs + 1):
    model.fit(
        x=x_train,
        y=y_train,
        epochs=1,
        batch_size=1,
        verbose=0,
        shuffle=False,
        callbacks=[history]
    )
    if history.losses[-1] < 1e-5:
        print("epoch: {0} - loss: {1:8.6f}".format(epoch_idx, history.losses[-1]))
        model.reset_states()    
        break
    model.reset_states()<jupyter_output>epoch: 0 - loss: 0.281627
epoch: 100 - loss: 0.000443
epoch: 200 - loss: 0.000414
epoch: 300 - loss: 0.000242
epoch: 378 - loss: 0.000008<jupyter_text>6. 학습과정 살펴보기<jupyter_code>import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(history.losses, color='g')
plt.yscale('log')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train'], loc='upper right')
plt.show()<jupyter_output><empty_output><jupyter_text>7. 모델 평가하기<jupyter_code>score = model.evaluate(x_train, y_train, batch_size=1)
print(score)<jupyter_output>195/195 [==============================] - 0s 2ms/sample - loss: 9.5681e-06
9.568052784877578e-06<jupyter_text>8. 모델 사용하기  한 스텝 예측<jupyter_code>x=np.linspace(0, 6 * np.pi, 200)
y=np.sin(x)
print(len(x), len(y))

plt.rcParams['figure.figsize'] = [20, 5]

plt.plot(x, y, color='r')
plt.title("SINE WAVE")
plt.show()
dataset = seq2dataset(seq=y, window_size=n_steps)

print("dataset.shape: {0}".format(dataset.shape))

x_train = dataset[:, 0: n_steps]
y_train = dataset[:, n_steps]

print("x_train: {0}".format(x_train.shape))
print("y_train: {0}".format(y_train.shape))

x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], n_inputs))

print("After pre-processing")
print("x_train: {0}".format(x_train.shape))
print("y_train: {0}".format(y_train.shape))
pred_count = 195
pred_out_list = []

pred_out = model.predict(x_train)

for i in range(pred_count):
    pred_out_list.append(pred_out[i][0])  # seq_out는 최종 악보이므로 인덱스 값을 코드로 변환하여 저장

model.reset_states()

print("one step prediction : ", pred_out_list)
pred_out_list_2 = list(y)[0:5] + list(pred_out_list)

print(len(x), len(y), len(pred_out_list_2))

plt.rcParams['figure.figsize'] = [20, 5]

plt.plot(x, y, marker="o", color='r')
plt.plot(x, pred_out_list_2, marker="o", color='b')
plt.title("SINE WAVE")
plt.show()<jupyter_output>200 200 200<jupyter_text>전체 예측 (시드만을 가지고 전체 곡선 생성)<jupyter_code>x=np.linspace(0, 6 * np.pi * (6 / 200), 6)
y=np.sin(x)

dataset = seq2dataset(seq=y, window_size=n_steps)

print("dataset.shape: {0}".format(dataset.shape))

x_train = dataset[:, 0: n_steps]
y_train = dataset[:, n_steps]

print("x_train: {0}".format(x_train.shape))
print("y_train: {0}".format(y_train.shape))

x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], n_inputs))

print("After pre-processing")
print("x_train: {0}".format(x_train.shape))
print("y_train: {0}".format(y_train.shape))
pred_count = 195
pred_out_list = []


for i in range(pred_count):
    pred_out = model.predict(x_train)
    pred_out_value = pred_out[0][0]
    
    pred_out_list.append(pred_out_value)
    
    x_train = list(np.reshape(x_train, (n_steps,)))
    x_train.append(pred_out_value)
    x_train.pop(0)
    x_train = np.array(x_train)
    x_train = np.reshape(x_train, (1, n_steps, n_inputs))

print(len(pred_out_list))
x=np.linspace(0, 6 * np.pi, 200)

pred_out_list_2 = list(y)[0:5] + list(pred_out_list)

print(len(x), len(pred_out_list_2))

plt.rcParams['figure.figsize'] = [20, 5]

plt.plot(x, pred_out_list_2, marker="o", color='b')
plt.title("SINE WAVE")
plt.show()<jupyter_output>200 200
{'fraction_non_alphanumeric': 0.17478364160868828, 'fraction_numerical': 0.04174444255896827, 'mean_word_length': 4.995930824008139, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '311203', 'license': 'MIT', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2745, 'n_words': 402, 'path': '1.DeepLearning/08.RNN/stock_price/sine_lstm.ipynb', 'repo_name': 'link-kut/deeplink_public', 'size': '5893'}