线性回归简述
在这里,我们仅仅讨论单变量的线型回归模型。不对回归算法进行过多的展开。重点放在Tensorflow
的学习上。
下图展示的分别是:单变量线性回归模型的公式;学习的参数;损失函数(采用的均方误差);目标函数的优化求解(SGD)。
Tensorflow 线性回归
1 | import tensorflow as tf |
参数设置
1 | # 参数设置 |
生成训练数据
1 | # 生成训练数据 |
构造线型回归模型
1 | # tf 图的输入 |
1 | # 构造一个线性模型 |
定义损失函数
1 | # 损失函数设置为均方误差 |
定义优化方法
1 | # 梯度下降 |
1 | # 初始化变量(i.e. assign their default value) |
训练
1 | # 开始训练 |
Epoch: 0050 cost= 0.160369754 W= 0.41108337 b= -0.36027926
Epoch: 0100 cost= 0.150733337 W= 0.40147883 b= -0.2911848
Epoch: 0150 cost= 0.142209828 W= 0.39244553 b= -0.22619964
Epoch: 0200 cost= 0.134670869 W= 0.38394934 b= -0.16507955
Epoch: 0250 cost= 0.128002644 W= 0.37595856 b= -0.10759445
Epoch: 0300 cost= 0.122104712 W= 0.36844307 b= -0.05352829
Epoch: 0350 cost= 0.116888084 W= 0.3613746 b= -0.0026777028
Epoch: 0400 cost= 0.112274118 W= 0.35472643 b= 0.04514854
Epoch: 0450 cost= 0.108193211 W= 0.34847358 b= 0.09013041
Epoch: 0500 cost= 0.104583815 W= 0.34259278 b= 0.13243689
Epoch: 0550 cost= 0.101391472 W= 0.33706158 b= 0.17222734
Epoch: 0600 cost= 0.098568030 W= 0.33185956 b= 0.20965117
Epoch: 0650 cost= 0.096070863 W= 0.32696673 b= 0.2448493
Epoch: 0700 cost= 0.093862340 W= 0.32236505 b= 0.2779539
Epoch: 0750 cost= 0.091909051 W= 0.3180369 b= 0.3090903
Epoch: 0800 cost= 0.090181611 W= 0.31396636 b= 0.33837357
Epoch: 0850 cost= 0.088653855 W= 0.31013775 b= 0.365916
Epoch: 0900 cost= 0.087302707 W= 0.3065368 b= 0.39182106
Epoch: 0950 cost= 0.086107843 W= 0.30315018 b= 0.41618422
Epoch: 1000 cost= 0.085051164 W= 0.29996493 b= 0.43909845
Optimization Finished!
Training cost= 0.085051164 W= 0.29996493 b= 0.43909845
Tensorflow 线性回归(Eager API)
1 | from __future__ import absolute_import, division, print_function |
设置Eager API
1 | # Set Eager API |
生成训练数据
1 | # Training Data |
设置参数
1 | # Parameters |
初始化参数
1 | # Weight and Bias |
构建线性回归模型
1 | # Linear regression (Wx + b) |
定义损失函数(均方误差)
1 | # Mean square error |
调用优化器(SGD)
1 | # SGD Optimizer |
训练
1 | # Initial cost, before optimizing |
Initial cost= 34.570991516 W= -0.89701766 b= 0.09529222
Epoch: 0001 cost= 10.462613106 W= -0.3445475 b= 0.17387688
Epoch: 0100 cost= 0.090614140 W= 0.31795835 b= 0.32859808
Epoch: 0200 cost= 0.087662779 W= 0.31037292 b= 0.38237545
Epoch: 0300 cost= 0.085347883 W= 0.30365503 b= 0.4300023
Epoch: 0400 cost= 0.083532237 W= 0.29770544 b= 0.472182
Epoch: 0500 cost= 0.082108125 W= 0.29243633 b= 0.5095375
Epoch: 0600 cost= 0.080991186 W= 0.28776988 b= 0.54262066
Epoch: 0700 cost= 0.080115080 W= 0.28363708 b= 0.5719204
Epoch: 0800 cost= 0.079427943 W= 0.27997696 b= 0.5978689
Epoch: 0900 cost= 0.078888975 W= 0.27673545 b= 0.6208498
Epoch: 1000 cost= 0.078466244 W= 0.27386472 b= 0.64120203