*Softmax Classification의 multinominal 개념 :



x1 , x2 , y 값이 주어졌을 때

좌표에 [x1, x2] = y로 표시한다. 


[wa1, wa2, wa3] [x1, x2, x3] (세로)  = [w1x1 + w2x2 + w3x3]  => a인지 아닌지 여부  [2.0]   => 0.7 (2.0/전체값 = softmax)

[wb1, wb2, wb3] [x1, x2, x3] (세로)  = [w1x1 + w2x2 + w3x3] => b인지 아닌지 여부 [1.0] => 0.2 (1.0/전체값 = softmax)

[wc1, wc2, wc3] [x1, x2, x3] (세로)  = [w1x1 + w2x2 + w3x3] => c인지 아닌지 여부 [0.1] =0.1 (0.1/전체값 = softmax)


H(x) = softmax(WX + b)

=> softmax 값이 가장 큰 것을 취한다(위에서는 'A')


argmax라는 함수를 쓴다

argmax에 확률을 집어넣어준다 (위 예시에서는 argmax(0.7) =1 , argmax(0.2) = 0, argmax(0.1) = 0 )




*A, B, C학점 예측 소스코드 :


05train.txt

1
2
3
4
5
6
7
8
9
#x0 x1 x2 y[A   B   C]
1   2   1   0   0   1
1   3   2   0   0   1
1   3   4   0   0   1
1   5   5   0   1   0
1   7   5   0   1   0
1   2   5   0   1   0
1   6   6   1   0   0
1   7   7   1   0   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
import tensorflow as tf
import numpy as np
 
xy = np.loadtxt('./data/05train.txt', dtype='float32')
 
x_data = xy[:, 0:3]
y_data = xy[:, 3:]
print(x_data.shape, y_data.shape)
 
= tf.placeholder(tf.float32)
= tf.placeholder(tf.float32)
 
= tf.Variable(tf.zeros([3,3]))
hypothesis = tf.nn.softmax(tf.matmul(X,W))
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
 
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
 
    for step in range(5001):
        sess.run(train, feed_dict={X:x_data, Y:y_data})
        if step % 200 == 0:
            print(step, sess.run(cost, feed_dict={X:x_data, Y:y_data}), sess.run(W))
    a = sess.run(hypothesis, feed_dict={X:[[1,11,7]]})
    print(a, sess.run(tf.argmax(a, 1)))
#[[0.7576524  0.2345292  0.00781842]] [0]   0번째 있는 값이 제일 크다!! A일 확율이 76%
    c = sess.run(hypothesis, feed_dict={X:[[1,1,0]]})
    print(c, sess.run(tf.argmax(c, 1)))
#[[0.00227058 0.0237952  0.9739342 ]] [2]   3번째 있는 값이 제일 크다!! C일 확율이 97%
 
cs


*A, B, C학점 예측 소스코드(accuracy 포함) :

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
import tensorflow as tf
import numpy as np
 
xy = np.loadtxt('./data/05train.txt', dtype='float32')
 
x_data = xy[:, 0:3]
y_data = xy[:, 3:]
print(x_data.shape, y_data.shape)
 
= tf.placeholder(tf.float32)
= tf.placeholder(tf.float32)
 
= tf.Variable(tf.zeros([3,3]))
hypothesis = tf.nn.softmax(tf.matmul(X,W))
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
 
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
 
    for step in range(5001):
        sess.run(train, feed_dict={X:x_data, Y:y_data})
        if step % 200 == 0:
            print(step, sess.run(cost, feed_dict={X:x_data, Y:y_data}), sess.run(W))
    a = sess.run(hypothesis, feed_dict={X:[[1,11,7]]})
    print(a, sess.run(tf.argmax(a, 1)))
#[[0.7576524  0.2345292  0.00781842]] [0]   0번째 있는 값이 제일 크다!! A일 확율이 76%
    c = sess.run(hypothesis, feed_dict={X:[[1,1,0]]})
    print(c, sess.run(tf.argmax(c, 1)))
#[[0.00227058 0.0237952  0.9739342 ]] [2]   3번째 있는 값이 제일 크다!! C일 확율이 97%
 
 
    correct_prediction = tf.equal(tf.argmax(hypothesis, 1),
                                  tf.argmax(Y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction
                                      , tf.float32))
    print(sess.run(accuracy, feed_dict={X:x_data, Y: y_data})) #0.875 87%의 정확도
cs





+ Recent posts