-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNN_basic_DNN.py
54 lines (36 loc) · 1.29 KB
/
NN_basic_DNN.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)
W1 = tf.Variable(tf.random_uniform([2,10],-1.,1.))
W2 = tf.Variable(tf.random_uniform([10,3],-1.,1.))
b1 = tf.Variable(tf.zeros([10]))
b2 = tf.Variable(tf.zeros([3]))
L1 = tf.add(tf.matmul(X,W1),b1)
L1 = tf.nn.relu(L1)
model = tf.add(tf.matmul(L1,W2),b2)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=Y,logits=model))
opt = tf.train.AdamOptimizer(learning_rate=0.01)
train_op = opt.minimize(cost)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(100):
sess.run(train_op, 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}))