TechTogetWorld

인공지능 구현에 대한 글입니다.


글의 순서는 아래와 같습니다.


================================================

1. # lab-09-1-xor 

    ==># 코딩은 이상이 없으나, 정확도는 50% 수준밖에는 않됩니다.

2. # lab-09-2-xor-nn

   ==>정확도 50%를 개선하는 방법으로 neural network ( 신경망 )을 이용하는 방법을 구현 ==> 정확도 100%

  ==>layer 1 이 다른곳에 입력으로 들어감 : 신경망 구성

    layer1은 출력 2개 로 구성한다 , 이유는 최종 가설의 입력(2개의 data)으로 들어가기 때문임

    정확도 1.0으로 개선이 됨

3. # lab-09-3-xor-nn-wide-deep

  ==> layer1의 출력을 10개  넓게 펴주고, 최종 가설에는 출력을 1개로 설정함 

       layer1,2,3로 더 깊게 만들고, 최종 출력을 1개로 설정함

  ==> 즉 더 넓게, 깊게 신경망을 구성해서  정확도 1.0으로 개선됨, 정밀도 높아짐 (큰것은 더 크게, 작은것은 더 작게)

4. 코딩탐구 필요(추가)

  ==> lab-09-4-xor_tensorboard

       lab-09-5-linear_back_prop

       lab-09-6-multi-linear_back_prop

       lab-09-7-sigmoid_back_prop

       lab-09-x-xor-nn-back_prop

5. 참고자료

=================================================


[rate overfitting , regularization tips]


1. COST 값이 줄지않고, 늘어난다면  Learning rate을 더 작게 변경해주어야함

   반대로 너무 작게 줄거나, 도중에 멈춘다면 learing rate을 좀더 크게 해주어야함

   보통 0.01을 기준으로 늘리거나 줄이면서 조정해 나가면 된다.


2.  x data 값이 차이가 큰 경우, cost가 잘 줄지않거나, 학습이 잘 일어나지 않음 , 이럴때는 NOMALIZATION 을 해주어야 함.

3. OVERFITTING

  - 더많은 TRAINING DATA

  - REDUCE FEATURES

  - REGULARIZATION ==> 구별선이 구부러지지 않토록 , X값을 일반화 시킴


[lab-09-1-xor]

# lab-09-1-xor

# 코딩은 이상이 없으나, 정확도는 50% 수준밖에는 않됩니다.



import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 9 XOR

import tensorflow as tf

import numpy as np


tf.set_random_seed(777)  # for reproducibility

learning_rate = 0.1


x_data = [[0, 0],

          [0, 1],

          [1, 0],

          [1, 1]]

y_data = [[0],

          [1],

          [1],

          [0]]

x_data = np.array(x_data, dtype=np.float32)

y_data = np.array(y_data, 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')

b = tf.Variable(tf.random_normal([1]), name='bias')


# 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=learning_rate).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):

        sess.run(train, feed_dict={X: x_data, Y: y_data})

        if step % 100 == 0:

            print(step, sess.run(cost, feed_dict={

                  X: x_data, Y: y_data}), sess.run(W))


    # 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)


'''

Hypothesis:  [[ 0.5]

 [ 0.5]

 [ 0.5]

 [ 0.5]]

Correct:  [[ 0.]

 [ 0.]

 [ 0.]

 [ 0.]]

Accuracy:  0.5


'''

[ lab-09-2-xor-nn ]

# lab-09-2-xor-nn

""" 정확도 50%를 개선하는 방법으로 neural network ( 신경망 )을 이용하는 방법을 구현하함"""

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 9 XOR

import tensorflow as tf

import numpy as np


tf.set_random_seed(777)  # for reproducibility

learning_rate = 0.1


x_data = [[0, 0],

          [0, 1],

          [1, 0],

          [1, 1]]

y_data = [[0],

          [1],

          [1],

          [0]]

x_data = np.array(x_data, dtype=np.float32)

y_data = np.array(y_data, dtype=np.float32)


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

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


W1 = tf.Variable(tf.random_normal([2, 2]), name='weight1')

b1 = tf.Variable(tf.random_normal([2]), name='bias1')

layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)


""" layer 1 이 다른곳에 입력으로 연결함 : 신경망 구성

    layer1은 입력1개, 출력 2로 구성한다 , 이유는 최종 가설의 입력으로 들어가기 때문임

    정확도 1.0으로 개선이 됨

"""

W2 = tf.Variable(tf.random_normal([2, 1]), name='weight2')

b2 = tf.Variable(tf.random_normal([1]), name='bias2')

hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)


# cost/loss function

cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *

                       tf.log(1 - hypothesis))


train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).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):

        sess.run(train, feed_dict={X: x_data, Y: y_data})

        if step % 100 == 0:

            print(step, sess.run(cost, feed_dict={

                  X: x_data, Y: y_data}), sess.run([W1, W2]))


    # 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)



'''

Hypothesis:  [[ 0.01338218]

 [ 0.98166394]

 [ 0.98809403]

 [ 0.01135799]]

Correct:  [[ 0.]

 [ 1.]

 [ 1.]

 [ 0.]]

Accuracy:  1.0

'''


[ lab-09-3-xor-nn-wide-deep ]


# lab-09-3-xor-nn-wide-deep


import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 9 XOR

import tensorflow as tf

import numpy as np


tf.set_random_seed(777)  # for reproducibility

learning_rate = 0.1


x_data = [[0, 0],

          [0, 1],

          [1, 0],

          [1, 1]]

y_data = [[0],

          [1],

          [1],

          [0]]

x_data = np.array(x_data, dtype=np.float32)

y_data = np.array(y_data, dtype=np.float32)


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

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


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)

""" layer1의 출력을 10개  넓게 펴주고, 최종 가설에는 출력을 1개로 설정함 

    layer1,2,3로 더 깊게 만들고, 최종 출력을 1개로 설정함

    ==> 즉 더 넓게, 깊게 신경망을 구성해서  정확도 1.0으로 개선됨 """


W2 = tf.Variable(tf.random_normal([10, 10]), name='weight2')

b2 = tf.Variable(tf.random_normal([10]), name='bias2')

layer2 = tf.sigmoid(tf.matmul(layer1, W2) + b2)


W3 = tf.Variable(tf.random_normal([10, 10]), name='weight3')

b3 = tf.Variable(tf.random_normal([10]), name='bias3')

layer3 = tf.sigmoid(tf.matmul(layer2, W3) + b3)


W4 = tf.Variable(tf.random_normal([10, 1]), name='weight4')

b4 = tf.Variable(tf.random_normal([1]), name='bias4')

hypothesis = tf.sigmoid(tf.matmul(layer3, W4) + b4)


# cost/loss function

cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *

                       tf.log(1 - hypothesis))


train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).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):

        sess.run(train, feed_dict={X: x_data, Y: y_data})

        if step % 100 == 0:

            print(step, sess.run(cost, feed_dict={

                  X: x_data, Y: y_data}), sess.run([W1, W2]))


    # 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)



'''

Hypothesis:  [[  7.80511764e-04]

 [  9.99238133e-01]

 [  9.98379230e-01]

 [  1.55659032e-03]]

Correct:  [[ 0.]

 [ 1.]

 [ 1.]

 [ 0.]]

Accuracy:  1.0

'''


[참고자료 ]

https://www.inflearn.com/course/기본적인-머신러닝-딥러닝-강좌

https://github.com/hunkim/deeplearningzerotoall