본문 바로가기

잡다한 IT/머신러닝 & 딥러닝

06-1. Softmax classfier

■ Softmax 구현 (Multinomial Logistic Regression)


hypothesis = tf.nn.softmax(tf.matmul(X,W)+b)

- hypothesis 에서 tf.matmul(X,W)+b 에 tf.nn.softmax 함수 적용


cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(hypothesis),axis=1))

- Cross Entropy 를 코스트 함수로 사용한다.


optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)

- learining_rate 를 0.1로 학습함.



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Lab06-1 Softmax Classifier
 
import tensorflow as tf
 
tf.set_random_seed(777)  # for reproducibility
 
 
x_data = [[1211],
 
          [2132],
 
          [3134],
 
          [4155],
 
          [1755],
 
          [1256],
 
          [1666],
 
          [1777]]
 
y_data = [[001],
 
          [001],
 
          [001],
 
          [010],
 
          [010],
 
          [010],
 
          [100],
 
          [100]]
 
 
= tf.placeholder("float", [None, 4])
 
= tf.placeholder("float", [None, 3])
 
nb_classes = 3
 
 
= tf.Variable(tf.random_normal([4, nb_classes]), name='weight')
 
= tf.Variable(tf.random_normal([nb_classes]), name='bias')
 
 
# tf.nn.softmax computes softmax activations
 
# softmax = exp(logits) / reduce_sum(exp(logits), dim)
 
hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)
 
 
# Cross entropy cost/loss
 
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
 
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
 
 
# Launch graph
 
with tf.Session() as sess:
 
    sess.run(tf.global_variables_initializer())
 
 
    for step in range(2001):
 
        sess.run(optimizer, 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}))
 
 
    print('--------------')
 
 
    # Testing & One-hot encoding
 
    a = sess.run(hypothesis, feed_dict={X: [[11179]]})
 
    print(a, sess.run(tf.argmax(a, 1)))
 
 
    print('--------------')
 
 
    b = sess.run(hypothesis, feed_dict={X: [[1343]]})
 
    print(b, sess.run(tf.argmax(b, 1)))
 
 
    print('--------------')
 
 
    c = sess.run(hypothesis, feed_dict={X: [[1101]]})
 
    print(c, sess.run(tf.argmax(c, 1)))
 
 
    print('--------------')
 
 
    all = sess.run(hypothesis, feed_dict={
 
                   X: [[11179], [1343], [1101]]})
 
    print(all, sess.run(tf.argmax(all, 1)))
 
cs


반응형