当前位置:网站首页>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
边栏推荐
- 【动手学深度学习V2】循环神经网络-1.序列模型
- RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio
- 电子采购如何成为供应链中的增值功能?
- easyswoole环境配置
- 2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
- Echo "new password" |passwd -- stdin user name
- What business scenarios will the BGP server be used in?
- Shardingsphere introduction and sub table usage
- PID精讲
- 揭秘被Arm编译器所隐藏的浮点运算
猜你喜欢

Find the largest number of two-dimensional arrays

What are the common proxy IP problems?

RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio

CDR2022首发全新版本性能介绍

ESP32使用freeRTOS的消息队列

What is a proxy IP pool and how to build it?

Error in face detection and signature of Tencent cloud interface

Use of j-link RTT
![[经验教程]支付宝余额自动转入余额宝怎么设置关闭取消支付宝余额自动转入余额宝?](/img/d5/6aa14af59144b8c99aa6a367479f6b.png)
[经验教程]支付宝余额自动转入余额宝怎么设置关闭取消支付宝余额自动转入余额宝?

什么是代理IP池,如何构建?
随机推荐
ESP32使用freeRTOS的消息队列
How to initialize "naming and surname" in C language
[Dahua cloud native] micro service chapter - service mode of five-star hotels
如何设置电脑ip?
How to write the resume of Software Test Engineer so that HR can see it?
领导/老师让填写电子excel表格文档可手机上如何编辑word/excel文件填写excel/word电子文档?
《维C中国》乡村助农暖人心第三站嘉宝果农场
批处理多个文件合成一个HEX
搭建网站是用物理机还是云主机好?
力扣(LeetCode)112. 路径总和(2022.04.22)
Some tips for using proxy IP.
Digital collection platform settled in digital collection platform to develop SaaS platform of digital collection
Question bank and online simulation examination for safety management personnel of hazardous chemical business units in 2022
客户端项目管理经常面临的挑战
[experience tutorial] Alipay balance automatically transferred to the balance of treasure how to set off, cancel Alipay balance automatically transferred to balance treasure?
[Leetcode每日一题]396. 旋转函数
如何选择一台好的拨号服务器?
PHP & laravel & master several ways of generating token by API and some precautions (PIT)
K zeros after leetcode factorial function
2022 low voltage electrician examination questions and answers