当前位置:网站首页>windows10 tensorflow(二)原理实战之回归分析,深度学习框架(梯度下降法求解回归参数)
windows10 tensorflow(二)原理实战之回归分析,深度学习框架(梯度下降法求解回归参数)
2020-11-06 01:22:00 【IT界的小小小学生】
windows10 tensorflow(二)原理实战之回归分析,深度学习框架(梯度下降法求解回归参数)
TF数据生成方式:参考TF数据生成12法
TF基本原理与概念理解: tensorflow(一)windows 10 64位安装tensorflow1.4与基本概念解读tf.global_variables_initializer
模型:
一个简单的线性回归y = W * x + b,采用numpy构建完整回归数据,并增加干扰噪声
import numpy as np
#建立一个一元线性回归方程y=0.1x1+0.3 ,同时一个正太分布偏差np.random.normal(0.0,0.03)用于见证TF的算法
num_points=1000
vectors_set=[]
for i in range(num_points):
x1=np.random.normal(loc=0.0,scale=0.66)
y1=x1*0.1+0.3+np.random.normal(0.0,0.03)
vectors_set.append([x1,y1])
x_data=[v[0] for v in vectors_set]
y_data=[v[1] for v in vectors_set]
Graphic display出数据分布结果
import matplotlib.pyplot as plt
#https://www.cnblogs.com/zqiguoshang/p/5744563.html
##line_styles=['ro-','b^-','gs-','ro--','b^--','gs--'] #set line style
plt.plot(x_data,y_data,'ro',marker='^',c='blue',label='original_data')
plt.legend()
plt.show()
通过TensorFlow代码找到最佳的参数W与b,使的输入数据x_data,生成输出数据y_data,本例中将会一条直线y_data=W*x_data+b。读者知道W会接近0.1,b接近0.3,但是TensorFlow并不知道,它需要自己来计算得到该值。因此采用梯度下降法来迭代求解数据
import tensorflow as tf
import math
#一、创建graph数据
#随便构建一个一元回归方程的参数W与b
W=tf.Variable(tf.random_uniform([1], minval=-1.0, maxval=1.0))
b=tf.Variable(tf.zeros([1]))
y=W*x_data+b
#定义下面的最小化方差
#1.定义最小化误差平方根
loss=tf.reduce_mean(tf.square(y-y_data))
#2.learning_rate=0.5
optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.5)
#3.最优化最小值
train=optimizer.minimize(loss)
#二、初始化变量
init=tf.global_variables_initializer()
#三、启动graph
sess=tf.Session()
sess.run(init)
for step in range(8):
sess.run(train)
print("step={},sess.run=(W)={},sess.run(b)={}".format(step,sess.run(W),sess.run(b)))
以下是迭代8次的结果。梯度就像一个指南针,指引我们朝着最小的方向前进。为了计算梯度,TensorFlow会对错误函数求导,在我们的例子中就是,算法需要对W和b计算部分导数,以在每次迭代中为前进指明方向。
下面是每次迭代的可视化效果图:
#Graphic display
# print(sub_1+'41')
#注意:各参数可以用逗号,分隔开。第一个参数代表子图的行数;第二个参数代表该行图像的列数; 第三个参数代表每行的第几个图像,从左致右,从上到下一次增加。
plt.subplot(4,2,step+1)
plt.plot(x_data,y_data,'ro')
plt.plot(x_data,sess.run(W)*x_data+
sess.run(b),label=step)
plt.legend()
plt.show()

版权声明
本文为[IT界的小小小学生]所创,转载请带上原文链接,感谢
https://vip01.blog.csdn.net/article/details/78981951
边栏推荐
- DTU连接经常遇到的问题有哪些
- Serilog原始碼解析——使用方法
- 【Flutter 實戰】pubspec.yaml 配置檔案詳解
- 【C/C++ 1】Clion配置与运行C语言
- 技術總監,送給剛畢業的程式設計師們一句話——做好小事,才能成就大事
- 分布式ID生成服务,真的有必要搞一个
- 幽默:黑客式编程其实类似机器学习!
- How to get started with new HTML5 (2)
- Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
- 中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律
猜你喜欢
python 保存list数据
Anomaly detection method based on SVM
用Python构建和可视化决策树
Flink的DataSource三部曲之二:内置connector
Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
mac 安装hanlp,以及win下安装与使用
你的财务报告该换个高级的套路了——财务分析驾驶舱
事半功倍:在没有机柜的情况下实现自动化
通过深层神经网络生成音乐
有关PDF417条码码制的结构介绍
随机推荐
Didi elasticsearch cluster cross version upgrade and platform reconfiguration
Every day we say we need to do performance optimization. What are we optimizing?
50 + open source projects are officially assembled, and millions of developers are voting
JetCache埋点的骚操作,不服不行啊
GBDT与xgb区别,以及梯度下降法和牛顿法的数学推导
mac 下常用快捷键,mac启动ftp
利用 AWS SageMaker BlazingText 对不均衡文本进行多分类
Serilog原始碼解析——使用方法
被老程式設計師壓榨怎麼辦?我不想辭職
drf JWT認證模組與自定製
Cos start source code and creator
【新閣教育】窮學上位機系列——搭建STEP7模擬環境
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
網路程式設計NIO:BIO和NIO
自然语言处理之命名实体识别-tanfordcorenlp-NER(一)
接口压力测试:Siege压测安装、使用和说明
Troubleshooting and summary of JVM Metaspace memory overflow
Polkadot series (2) -- detailed explanation of mixed consensus
keras model.compile损失函数与优化器
做外包真的很难,身为外包的我也无奈叹息。