Last active
August 3, 2016 03:49
-
-
Save sokcuri/64a038d67322da1bbaadc41fbdbebf1a to your computer and use it in GitHub Desktop.
lab9-1: XOR을 위한 텐스플로우 딥넷트웍 https://www.youtube.com/watch?v=9i7FBbcZPMA&feature=youtu.be
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import numpy as np | |
xy = np.loadtxt('train.txt', unpack=True) | |
x_data = xy[0:-1] | |
y_data = xy[-1] | |
X = tf.placeholder(tf.float32) | |
Y = tf.placeholder(tf.float32) | |
W = tf.Variable(tf.random_uniform([1, len(x_data)], -1.0, 1.0)) | |
h = tf.matmul(W, X) | |
hypothesis = tf.div(1., 1.+tf.exp(-h)) | |
cost = -tf.reduce_mean(Y*tf.log(hypothesis) + (1-Y)*tf.log(1-hypothesis)) | |
a = tf.Variable(0.01) | |
optimizer = tf.train.GradientDescentOptimizer(a) | |
train = optimizer.minimize(cost) | |
init = tf.initialize_all_variables() | |
with tf.Session() as sess: | |
sess.run(init) | |
for step in xrange(1000): | |
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) | |
correct_prediction = tf.equal(tf.floor(hypothesis+0.5), Y) | |
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) | |
print sess.run([hypothesis, tf.floor(hypothesis+0.5), correct_prediction, accuracy], feed_dict={X:x_data, Y:y_data}) | |
print "Accuracy:", accuracy.eval({X:x_data, Y:y_data}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment