4 Chapter 9 TensorFlow Programming Basics Experiment Guide

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

HCIA-AI Huawei Certification Course

HCIA-AI
TensorFlow Programming Basics
Experiment Guide

Version: 1.0

Huawei Technologies Co., Ltd.


Copyright © Huawei Technologies Co., Ltd. 2018. All rights reserved.
No part of this document may be reproduced or transmitted in any form or by any means without prior written
consent of Huawei Technologies Co., Ltd.

Trademarks and Permissions

and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd.
All other trademarks and trade names mentioned in this document are the property of their respective holders.

Notice
The purchased products, services and features are stipulated by the contract made between Huawei and the customer.
All or part of the products, services and features described in this document may not be within the purchase scope or
the usage scope. Unless otherwise specified in the contract, all statements, information, and recommendations in this
document are provided "AS IS" without warranties, guarantees or representations of any kind, either express or
implied.
The information in this document is subject to change without notice. Every effort has been made in the preparation
of this document to ensure accuracy of the contents, but all statements, information, and recommendations in this
document do not constitute a warranty of any kind, express or implied.

Huawei Technologies Co., Ltd.


Address: Huawei Industrial Base
Bantian, Longgang
Shenzhen 518129
People's Republic of China
Website: http://e.huawei.com

Email: [email protected]

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. i


TensorFlow Programming Basics
Experiment Guide

Introduction to Huawei Certification System


Based on cutting-edge technologies and professional training systems, Huawei certification meets the diverse AI
technology demands of clients. Huawei is committed to providing practical and professional technical certification
for our clients.
HCIA-AI V1.0 certification is intended to popularize AI and help understand deep learning and Huawei Cloud EI,
and learn the basic capabilities of programming based on the TensorFlow framework, as a motive to promote
talent training in the AI industry.
Content of HCIA-AI V1.0 includes but is not limited to: AI overview, Python programming and experiments,
mathematics basics and experiments, TensorFlow introduction and experiments, deep learning pre-knowledge,
deep learning overview, Huawei cloud EI overview, and application experiments for image recognition, voice
recognition and man-machine dialogue.
HCIA-AI certification will prove that you systematically understand and grasp Python programming, essential
mathematics knowledge in AI, basic programming methods of machine learning and deep learning platform
TensorFlow, pre-knowledge and overview of deep learning, overview of Huawei cloud EI, basic programming for
image recognition, voice recognition, and man-machine dialogue. With this certification, you have required
knowledge and techniques for AI pre-sales basic support, AI after-sales technical support, AI products sales, AI
project management, and are qualified for positions such as natural language processing (NLP) engineers, image
processing engineers, voice processing engineers and machine learning algorithm engineers.
Enterprises with HCIA-AI-certified engineers have the basic understanding of AI technology, framework, and
programming, and capable of leveraging AI, machine learning, and deep learning technologies, as well as the
open-source TensorFlow framework to design and develop AI products and solutions like machine learning, image
recognition, voice recognition, and man-machine dialogue.
Huawei certification will help you open the industry window and the door to changes, standing in the forefront of
the AI world!

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. ii


TensorFlow Programming Basics
Experiment Guide Preface

Preface

Brief Introduction
This document is an HCIA-AI certification training course, intended to trainees who are
preparing for HCIA-AI tests or readers who want to know about AI basics and TensorFlow
basic programming.

Content Description
This experiment guide includes nine experiments, introducing basic equipment operation and
configuration, TensorFlow's helloworld, sessions, matrix multiplication, TensorFlow
virtualization, and housing price prediction.
 Experiment 1: "Hello, TensorFlow".
 Experiment 2: Understand functions of sessions through a session experiment using the
with session function.
 Experiment 3: Understand matrix multiplication by multiplying two matrices with ranks
of tensors greater than 2.
 Experiment 4: Understand the definition of variables. Define variables with Variable and
get_variable respectively and observe the difference between these two methods.
 Experiment 5: Understand the visualization of TensorBoard. TensorBoard aggregates all
kinds of data into a log file. You can enable TensorBoard service to read the log file and
enable the 6060 port to provide web services so that users can view data via a browser.
 Experiment 6: Understand data reading and processing by reading .csv files and
displaying them based on given conditions.
 Experiment 7: Understand graphic operations. Create a graph in three ways and set it as
the default graph. Use the get_default_graph() function to access the default graph and
verify its settings.
 Experiment 8: Understand save and use of models. After importing data, analyze data
characteristics and define variables based on the characteristics. Create a model and
define output nodes. Build the structure for forward propagation and then the structure
for backpropagation. Compile and train the model to get appropriate parameters. After
training data and testing the model, create a saver and a path to save parameters in the
session automatically. When the model is saved, you can access the model for use.
 Experiment 9: A comprehensive experiment of forecasting housing price through the
instantiation of linear regression. Use the dataset of housing prices in Beijing and skills
in the prior eight experiments to forecast the housing price.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. iii


TensorFlow Programming Basics
Experiment Guide Preface

Knowledge Background of Readers


This course is intended for Huawei certification. To better understand content of this
document, readers are required to meet the following basic conditions:
Have background knowledge of Python, be familiar with conceptual framework of
TensorFlow, and have basic knowledge of Python programming.

Experimental Environment
Networking
This experimental environment is compiled for AI engineers who are preparing for HCIA-AI
tests. Each set of experimental environment includes a remote elastic cloud server (ECS) with
two cores, 4-GB memory, and 64-bit CentOS 7.4.

Equipment
To meet the HCIA-AI experiment needs, adopt the following configuration for each set of
experimental environment:
The following table shows the device names and types and software versions.

Device Name Device Type Software Version


CPU General computing; s2.large.2;
2 cores; 4-GB memory
System 64-bit CentOS 7.4
Bandwidth 10 Mbit/s
Bandwidth Exclusive
sharing mode
NIC One

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. iv


TensorFlow Programming Basics
Experiment Guide Contents

Contents

Preface......................................................................................................................................... iii
1 Hello, TensorFlow! ................................................................................................................... 1
1.1 Introduction to the Experiment............................................................................................................................... 1
1.1.1 About the Experiment ......................................................................................................................................... 1
1.1.2 Objectives of the Experiment .............................................................................................................................. 1
1.1.3 Experiment Content ............................................................................................................................................ 1
1.1.4 Experimental Operations..................................................................................................................................... 1
1.2 Experimental Process ............................................................................................................................................ 2
1.2.1 Setting Encoding Declarations ............................................................................................................................ 2
1.2.2 Importing a Module ............................................................................................................................................ 2
1.2.3 Defining Variables .............................................................................................................................................. 2
1.2.4 Creating a Session .............................................................................................................................................. 2
1.2.5 Closing the Session ............................................................................................................................................. 2
1.2.6 Experimental Results .......................................................................................................................................... 3
1.3 Instance Description .............................................................................................................................................. 3

2 Session ....................................................................................................................................... 4
2.1 Introduction to the Experiment............................................................................................................................... 4
2.1.1 About the Experiment ......................................................................................................................................... 4
2.1.2 Objectives of the Experiment .............................................................................................................................. 4
2.1.3 Experiment Content ............................................................................................................................................ 4
2.1.4 Experimental Operations..................................................................................................................................... 4
2.2 Experimental Process ............................................................................................................................................ 5
2.2.1 Setting Encoding Declarations ............................................................................................................................ 5
2.2.2 Importing a Module ............................................................................................................................................ 5
2.2.3 Defining Constants ............................................................................................................................................. 5
2.2.4 Creating a Session .............................................................................................................................................. 5
2.2.5 Experimental Results .......................................................................................................................................... 6
2.3 Instance Description .............................................................................................................................................. 6

3 Matrix Multiplication .............................................................................................................. 7


3.1 Introduction to the Experiment............................................................................................................................... 7
3.1.1 About the Experiment ......................................................................................................................................... 7
3.1.2 Objectives of the Experiment .............................................................................................................................. 7

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. v


TensorFlow Programming Basics
Experiment Guide Contents

3.1.3 Experiment Content ............................................................................................................................................ 7


3.1.4 Experimental Operations..................................................................................................................................... 7
3.2 Experimental Process ............................................................................................................................................ 8
3.2.1 Setting Encoding Declarations ............................................................................................................................ 8
3.2.2 Importing a Module ............................................................................................................................................ 8
3.2.3 Starting a TensorFlow Default Session ................................................................................................................ 8
3.2.4 Creating Matrix Variables ................................................................................................................................... 8
3.2.5 Defining a Constant Matrix ................................................................................................................................. 8
3.2.6 Initializing Global Variables ................................................................................................................................ 9
3.2.7 Matrix Multiplication .......................................................................................................................................... 9
3.2.8 Experimental Results .......................................................................................................................................... 9
3.3 Instance Description .............................................................................................................................................. 9

4 Definition of Variables ......................................................................................................... 10


4.1 Introduction to the Experiment..............................................................................................................................10
4.1.1 About the Experiment ........................................................................................................................................10
4.1.2 Objectives of the Experiment .............................................................................................................................10
4.1.3 Experiment Content ...........................................................................................................................................10
4.1.4 Experimental Operations....................................................................................................................................10
4.2 Experimental Process ........................................................................................................................................... 11
4.2.1 Setting Encoding Declarations ........................................................................................................................... 11
4.2.2 Importing a Module ........................................................................................................................................... 11
4.2.3 Resetting the Computation Graph....................................................................................................................... 11
4.2.4 Defining Variables ............................................................................................................................................. 11
4.2.5 Defining get_variable variables .......................................................................................................................... 11
4.2.6 Outputting Variables .......................................................................................................................................... 11
4.2.7 Experimental Results .........................................................................................................................................12

5 Visualization of TensorBoard............................................................................................... 13
5.1 Introduction to the Experiment..............................................................................................................................13
5.1.1 About the Experiment ........................................................................................................................................13
5.1.2 Objectives of the Experiment .............................................................................................................................13
5.1.3 Experiment Content ...........................................................................................................................................13
5.1.4 Experimental Operations....................................................................................................................................13
5.2 Experimental Process ...........................................................................................................................................14
5.2.1 Setting Encoding Declarations ...........................................................................................................................14
5.2.2 Importing a Module ...........................................................................................................................................14
5.2.3 Generating Analog Data .....................................................................................................................................14
5.2.4 Resetting the Computation Graph.......................................................................................................................14
5.2.5 Creating a Model ...............................................................................................................................................14
5.2.6 Creating a Forward Structure .............................................................................................................................15
5.2.7 Reverse Optimization ........................................................................................................................................15
5.2.8 Initializing Variables ..........................................................................................................................................15

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. vi


TensorFlow Programming Basics
Experiment Guide Contents

5.2.9 Starting a Session ..............................................................................................................................................15


5.2.10 Writing Data to the Model ................................................................................................................................16
5.2.11 Graphical Display ............................................................................................................................................16
5.2.12 Experimental Results .......................................................................................................................................17
5.3 Instance Description .............................................................................................................................................19

6 Data Reading and Processing ............................................................................................... 20


6.1 Introduction to the Experiment..............................................................................................................................20
6.1.1 About the Experiment ........................................................................................................................................20
6.1.2 Objectives of the Experiment .............................................................................................................................20
6.1.3 Experiment Content ...........................................................................................................................................20
6.1.4 Experimental Operations....................................................................................................................................20
6.2 Experimental Process ...........................................................................................................................................21
6.2.1 Setting Encoding Declarations ...........................................................................................................................21
6.2.2 Importing a Module ...........................................................................................................................................21
6.2.3 Reading Data from Files ....................................................................................................................................21
6.2.4 Getting Queue Values ........................................................................................................................................22
6.2.5 Initializing Variables ..........................................................................................................................................22
6.2.6 Starting a Session ..............................................................................................................................................23
6.2.7 Experimental Results .........................................................................................................................................23
6.3 Instance Description .............................................................................................................................................24

7 Graphic Operation ................................................................................................................. 25


7.1 Introduction to the Experiment..............................................................................................................................25
7.1.1 About the Experiment ........................................................................................................................................25
7.1.2 Objectives of the Experiment .............................................................................................................................25
7.1.3 Experiment Content ...........................................................................................................................................25
7.1.4 Experimental Operations....................................................................................................................................25
7.2 Experimental Process ...........................................................................................................................................26
7.2.1 Creating a Graph ...............................................................................................................................................26
7.2.2 Getting the Tensor..............................................................................................................................................26
7.2.3 Getting an Operation..........................................................................................................................................26
7.2.4 Getting All Lists ................................................................................................................................................27
7.2.5 Getting an Object ...............................................................................................................................................27
7.2.6 Experimental Results .........................................................................................................................................27
7.3 Instance Description .............................................................................................................................................29

8 Save and Use of Models ........................................................................................................ 30


8.1 Introduction to the Experiment..............................................................................................................................30
8.1.1 About the Experiment ........................................................................................................................................30
8.1.2 Objectives of the Experiment .............................................................................................................................30
8.1.3 Experiment Content ...........................................................................................................................................30
8.1.4 Experimental Operations....................................................................................................................................30
8.2 Experimental Process ...........................................................................................................................................31

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. vii


TensorFlow Programming Basics
Experiment Guide Contents

8.2.1 Importing a Dataset ...........................................................................................................................................31


8.2.2 Defining Variables .............................................................................................................................................31
8.2.3 Initializing Variables ..........................................................................................................................................31
8.2.4 Running a Session .............................................................................................................................................32
8.2.5 Creating a Saving Directory for the Model .........................................................................................................32
8.2.6 Saving the Model ...............................................................................................................................................32
8.2.7 Experimental Results .........................................................................................................................................32
8.3 Using the Model ...................................................................................................................................................33
8.3.1 Importing a Dataset ...........................................................................................................................................33
8.3.2 Creating a Session .............................................................................................................................................33
8.3.3 Defining Variables .............................................................................................................................................33
8.3.4 Restoring the Model ..........................................................................................................................................33
8.3.5 Computing Results ............................................................................................................................................34
8.3.6 Experimental Results .........................................................................................................................................34
8.4 Instance Description .............................................................................................................................................34

9 Linear Regression: Housing Price Forecast ......................................................................... 35


9.1 Introduction to the Experiment..............................................................................................................................35
9.1.1 About the Experiment ........................................................................................................................................35
9.1.2 Objectives of the Experiment .............................................................................................................................35
9.1.3 Experiment Content ...........................................................................................................................................35
9.1.4 Experimental Operations....................................................................................................................................35
9.2 Experimental Process ...........................................................................................................................................36
9.2.1 Setting Encoding Declarations ...........................................................................................................................36
9.2.2 Importing a Module ...........................................................................................................................................36
9.2.3 Importing Data ..................................................................................................................................................36
9.2.4 Defining Parameters ..........................................................................................................................................36
9.2.5 Defining Placeholders ........................................................................................................................................37
9.2.6 Creating a Structure of Forward Propagation ......................................................................................................37
9.2.7 Initializing Variables ..........................................................................................................................................37
9.2.8 Enabling Circulation ..........................................................................................................................................37
9.2.9 Displaying Training Results ...............................................................................................................................38
9.2.10 Experimental Results .......................................................................................................................................38
9.3 Instance Description .............................................................................................................................................38

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. viii


TensorFlow Programming Basics
Experiment Guide 1 Hello, TensorFlow!

1 Hello, TensorFlow!

1.1 Introduction to the Experiment


1.1.1 About the Experiment
This experiment introduces the function of sessions by outputting "Hello, TensorFlow!".

1.1.2 Objectives of the Experiment


 Understand the definition of sessions.
 Understand how to create a session.

1.1.3 Experiment Content


Two data flow mechanisms are involved in the interaction between a session and a graph.
Feed mechanism: Transmit data into the graph by placeholders.
Fetch mechanism: Retrieve the result of an operation in the graph.
This experiment introduces the function of sessions by creating a session.
Create a session and output "Hello, TensorFlow!" in the session.

1.1.4 Experimental Operations


Step 1 Log in to Hicloud on www.huaweicloud.com.

Step 2 Click Console in the upper right corner.

Step 3 Select an ECS. Applicable operations of the ECS are displayed. Select Remote Login to log
in to the ECS.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 1


TensorFlow Programming Basics
Experiment Guide 1 Hello, TensorFlow!

My Resources [North China – Beijing -]

ESC (1) BMS (0) AS (0) EVS (1)

EVS backup (0) VPC(1) ELB (0) KMS (0)

Relational
database (0)

Step 4 Input the instruction ll to view files in the current directory.


Step 5 Run the vi sessionhelloworld.py command to create a Python script.
Step 6 Run the i command to enable the edit mode. Enter script contents.
Step 7 Run the :wq! command. Save and exit.
Step 8 Run the cat sessionhelloworld.py command to view the code.
Step 9 Run the test.
Run the python3 sessionhelloworld.py command.
----End

1.2 Experimental Process


1.2.1 Setting Encoding Declarations
# -*- coding: utf-8 -*-

1.2.2 Importing a Module


import tensorflow as tf

1.2.3 Defining Variables


hello = tf.constant('Hello, TensorFlow!') #Define a constant.

1.2.4 Creating a Session


sess = tf.Session() #Create a session.
print (sess.run(hello)) #Run the command session.run to get the
result.

1.2.5 Closing the Session


sess.close() #Close the session.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 2


TensorFlow Programming Basics
Experiment Guide 1 Hello, TensorFlow!

1.2.6 Experimental Results


Output:
b'Hello, TensorFlow!'

1.3 Instance Description


tf.constant defines a constant. The content after hello can only be returned by the session.run
command.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 3


TensorFlow Programming Basics
Experiment Guide 2 Session

2 Session

2.1 Introduction to the Experiment


2.1.1 About the Experiment
This experiment introduces the function of sessions by enabling a session using the with
session syntax.

2.1.2 Objectives of the Experiment


 Understand the definition of sessions.
 Understand how to use the with session syntax to create a session.

2.1.3 Experiment Content


The syntax with session is the most commonly used one for enabling a session. It adopts the
with syntax in Python which allows a session to close automatically upon termination of a
program without requiring the close command.

2.1.4 Experimental Operations


Step 1 Log in to Hicloud.

Step 2 Click Console in the upper right corner.

Step 3 Select an ECS. Applicable operations of the ECS are displayed. Select Remote Login to log
in to the ECS.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 4


TensorFlow Programming Basics
Experiment Guide 2 Session

My Resources [North China – Beijing -]

ESC (1) BMS (0) AS (0) EVS (1)

EVS backup (0) VPC(1) ELB (0) KMS (0)


Relational
database (0)

Step 4 Input the instruction ll to view files in the current directory.


Step 5 Run the vi with session.py command to create a Python script.
Step 6 Run the i command to enter the insert edit mode and start editing. Enter script contents.
Step 7 Run the :wq! command. Save and exit.
Step 8 Run the cat with session.py command to view the code.
Step 9 Run the test.
Run the python3 with session.py command.
----End

2.2 Experimental Process


2.2.1 Setting Encoding Declarations
# -*- coding: utf-8 -*-

2.2.2 Importing a Module


import tensorflow as tf

2.2.3 Defining Constants


a = tf.constant(3) #Defining constant 3.
b = tf.constant(4) #Defining constant 4.

2.2.4 Creating a Session


with tf.Session() as sess: #Create a session.
print ("Add: %i" % sess.run(a+b)) #Calculate and output the sum of the two constants.
print ("Multiply: %i" % sess.run(a*b)) #Calculate and output the product of the two
constants.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 5


TensorFlow Programming Basics
Experiment Guide 2 Session

2.2.5 Experimental Results


Output:
Add: 7
Multiply: 12

2.3 Instance Description


Use the with session syntax to create a session. Calculate and output the sum and product of
two constants (3 and 4).

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 6


TensorFlow Programming Basics
Experiment Guide 3 Matrix Multiplication

3 Matrix Multiplication

3.1 Introduction to the Experiment


3.1.1 About the Experiment
This experiment introduces the tensor of TensorFlow to help you understand the use of matrix
multiplication functions.

3.1.2 Objectives of the Experiment


 Understand the tensor of TensorFlow.
 Understand how to use TensorFlow to multiply matrixes.

3.1.3 Experiment Content


In TensorFlow, you can use the tf.matmul() command to multiply matrixes. However, it
cannot be used to multiply a matrix by a vector. This experiment introduces the matrix
multiplication of TensorFlow.

3.1.4 Experimental Operations


Step 1 Log in to Hicloud.
Step 2 Click Console in the upper right corner.
Step 3 Select an ECS. Applicable operations of the ECS are displayed. Select Remote Login to log
in to the ECS.
Step 4 Input the instruction ll to view files in the current directory.
Step 5 Run the vi matmul.py command to create a Python script.
Step 6 Run the i command to enable the edit mode. Enter script contents.
Step 7 Run the :wq! command. Save and exit.
Step 8 Run the cat matmul.py command to view the code.
Step 9 Run the test.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 7


TensorFlow Programming Basics
Experiment Guide 3 Matrix Multiplication

Run the python3 matmul.py command.


----End

3.2 Experimental Process


3.2.1 Setting Encoding Declarations
# -*- coding: utf-8 -*-

3.2.2 Importing a Module


import tensorflow as tf

3.2.3 Starting a TensorFlow Default Session


#Tensorflow default session.
sess = tf.InteractiveSession()

3.2.4 Creating Matrix Variables


#Create two matrix variables w1 and w2.
#tf.random_normal(shape,
# mean=0.0,
# stddev=1.0,
# dtype=dtypes.float32,
# seed=None,
# name=None)
#A random normal distribution is generated.
#shape stands for the matrix dimension. For example,
#tf.random_normal([2,3],mean=1.0, stddev=1.0) is a matrix of two rows and three columns.
#mean stands for an average. Its default value is 0.0. stddev stands for standard deviation. Its
default value is 1.0.
#seed stands for random seed. Its default value is None.

w1 = tf.Variable(tf.random_normal([2,3],mean=1.0, stddev=1.0))
w2 = tf.Variable(tf.random_normal([3,1],mean=1.0, stddev=1.0))

3.2.5 Defining a Constant Matrix


#Define a two-dimension constant matrix (not a one-dimensional array).
x = tf.constant([[0.7, 0.9]])

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 8


TensorFlow Programming Basics
Experiment Guide 3 Matrix Multiplication

3.2.6 Initializing Global Variables


#Initialize global variables. This step initializes variables w1 and w2 only as these two
variables have not been initialized (tensors of them have been defined in previous steps).
tf.global_variables_initializer().run()

3.2.7 Matrix Multiplication


#Multiply matrixes a= x*w1 For information about matrix multiplication, see linear algebra.
a = tf.matmul(x ,w1)
#Multiply matrixes y=a*w2
y = tf.matmul(a, w2)
#The output computation result is a two-dimension matrix of one row and one column.
print(y.eval())

3.2.8 Experimental Results


Output:
[[4.263941]]

3.3 Instance Description


Use the with session syntax to create a session and multiply matrixes in the session.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 9


TensorFlow Programming Basics
Experiment Guide 4 Definition of Variables

4 Definition of Variables

4.1 Introduction to the Experiment


4.1.1 About the Experiment
In this experiment, you can understand the use of variables by creating variables (nodes) with
the tf.Variable function.

4.1.2 Objectives of the Experiment


 Understand the tf.Variable and the get_variable functions. Use the get_variable function
to get variables.
 Understand the difference between the tf.Variable and the get_variable functions.

4.1.3 Experiment Content


In this experiment, you can understand the use of the tf.Variable and the get_variable
functions and the difference between them.

4.1.4 Experimental Operations


Step 1 Log in to Hicloud.
Step 2 Click Console in the upper right corner.
Step 3 Select an ECS. Applicable operations of the ECS are displayed. Select Remote Login to log
in to the ECS.
Step 4 Input the instruction ll to view files in the current directory.
Step 5 Run the vi get_variable.py command to create a Python script.
Step 6 Run the i command to enable the edit mode. Enter script contents.
Step 7 Run the :wq! command. Save and exit.
Step 8 Run the cat get_variable.py command to view the code.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 10


TensorFlow Programming Basics
Experiment Guide 4 Definition of Variables

Step 9 Run the test.


Run the python3 get_variable.py command.
----End

4.2 Experimental Process


4.2.1 Setting Encoding Declarations
# -*- coding: utf-8 -*-

4.2.2 Importing a Module


import tensorflow as tf

4.2.3 Resetting the Computation Graph


tf.reset_default_graph()

4.2.4 Defining Variables


var1 = tf.Variable(10.0 , name="varname")
var2 = tf.Variable(11.0 , name="varname")
var3 = tf.Variable(12.0 )
var4 = tf.Variable(13.0 )

4.2.5 Defining get_variable variables


with tf.variable_scope("test1" ):
var5 = tf.get_variable("varname",shape=[2],dtype=tf.float32)

with tf.variable_scope("test2"):
var6 = tf.get_variable("varname",shape=[2],dtype=tf.float32)

4.2.6 Outputting Variables


print("var1:",var1.name) #print variable 1
print("var2:",var2.name) #print variable 2
print("var3:",var3.name) #print variable 3
print("var4:",var4.name) #print variable 4
print("var5:",var5.name) #print get_variable 1
print("var6:",var6.name) #print get_variable 2

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 11


TensorFlow Programming Basics
Experiment Guide 4 Definition of Variables

4.2.7 Experimental Results


Output:
var1: varname:0
var2: varname_1:0
var3: Variable:0
var4: Variable_1:0
var5: test1/varname:0
var6: test2/varname:0

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 12


TensorFlow Programming Basics
Experiment Guide 5 Visualization of TensorBoard

5 Visualization of TensorBoard

5.1 Introduction to the Experiment


5.1.1 About the Experiment
This experiment shows the visualization of TensorBoard.

5.1.2 Objectives of the Experiment


Understand the virtualization tool TensorBoard.

5.1.3 Experiment Content


TensorFlow provides a virtualization tool, that is, TensorBoard. This tool can show receipt
data of the training process, including the scalar, picture, audio, computation graph, data
distribution, column diagram, and embedded vector. You can observe the model structure and
parameter variation in the training on the website. TensorBoard is a log display system. When
you run a graph in a session, it aggregates all types of data needs and outputs them into log
files. Enable TensorBoard to read the log files and enable the 6060 port to provide web
services so that users can view data via a browser.

5.1.4 Experimental Operations


Step 1 Log in to Hicloud.
Step 2 Click Console in the upper right corner.
Step 3 Select an ECS. Applicable operations of the ECS are displayed. Select Remote Login to log
in to the ECS.
Step 4 Input the instruction ll to view files in the current directory.
Step 5 Run the vi get_tensorboard.py command to create a Python script.
Step 6 Run the i command to enable the edit mode. Enter script contents.
Step 7 Run the :wq! command. Save and exit.
Step 8 Run the cat tensorboard.py command to view the code.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 13


TensorFlow Programming Basics
Experiment Guide 5 Visualization of TensorBoard

Step 9 Run the test.


Run the python3 tensorboard.py command.
----End

5.2 Experimental Process


5.2.1 Setting Encoding Declarations
# -*- coding: utf-8 -*-

5.2.2 Importing a Module


import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

plotdata = { "batchsize":[], "loss":[] }


def moving_average(a, w=10):
if len(a) < w:
return a[:]
return [val if idx < w else sum(a[(idx-w):idx])/w for idx, val in enumerate(a)]

5.2.3 Generating Analog Data


# Generate analog data.
train_X = np.linspace(-1, 1, 100)
train_Y = 2*train_X + np.random.randn(*train_X.shape)*0.3 # y=2x, but some noise is
added.

5.2.4 Resetting the Computation Graph


#Display the graph.
# plt.plot(train_X, train_Y, 'ro', label='Original data')
# plt.legend()
plt.show()

tf.reset_default_graph()

5.2.5 Creating a Model


#Create a model.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 14


TensorFlow Programming Basics
Experiment Guide 5 Visualization of TensorBoard

#Placeholders
X = tf.placeholder("float")
Y = tf.placeholder("float")

#Model parameters
W = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.zeros([1]), name="bias")

5.2.6 Creating a Forward Structure


#A forward structure
z = tf.multiply(X, W)+ b
tf.summary.histogram('z',z)#Display predicted values in a histogram.

5.2.7 Reverse Optimization


#Reverse optimization
cost =tf.reduce_mean( tf.square(Y - z))
tf.summary.scalar('loss_function', cost)#Show the loss by scalars.
learning_rate = 0.01
#Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

5.2.8 Initializing Variables


#Initialize variables.
init = tf.global_variables_initializer()

#Set parameters.
training_epochs = 20
display_step = 2

5.2.9 Starting a Session


#Start a session.
with tf.Session() as sess:
sess.run(init)
#Merge all summaries.
merged_summary_op = tf.summary.merge_all()
#Create summary writer for file writing.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 15


TensorFlow Programming Basics
Experiment Guide 5 Visualization of TensorBoard

summary_writer = tf.summary.FileWriter('log/mnist_with_summaries',sess.graph)

5.2.10 Writing Data to the Model


#Write data to the model.
for epoch in range(training_epochs):
for (x, y) in zip(train_X, train_Y):
sess.run(optimizer, feed_dict={X: x, Y: y})

#Generate a summary.
summary_str = sess.run(merged_summary_op,feed_dict={X: x, Y: y});
summary_writer.add_summary(summary_str, epoch);#Write summary to files.

#Display all detailed information of training.


if epoch % display_step == 0:
loss = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
print ("Epoch:", epoch+1, "cost=", loss,"W=", sess.run(W), "b=", sess.run(b))
if not (loss == "NA" ):
plotdata["batchsize"].append(epoch)
plotdata["loss"].append(loss)

print (" Finished!")


print ("cost=", sess.run(cost, feed_dict={X: train_X, Y: train_Y}), "W=", sess.run(W),
"b=", sess.run(b))
print ("cost:",cost.eval({X: train_X, Y: train_Y}))

5.2.11 Graphical Display


Visualized 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()

plotdata["avgloss"] = moving_average(plotdata["loss"])
plt.figure(1)
plt.subplot(211)

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 16


TensorFlow Programming Basics
Experiment Guide 5 Visualization of TensorBoard

plt.plot(plotdata["batchsize"], plotdata["avgloss"], 'b--')


plt.xlabel('Minibatch number')
plt.ylabel('Loss')
plt.title('Minibatch run vs. Training loss')

plt.show()

Test on model results:


print("x=0.2, z=", sess.run(z, feed_dict={X: 0.2}))

5.2.12 Experimental Results


Epoch: 1 cost= 0.26367584 W= [1.4959534] b= [0.1584389]
Epoch: 3 cost= 0.11837075 W= [1.927753] b= [0.04088809]
Epoch: 5 cost= 0.10505076 W= [2.04464] b= [-0.00315647]
Epoch: 7 cost= 0.10374546 W= [2.074953] b= [-0.01477524]
Epoch: 9 cost= 0.10355354 W= [2.0827925] b= [-0.01778343]
Epoch: 11 cost= 0.10351367 W= [2.0848196] b= [-0.01856134]
Epoch: 13 cost= 0.10350403 W= [2.0853424] b= [-0.01876191]
Epoch: 15 cost= 0.10350155 W= [2.0854788] b= [-0.01881423]
Epoch: 17 cost= 0.10350095 W= [2.0855126] b= [-0.01882721]
Epoch: 19 cost= 0.10350082 W= [2.0855196] b= [-0.01882991]
Finished!
cost= 0.1035008 W= [2.085521] b= [-0.01883046]

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 17


TensorFlow Programming Basics
Experiment Guide 5 Visualization of TensorBoard

Figure 5-1 Visualization of TensorBoard 1

Figure 5-2 Visualization of TensorBoard 2

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 18


TensorFlow Programming Basics
Experiment Guide 5 Visualization of TensorBoard

5.3 Instance Description


After running the code, the displayed result is the same as before, but there is a new folder log
under the generated directory. This folder has a sub-folder mnist_with_summaries, which
contains a file events.out.tfevents.1525835939. Click Start | Run. Enter cmd to launch a
command-line process. Enter the following commands in the upper path of the summary log:
tensorBoard -logdir D:\Project\HCIA-AI-ENG\log\mnist_with_summaries
The following result is displayed:
TensorBoard 1.8.0 at http://DWX536903:6006 (Press CTRL+C to quit)
Enter the above URL in Chrome browser. The interface shown in Figure 5-2 is displayed.
The Chrome browser is recommended.
To start TensorBoard by commands, you need to access the upper path of the log first.
Otherwise, you cannot find the created information on the website.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 19


TensorFlow Programming Basics
Experiment Guide 6 Data Reading and Processing

6 Data Reading and Processing

6.1 Introduction to the Experiment


6.1.1 About the Experiment
The official website of TensorFlow provides three ways of data reading, including providing
data, reading data from files, and preloading data. This experiment mainly adopts reading data
from files.

6.1.2 Objectives of the Experiment


Understand how TensorFlow reads data.
Understand how to read data from files.

6.1.3 Experiment Content


Preloading data and providing data apply to small amount of data. Running of mass data
consumes much memory. In this condition, it is advised to read data from files. TensorFlow
can read multiple types of files including CSV files, binary files, TFRecords files, and image
files. In this experiment, TensorFlow reads CSV files.

6.1.4 Experimental Operations


Step 1 Log in to Hicloud.
Step 2 Click Console in the upper right corner.
Step 3 Select an ECS. Applicable operations of the ECS are displayed. Select Remote Login to log
in to the ECS.
Step 4 Input the instruction ll to view files in the current directory.
Step 5 Run the vi dealdatas.py command to create a Python script.
Step 6 Run the i command to enable the edit mode. Enter script contents.
Step 7 Run the :wq! command. Save and exit.
Step 8 Run the cat dealdatas.py command to view the code.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 20


TensorFlow Programming Basics
Experiment Guide 6 Data Reading and Processing

Step 9 Run the test.


Run the python3 dealdatas.py command.
----End

6.2 Experimental Process


6.2.1 Setting Encoding Declarations
#coding:utf-8

6.2.2 Importing a Module


import tensorflow as tf

6.2.3 Reading Data from Files


filename_queue = tf.train.string_input_producer(["tf_read.csv"]) #Read existing data.
reader = tf.TextLineReader()
There are 30 rows of data as follows in the file:

-0.76 15.67 -0.12 15.67


-0.48 12.52 -0.06 12.51
1.33 9.11 0.12 9.1
-0.88 20.35 -0.18 20.36
-0.25 3.99 -0.01 3.99
-0.87 26.25 -0.23 26.25
-1.03 2.87 -0.03 2.87
-0.51 7.81 -0.04 7.81
-1.57 14.46 -0.23 14.46
-0.1 10.02 -0.01 10.02
-0.56 8.92 -0.05 8.92
-1.2 4.1 -0.05 4.1
-0.77 5.15 -0.04 5.15
-0.88 4.48 -0.04 4.48
-2.7 10.82 -0.3 10.82
-1.23 2.4 -0.03 2.4
-0.77 5.16 -0.04 5.15

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 21


TensorFlow Programming Basics
Experiment Guide 6 Data Reading and Processing

-0.76 15.67 -0.12 15.67


-0.81 6.15 -0.05 6.15
-0.6 5.01 -0.03 5
-1.25 4.75 -0.06 4.75
-2.53 7.31 -0.19 7.3
-1.15 16.39 -0.19 16.39
-1.7 5.19 -0.09 5.18
-0.62 3.23 -0.02 3.22
-0.74 17.43 -0.13 17.41
-0.77 15.41 -0.12 15.41
0 47 0 47.01
0.25 3.98 0.01 3.98
-1.1 9.01 -0.1 9.01
-1.02 3.87 -0.04 3.87

6.2.4 Getting Queue Values


#Get queue values.
key, value = reader.read(filename_queue)
#key represents the information of the read file and the number of rows.
#value represents the raw strings read by row, which are sent to the decoder for decoding.
#key represents the file information and the number of rows of the read data. value represents
raw strings.
record_defaults = [[1.], [1.], [1.], [1.]]#The data type here determines the type of data to be
read, which should be in the list form.
col1, col2, col3, col4 = tf.decode_csv(value, record_defaults=record_defaults)#Each parsed
attribute is a scalar with the rank value of 0.

features = tf.stack([col1, col2, col3])

6.2.5 Initializing Variables


init_op = tf.global_variables_initializer()
local_init_op = tf.local_variables_initializer()

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 22


TensorFlow Programming Basics
Experiment Guide 6 Data Reading and Processing

6.2.6 Starting a Session


with tf.Session() as sess:
#Start a session and perform initialization.
sess.run(init_op)
sess.run(local_init_op)

# Start populating the filename queue.Start a coordinator.


coord = tf.train.Coordinator()
#Run the start_queue_runners to feed the queue.
threads = tf.train.start_queue_runners(coord=coord)
#
try:
for i in range(30):
example, label = sess.run([features, col4])
print(example)
# print(label)
except tf.errors.OutOfRangeError:
print('Done !!!')
#This exception appears when TensorFlow reads to the end of the file queue.
finally: #Coordinator coord outputs signal for terminating all threads.
coord.request_stop()
print('all threads are asked to stop!')
coord.join(threads)
print('all threads are stopped!')

#Run the coord.request_stop () command to terminate all threads. Run the coord.join
(threads) to add threads to the main thread. Wait for the termination of threads.

6.2.7 Experimental Results


[-0.76 15.67 -0.12]
[-0.48 12.52 -0.06]
[1.33 9.11 0.12]
[-0.88 20.35 -0.18]
[-0.25 3.99 -0.01]

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 23


TensorFlow Programming Basics
Experiment Guide 6 Data Reading and Processing

[-0.87 26.25 -0.23]


[-1.03 2.87 -0.03]
[-0.51 7.81 -0.04]
[-1.57 14.46 -0.23]
[-1.000e-01 1.002e+01 -1.000e-02]
[-0.56 8.92 -0.05]
[-1.2 4.1 -0.05]
[-0.77 5.15 -0.04]
[-0.88 4.48 -0.04]
[-2.7 10.82 -0.3 ]
[-1.23 2.4 -0.03]
[-0.77 5.16 -0.04]
[-0.81 6.15 -0.05]
[-0.6 5.01 -0.03]
[-1.25 4.75 -0.06]
[-2.53 7.31 -0.19]
[-1.15 16.39 -0.19]
[-1.7 5.19 -0.09]
[-0.62 3.23 -0.02]
[-0.74 17.43 -0.13]
[-0.77 15.41 -0.12]
[ 0. 47. 0.]
[0.25 3.98 0.01]
[-1.1 9.01 -0.1 ]
[-1.02 3.87 -0.04]

6.3 Instance Description


This experiment selects functions based on the file type. To read data from CSV files, execute
the TextLineReader and decode_csv operations. This experiment realizes data reading and
processing by file operations.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 24


TensorFlow Programming Basics
Experiment Guide 7 Graphic Operation

7 Graphic Operation

7.1 Introduction to the Experiment


7.1.1 About the Experiment
This experiment introduces operations of computation graph via several instances.

7.1.2 Objectives of the Experiment


Understand graphic operations.

7.1.3 Experiment Content


This experiment demonstrates how to create a graph and set it to the default graph. It uses the
get_default_graph() function to get the current default graph and verifies the validity of its
configurations.
This experiment shows how to get graph contents.

7.1.4 Experimental Operations


Step 1 Log in to Hicloud.
Step 2 Click Console in the upper right corner.
Step 3 Select an ECS. Applicable operations of the ECS are displayed. Select Remote Login to log
in to the ECS.
Step 4 Input the instruction ll to view files in the current directory.
Step 5 Run the vi graph.py command to create a Python script.
Step 6 Run the i command to enable the edit mode. Enter script contents.
Step 7 Run the :wq! command. Save and exit.
Step 8 Run the cat graph.py command to view the code.
Step 9 Run the test. Run the python3 graph.py command.
----End

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 25


TensorFlow Programming Basics
Experiment Guide 7 Graphic Operation

7.2 Experimental Process


# -*- coding: utf-8 -*-
import numpy as np
import tensorflow as tf

7.2.1 Creating a Graph


# 1 The way of creating a graph
c = tf.constant(0.0)

g = tf.Graph()
with g.as_default():
c1 = tf.constant(0.0)
print(c1.graph)
print(g)
print(c.graph)

g2 = tf.get_default_graph()
print(g2)

tf.reset_default_graph()
g3 = tf.get_default_graph()
print(g3)

7.2.2 Getting the Tensor


# 2. Get the tensor.
print(c1.name)
t = g.get_tensor_by_name(name = "Const:0")
print(t)

7.2.3 Getting an Operation


# 3 Get an operation.
a = tf.constant([[1.0, 2.0]])
b = tf.constant([[1.0], [3.0]])

tensor1 = tf.matmul(a, b, name='exampleop')

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 26


TensorFlow Programming Basics
Experiment Guide 7 Graphic Operation

print(tensor1.name,tensor1)
test = g3.get_tensor_by_name("exampleop:0")
print(test)

print(tensor1.op.name)
testop = g3.get_operation_by_name("exampleop")
print(testop)

with tf.Session() as sess:


test = sess.run(test)
print(test)
test = tf.get_default_graph().get_tensor_by_name("exampleop:0")
print (test)

7.2.4 Getting All Lists


# 4 Get all lists.
#Return the list of operating nodes in the graph.
tt2 = g.get_operations()
print(tt2)

7.2.5 Getting an Object


# 5 Get an object.
tt3 = g.as_graph_element(c1)
print(tt3)

7.2.6 Experimental Results


<tensorflow.python.framework.ops.Graph object at 0x0000000009B17940>
<tensorflow.python.framework.ops.Graph object at 0x0000000009B17940>
<tensorflow.python.framework.ops.Graph object at 0x0000000002892DD8>
<tensorflow.python.framework.ops.Graph object at 0x0000000002892DD8>
<tensorflow.python.framework.ops.Graph object at 0x0000000009B17A90>
Const:0
Tensor("Const:0", shape=(), dtype=float32)
exampleop:0 Tensor("exampleop:0", shape=(1, 1), dtype=float32)
Tensor("exampleop:0", shape=(1, 1), dtype=float32)

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 27


TensorFlow Programming Basics
Experiment Guide 7 Graphic Operation

exampleop
name: "exampleop"
op: "MatMul"
input: "Const"
input: "Const_1"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "transpose_a"
value {
b: false
}
}
attr {
key: "transpose_b"
value {
b: false
}
}

2018-05-11 15:43:16.483655: I
T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU
suppo
rts instructions that this TensorFlow binary was not compiled to use: AVX2
[[7.]]
Tensor("exampleop:0", shape=(1, 1), dtype=float32)
[<tf.Operation 'Const' type=Const>]
Tensor("Const:0", shape=(), dtype=float32)
________________________

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 28


TensorFlow Programming Basics
Experiment Guide 7 Graphic Operation

7.3 Instance Description


To use the tf.reset_default_graph function, ensure that all the current graphs have been
released. Otherwise, the system may report an error. You can find the corresponding elements
by names. get_tensor_by_name allows you to get tensors in the graph.
get_operation_by_name allows you to get node operations.
get_operations allows you to get the list of elements.
The tf.Graph.as_graph_element() function allows you to get elements by objects. After
importing an object, you can get a tensor or an OP back. The as_graph_element function gets
the real tensor object of c1 and assigns it to variable tt3.
This experiment only introduces basic graphic operations.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 29


TensorFlow Programming Basics
Experiment Guide 8 Save and Use of Models

8 Save and Use of Models

8.1 Introduction to the Experiment


8.1.1 About the Experiment
This experiment introduces how to save models and use the saved models. Generally, all
trained models should be saved.

8.1.2 Objectives of the Experiment


Understand how to save models.
Understand how to load and use models.

8.1.3 Experiment Content


This experiment adds functions of saving and loading models based on previous experiments.
After generating analog data in the file, add filling value of graph variables. Define the saver
and the saving path before starting a session and save the model after the training in the
session.

8.1.4 Experimental Operations


Step 1 Log in to Hicloud.
Step 2 Click Console in the upper right corner.
Step 3 Select an ECS. Applicable operations of the ECS are displayed. Select Remote Login to log
in to the ECS.
Step 4 Input the instruction ll to view files in the current directory.
Step 5 Run the vi mnist_train.py command to create a Python script.
Step 6 Run the i command to enable the edit mode. Enter script contents.
Step 7 Run the :wq! command. Save and exit.
Step 8 Run the cat mnist_train.py command to view the code.
Step 9 Run the test. Run the python3 mnist_train.py command.
----End

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 30


TensorFlow Programming Basics
Experiment Guide 8 Save and Use of Models

8.2 Experimental Process


8.2.1 Importing a Dataset
# -*- coding: utf-8 -*-
#!/usr/bin/env python

#Import the mnist database.


from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

import tensorflow as tf
import os

8.2.2 Defining Variables


#Define an input variable.
x = tf.placeholder(tf.float32, [None, 784])

#Define parameters.
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

#Define an excitation function.


y = tf.nn.softmax(tf.matmul(x, W) + b)

#Define an output variable.


y_ = tf.placeholder(tf.float32, [None, 10])

#Define a cost function.


cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

#Define an optimization function.


train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

8.2.3 Initializing Variables


#Initialize variables.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 31


TensorFlow Programming Basics
Experiment Guide 8 Save and Use of Models

init = tf.global_variables_initializer()

#Define a session.
sess = tf.Session()

8.2.4 Running a Session


#Initialize a session.
sess.run(init)

#Define the saver of the model.


saver = tf.train.Saver()

#Perform 1,000 rounds of training.


for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_:batch_ys})

print("Training finished!")

8.2.5 Creating a Saving Directory for the Model


#Create a saving directory for the model.
model_dir = "mnist_model"
model_name = "ckp"
if not os.path.exists(model_dir):
os.mkdir(model_dir)

8.2.6 Saving the Model


#Save the model.
saver.save(sess, os.path.join(model_dir, model_name))

print("The model is saved!")

8.2.7 Experimental Results


Training finished!
The model is saved!

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 32


TensorFlow Programming Basics
Experiment Guide 8 Save and Use of Models

8.3 Using the Model


8.3.1 Importing a Dataset
# -*- coding: utf-8 -*-
#!/usr/bin/env python

#Import the mnist database.


from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("MNIST_data",one_hot=True)

import tensorflow as tf

8.3.2 Creating a Session


#Create a session.
sess = tf.Session()

8.3.3 Defining Variables


#Define an input variable.
x = tf.placeholder(tf.float32, [None, 784])

#Define parameters.
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

#Define a model and an excitation function.


y = tf.nn.softmax(tf.matmul(x, W) + b)

#Define the saver of the model.


saver = tf.train.Saver([W, b])

8.3.4 Restoring the Model


#Restore the model.
saver.restore(sess, "mnist/ckp")

print("The model is restored!")

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 33


TensorFlow Programming Basics
Experiment Guide 8 Save and Use of Models

#Fetch a test image.


idx=0
img = mnist.test.images[idx]

8.3.5 Computing Results


#Compute results based on the model.
ret = sess.run(y, feed_dict = {x : img.reshape(1, 784)})

print("The model result is computed!")

#Display the test results.


print("predicted result: %d"%(ret.argmax()))
print("actual result: %d"%(mnist.test.labels[idx].argmax()))

8.3.6 Experimental Results


The model is restored!
The model result is computed!
Predicted result: 7
Actual result: 7

8.4 Instance Description


This experiment saves models to facilitate later loading and use.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 34


TensorFlow Programming Basics 9 Linear Regression:
Experiment Guide Housing Price Forecast

9 Linear Regression:
Housing Price Forecast

9.1 Introduction to the Experiment


9.1.1 About the Experiment
This experiment is an instance of TensorFlow linear regression, that is, housing price forecast.

9.1.2 Objectives of the Experiment


Understand the linear regression.
Understand how to use TensorFlow to make a prediction.

9.1.3 Experiment Content


This experiment takes an instance to introduce how to use linear regression to predict the
housing price and how to make such a prediction in TensorFlow. Commonly, the dataset of
Boston housing price is used to predict the housing price. This experiment uses the dataset of
Beijing housing price, which is more applicable to China.

9.1.4 Experimental Operations


Step 1 Log in to Hicloud.
Step 2 Click Console in the upper right corner.
Step 3 Select an ECS. Applicable operations of the ECS are displayed. Select Remote Login to log
in to the ECS.
Step 4 Input the instruction ll to view files in the current directory.
Step 5 Run the vi house_price.py command to create a Python script.
Step 6 Run the i command to enable the edit mode. Enter script contents.
Step 7 Run the :wq! command. Save and exit.
Step 8 Run the cat house_price.py command to view the code.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 35


TensorFlow Programming Basics 9 Linear Regression:
Experiment Guide Housing Price Forecast

Step 9 Run the test. Run the python3 house_price.py command.


----End

9.2 Experimental Process


9.2.1 Setting Encoding Declarations
# coding:utf-8

9.2.2 Importing a Module


#Load the database required for this project.
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

9.2.3 Importing Data


The data in this experiment comes from the bj_housing2.csv file under the
https://github.com/cunxi1992/boston_housing directory.
Read data:
train = pd.read_csv("bj_housing2.csv")

9.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 parameters and set the learning rate.


learning_rate = 2
#Set the times of training.
training_epochs = 1000
#Set the frequency of display.
display_step = 50

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 36


TensorFlow Programming Basics 9 Linear Regression:
Experiment Guide Housing Price Forecast

9.2.5 Defining Placeholders


#Define the X and Y placeholders.
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

#Use learning parameters defined by variables.


W = tf.Variable(np.random.randn(), name="weight", dtype=tf.float32)
b = tf.Variable(np.random.randn(), name="bias", dtype=tf.float32)

9.2.6 Creating a Structure of Forward Propagation


#Create a structure of forward propagation.
pred = tf.add(tf.multiply(W, X), b)
#Loss function
cost = tf.reduce_sum(tf.pow(pred-Y, 2)) / (2 * n_samples)
#Use the gradient descent optimizer.
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

9.2.7 Initializing Variables


#Enable Init.
init = tf.global_variables_initializer()

#Start a session and initialize variables.


with tf.Session() as sess:
sess.run(init)

9.2.8 Enabling Circulation


#Enable circulation to start training.
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 all detailed information of 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)) #Display all detailed information of training.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 37


TensorFlow Programming Basics 9 Linear Regression:
Experiment Guide Housing Price Forecast

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')

9.2.9 Displaying Training Results


#Display 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()

9.2.10 Experimental 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
...

9.3 Instance Description


This experiment uses the dataset of Beijing housing price to predict the housing price of
Beijing and realize the application of TensorFlow linear regression.

Issue 1.0 (2018-09-30) Copyright © Huawei Technologies Co., Ltd. 38

You might also like