-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNN_basic_Classification.py
54 lines (34 loc) · 1.2 KB
/
NN_basic_Classification.py
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
import tensorflow as tf
import numpy as np
x_data = np.array([[0,0],[1,0],[1,1],[0,0],[0,0],[0,1]])
y_data = np.array([
[1,0,0],
[0,1,0],
[0,0,1],
[1,0,0],
[1,0,0],
[0,0,1]])
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
W = tf.Variable(tf.random_uniform([2,3],-1.,1.))
b = tf.Variable(tf.zeros([3]))
L = tf.add(tf.matmul(X,W),b)
L = tf.nn.relu(L)
model = tf.nn.softmax(L)
cost = tf.reduce_mean(- tf.reduce_sum(Y * tf.log(model),axis=1))
opt = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_opt = opt.minimize(cost)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(100):
sess.run(train_opt, feed_dict={X: x_data, Y: y_data})
if (step + 1) % 10 == 0:
print(step + 1, sess.run(cost, feed_dict={X : x_data, Y :y_data}))
prediction = tf.argmax(model,1)
target = tf.argmax(Y,1)
print('예측값 : ', sess.run(prediction, feed_dict={X : x_data}))
print('실제값 : ', sess.run(target, feed_dict={Y : y_data}))
is_correct = tf.equal(prediction, target)
accurary = tf.reduce_mean(tf.cast(is_correct,tf.float32))
print('정확도 : %.2f' % sess.run(accurary*100, feed_dict={X : x_data,Y : y_data}))