您的位置:首页 > 其它

Tensorflow笔记(二)—— Linear Regression Example

2018-02-05 12:24 183 查看
import tensorflow as tf
import numpy
import matplotlib.pyplot as plt
rng = numpy.random


# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50


# Training Data
train_X = numpy.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = numpy.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]


# tf Graph Input
X = tf.placeholder("float32")
Y = tf.placeholder("float32")

# Set model weights
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")


# Construct a linear model
pred = tf.add(tf.multiply(X, W), b)


# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)


# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()


# Start training
with tf.Session() as sess:
sess.run(init)

# Fit all training data
for epoch in range(training_epochs):
for (x, y) in zip(train_X, train_Y):
sess.run(optimizer, feed_dict={X: x, Y: y})

#Display logs per epoch step
if (epoch+1) % display_step == 0:
c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
"W=", sess.run(W), "b=", sess.run(b))

print("Optimization Finished!")
training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

#Graphic display
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
plt.legend(loc='best')
plt.show()

#
#result
Epoch: 0050 cost= 0.137396052 W= 0.387081 b= -0.18761
Epoch: 0100 cost= 0.130413085 W= 0.378904 b= -0.128785
Epoch: 0150 cost= 0.124236695 W= 0.371213 b= -0.0734586
Epoch: 0200 cost= 0.118773796 W= 0.36398 b= -0.0214226
Epoch: 0250 cost= 0.113941967 W= 0.357177 b= 0.0275185
Epoch: 0300 cost= 0.109668352 W= 0.350779 b= 0.0735489
Epoch: 0350 cost= 0.105888516 W= 0.344761 b= 0.116842
Epoch: 0400 cost= 0.102545433 W= 0.339101 b= 0.15756
Epoch: 0450 cost= 0.099588633 W= 0.333777 b= 0.195856
Epoch: 0500 cost= 0.096973538 W= 0.32877 b= 0.231874
Epoch: 0550 cost= 0.094660647 W= 0.324061 b= 0.265751
Epoch: 0600 cost= 0.092615075 W= 0.319632 b= 0.297613
Epoch: 0650 cost= 0.090806007 W= 0.315467 b= 0.327579
Epoch: 0700 cost= 0.089206077 W= 0.311549 b= 0.355763
Epoch: 0750 cost= 0.087791078 W= 0.307864 b= 0.382272
Epoch: 0800 cost= 0.086539745 W= 0.304399 b= 0.407203
Epoch: 0850 cost= 0.085433111 W= 0.301139 b= 0.430651
Epoch: 0900 cost= 0.084454477 W= 0.298073 b= 0.452705
Epoch: 0950 cost= 0.083589002 W= 0.29519 b= 0.473448
Epoch: 1000 cost= 0.082823686 W= 0.292478 b= 0.492957
Optimization Finished!
Training cost= 0.0828237 W= 0.292478 b= 0.492957


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