TensorFlow is a machine learning framework that many programmers are familiar with. It was opened in 2014 and has an active community. A few days ago, TensorFlow developer Conference announced TensorFlow for JS and Swift. Take this opportunity to write a primer on TensorFlow and how to get started with machine learning. This time, starting with the most basic linear regression, this article will perform linear regression on a data set and calculate the expression.

Linear regression (Linear Regression)

The current idea of regression is not difficult, the core idea of linear regression is to use a bunch of data to express linear analytic formula, that is, a one-time function. The first order function, which everyone learned in junior high, is y is equal to k times x plus b, where k is the slope and b is the vertical intercept. By this expression, if I give you an x value, I’ll give you a y value. Many phenomena in real life can also be represented by this model. Here, for example, the size and price of houses. Suppose we take a set of tables of the size and price of houses and plot them.

At this time, it can be found that there is a linear relationship between price and house size, and the price of any house size can be inferred by fitting a straight line.

To fit a line according to the coordinates of a given point, to get the analytic formula of the line is what linear regression needs to do. For ease of understanding, I rewrote y = k*x + b as y = w *x + b, where W stands for weight and B stands for bias. So, input a series of points, and the program calculates weight and bias for this purpose.

Data set reading

The first is to read the data set. I have generated some points in Excel in advance. The first column represents the abscissa and the second column represents the ordinate.

First read the CSV file using numpy and store the first and second columns in two arrays

import tensorflow as tf
import numpy as np

my_data = np.genfromtxt('dataset.csv', delimiter=',', dtype=float)
x_data = np.array(my_data[:,0])
y_data = np.array(my_data[:,1])

print(x_data)
print(y_data)
Copy the code

Run the program, x_data and y_data are printed

 

Model structures,

The weights variable is a -1 to 1 value, the default value of bias variable is 0, and the expression Y = weights * x_data + biases is the model.

Weights = tf.Variable(tf.random_uniform([1], -1,0, 1.0)) biases = tf.Variable(tf.zeros([1])) y = weights*x_data + biasesCopy the code

 

Calculation Error: Mean Square Error

After the model is specified, TensorFlow will continuously generate weight and bias after the training starts. However, how can we find the weight and bias that best match the points in the data set? In the following set of graphs, there are three curves, each with a slope and an intercept, and the first curve is the one that best matches these points. But how do you do that mathematically? That brings us to the squared loss, which is a number that represents the penalty for bad predictions.

In the following figure, y is the original coordinate, Y ‘is the prediction, and the red arrow is the Loss.

The formula for the squared loss is (y – y prime) ^ 2

More often, losses are calculated using the mean square error (MSE), which is the square error of all the sample points and the average

MSE is well represented in TensorFlow

loss = tf.reduce_mean(tf.square(y-y_data))
Copy the code

 

Error propagation: Gradient Descent

Now that the loss has been calculated, we must try to reduce the loss by adjusting the weight and bias of the new prediction through the incoming loss at each iteration. This process may sound abstract, but everyone played the game of guess the price, can you guess the price of a thing, I answered, “up” or “low” to feedback to you, you through my feedback to adjust the prices of new guess, and guess the right price, this is the core idea of gradient descent method.

Draw a relation diagram of loss and weight. In the ownership weight range (positive infinity to negative infinity), the relation between loss and weight is in a bowl shape. When the loss is at the lowest point, the corresponding weight at the lowest point is the appropriate weight. In the figure below, the loss is calculated from the yellow dot on the far left. Each advance is called Step or learning rate until it reaches the point of minimum loss. As for whether to push to the left or to the right, this is determined by calculating the partial derivative of the function, interested students can go to study the higher mathematics of the university.

The TensorFlow code is easy to implement, with a GradientDescentOptimizer declared in the following code at a learning rate of 0.5

The optimizer = tf. Train. GradientDescentOptimizer (0.5)Copy the code

 

training

Then you can train by calling global_variables_initializer() to initialize all your tensor variables, creating a new Session, and then running 201 iterations to output the values of weight and bias every 20 words.

Optimizer = tf. Train. GradientDescentOptimizer (0.5) "train" = optimizer. Minimize the init = (loss) tf.global_variables_initializer() sess = tf.Session() sess.run(init) for step in range(201): sess.run(train) if step % 20 == 0: print(step, 'weight: ',sess.run(weights), ' bias: ', sess.run(biases))Copy the code

After running, it can be found that the result converges, the final result of weight is about 0.4, and the final result of bias is 2.999

 

This is actually the expression I specified in Excel at the beginning, proving that linear regression works, TensorFlow calculates the correct values of W and B.

 

This article was first published by Miketech. It was written by Zhou Yigang. Please do not reprint without permission.

 

 

 

 

exceptional