您的位置:首页 > 其它

Tensorflow: Linear Regression

2016-06-28 22:57 309 查看

modified from https://github.com/sjchoi86/tensorflow-101/blob/master/notebooks/logistic_regression_mnist.ipynb

toy dataset

import numpy as np
import os
from scipy.misc import imread, imresize
import matplotlib.pyplot as plt
import pprint
from sklearn.datasets import load_boston

def toy_dataset(n):
w, b = 0.7, -1
noise_var = 0.001
x   = np.random.random((1, n))
gt = w * x + b
label  = gt + np.sqrt(noise_var)*np.random.randn(1, n)
return x, gt, label

n_samples = 100
data, gt, label = toy_dataset(n_samples)
print (" Type of 'train_X' is ", type(data))
print (" Shape of 'train_X' is %s" % (data.shape,))
print (" Type of 'train_Y' is ", type(label))
print (" Shape of 'train_Y' is %s" % (label.shape,))

plt.figure(1)
plt.plot(x[0, :], gt[0, :], 'ro', label='Original data')
plt.plot(x[0, :], y[0, :], 'bo',  label='Training data')
plt.axis('equal')
plt.legend(loc='lower right')
plt.show()




linear regression

X = tf.placeholder(tf.float32, name='input')
Y = tf.placeholder(tf.float32, name='output')
w = tf.Variable(np.random.randn(), name='weight')
b = tf.Variable(np.random.randn(), name='bias')

act = tf.add(tf.mul(X,w), b)

lr = 0.001
loss = tf.reduce_mean(tf.pow(act-Y,2))
# optimizer = tf.train.GradientDescentOptimizer(lr).minimize(loss)
optimizer = tf.train.RMSPropOptimizer(lr, 0.9).minimize(loss)
init = tf.initialize_all_variables()

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

training_epochs = 5000
snapshot = 50
loss_cache = []
for epoch in xrange(training_epochs):
for x, y in zip(data, label):
out = sess.run([optimizer, loss, w, b], feed_dict={X:x, Y:y})
loss_cache.append(out[1])
if epoch % snapshot == 0:
print '[Epoch: %d] loss: %.4f, w: %.4f, b: %.4f' % (epoch, out[1], out[2], out[3])

w_new = sess.run(w)
b_new = sess.run(b)
y_pre = x * w_new + b_new

print y_pre.shape

plt.figure(2)
plt.plot(data[0,:], gt[0,:], 'ro', label='Ground Truth')
plt.plot(data[0,:], label[0,:], 'bo', label='Training Label')
plt.plot(data[0,:], y_pre, 'k-', label='Fitted Line')
plt.axis('equal')
plt.legend(loc='lower right')
plt.show()




plt.figure(3)
plt.plot(range(training_epochs), loss_cache, 'b-', label='loss')
plt.legend(loc='upper right')
plt.show()


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: