Linear regression

Linear regression is a very common kind of regression, linear regression can be used to predict or classify, mainly to solve linear problems. See “Related Reading” for more information.

The main idea

The focus of linear regression processing in TensorFlow is to matrix samples and sample features.

Single feature linear regression

The single feature regression model is y = Wx + b

Build the model

X = tf.placeholder(tf.float32, [None, 1])
w = tf.Variable(tf.zeros([1.1]))
b = tf.Variable(tf.zeros([1]))
y = tf.matmul(X, w) + b
Y = tf.placeholder(tf.float32, [None, 1])Copy the code

Building cost function

cost = tf.reduce_mean(tf.square(Y-y))Copy the code

The gradient descent minimizes the cost function, and the gradient descent step is 0.01

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cost)Copy the code

Complete code with 10000 iterations

import tensorflow as tf X = tf.placeholder(tf.float32, [None, 1]) w = tf.Variable(tf.zeros([1, 1])) b = tf.Variable(tf.zeros([1])) y = tf.matmul(X, w) + b Y = tf.placeholder(tf.float32, [None, Sum (SQR (y_-y))/n cost = tf.reduce_mean(tf.square(y-y) Tf. Train. GradientDescentOptimizer (0.01). Minimize (cost) init = tf. Initialize_all_variables (sess) = tf. The Session () sess.run(init) x_train = [[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]] y_train = [[10], [11.5], [12], [13], [14.5], [15.5], [16.8], [17.3], [18], [18.7]] for I in range (10000) : sess.run(train_step, feed_dict={X: x_train, Y: y_train}) print("w:%f" % sess.run(w)) print("b:%f" % sess.run(b))Copy the code

Multifeature linear regression

The multi-feature regression model is y = (w{1}x{1} + W {2}x{2} +… +w{n}x{n}) + b, write y = wx + b.

Y is an M row 1 column matrix, x is an M row N column matrix, and w is an N row 1 column matrix. TensorFlow represents the model as follows.

Build the model

X = tf.placeholder(tf.float32, [None, n])
w = tf.Variable(tf.zeros([n, 1]))
b = tf.Variable(tf.zeros([1]))
y = tf.matmul(X, w) + b
Y = tf.placeholder(tf.float32, [None, 1])Copy the code

Building cost function

cost = tf.reduce_mean(tf.square(Y-y))Copy the code

The gradient descent minimizes the cost function, and the gradient descent step is 0.01

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cost)Copy the code

Complete code with 10000 iterations

import tensorflow as tf X = tf.placeholder(tf.float32, [None, 2]) w = tf.Variable(tf.zeros([2, 1])) b = tf.Variable(tf.zeros([1])) y = tf.matmul(X, w) + b Y = tf.placeholder(tf.float32, [None, Sum (SQR (y_-y))/n cost = tf.reduce_mean(tf.square(y-y) Tf. Train. GradientDescentOptimizer (0.01). Minimize (cost) init = tf. Initialize_all_variables (sess) = tf. The Session () sess.run(init) x_train = [[1, 2], [2, 1], [2, 3], [3, 5], [1, 3], [4, 2], [7, 3], [4, 5], [11, 3], [8, 7]] y_train = [[7], [8], [10], [14], [8], [13], [20], [16], [28], [26]] for i in range(10000): sess.run(train_step, feed_dict={X: x_train, Y: y_train}) print("w0:%f" % sess.run(w[0])) print("w1:%f" % sess.run(w[1])) print("b:%f" % sess.run(b))Copy the code

conclusion

In linear regression, TensorFlow can easily use matrix for multi-feature sample training.

reading

  • The least square method of linear regression
  • Gradient descent for machine learning
  • What is supervised learning in machine learning studying

Welcome to:

Write the picture description here