*Activation Function : 자극에 반응을 할지 안 할지 결정(뉴런이 하는 역할과 동일)


X-> W-> S -> Y(예측값)

여러 뉴런들이 각 특징들을 잘 뽑아내서 이해를 잘 시킨다.


*CNN 알고리즘(Convolutional Neural Networks)

1980(LeCun)

=>Big Problem : 

사람의 두뇌를 구성하려면, 15층 16층 정도로 깊게 쌓아야 하는데

Neural Network로는 레이어 구성으로 동작이 잘 안된다는 것을 깨달음

=> Breakthrough :

Neural networks with many layers really could be trained well, if the weights are initialized in a clever way rather than randomly




*Geoffrey Hinton's Summary of findings up to today

- Our labeled datasets were thousands of times too small

- 컴퓨터가 너무 느렸다.

- 초기값을 잘못 줬다.

- We used the wrong type of non-linearity


2단의 NN을 쌓으려면 

1단의 출력값을 2단의 입력값으로 쓰는 방식으로 연결해야 한다.


1) K(x) = sigmoid(WX1 + B1)

2) Y + H(x) = sigmoid(K(x) W2 + B2)




*XOR With logistic regression :


07train.txt

1
2
3
4
5
6
# xor
# x1 x2 y
0   0   0
0   1   1
1   0   1
1   1   0
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import tensorflow as tf
import numpy as np
tf.set_random_seed(777)  # for reproducibility
 
xy = np.loadtxt('./data/07train.txt')
 
x_data =  xy[:,0:-1]
y_data = xy[:,[-1]]
 
= tf.placeholder(tf.float32, [None, 2])
= tf.placeholder(tf.float32, [None, 1])
 
= tf.Variable(tf.random_uniform([21], -1.1.))
= tf.Variable(tf.random_uniform([1], -1.1.))
 
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
 
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
                       tf.log(1 - hypothesis))
 
train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
 
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
 
with tf.Session() as sess:
    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))
 
    h, c, a = sess.run([hypothesis, predicted, accuracy],
                       feed_dict={X: x_data, Y: y_data})
    print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)
 
 
cs





*Neuron이 2개일 때 소스코드 : 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import tensorflow as tf
import numpy as np
tf.set_random_seed(777)  # for reproducibility
 
xy = np.loadtxt('./data/07train.txt')
 
x_data =  xy[:,0:-1]
y_data = xy[:,[-1]]
 
= tf.placeholder(tf.float32, [None, 2])
= tf.placeholder(tf.float32, [None, 1])
 
# 2단으로 쌓는다 W 2개 b 2개  - W값은 [2, 2] 와 [2, 1]으로 되어 있는데 대각선으로 값이 맞아야 한다(2 = 2)
# b1, b2의 값은 W의 마지막 열과 일치해야 한다
W1 = tf.Variable(tf.random_uniform([22], -1.1.))
W2 = tf.Variable(tf.random_uniform([21], -1.1.))
b1 = tf.Variable(tf.random_uniform([2], -1.1.))
b2 = tf.Variable(tf.random_uniform([1], -1.1.))
 
L1 = tf.sigmoid(tf.matmul(X,W1) + b1)
hypothesis = tf.sigmoid(tf.matmul(L1, W2) + b2)
 
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
                       tf.log(1 - hypothesis))
 
train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
 
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
 
with tf.Session() as sess:
    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}))
 
    h, c, a = sess.run([hypothesis, predicted, accuracy],
                       feed_dict={X: x_data, Y: y_data})
    print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)
 
 
cs



*계층은 2단에 뉴런 10개를 사용한다고 했을 때 소스 코드 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import tensorflow as tf
import numpy as np
tf.set_random_seed(777)  # for reproducibility
 
xy = np.loadtxt('./data/07train.txt')
 
x_data =  xy[:,0:-1]
y_data = xy[:,[-1]]
 
= tf.placeholder(tf.float32, [None, 2])
= tf.placeholder(tf.float32, [None, 1])
 
# 2단으로 쌓는다 W 2개 b 2개  - W값은 [2, 2] 와 [2, 1]으로 되어 있는데 대각선으로 값이 맞아야 한다(2 = 2)
# b1, b2의 값은 W의 마지막 열과 일치해야 한다
# 계층은 2단에 뉴런의 개수를 10개로 지정했을 시 : 
W1 = tf.Variable(tf.random_uniform([210], -1.1.))
W2 = tf.Variable(tf.random_uniform([101], -1.1.))
b1 = tf.Variable(tf.random_uniform([10], -1.1.))
b2 = tf.Variable(tf.random_uniform([1], -1.1.))
 
L1 = tf.sigmoid(tf.matmul(X,W1) + b1)
hypothesis = tf.sigmoid(tf.matmul(L1, W2) + b2)
 
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
                       tf.log(1 - hypothesis))
 
train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
 
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
 
with tf.Session() as sess:
    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}))
 
    h, c, a = sess.run([hypothesis, predicted, accuracy],
                       feed_dict={X: x_data, Y: y_data})
    print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)
 
 
cs


*계층은 3단에 뉴런 5개를 사용한다고 했을 때 소스 코드 :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import tensorflow as tf
import numpy as np
tf.set_random_seed(777)  # for reproducibility
 
xy = np.loadtxt('./data/07train.txt')
 
x_data =  xy[:,0:-1]
y_data = xy[:,[-1]]
 
= tf.placeholder(tf.float32, [None, 2])
= tf.placeholder(tf.float32, [None, 1])
 
# 계층은 3단에 뉴런의 개수를 5개로 지정했을 시 :
W1 = tf.Variable(tf.random_uniform([25], -1.1.))
W2 = tf.Variable(tf.random_uniform([54], -1.1.))
W3 = tf.Variable(tf.random_uniform([41], -1.1.))
b1 = tf.Variable(tf.random_uniform([5], -1.1.))
b2 = tf.Variable(tf.random_uniform([4], -1.1.))
b3 = tf.Variable(tf.random_uniform([1], -1.1.))
 
# 3단으로 쌓을 시 :
L1 = tf.sigmoid(tf.matmul(X,W1) + b1)
L2 = tf.sigmoid(tf.matmul(L1,W2) + b2)
hypothesis = tf.sigmoid(tf.matmul(L2, W3) + b3)
 
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
                       tf.log(1 - hypothesis))
 
train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
 
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
 
with tf.Session() as sess:
    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}))
 
    h, c, a = sess.run([hypothesis, predicted, accuracy],
                       feed_dict={X: x_data, Y: y_data})
    print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)
 
 
cs




+ Recent posts