Artificial Intelligence AI training _TensorFlow achieve linear regression
1. Introduction experiment
1.1. About this experiment
In this study, namely housing prices predicted to be a real case TensorFlow linear regression.
1.2. Purpose
Understanding of linear regression.
Understand how to use TensorFlow make predictions.
1.3. Experimental Introduction
In this study, an instance of a forecast house prices to explain using linear regression to predict house prices, as well as how to achieve tensorflow in. Usually used to predict prices data set for the Boston housing data sets used in this experiment is Beijing's housing prices data collection, closer to people's lives.
1.4. Experimental Procedure
Step 1 Step 1 Log Huawei cloud.
Step 2. Click on the top right of the console.
Step 3 Select elastic cloud server, the operation of the cloud may be elastic web page appears, select the remote login. That is to log on to elastic cloud server.
Step 4 Enter the command ll, view files in the current directory.
Step 5 Enter the command vi house_price.py, create a new Python script.
Step 6 Enter the command i, enter the edit mode to start editing, enter the script content.
Step 7 Enter the command: wq !, save and exit.
Step 8 cat house_price.py input command to view the code.
Step 9 Run the test. Enter the command: python3 house_price.py.
2. Experimental procedure
2.1 Setting Code Description
# coding:utf-8
2.2. Import module
# Load required for this project library
from __future__ import print_function, division
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn
2.3. Import Data
The experimental data sources: https: //github.com/cunxi1992/boston_housing in bj_housing2.csv file.
Read data:
train = pd.read_csv("bj_housing2.csv")
2.4 Defining parameters
train = train[train['Area'] < 12000]
train_X = train['Area'].values.reshape(-1, 1)
train_Y = train['Value'].values.reshape(-1, 1)
n_samples = train_X.shape[0]
# Define the parameters set learning rate
learning_rate = 2
# Set the training times
training_epochs = 1000
# Set the number of times a show
display_step = 50
2.5 Definitions placeholder
# Define X, Y placeholder
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
# Variable parameters defined using the learning
W = tf.Variable(np.random.randn(), name="weight", dtype=tf.float32)
b = tf.Variable(np.random.randn(), name="bias", dtype=tf.float32)
2.6. Construction of the forward propagating structures
# Build forward propagation structure
pred = tf.add(tf.multiply(W, X), b)
# Loss function
cost = tf.reduce_sum(tf.pow(pred-Y, 2)) / (2 * n_samples)
# Gradient descent Optimizer
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
2.7. Initialization
# Activate Init
init = tf.global_variables_initializer()
# Starts session, initialized variable
with tf.Session() as sess:
sess.run (ins)
2.8. Start Cycle
# Start to start training cycle
for epoch in range(training_epochs):
for (x, y) in zip(train_X, train_Y):
sess.run(optimizer, feed_dict={X: x, Y: y})
# Display detailed information Training
if (epoch + 1) % display_step == 0:
c = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
print ( "Epoch:", '% 04d'% (epoch + 1), "cost =", "{: .3f}". format (c), "W =", sess.run (W), "b = ", sess.run (b)) # show details training
print("Optimization Finished!")
training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')
2.9. The results show training
# Show training results
plt.plot(train_X, train_Y, 'ro', label="Original data")
plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label="Fitted line")
plt.legend ()
plt.show()
2.10. The results
Output:
Epoch: 0050 cost= 16308.796 W= 1.8889627 b= 155.08276
Epoch: 0100 cost= 16308.796 W= 1.8889627 b= 155.08276
Epoch: 0150 cost= 16308.796 W= 1.8889627 b= 155.08276
Epoch: 0200 cost= 16308.796 W= 1.8889627 b= 155.08276
Epoch: 0250 cost= 16308.796 W= 1.8889627 b= 155.08276
Epoch: 0300 cost= 16308.796 W= 1.8889627 b= 155.08276
Epoch: 0350 cost= 16308.796 W= 1.8889627 b= 155.08276
Epoch: 0400 cost= 16308.796 W= 1.8889627 b= 155.08276
Epoch: 0450 cost= 16308.796 W= 1.8889627 b= 155.08276
Epoch: 0500 cost= 16308.796 W= 1.8889627 b= 155.08276
Epoch: 0550 cost= 16308.796 W= 1.8889627 b= 155.08276
Epoch: 0600 cost= 16308.796 W= 1.8889627 b= 155.08276
……
Example 3. Description
In this study, use of existing data sets online Beijing housing prices predicted housing prices in Beijing, to achieve a linear regression of application TensorFlow.
Reproduced in: https: //www.jianshu.com/p/b4e3a8d0c33e