当前位置:网站首页>Realize linear regression with tensorflow (including problems and solutions in the process)
Realize linear regression with tensorflow (including problems and solutions in the process)
2022-04-23 02:00:00 【Fish never spit out thorns】
use TensorFlow Realize linear regression experiment
The experiment purpose :
Through this experiment, let students understand TensorFlow The basic idea of constructing the whole neural network training model , master TensorFlow The process and method of realizing linear regression , And learn to use tensorboard graph To view and check the neural network model designed by yourself in a graphical way .
Experimental instruments, equipment and materials :
Installed with Python Computer running environment .
Experimental content
One .TensorFlow Environmental installation
1. stay anaconda prompt Window for installation (anaconda3) It's best to create a new virtual environment , Then operate and install in the environment ( The default environment is base)
conda create -n tf2 python=3.6.5
This is a new one named tf2, also python The version is 3.6.5 An environment of (python The version number should match your own version number ).
Switch to an environment :conda activate Environment name .
2. Enter the just created tf2 Environmental Science :
conda activate tf2
3. install TensorFlow2.4.0
pip install tensorflow-cpu==2.4.0
Or mirror installation pip install tensorflow-cpu==2.4.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
It may be a little long here , Make sure that the network cannot be disconnected . It will install other packages , These packages are also often used in machine learning . If the scarlet letter appears , Install it again . Until it appears successfull installed …, Indicates that it has been successfully installed tensorflow package . The picture below is 2.5.0 Schematic diagram of successful installation of version :
4. Then if you use pycharm Tool software , No need to install TensorFlow, Just configure the environment
Two . utilize TensorFlow Carry out linear regression experiment
Given a batch by y = 3x + 2 Generated data sets (x, y), Build a linear regression model h(x) = wx + b, Predict w = 3 and b = 2.
The experimental requirements :
1 Generate fitted data sets
The dataset contains only one eigenvector , Note that the error term must satisfy the Gaussian distribution . Used numpy and matplotlib library .numpy yes Python An open source numerical Scientific Computing Library , Can be used to store and process large matrices .matplotlib yes Python Drawing library of , It can be connected with numpy Use it together , Provides an effective MatLab Open source alternatives
The code is as follows :
# First, import. 3 Databases :
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
# Randomly generate data points 100 individual , The random probability conforms to Gaussian distribution ( Normal distribution )
num_points = 100
vectors_set = []
for i in range(num_points):
x1 = np.random.normal(0.,0.55)
y1 = x1 * 0.1 + 0.3 + np.random.normal(0.0,0.03)
vectors_set.append([x1,y1])
# Define eigenvectors x
x_data = [v[0] for v in vectors_set]
# Define the label vector y
y_data = [v[1] for v in vectors_set]
# Press [x_data,y_data] stay X-Y In the coordinate system, it is displayed in dot mode , call plt Establish the coordinate system and print out the values
plt.scatter(x_data,y_data,c='b')
plt.show()
The resulting data distribution is as follows :
2 Construct a linear regression model Graph
# utilize TensorFlow Randomly generated w and b, For graphic display, you need , Define the names of myw and myb
w = tf.Variable(tf.random_uniform([1],-1.,1.),name='myw')
b = tf.Variable(tf.zeros([1]),name='myb')
# Based on randomly generated w and b, Combined with the above randomly generated feature vector x_data, Estimated value after calculation
y = w * x_data + b
# Estimated value y And actual value y_data The mean square deviation between is taken as the loss
loss = tf.reduce_mean(tf.square(y-y_data,name='mysquare'), name='myloss')
# The gradient descent method is used to optimize the parameters
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss,name='mytrain')
3 stay Session Run the built Graph
#global_variables_initializer initialization Variable Equivariant
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print("w=", sess.run(w),"b=",sess.run(b),sess.run(loss))
# iteration 20 Time train
for step in range(20):
sess.run(train)
print("w=", sess.run(w),"b=",sess.run(b),sess.run(loss))
# Write to disk , Provide tensorboard Show in browser with
writer = tf.summary.FileWriter("./mytmp",sess.graph)
Print w and b, Change of loss value , You can see the loss value from 0.24 drop to 0.0008.
4 Draw the fitting curve
plt.scatter(x_data,y_data,c='b')
plt.plot(x_data,sess.run(w)*x_data+sess.run(b))
plt.show()
Pictured :
5 tensorboard Show the application of neural network graph
In the above program design, there is code :writer = tf.summary.FileWriter("./mytmp",sess.graph)
After running the modified code, the node information of the whole neural network can be written to ./mytmp
Under the table of contents ( This directory is in the same directory as the previously established program ). stay cmd Pass through “cd Catalog ” Switch to the directory , Input “dir” The command displays the log files just run in this directory , Last input tensorboard --logdir=D:\PyCharm2018.3.7\workpase\eg001\mytmp
, The display message appears after the enter operation , In the last line of the message “TensorBoard 2.0.2 at http://localhost:6006/ (Press CTRL+C to quit)
”, open chrome browser , Type in the browser address bar http://localhost:6006
, It will show the graphic display of the neural network just programmed . As shown in the figure below :
Experiment records, problems encountered in the experiment and solutions :
1. Program running error
tensorflow Show no random_uniform modular
terms of settlement :tf2.0 I changed my name in , use tf.random.uniform
Instead of
2. Program running error
TensorFlow2.0 The version running program reports an error
terms of settlement :
import tensorflow as tf
Change it to
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tf.disable_v2_behavior(): This function can be called at the beginning of the program ( In creating a tensor 、 Before graphics or other structures , And before initializing the device ). It will TensorFlow 1.x and 2.x Switch between all different global behaviors to predetermined 1.x Behavior , It's shielding 2.x edition .
3. Format error
To look at first tensorboard Installation position , I used Anaconda3, Can be in Anaconda3 Search under tensorflow, Find the bottom Scripts Look under the file for tensorboard.exe file , If it exists, there is no configuration tensorboard environment variable .
After this is done, there is a problem ,
There can be no spaces in the path , Just delete it
4. Page error reporting
Get into http://localhost:6006
Will report a mistake
Change the user name of the host to localhost
(Win10 System host user name modification ( After modification, you need to restart the computer ), Or an error ,( This method is not only useless , It will also lead to the subsequent lack of access to the Internet , Readers are not advised to use , Because the author has used , Here are only records for reference )
stay pycharm Try again in :( Turn off the cmd Command line !!! According to observation , If you want to open another new calculation chart , Be sure to put the original cmd The command line window closes , Repeat the above steps , Otherwise, the previous calculation diagram will still be opened .)
stay pycharm The menu bar of , choice View–Tool Windows–Terminal
And then execute :
tensorboard --logdir=mytmp
After entering the page , Successful implementation !!!
版权声明
本文为[Fish never spit out thorns]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230159048341.html
边栏推荐
猜你喜欢
一加一为什么等于二
postman里面使用 xdebug 断点调试
Cc2541 emulator CC debugger tutorial
leetcode:27. 移除元素【count remove小操作】
Some tips for using proxy IP.
Ziguang micro financial report is outstanding. What does the triple digit growth of net profit in 2021 depend on
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
单片机和4G模块通信总结(EC20)
What businesses use physical servers?
The sixth season of 2022, the perfect children's model IPA national race leads the yuanuniverse track
随机推荐
Performance introduction of the first new version of cdr2022
教程】如何用GCC“零汇编”白嫖MDK
How to write the resume of Software Test Engineer so that HR can see it?
[experience tutorial] Alipay balance automatically transferred to the balance of treasure how to set off, cancel Alipay balance automatically transferred to balance treasure?
如何“优雅”的测量系统性能
What is a proxy IP pool and how to build it?
What code needs unit testing?
postman里面使用 xdebug 断点调试
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
[Leetcode每日一题]396. 旋转函数
C语言实现Base64编码/解码
揭秘被Arm编译器所隐藏的浮点运算
What businesses use physical servers?
拨号vps会遇到什么问题?
动态代理ip的测试步骤有哪些?
Uncover floating-point operations hidden by the ARM compiler
When should I write unit tests? What is TDD?
客户端项目管理经常面临的挑战
Virtual serial port function of j-link V9 using skills
keil mdk中文乱码,两种解决方法,字体不再难看