欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

机器学习系列-tensorflow-03-线性回归Linear Regression

程序员文章站 2022-04-14 20:28:13
利用tensorflow实现数据的线性回归 导入相关库 import tensorflow as tf import numpy import matplotlib.pyplot as plt rng = numpy.random 参数设置 learning_rate = 0.01 training ......

利用tensorflow实现数据的线性回归

导入相关库

import tensorflow as tf
import numpy
import matplotlib.pyplot as plt
rng = numpy.random

参数设置

learning_rate = 0.01
training_epochs = 1000
display_step = 50

训练数据

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图输入

x = tf.placeholder("float")
y = tf.placeholder("float")

设置权重和偏置

w = tf.variable(rng.randn(), name="weight")
b = tf.variable(rng.randn(), name="bias")

构建线性模型

pred = tf.add(tf.multiply(x, w), b)

均方误差

cost = tf.reduce_sum(tf.pow(pred-y, 2))/(2*n_samples)

梯度下降

optimizer = tf.train.gradientdescentoptimizer(learning_rate).minimize(cost)

初始化变量

init = tf.global_variables_initializer()

开始训练

with tf.session() as sess:
    sess.run(init)
    # 适合所有训练数据
    for epoch in range(training_epochs):
        for (x, y) in zip(train_x, train_y):
            sess.run(optimizer, feed_dict={x: x, y: y})
        # 显示每个纪元步骤的日志
        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')
    # 画图显示
    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()
    plt.show()

结果展示

epoch: 0050 cost= 0.183995649 w= 0.43250677 b= -0.5143978
epoch: 0100 cost= 0.171630666 w= 0.42162812 b= -0.43613702
epoch: 0150 cost= 0.160693780 w= 0.41139638 b= -0.36253116
epoch: 0200 cost= 0.151019916 w= 0.40177315 b= -0.2933027
epoch: 0250 cost= 0.142463341 w= 0.39272234 b= -0.22819161
epoch: 0300 cost= 0.134895071 w= 0.3842099 b= -0.16695316
epoch: 0350 cost= 0.128200993 w= 0.37620357 b= -0.10935676
epoch: 0400 cost= 0.122280121 w= 0.36867347 b= -0.055185713
epoch: 0450 cost= 0.117043234 w= 0.36159125 b= -0.004236537
epoch: 0500 cost= 0.112411365 w= 0.3549302 b= 0.04368245
epoch: 0550 cost= 0.108314596 w= 0.34866524 b= 0.08875148
epoch: 0600 cost= 0.104691163 w= 0.34277305 b= 0.13114017
epoch: 0650 cost= 0.101486407 w= 0.33723122 b= 0.17100765
epoch: 0700 cost= 0.098651998 w= 0.33201888 b= 0.20850417
epoch: 0750 cost= 0.096145160 w= 0.32711673 b= 0.24377018
epoch: 0800 cost= 0.093927994 w= 0.32250607 b= 0.27693948
epoch: 0850 cost= 0.091967128 w= 0.31816947 b= 0.308136
epoch: 0900 cost= 0.090232961 w= 0.31409115 b= 0.33747625
epoch: 0950 cost= 0.088699281 w= 0.31025505 b= 0.36507198
epoch: 1000 cost= 0.087342896 w= 0.30664718 b= 0.39102668
optimization finished!
training cost= 0.087342896 w= 0.30664718 b= 0.39102668

机器学习系列-tensorflow-03-线性回归Linear Regression


参考:
author: aymeric damien
project: https://github.com/aymericdamien/tensorflow-examples/