Created
May 2, 2016 19:16
-
-
Save nfmcclure/46c323f0a55ae1628808f7a58b5d437f to your computer and use it in GitHub Desktop.
simple_binary_classifier
This file contains 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 numpy as np | |
import tensorflow as tf | |
# Create graph | |
sess = tf.Session() | |
# Create data | |
x_vals = np.concatenate((np.random.normal(-1, 1, 50), np.random.normal(1, 1, 50))) | |
y_vals = np.concatenate((np.repeat(0, 50), np.repeat(1, 50))) | |
x_data = tf.placeholder(shape=[1], dtype=tf.float32) | |
y_target = tf.placeholder(shape=[1], dtype=tf.int32) | |
# Create variable (one model parameter = A) | |
A = tf.Variable(tf.random_normal(shape=[1])) | |
# Add operation to graph | |
# Want to create a pair = [1,0] if <A or [0,1] if >=A | |
my_output = tf.concat(0,[tf.to_float(tf.less(x_data, A)), tf.to_float(tf.greater_equal(x_data, A))]) | |
my_output_expanded = tf.expand_dims(my_output, 0) | |
# Add classification loss (cross entropy) | |
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(my_output_expanded, y_target) | |
# Initialize variables | |
init = tf.initialize_all_variables() | |
sess.run(init) | |
# Create Optimizer | |
my_opt = tf.train.GradientDescentOptimizer(0.1) | |
train_step = my_opt.minimize(xentropy) | |
# Run loop | |
for i in range(901): | |
rand_index = np.random.choice(100) | |
rand_x = [x_vals[rand_index]] | |
rand_y = [y_vals[rand_index]] | |
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y}) | |
if i%100==0: | |
print('Step #' + str(i+1) + ' A = ' + str(sess.run(A))) | |
print('Loss = ' + str(sess.run(xentropy, feed_dict={x_data: rand_x, y_target: rand_y}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment