当前位置:网站首页>Windows 10 tensorflow (2) regression analysis of principles, deep learning framework (gradient descent method to solve regression parameters)
Windows 10 tensorflow (2) regression analysis of principles, deep learning framework (gradient descent method to solve regression parameters)
2020-11-06 01:22:00 【Elementary school students in IT field】
windows10 tensorflow( Two ) Regression analysis of principle and actual combat , Deep learning framework ( The gradient descent method is used to solve the regression parameters )
TF Data generation : Reference resources TF The data generated 12 Law
TF Basic principles and conceptual understanding : tensorflow( One )windows 10 64 Bit installation tensorflow1.4 And basic concept interpretation tf.global_variables_initializer
Model :
A simple linear regression y = W * x + b, use numpy Building complete regression data , And increase interference noise
import numpy as np
# Establish a linear regression equation of one variable y=0.1x1+0.3 , At the same time, a positive distribution deviation np.random.normal(0.0,0.03) For witnessing TF The algorithm of
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 Data distribution results
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()

adopt TensorFlow The code finds the best parameters W And b, Make the input data of x_data, Generate output data y_data, In this case, there will be a straight line y_data=W*x_data+b. The reader knows W It will be close 0.1,b near 0.3, however TensorFlow Don't know , It needs to calculate the value itself . Therefore, the gradient descent method is used to solve the data iteratively
import tensorflow as tf
import math
# One 、 establish graph data
# Arbitrarily construct the parameters of a univariate regression equation W And 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
# Define the following minimum variance
#1. Define the minimum square root of error
loss=tf.reduce_mean(tf.square(y-y_data))
#2.learning_rate=0.5
optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.5)
#3. Optimize the minimum
train=optimizer.minimize(loss)
# Two 、 Initialize variable
init=tf.global_variables_initializer()
# 3、 ... and 、 start-up 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)))
Here's the iteration 8 Results of . Gradient is like a compass , Guiding us in the smallest direction . To calculate the gradient ,TensorFlow It will take the derivative of the wrong function , In our case , The algorithm needs to work on W and b Calculating partial derivatives , To indicate the direction of advance in each iteration .

The following is the visualization of each iteration :
#Graphic display
# print(sub_1+'41')
# Be careful : You can use commas for each parameter , Separate . The first parameter represents the number of rows in the subgraph ; The second parameter represents the number of columns in the row of images ; The third parameter represents the number of images in each row , From left to right , From top to next add .
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()

版权声明
本文为[Elementary school students in IT field]所创,转载请带上原文链接,感谢
边栏推荐
- PHP应用对接Justswap专用开发包【JustSwap.PHP】
- 做外包真的很难,身为外包的我也无奈叹息。
- Group count - word length
- 比特币一度突破14000美元,即将面临美国大选考验
- I've been rejected by the product manager. Why don't you know
- What is the side effect free method? How to name it? - Mario
- 基於MVC的RESTFul風格API實戰
- [event center azure event hub] interpretation of error information found in event hub logs
- The choice of enterprise database is usually decided by the system architect - the newstack
- 6.3 handlerexceptionresolver exception handling (in-depth analysis of SSM and project practice)
猜你喜欢

在大规模 Kubernetes 集群上实现高 SLO 的方法

How to select the evaluation index of classification model

Tool class under JUC package, its name is locksupport! Did you make it?

Filecoin主网上线以来Filecoin矿机扇区密封到底是什么意思

至联云分享:IPFS/Filecoin值不值得投资?

I'm afraid that the spread sequence calculation of arbitrage strategy is not as simple as you think

Face to face Manual Chapter 16: explanation and implementation of fair lock of code peasant association lock and reentrantlock

向北京集结!OpenI/O 2020启智开发者大会进入倒计时

vue-codemirror基本用法:实现搜索功能、代码折叠功能、获取编辑器值及时验证

This article will introduce you to jest unit test
随机推荐
Tool class under JUC package, its name is locksupport! Did you make it?
CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
快快使用ModelArts,零基础小白也能玩转AI!
Every day we say we need to do performance optimization. What are we optimizing?
数据产品不就是报表吗?大错特错!这分类里有大学问
axios学习笔记(二):轻松弄懂XHR的使用及如何封装简易axios
Save the file directly to Google drive and download it back ten times faster
使用 Iceberg on Kubernetes 打造新一代云原生数据湖
I think it is necessary to write a general idempotent component
快快使用ModelArts,零基礎小白也能玩轉AI!
A debate on whether flv should support hevc
Grouping operation aligned with specified datum
一篇文章带你了解CSS3圆角知识
ipfs正舵者Filecoin落地正当时 FIL币价格破千来了
html
一篇文章带你了解SVG 渐变知识
High availability cluster deployment of jumpserver: (6) deployment of SSH agent module Koko and implementation of system service management
Summary of common string algorithms
PHP应用对接Justswap专用开发包【JustSwap.PHP】
Keyboard entry lottery random draw