Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use tensorflow to do Linear regression

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use tensorflow to do linear regression, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

01

-

Tensorflow does linear regression.

Previously realized the linear regression algorithm of the least square method, the gradient descent solution process, see the article for details:

Linear regression of machine learning: the algorithm is cashed into python code

So, how to realize the linear regression of least square method with the help of tensorflow? The basic idea is to first generate the fitted data set, then construct the linear regression Graph, and finally iterate the train in Session to get the fitting parameters w and b, and draw the fitting curve.

1.1 generate a fitted data set, which contains only one feature. Note that the error term needs to satisfy the Gaussian distribution. The code for its distribution is as follows. Import three libraries first.

Import numpy as np

Import tensorflow as tf

Import matplotlib.pyplot as plt

# 100 data points

Num_points = 100

Vectors_set = []

For i in range (num_points):

X1 = np.random.normal (0.cm0.55)

Y1 = x1 * 0. 1 + 0. 3 + np.random.normal (0. 013)

Vectors_set.append ([x1ther y1])

# feature x

X_data = [v [0] for v in vectors_set]

# tag value y

Y_data = [v [1] for v in vectors_set]

Plt.scatter (xenvelopdatatheconcentration data)

Plt.show ()

The resulting data distribution is as follows:

1.2 Construction of linear regression Graph

W = tf.Variable (tf.random_uniform ([1],-1.Power1.), name='myw')

B = tf.Variable (tf.zeros ([1]), name='myb')

# the estimated value is calculated.

Y = w * x_data + b

# take the mean square deviation between the estimated y and the actual value y_data as the loss

Loss = tf.reduce_mean (tf.square (ymurybaby data), name='myloss')

# using gradient descent method to optimize parameters

Optimizer = tf.train.GradientDescentOptimizer (0.5)

Train = optimizer.minimize (loss,name='mytrain')

1.3 run the built Graph in Session

# global_variables_initializer initializes variables such as Variable

Sess = tf.Session ()

Init = tf.global_variables_initializer ()

Sess.run (init)

Print ("w =", sess.run (w), "b =", sess.run (b), sess.run (loss))

# iterate 20 times train

For step in range (20):

Sess.run (train)

Print ("w =", sess.run (w), "b =", sess.run (b), sess.run (loss))

# write to disk and provide tensorboard for display in browser

Writer = tf.summary.FileWriter (". / mytmp", sess.graph)

Print w and b, the change of the loss value, you can see that the loss value decreased from 0.24 to 0.0008.

1.4 draw a fitting curve

Plt.scatter (xenvelopdatatheconcentration data)

Plt.plot (xresume data, Sess.run (w) * x_data+sess.run (b))

Plt.show ()

02

-

Tensorboard display Graph

On how to show the built Graph in tensorboard, please refer to the article, do not repeat, directly analyze the graph drawn by tensorflow.

TensorFlow Notes | Get Started

The resulting Graph interface is as follows:

Main Graph view enlarged version, how the data from the bottom through the Operators, flow to the top, you can follow.

The above is how to use tensorflow to do linear regression, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report