본문 바로가기

WINK-(Web & App)/인공지능 스터디

[인공지능 스터디] 김윤희 #2주차 모두를 위한 딥러닝 - 기본적인 머신러닝과 딥러닝 강좌 섹션 7 ~ 15 #완강!

반응형

섹션7. ML의 실용과 몇가지 팁

1. 학습 rate, Overfitting, 그리고 일반화 (Regularization)

  •  learning rate
    굉장히 큰 값으로 정한다면 → overshooting! cost가 줄어들지 않아요!
    굉장히 작은 값으로 정한다면 → cost가 너무 작게 변해!
    보통 0.01로 시작합니다!
  • preprocessing
    데이터 간 값의 차이가 너무 클 때 데이터 전처리를 해야 해!
    - Standardization  → 값의 분산과 평균을 가지고 !

standardization 식

  • overfitting
    train data에만 너무 딱 맞게 학습되는 경우
    oeverfitting을 해결하는 방법
    - train data를 많이 가지고 있기 
    - features의 개수를 줄이기
    - Regularization! → 일반화해요 → 구부리지말구 펴! → weight값을 너무 크게 하지말자
    → regularization strength를 적당히(?)

2. Training/Testing 데이타 셋

  • original data
    - train set : 우리들의 교과서
    - validation set : 모의시험
    - test set : 실전 문제
    라고 생각하면 편하답니다 ~ 
  • Online learning
    → 데이터를 쪼개서 학습시키는 방법
    나중에 데이터가 추가되었을 때 처음부터 다시 학습시키지 않고 추가적으로
    해당 데이터만 학습시키면 된다는 장점!
  • Accuracy
    최근 이미지를 예측하는 모델은 95% ~ 99% 정확도랍니다~
    y값 (우리들의 lable)과 모델이 예측한 값을 비교!

3. lab 07-1: training/test dataset, learning rate, normalization (new)

→ 우리가 가진 데이터를 train과 test로 나눈다!

  • learning rate
    너무 작으니 수백번 반복해도 cost값이 같아! local minimum에 빠졌을 것
    너무 크니 무한대로 빠졌어! 밖으로 확 튕겨나가는 순간일 것
  • normalized inputs
    ex) MinMaxScaler → 데이터의 값을 0과 1사이로 정규화하기

4. lab 07-2: Meet MNIST Dataset (new)

  • epoch : 전체 data set을 한 번 다 학습시킨 것을 1epoch!
  • batch size : 만약 100개씩 학습시킨다면 batch size는 100!

 

섹션8. 딥러닝의 기본 개념과, 문제, 그리고 해결

1. 딥러닝의 기본 개념: 시작과 XOR 문제

→ 뭐 .. 인간의 뇌 구조에서 영감을 얻은 처리 방식 ..?

XOR문제

→ Minsky 교수님 : xor은 안돼! → 여러개를 합치면 될 수도 있어!

→ Backpropagation 알고리즘 발표!

xor도 해결 가능!!!  but, 복잡할 수록 앞부분 까지 에러가 전달되지 않아 학습이 잘 되지 않아 ..

 

2. 딥러닝의 기본 개념2: Back-propagation 과 2006/2007 ‘딥’의 출현
Neural networks → Deep Learning

유튜브 자막, 페이스북, 구글 등등이 그 예!

 

3. Lab : Tensor Manipulation (new)

: 김밥을 보면서 1차원 array를 생각한답니다 .. 

rank : 몇차원이냐 ! shape : 원소가 몇개 있냐!

import tensorflow as tf
import numpy as np

t = tf.constant([[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],[[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]]])
tf.shape(t).eval()

위 코드 결과

axis 는 안쪽에 있는 것이 가장 큰 값, 바깥으로 갈수록 가장 작은 값

but, 가장 안쪽에 있는 축은 -1이라고도 표현.

  • broadcasting
    → shape이 달라도 연산이 가능하게 해주는 것
    matrix1= tf.constant([[3., 3.]])
    matrix2= tf.constant([[2., 2.]])
    (matrix1+matrix2).eval()
    
    matrix1= tf.constant([[3., 3.]])
    matrix2= tf.constant([2.])
    (matrix1+matrix2).eval()
    
    → 위 세개의 모든 연산의 결과값이 같음.
    이왕이면 같은 shape의 연산을 하는 것이 좋다.

  • matrix1= tf.constant([[3., 3.]]) matrix2= tf.constant([[2.],[2.]]) (matrix1+matrix2).eval()
  • reduce mean
x= [[1., 2.],
     [3., 4.]]  
tf.reduce_mean(x).eval()

#축이 0일때
tf.reduce_mean(x, axis=0).eval() #array([ 2.,  3.], dtype=float32)

#축이 1일때
tf.reduce_mean(x, axis=1).eval() #array([ 1.5,  3.5], dtype=float32)

#축이 -1일때 == 1
tf.reduce_mean(x, axis=-1).eval() #array([ 1.5,  3.5], dtype=float32)

보통

tf.reduce_mean(tf.reduce_sum(x, axis=-1)).eval()이렇게 많이 사용한다고 합니당.

  • Argmax
    → 가장 큰 값의 위치를 반환
     
  • x= [[0, 1, 2], [2, 1, 0]] tf.argmax(x, axis=0).eval() #array([1, 0, 0]) tf.argmax(x, axis=1).eval() #array([2, 0]) tf.argmax(x, axis=-1).eval() #array([2, 0])
  • Reshape → 가장 많이 사용한답니다!
    내가 원하는 형태의 shape을 준다면 reshape 됨! 보통 가장 안 쪽에 있는 값은 건드리지 않아요
t= np.array([[[0, 1, 2],
               [3, 4, 5]],

              [[6, 7, 8],
               [9, 10, 11]]])
t.shape 
#(2, 2, 3)

tf.reshape(t, shape=[-1, 3]).eval()
# array([[ 0,  1,  2],
#       [ 3,  4,  5],
#       [ 6,  7,  8],
#       [ 9, 10, 11]])

    - squeeze, expand 

tf.squeeze([[0], [1], [2]]).eval()
#array([0, 1, 2], dtype=int32)

tf.expand_dims([0, 1, 2], 1).eval()
#array([[0],
#       [1],
#       [2]], dtype=int32) -> 텐서의 차원을 추가!
  • One hot
    → one hot은 자동적으로 rank하나를 expand함. 그게 싫다면!
tf.one_hot([[0], [1], [2], [0]], depth=3).eval()
#array([[[ 1.,  0.,  0.]],
#       [[ 0.,  1.,  0.]],
#       [[ 0.,  0.,  1.]],
#       [[ 1.,  0.,  0.]]], dtype=float32) 특정 인덱스만 hot하게!

    → 다시 reshape해주세요!

t= tf.one_hot([[0], [1], [2], [0]], depth=3)
tf.reshape(t, shape=[-1, 3]).eval()
#array([[ 1.,  0.,  0.],
#       [ 0.,  1.,  0.],
#       [ 0.,  0.,  1.],
#       [ 1.,  0.,  0.]], dtype=float32)
  • Casting
tf.cast([1.8, 2.2, 3.3, 4.9], tf.int32).eval() #int로 바꾸기 
tf.cast([True, False, 1 == 1, 0 == 1], tf.int32).eval() #t/f를 0/1로 바꾸기

→ accuracy 계산 시 자주 사용

  • Stack
x= [1, 4]
y= [2, 5]
z= [3, 6]

# Pack along first dim.
tf.stack([x, y, z]).eval()

#array([[1, 4],
#       [2, 5],
#       [3, 6]], dtype=int32) -> 그대로 쌓아! 

tf.stack([x, y, z], axis=1).eval()
#array([[1, 2, 3],
#       [4, 5, 6]], dtype=int32) -> 설정한 축대로 쌓기
  • One and Zeros like
x= [[0, 1, 2],
     [2, 1, 0]]

tf.ones_like(x).eval()
#array([[1, 1, 1],
#       [1, 1, 1]], dtype=int32) -> ones니까 똑같은 모양인데 다 1로 채움!
#                                -> zero면 0으로!
  • Zip
for x, yin zip([1, 2, 3], [4, 5, 6]):
    print(x, y)

#1 4
#2 5
#3 6 -> 복수개의 tensor를 zip으로 묶어서 한 번에 처리!

 

섹션9. Neural Network 1: XOR 문제와 학습방법, Backpropagation

1. XOR 문제 딥러닝으로 풀기

k(x) = sigmoid(xw1 + b1)

y = H(x) = sigmoid(k(x)w2 + b2) 이 식들을 

k = tf.sigmoid(tf.matmul(x, w1) + b1)
hypothesis = tf.sigmoid(tf.matmul(k + w2) + b2)

이렇게 나타낼 수 있음. 

2. 특별편: 10분안에 미분 정리하기

  • basic derivative

  • partial derivative
    미분하고자 하는 변수 외에는 상수취급하기
  • chain rule

    chain rul은 쉽게 
    - x가 변화했을 때, g가 얼마나 변하는지, 
    -함수 g의 변화로 인해 함수 f가 얼마나 변하는 지, 

    - 함수 f의 인자가 함수 g면 최종 값의 변화량에 기여하는 각 함수 f, g의 기여도를 알 수 있다. 

 

3. 딥넷트웍 학습 시키기 (backpropagation)

  • back propagation
    → target과 실제 모델이 예측한 값이 얼마나 차이가 나는지를 구한 후 그 오차값을 뒤로 전파(?)해가면서 각 노드의 변수들을 업데이트 해주는 것 !
    한국말로 역전파 라고 합니다.
    여기서 각 노드의 변수 즉, weigth와 bias를 업데이트 할 때 chain rule을 사용하여 업데이트 해 준다는 것.

back propagation

4. Lab 9-1: XOR을 위한 텐스플로우 딥넷트웍 (new)

# Lab 9 XOR
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import numpy as npp

tf.set_random_seed(777)  # for reproducibility

x_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
y_data = np.array([[0], [1], [1], [0]], dtype=np.float32)

X = tf.placeholder(tf.float32, [None, 2])
Y = tf.placeholder(tf.float32, [None, 1])

W = tf.Variable(tf.random_normal([2, 1]), name="weight") #in, out
b = tf.Variable(tf.random_normal([1]), name="bias") #bias는 항상out의 수!

# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)

# cost/loss function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))
train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)

# Accuracy computation
# True if hypothesis>0.5 else False
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) #실제 정답과 비

# Launch graph
with tf.Session() as sess:
    # Initialize TensorFlow variables
    sess.run(tf.global_variables_initializer())

    for step in range(10001):
        _, cost_val, w_val = sess.run(
                  [train, cost, W], feed_dict={X: x_data, Y: y_data}
        )
        if step % 100 == 0:
            print(step, cost_val, w_val)

    # Accuracy report
    h, c, a = sess.run(
              [hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data}
    )
    print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)

→ 실망스러운 결과 …. ⇒

  • Neural Net사용하기
layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)
hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)
  • wide NN 
w1 = tf.Variable(tf.random_normal([2, 10]), name="weight1")
b1 = tf.Variable(tf.random_normal([10]), name="bias1")
layer1 = tf.sigmoid(tf.matmul(X, w1) + b1)

w2 = tf.Variable(tf.random_normal([10, 1]), name="weight2")
b2 = tf.Variable(tf.random_normal([1]), name="bias2")
hypothesis = tf.sigmoid(tf.matmul(X, w2) + b2)

→ 모델이 더 잘 학습됨니다!

  • Deep NN
    layer를 더 많이 연결하면 됨.
    out값을 다음 layer의 input으로 잘 연결하기!

5. Lab 9-2: Tensor Board로 딥네트웍 들여다보기 (new)

  • TensorBoard : 학습의 진행사항을 한 눈에 볼 수 있음
    • 사용법
    1. 어떤 값을 logging할 것인지!
    2. merge all summaries!
    3. summary를 어디 위치에 저장할 것인지 파일 위치 저장
    4. summary를 실행시켜준 후 파일에 기록하기
    5. $ tensorboard - - logdir=./logs 명령어 입력하기
    • name_scope
      layer별로 깔끔하게 정리해줌!

 

섹션10. Neural Network 2: ReLU and 초기값 정하기 (2006/2007 breakthrough)

1. XSigmoid 보다 ReLU가 더 좋아

  • vanishing gradient
    최종 단과 가까운 곳은 괜찮지만 단 수가 깊어질수록 경사와 기울기의 의미가 없어진다. → ReLU가 해결!
  • ReLU
    → L1 = tf.nn.relu(tf.matmul(X, W1) + b1)
    하지만 마지막 단의 출력은 sigmoid사용! 마지막 출력은 0~1사이어야 하기 때문!

2. Weight 초기화 잘해보자

  • 절대로 모든 초기값에 0을 주면 안돼!
  • RBM
    : forward(incode), backward(decode)의 값을 같게 해주기 위해 weight를 조정하는 것 (?)

3. Dropout 과 앙상블

  • dropout
    서로 연결된 layer에서 랜덤하게 0-1 사이의 확률로 뉴런을 제거하는 것
    train할 때 반드시 dropout을 사용
    overfitting을 방지하기 위해 사용
    dropout_rate = tf.placeholder("float")
    _L1 = tf.nn.relu(tf.add(tf.matmul(X, W1), B1))
    L1 = tf.nn.dropout(
    _L1, dropout_rate)
    
  • Ensemble
    → 하나의 모델만을 학습시켜 사용하지 않고 여러 모델을 학습시켜 결합하는 방식으로 문제를 처리함!
    성능이 효과적이다!

ensemble

4. 레고처럼 넷트웍 모듈을 마음껏 쌓아 보자

  • split & merge
  • recurrent network (RNN) : 옆으로 뻗어 ! …

 

5. Lab 10: 딥러닝으로 MNIST 98%이상 해보기(new)

  • NN for MNIST
# input place holders
X = tf.placeholder(tf.float32, [None, 784])
Y = tf.placeholder(tf.float32, [None, 10])

# weights & bias for nn layers
W1 = tf.Variable(tf.random_normal([784, 256]))
b1 = tf.Variable(tf.random_normal([256]))
L1 = tf.nn.relu(tf.matmul(X, W1) + b1)

W2 = tf.Variable(tf.random_normal([256, 256]))
b2 = tf.Variable(tf.random_normal([256]))
L2 = tf.nn.relu(tf.matmul(L1, W2) + b2)

W3 = tf.Variable(tf.random_normal([256, 10]))
b3 = tf.Variable(tf.random_normal([10]))
hypothesis = tf.matmul(L2, W3) + b3

→ 정확도 90에서 94로 증가!

  • xavier for MNIST
W1 = tf.get_variable("W1", shape=[784, 256],
                     initializer=tf.contrib.layers.xavier_initializer())
#W2, W3도 동일하게 적용

    → 94에서 97.8로 증가

  • Deep NN for MNIST
    넓고 깊게!
    5단으로 늘리고, xavier사용 but, 오히려 낮아짐 ⇒ overfittig!!! → dropout사용해바

  • optimizers는 Adam 사용을 추천~

 

섹션11. Convolutional Neural Networks

1. ConvNet의 Conv 레이어 만들기

  • stride가 1이면 필터를 1칸씩 이동!
  • padding
    → 원래 입력과 출력의 이미지 사이즈를 같게하기 위해서! 정보를 잃어버리지 않으려고!

 

2. ConvNet Max pooling 과 Full Network

  • pooling layer(sampling)
    conv layer에서 하나의 layer만 뽑아내어 resize(sampling)하는 것!
    그렇게 모든 layer을 사이즈를 줄여(?) 다시 쌓는 것이 pooling!

  • max pooling
    → 한 필터에서 가장 큰 값으로 sampling하는 것

3. ConvNet의 활용 예

  • LeNet
  • AlexNet
  • GoodLeNet
  • ResNet

4. 실습1: TensorFlow CNN 의 기본

: cnn은 이미지 처리에 강해!

image : (1, 3, 3, 1) → 1개의 3x3이미지를 사용! 마지막 1은 색을 의미

filter : (2, 2, 1, 1) → 2x2 사이즈의 필터 사용! 1은 색을 의미, 마지막 1은 개수를 의미 !

stride : 1 → 필터를 1칸씩 이동

padding : same → 원래 이미지 사이즈와 같게 해줄게!

# print("imag:\n", image)print("image.shape", image.shape)

weight= tf.constant([[[[1.]],[[1.]]],
                      [[[1.]],[[1.]]]])
print("weight.shape", weight.shape)
conv2d= tf.nn.conv2d(image, weight, strides=[1, 1, 1, 1], padding='SAME')
conv2d_img= conv2d.eval()
print("conv2d_img.shape", conv2d_img.shape)
conv2d_img= np.swapaxes(conv2d_img, 0, 3)
for i, one_imgin enumerate(conv2d_img):
    print(one_img.reshape(3,3))
    plt.subplot(1,2,i+1), plt.imshow(one_img.reshape(3,3), cmap='gray')
  • max pooling
image = np.array([[[[4],[3]],
                    [[2],[1]]]], dtype=np.float32)
pool = tf.nn.max_pool(image, ksize=[1, 2, 2, 1],
                    strides=[1, 1, 1, 1], padding='VALID')
print(pool.shape)
print(pool.eval())
  • MNIST Convolution layer 실습
#5sess = tf.InteractiveSession()

img = img.reshape(-1,28,28,1) #-1은 너히 알아서 계산해! (reshape의 방법)
W1 = tf.Variable(tf.random_normal([3, 3, 1, 5], stddev=0.01)) #5개의 필터 사용 
conv2d = tf.nn.conv2d(img, W1, strides=[1, 2, 2, 1], padding='SAME') #SAME이지만 stride가 2이므로 14x14가 출력될 
print(conv2d)
sess.run(tf.global_variables_initializer())
conv2d_img = conv2d.eval()
conv2d_img = np.swapaxes(conv2d_img, 0, 3)
for i, one_img in enumerate(conv2d_img):
    plt.subplot(1,5,i+1), plt.imshow(one_img.reshape(14,14), cmap='gray')

→ 필터를 5개 사용했으므로 5개의 이미지가 출력,

→ 랜덤한 weight를 주었으므로 조금씩 다른 형태의 이미지가 출력

5. 실습2: TensorFlow로 구현하자 (MNIST 99%)

  • Deep CNN
    → 단을 더 쌓기! → 정확도를 높일 수 있음. (99%)
    - keep_prob을 test 할 때는 반드시 1로 ! 학습할 땐 0.5 or 0.7 
# L4 FC 4x4x128 inputs -> 625 outputs
W4 = tf.get_variable("W4", shape=[128 * 4 * 4, 625],
                     initializer=tf.contrib.layers.xavier_initializer())
b4 = tf.Variable(tf.random_normal([625]))
L4 = tf.nn.relu(tf.matmul(L3_flat, W4) + b4)
L4 = tf.nn.dropout(L4, keep_prob=keep_prob)

# L5 Final FC 625 inputs -> 10 outputs
W5 = tf.get_variable("W5", shape=[625, 10],
                     initializer=tf.contrib.layers.xavier_initializer())
b5 = tf.Variable(tf.random_normal([10]))
logits = tf.matmul(L4, W5) + b5

6. 실습3: Class, tf.layers, Ensemble (MNIST 99.5%)

  • class로 만들어라! 간단해!
  • tf.layers : 복잡한 숫자들을 layer를 사용하여 간단하게!
  • Ensemble : 여러개의 모델을 학습시켜서 조합해 …
models = []
num_models = 2
for m in range(num_models):
    models.append(Model(sess, "model" + str(m)))

→ 모델을 담은 리스트와 개수를 정의

# Test model and check accuracy
test_size = len(mnist.test.labels)
predictions = np.zeros([test_size, 10])
for m_idx, m in enumerate(models):
    print(m_idx, 'Accuracy:', m.get_accuracy(
        mnist.test.images, mnist.test.labels))
    p = m.predict(mnist.test.images)
    predictions += p

ensemble_correct_prediction = tf.equal(
    tf.argmax(predictions, 1), tf.argmax(mnist.test.labels, 1))
ensemble_accuracy = tf.reduce_mean(
    tf.cast(ensemble_correct_prediction, tf.float32))
print('Ensemble accuracy:', sess.run(ensemble_accuracy))

→ 모델을 하나씩 꺼내와서 test데이터를 주고 예측한 값들을 더해!

 

섹션12. Recurrent Neural Network

1. NN의 꽃 RNN 이야기

  • sequence data
    말 그대로 순서가 있는 데이터
  • laguage model 

  • RNN
    입력과 출력을 시퀀스 단위로 처리!
    output이 다음의 cell로 연결됨!
    순차 데이터나 시계열 데이터를 이용하는 인공 신경망의 유형.
  • RNN의 여러가지 형태
    • one to one : vanilla NN → 하나의 입력, 하나의 출력
    • one to many : image Captioning → sequence of words
    • many to one : Sentiment Classification → 문장을 넣고, 하나가 출력
    • many to many : Machine Translation, Video classification

2. Lab 12-1 RNN - Basic (new)

  • RNN in Tensorflow
cell = tf.contrib.rnn.BasicRNNcell(num_units=hidden_size) #출력의 size를 정해
outputs, _states = tf.nn.dynamic_rnn(cell, x_data, dtype=tf.float32)
  • One node: 4(input-dim) in 2(hidden_size)
# One cell RNN input_dim (4) -> output_dim (2)hidden_size= 2
    cell= tf.keras.layers.SimpleRNNCell(units=hidden_size)
    print(cell.output_size, cell.state_size)

    x_data= np.array([[h]], dtype=np.float32)
		# x_data = [[[1,0,0,0]]]pp.pprint(x_data)
    outputs, _states= tf.nn.dynamic_rnn(cell, x_data, dtype=tf.float32)

→ hidden size가 2 이므로 array([[[ 0.28406101, 0.53163123]]], dtype=float32) 가 출력

  • sequence_length
    말 그대로! hello 면 값은 5!
  • Batching input
    batch_size! 학습을 시킬 때, 한 줄씩 학습하면 너무 비효율적 !!
    → 여러개를 한 번에 학습시키자!
  • ⇒ shape(3, 5, 2)에서
    Hidden_size = 2, sequence_length = 5, batch_size = 3 !

 

3. Lab 12-2 RNN - Hi Hello Training (new)

→ hihello를 훈련시켜보기!

  • one-hot encoding

  • parameter
    input_dim = 5
    sequence_length = 6
    batch_size = 1
    hidden_size = 5

 

4.Lab 12-3 : Long Sequence RNN (new)
5.
Lab12-4: Stacked RNN + Softmax Layer (new)

12-3이 학습이 잘 되지 않음 ! → RNN은 wide하고 deep하게 해줘야해!

  • sol1 : stacked RNN
cell = rnn.MultiRNNCell([cell] * 2, state_is_tuple=True) # *2 하면 2개가 쌓여짐!!
  • sol2 : Softmax
    1. softmax에 맞게 reshape해주기!
    X_for_sotfmax = tf.reshape(outputs, [-1, hidden_size])
    
    → hidden_size에 맞게 나머지는 알아서(-1) 쌓아!
    1. 그대로 출력한 것을 다시 펼쳐!
    outputs = tf.reshape(outputs, [batch_size, seq_length, num_classes])
    
     

6. Lab12-5: Dynamic RNN (new)

  • different sequence length
    sequence_length = [5, 2, 3] 로 설정한 후 2인 경우, 2개의 값만 내고 나머지는 0으로 처리 !
    → 없는 데이터는 0으로 처리하여 loss가 잘 동작되도록 함!
    각 문자열이 다 다른 경우!

 

7. Lab12-6: RNN with Time Series Data (new)

시간이 지나면서 값이 지나는 데이터를 예측해보자!

  • 전체코드
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()import numpy as np
import matplotlib
import os

tf.set_random_seed(777)  # reproducibility

if "DISPLAY" not in os.environ:
    # remove Travis CI Error
    matplotlib.use('Agg')

import matplotlib.pyplot as plt


def MinMaxScaler(data):
    numerator = data - np.min(data, 0)
    denominator = np.max(data, 0) - np.min(data, 0)
    # noise term prevents the zero division
    return numerator / (denominator + 1e-7)


# train Parameters
seq_length = 7
data_dim = 5
hidden_dim = 10
output_dim = 1
learning_rate = 0.01
iterations = 500

# Open, High, Low, Volume, Close
xy = np.loadtxt('data-02-stock_daily.csv', delimiter=',')
xy = xy[::-1]  # reverse order (chronically ordered)

# train/test split
train_size = int(len(xy) * 0.7)
train_set = xy[0:train_size]
test_set = xy[train_size - seq_length:]  # Index from [train_size - seq_length] to utilize past sequence

# Scale each
train_set = MinMaxScaler(train_set)
test_set = MinMaxScaler(test_set)

# build datasets
def build_dataset(time_series, seq_length):
    dataX = []
    dataY = []
    for i in range(0, len(time_series) - seq_length):
        _x = time_series[i:i + seq_length, :]
        _y = time_series[i + seq_length, [-1]]  # Next close price
        print(_x, "->", _y)
        dataX.append(_x)
        dataY.append(_y)
    return np.array(dataX), np.array(dataY)

trainX, trainY = build_dataset(train_set, seq_length)
testX, testY = build_dataset(test_set, seq_length)

# input place holders
X = tf.placeholder(tf.float32, [None, seq_length, data_dim])
Y = tf.placeholder(tf.float32, [None, 1])

# build a LSTM network
cell = tf.contrib.rnn.BasicLSTMCell(
    num_units=hidden_dim, state_is_tuple=True, activation=tf.tanh)
outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
Y_pred = tf.contrib.layers.fully_connected(
    outputs[:, -1], output_dim, activation_fn=None)  # We use the last cell's output

# cost/loss
loss = tf.reduce_sum(tf.square(Y_pred - Y))  # sum of the squares
# optimizer
optimizer = tf.train.AdamOptimizer(learning_rate)
train = optimizer.minimize(loss)

# RMSE
targets = tf.placeholder(tf.float32, [None, 1])
predictions = tf.placeholder(tf.float32, [None, 1])
rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions)))

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)

    # Training step
    for i in range(iterations):
        _, step_loss = sess.run([train, loss], feed_dict={
                                X: trainX, Y: trainY})
        print("[step: {}] loss: {}".format(i, step_loss))

    # Test step
    test_predict = sess.run(Y_pred, feed_dict={X: testX})
    rmse_val = sess.run(rmse, feed_dict={
                    targets: testY, predictions: test_predict})
    print("RMSE: {}".format(rmse_val))

    # Plot predictions
    plt.plot(testY)
    plt.plot(test_predict)
    plt.xlabel("Time Period")
    plt.ylabel("Stock Price")
    plt.show()

섹션13, 14, 15는 정리보다는 영상 참고하여 실습하기 ! (돈없어서 못하더라구요ㅜ.ㅜ)

반응형