logistic-regression


#!/usr/bin/env python
# coding=utf-8

import numpy as np

num_points=100
vectors_set=[]

for idx in range(num_points):
    x1=np.random.normal(0.0, 1)
    y1=1 if x1*0.3+0.1+np.random.normal(0.0,0.3)>0 else 0
    vectors_set.append([x1,y1])

x_data=[v[0] for v in vectors_set]
y_data=[v[1] for v in vectors_set]

import matplotlib.pyplot as plt

plt.plot(x_data,y_data, 'ro', label="Original data")
plt.legend()
plt.show()

#optimize linear regression with tensorflow
import tensorflow as tf

W=tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b=tf.Variable(tf.zeros([1]))

y=tf.sigmoid(W*x_data+b)
print('y.get_shape()', y.get_shape())

#print(y.get_shape()[0])
one=tf.ones(y.get_shape(), dtype=tf.float32)
print(one.get_shape())

loss=-tf.reduce_mean(y_data*tf.log(y)+(one-y_data)*tf.log(one-y))

optimizer=tf.train.GradientDescentOptimizer(0.5)
train=optimizer.minimize(loss)

init=tf.initialize_all_variables()
sess=tf.Session()
sess.run(init)

#print sess.run(one)

print 'params-before-training', sess.run(W), sess.run(b), '\n'

thresholdvec=tf.ones_like(one, dtype=tf.float32)*0.5
print sess.run(thresholdvec)

correct_prediction=tf.equal(tf.cast(y_data, tf.int32), tf.cast(tf.greater(y, thresholdvec),tf.int32))
accuracy=tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

for step in xrange(200):
    sess.run(train)
    if step %10 ==0:
        print('accuracy:', sess.run(accuracy))
        print'params', step, sess.run(W), sess.run(b), '\n'











發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章