Skip to content

Instantly share code, notes, and snippets.

@leechanwoo
Last active September 11, 2017 17:37
Show Gist options
  • Save leechanwoo/a2927b680f6f3d630c8fa21b095fd4fe to your computer and use it in GitHub Desktop.
Save leechanwoo/a2927b680f6f3d630c8fa21b095fd4fe to your computer and use it in GitHub Desktop.
logistic regression example code
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
samples = 1000
data = [[float(i)*0.01] for i in range(-samples, samples)]
label = [[1 if i[0] > 2.5 else 0] for i in data]
x = tf.placeholder(tf.float32)
y_ = tf.placeholder(tf.float32)
weight = tf.Variable(-0.9)
bias = tf.Variable(0.2)
y = x * weight + bias
loss = tf.losses.sigmoid_cross_entropy(y_, y)
train_op = tf.train.GradientDescentOptimizer(5e-1).minimize(loss)
pred = tf.nn.sigmoid(y)
accuracy = tf.metrics.accuracy(y_, tf.round(pred))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
for i in range(100):
_, _loss, _acc = sess.run([train_op, loss, accuracy], {x: data, y_: label})
print('step {}'.format(i))
print('loss: {}'.format(_loss))
print('accuracy: {}'.format(_acc))
_pred = sess.run(pred, {x: data})
plt.scatter(data, _pred, 1, 'b')
plt.scatter(data, label, 1, 'r')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment