HCIA-AI V1.0 Lab Guide

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

HCIA-AI Huawei Certification Course

HCIA-AI
Development Environment
Establishment
Experimental Guide
Version: 1.0

Huawei Technologies Co., Ltd.

1
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

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. i


Introduction to Huawei Certification System
Based on cutting-edge technologies and professional training systems, Huawei certification
meets the diverse AI technology demands of various 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-08-02) Copyright © Huawei Technologies Co., Ltd. ii


Development Environment Establishment
Experimental 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. After
understanding this document, you can perform simple Python basic programming, laying a
solid foundation for later AI development.

Contents
This experimental guide includes five experiments, covering AI environment setup, and is
intended to help trainees and readers easily develop capabilities of developing AI.
Experiment 1 — Introduction to the AI experiment environment: Understand the software and
framework required for establishing an HCIA-AI environment.
Experiment 2 — How to apply for and activate Huawei cloud services: Understand Huawei
Cloud service platform, register with the platform, and get familiar with related operations.
Experiment 3 — Environment establishment: Install related software modules, such as Python,
pip, NumPy, Pandas, and TensorFlow.
Experiment 4 — Test cases: Understand how to compile scripts in real time on the Elastic
Cloud Server (ECS) or download existing scripts from GitHub for testing.
Experiment 5 — AI experiment environment configuration: Understand the installation and
deployment of Gnome and anaconda.

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 basic Python language programming capabilities;
Have a basic understanding of data structures;
Have a basic understanding of AI.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. iii


Development Environment Establishment
Experimental Guide Preface

Experimental Environment
Python Development Tool
This experiment environment is developed and compiled based on the Python 3.6.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. iv


Development Environment Establishment
Experimental Guide Contents

Contents

Preface......................................................................................................................................... iii
1 Introduction to HCIA-AI Experiment Environment ........................................................... 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 Required Software and Frameworks .................................................................................................................... 1
1.2 Experimental Tasks................................................................................................................................................ 2
1.2.1 Experiment Guideline ......................................................................................................................................... 2
1.2.2 Experiment Procedure......................................................................................................................................... 2
1.3 Result Verification ................................................................................................................................................. 2
1.3.1 Viewing Information About the Huawei Cloud Environment ............................................................................... 2
1.3.2 Viewing Configuration Information About the Experiment Environment ............................................................. 3
1.4 Questions .............................................................................................................................................................. 3

2 Applying for and Activating Huawei Cloud Services ........................................................ 4


2.1 Cloud Service Configuration List ........................................................................................................................... 4
2.2 Account Registration ............................................................................................................................................. 4
2.2.1 Account Function................................................................................................................................................ 4
2.2.2 Registration Procedure ........................................................................................................................................ 5
2.3 Applying for and Activating Huawei Cloud Services .............................................................................................. 8
2.3.1 Application Guideline ......................................................................................................................................... 8
2.3.2 Procedure ........................................................................................................................................................... 8
2.4 Elastic Cloud Server Operations............................................................................................................................13
2.4.1 Operation Guideline...........................................................................................................................................13
2.4.2 Procedure ..........................................................................................................................................................13

3 Environment Establishment and Configuration ................................................................ 16


3.1 Installing Python 3.6 .............................................................................................................................................16
3.1.1 Viewing the Original Version .............................................................................................................................16
3.1.2 Installing Python 3.6 ..........................................................................................................................................16
3.2 Installing the pip ...................................................................................................................................................22
3.2.1 Installation Description ......................................................................................................................................22
3.2.2 Installation Procedure ........................................................................................................................................23
3.3 Installing the NumPy ............................................................................................................................................24

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. v


Development Environment Establishment
Experimental Guide Contents

3.3.1 Introduction to NumPy ......................................................................................................................................24


3.3.2 Installation Procedure ........................................................................................................................................24
3.4 Installing the Pandas .............................................................................................................................................25
3.4.1 Introduction to Pandas .......................................................................................................................................25
3.4.2 Installation Procedure ........................................................................................................................................25
3.5 Installing TensorFlow ...........................................................................................................................................26
3.5.1 Introduction to TensorFlow ................................................................................................................................26
3.5.2 Installation Procedure ........................................................................................................................................26

4 Case Test .................................................................................................................................. 29


4.1 Compiling Scripts in Real Time ............................................................................................................................29
4.1.1 Test Guideline....................................................................................................................................................29
4.1.2 Test Process .......................................................................................................................................................29
4.2 Downloading an Existing Script ............................................................................................................................31
4.2.1 Test Guideline....................................................................................................................................................31
4.2.2 Test Process .......................................................................................................................................................31

5 Other Configurations............................................................................................................. 34
5.1 GUI-based GNOME Installation ...........................................................................................................................34
5.1.1 Introduction to GNOME ....................................................................................................................................34
5.1.2 GNOME Installation ..........................................................................................................................................34
5.2 anaconda Installation ............................................................................................................................................37
5.2.1 Introduction to anaconda ....................................................................................................................................37
5.2.2 anaconda Installation .........................................................................................................................................37

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. vi


Development Environment Establishment
Experimental Guide 1 Introduction to HCIA-AI Experiment Environment

1 Introduction to HCIA-AI Experiment


Environment

1.1 Introduction to the Experiment


1.1.1 About the Experiment
The AI development environment is established by applying for required resources from
Huawei Cloud platform. To establish such an environment, you need to apply for and activate
the Elastic Cloud Server (ECS), establish and configure the environment, and perform case
test and other configurations.

1.1.2 Objectives of the Experiment


 Understand the details about the AI development environment.
 Establish a complete AI development environment on your own.
 Perform function tests based on the established environment.

1.1.3 Required Software and Frameworks


Python 3.6.4
pip 9.0.1
NumPy
Pandas
TensorFlow
Vim
Gnome
anaconda
Git

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 1


Development Environment Establishment
Experimental Guide 1 Introduction to HCIA-AI Experiment Environment

1.2 Experimental Tasks


1.2.1 Experiment Guideline
1. Establish a basic development environment based on Huawei Cloud platform, and master
the configurations required for the development environment.
2. The development environment includes the computing module, development module,
and framework module. Establish and configure a complete AI development
environment based on the basic development requirements.

1.2.2 Experiment Procedure


Step 1 Apply for and activate a Huawei elastic cloud service.
Activate the environment based on the elastic cloud service provided by Huawei public cloud,
providing a basis for establishing the AI development environment.
Step 2 Install and configure the environment.
Install and configure the software, frameworks, and modules related to AI development.
Step 3 Perform case test.
Perform a demo test based on the established AI development environment to verify whether
the established experiment environment is valid.
Step 4 Perform other configurations.
Install and configure other resources related to the experiment environment to facilitate
follow-up experiment development and test.
----End

1.3 Result Verification


1.3.1 Viewing Information About the Huawei Cloud Environment
After applying for and activating the cloud service, click the ECS name to view related
configuration information. The following page is displayed.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 2


Development Environment Establishment
Experimental Guide 1 Introduction to HCIA-AI Experiment Environment

Figure 1-1 Huawei ESC configuration information

1.3.2 Viewing Configuration Information About the Experiment


Environment
View information about the software, frameworks, and resources configured for the
environment.

1.4 Questions
When you apply for an elastic cloud service, which server is suitable for calculating
traditional machine learning iterative tasks, and which server is suitable for deep learning
tasks?
When establishing the environment, how can you install the Python framework without using
pip?
After the experimental tasks are completed and the server is shut down, are the occupied
resources, including the storage, IP address, and bandwidth, continue to be charged?

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 3


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

2 Applying for and Activating Huawei


Cloud Services

2.1 Cloud Service Configuration List


The cloud service configuration list is as follows:
Server type
CPU cores
Memory size
Image
Image version
Disk
Virtual private cloud
Security group
Network adapter
Elastic IP address
Bandwidth

2.2 Account Registration


2.2.1 Account Function
1. Activates corresponding services on the Huawei Cloud official website to ensure that the
experiments can be carried out smoothly.
2. Enables the users to experience various convenient and cost-effective Huawei cloud
services, including deep learning, machine learning, image recognition, speech
recognition, and natural language processing (NLP).

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 4


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

2.2.2 Registration Procedure


Step 1 Log in to Huawei Cloud official website.
Visit https://www.huaweicloud.com/en-us/. The following page is displayed.

Figure 2-1 Home page of Huawei Cloud

Step 2 Click Register in the upper right corner on the home page.
Click Register in the upper right corner to register an account. The following page is
displayed.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 5


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

Figure 2-2 Huawei Cloud account registration page

Step 3 Perform real-name authentication.


Choose Real-Name Auth > Individual.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 6


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

Figure 2-3 Account type authentication

Choose Individual Bank Card Authentication.

Figure 2-4 Real-name authentication mode

Fill in related information and click Submit for Review.

Figure 2-5 Filling in the bank card authentication information

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 7


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

Then, the real-name authentication is complete.


----End

2.3 Applying for and Activating Huawei Cloud Services


2.3.1 Application Guideline
1. Huawei Cloud provides a large number of cloud services. Users can apply for cloud
resources based on their own tasks and requirements.
2. You can apply for cloud services and establish an environment as promoted on the
corresponding pages.

2.3.2 Procedure
Step 1 Log in to Huawei Cloud official website.
Visit https://www.huaweicloud.com/en-us/ and choose Products > Computing > Elastic
Cloud Server. The following page is displayed.

Figure 2-6 Huawei Cloud products

Step 2 Purchase a server.


Click Buy Now to apply for and purchase a cloud service.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 8


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

Figure 2-7 Elastic Cloud Server page

Step 3 Configure the cloud service.


Go to the page for applying for cloud services and configure the service.

Figure 2-8 Elastic Cloud Server configuration page

Set Billing Mode to Pay-per-use.


Do not change the current setting of Region.
Do not change the current setting of AZ.
Set Type to General computing.
Select 2vCPUs.
Select 4G.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 9


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

Figure 2-9 Elastic Cloud Server type

Set Type to General computing.


Select CentOS and CentOS 7.4 64bit (40GB) for the image.
Select High I/O and 100 GB for System Disk.
Do not change the current setting of VPC.
Do not change the current setting of Security Group.

Figure 2-10 Elastic Cloud Server image and hard disk

Do not change the current setting of NIC.


Do not change the current setting of the elastic IP address.
Enter Static BGP.
Set Billing Mode to Pay-per-use.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 10


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

Set Bandwidth to 10 Mbit/s.


Set Login Mode to Password.
Set Advanced Settings to Not Required.
Set ECS Name to esc-HCIA-AI.
Enter the password and click Buy Now.

Figure 2-11 Elastic Cloud Server

Select I have read and agree to Huawei Image Disclaimer and click Submit.

Figure 2-12 Buying an Elastic Cloud Server

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 11


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

Step 4 Generate a cloud service.

Figure 2-13 Generating an elastic cloud server

You can click Start, Stop, Restart, Delete, Remote Login, or More to perform related
operations on the service you applied for. Click Remote Login to go to the application
environment.

Figure 2-14 Logging in to the Elastic Cloud Server

Enter the account and password. The following page is displayed.

Figure 2-15 Elastic Cloud Server environment

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 12


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

Then, the Elastic Cloud Server application is complete.


----End

2.4 Elastic Cloud Server Operations


2.4.1 Operation Guideline
1. Huawei ECS services are charged although they are not in use. Therefore, trainees need
to disable related services in the non-experiment phase to prevent extra fees.
2. The operations include start, stop, restart, and delete.

2.4.2 Procedure
Step 1 Log in to Huawei Cloud official website.
Visit http://www.huaweicloud.com/. The following page is displayed.

Figure 2-16 Home page of Huawei Cloud

Click Console in the upper right corner of the home page. The following page is displayed.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 13


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

Figure 2-17 Elastic Cloud Service console

Choose Elastic Cloud Server. On the page that is displayed, select an elastic cloud server, as
shown in the following figure.

Figure 2-18 Elastic Cloud Server operation page

Click Delete, select "Release the EIPs bound to the following ECSs" and "Delete the data
disks attached to the following ECSs", and click OK to delete an elastic cloud server. The
operation page is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 14


Development Environment Establishment
Experimental Guide 2 Applying for and Activating Huawei Cloud Services

Figure 2-19 Elastic Cloud Server deletion page

Operations such as start, stop, and restart on other servers are similar.
----End

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 15


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

3 Environment Establishment and


Configuration

3.1 Installing Python 3.6


Python 2.7.5 is the original version installed for the CentOS. However, Python 3.6 is used in
this course after comprehensive consideration. Therefore, you need to install Python 3.6 in the
CentOS system.

3.1.1 Viewing the Original Version


Enter python in the command line to view the current version. The result is as follows.

Figure 3-1 Checking the original python version

Press Ctrl+Z to exit the python3 environment.

3.1.2 Installing Python 3.6


Step 1 Install the dependency packages that may be used.
Enter yum -y groupinstall "Development tools" to install the dependency package. The
result is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 16


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

Figure 3-2 Installing a dependency package

Enter yum –y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel


sqlite-devel to install the dependency package. The result is as follows.

Figure 3-3 Installing a dependency package

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 17


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

Figure 3-4 Completing the dependency package installation

Step 2 Download the Python 3.6 installation package.


Go to the Python website, find the download path, and run the command wget
https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz to download the Python 3.6
installation package. The result is as follows.

Figure 3-5 Downloading the Python 3.6.4 installation package

Step 3 Create a folder.


Enter mkdir /usr/local/python3 to create a file folder and check the creation result. The
result is as follows.

Figure 3-6 Creating the python3 folder

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 18


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

Step 4 Decompress the Python 3.6.4 installation package.


Enter ll to check the installation package location. The result is as follows.

Figure 3-7 Checking the position of the Python installation package

Enter tar -xvJf Python-3.6.4.tar.xz to decompress the installation package. The result is as
follows.

Figure 3-8 Decompressing the installation package

Go to the folder where the decompressed file package is stored and check whether the
package is decompressed successfully.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 19


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

Figure 3-9 Location of the files in the decompressed installation package

Step 5 Configure the source code.


Enter ./configure --prefix=/usr/local/python3 to configure the source code.

Figure 3-10 Configuring the source code

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 20


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

Step 6 Compile and install the source code.


Enter make to compile the source code. The result is as follows.

Figure 3-11 Compiling the source code

Enter make install to compile the source code. The result is as follows.

Figure 3-12 Installing the source code

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 21


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

Step 7 Create a symbolic link.


Enter the following commands:
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

The operation is as follows.

Figure 3-13 Creating a symbolic link

Step 8 Test the Python version.


Enter python to test the old Python version.

Figure 3-14 Testing the old Python version

Press Ctrl+Z to exit the python3 environment.


Enter python3 to test the new Python version.

Figure 3-15 Testing the new Python version

Press Ctrl+Z to exit the python3 environment.


----End

3.2 Installing the pip


3.2.1 Installation Description
1. pip is a framework installation scheme that matches the Python. However, the CentOS7
is not configured with the pip. In this course, the pip is installed independently based on
the original system, facilitating the configuration of the experiment environment.
2. A Python framework can be installed in the original system even if the pip is not
installed. Therefore, the pip installation is not mandatory.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 22


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

3.2.2 Installation Procedure


Step 1 Install the pip.
Enter yum –y install python-pip. The result is as follows.

Figure 3-16 Installing the pip

Step 2 Upgrade the version.


Enter pip install --upgrade pip. The result is as follows.

Figure 3-17 Upgrading the pip version

Step 3 Perform pip test.


Enter pip3 list. The result is as follows.

Figure 3-18 Performing pip installation test

----End

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 23


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

3.3 Installing the NumPy


3.3.1 Introduction to NumPy
The NumPy framework is a numerical calculation and expansion framework of Python.
NumPy can be used to store and process large matrices, which is much more efficient than
Python's nested list structure.

3.3.2 Installation Procedure


Step 1 Install the NumPy.
Enter pip3 install numpy. The result is as follows.

Figure 3-19 Installing the NumPy

The installation might take a long time. You can enter nohup pip3 install numpy to install
the NumPy in the background. The result is as follows.

Figure 3-20 Installing the NumPy in the background

Step 2 Check the installation result.


Enter pip3 list. The result is as follows.

Figure 3-21 Performing NumPy installation test

----End

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 24


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

3.4 Installing the Pandas


3.4.1 Introduction to Pandas
Pandas, a data processing tool based on NumPy, is created to resolve data analysis tasks. It
incorporates a large number of libraries and some standard data models and provides tools
that can efficiently operate large data sets. It also provides a large number of functions and
methods that help us to quickly and conveniently process data.

3.4.2 Installation Procedure


Step 1 Install the Pandas.
Enter pip3 install pandas. The result is as follows.

Figure 3-22 Installing the Pandas

When the Pandas is installed, its dependent framework packages, including python-dateutil
and pytz, are automatically installed as well.
The installation might take a long time. You can enter nohup pip3 install pandas to install
the Pandas in the background. The result is as follows.

Figure 3-23 Installing the Pandas in the background

Step 2 Check the installation result.


Enter pip3 list. The result is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 25


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

Figure 3-24 Performing Pandas installation test

----End

3.5 Installing TensorFlow


3.5.1 Introduction to TensorFlow
TensorFlow is Google's second-generation artificial intelligence learning system based on
DistBelief and is an open source software library for numerical computation using data flow
graphs. Its name derives from its own operating principle. Tensor indicates the N-dimensional
array and Flow indicates the calculation based on the data graph. TensorFlow is a calculation
process of the Tensor from one end of the graph to the other end. The TensorFlow system
transmits complex data structures to the AI neural network for analysis and processing.
TensorFlow features high level machine learning computing, greatly simplifies the first
generation system, and has better flexibility and scalability. It supports distributed computing
of heterogeneous devices and can automatically run models on various platforms.

3.5.2 Installation Procedure


Step 1 Install TensorFlow.
Enter pip3 install tensorflow. The result is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 26


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

Figure 3-25 Installing the TensorFlow

The installation might take a long time. You can enter the command nohup pip3 install
tensorflow to install the TensorFlow in the background. The result is as follows.

Figure 3-26 Installing the TensorFlow in the background

Step 2 Check the installation result.


Enter pip3 list. The result is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 27


Development Environment Establishment
Experimental Guide 3 Environment Establishment and Configuration

Figure 3-27 Performing TensorFlow installation test

----End

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 28


Development Environment Establishment
Experimental Guide 4 Case Test

4 Case Test

4.1 Compiling Scripts in Real Time


4.1.1 Test Guideline
Perform the Python script test based on the established experiment environment and VIM
editor.

4.1.2 Test Process


Step 1 Open vim and create the Python script.
Enter vi tf.py. The operation is as follows.

Figure 4-1 Creating a Python script

The result is as follows.

Figure 4-2 Created Python script

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 29


Development Environment Establishment
Experimental Guide 4 Case Test

Step 2 Edit the script.


Enter i to edit the script. The result is as follows.

Figure 4-3 Python script editing mode

Enter the following script content:


-start-
#/usr/bin/env

import tensorflow as tf
import numpy as np

# Create two op constants to generate two matrices.


matrix1 = tf.constant(np.array([[1.,2.]]))
matrix2 = tf.constant(np.array([[2.],[2.]]))

# Create a matrix multiplication matmul.


product = tf.matmul(matrix1, matrix2)

# Start the default graph.


sess = tf.Session()

# Invoke run() of sess to perform matrix multiplication.


result = sess.run(product)
print(result)

# Close the session after the task is complete.

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


Development Environment Establishment
Experimental Guide 4 Case Test

sess.close()
-over-

Enter wq!, save the settings, and exit.


Step 3 View the script content.
Enter cat tf.py. The result is as follows.

Figure 4-4 Python script content

Step 4 Run the script for test.


Enter python3 tf.py. The result is as follows.

Figure 4-5 Running the Python script

----End

4.2 Downloading an Existing Script


4.2.1 Test Guideline
Perform the test by downloading the Python script from GitHub based on the preceding
experiment environment.

4.2.2 Test Process


Step 1 Install Git.
Enter yum –m install git. The operation is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 31


Development Environment Establishment
Experimental Guide 4 Case Test

Figure 4-6 Installing Git

Figure 4-7 Completing Git installation

Step 2 Download the Python script from GitHub.


Enter git clone https://github.com/yifengyiye/HCIA-AI. The operation is as follows.

Figure 4-8 Downloading files from GitHub

Enter ls –ll to check the downloaded file. The result is as follows.

Figure 4-9 Checking the downloaded file

Enter cd HCIA-AI and ll to switch to the corresponding folder and view the files. The result
is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 32


Development Environment Establishment
Experimental Guide 4 Case Test

Figure 4-10 Check a specific script

Step 3 Run the downloaded script.


Enter python keras_mlp_softmax.py. The operation is as follows.

Figure 4-11 Running the Python script

The test is complete.


----End

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 33


Development Environment Establishment
Experimental Guide 5 Other Configurations

5 Other Configurations

5.1 GUI-based GNOME Installation


5.1.1 Introduction to GNOME
The GNOME is one of the most widely used graphical desktop environments in the Linux
operating system. It is the free software that runs on the operating system and provides a
graphical desktop environment. It contains panels (which are used to start the program and
display the status), desktops (where applications and data are placed), and a series of standard
desktop tools and applications that allow all applications to work properly.

5.1.2 GNOME Installation


The installation procedure is as follows:
Step 1 Check whether yum is installed properly.
Enter yum --version. The operation is as follows.

Figure 5-1 Checking the yum version

Step 2 Install the Gnome package.


Enter yum groupinstall "GNOME Desktop" "Graphical Administration Tools".
The operation is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 34


Development Environment Establishment
Experimental Guide 5 Other Configurations

Figure 5-2 Installing the GNOME version

The result is as follows.

Figure 5-3 Installing the GNOME version

Figure 5-4 Completing GNOME installation

Step 3 Update the system operating level.


Enter ln -sf /lib/systemd/system/runlevel5.target /etc/systemd/system/default.target.
The operation is as follows.

Figure 5-5 Updating the system operating level

Step 4 Restart the server.


Enter reboot. The process is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 35


Development Environment Establishment
Experimental Guide 5 Other Configurations

Figure 5-6 Restarting the server

The GNOME GUI is as follows.

Figure 5-7 GNOME GUI

Press Ctrl+Alt+F2 to switch to the DOS page. Enter init 5 in the command line to switch to
the GUI.
----End

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 36


Development Environment Establishment
Experimental Guide 5 Other Configurations

5.2 anaconda Installation


5.2.1 Introduction to anaconda
anaconda, an open source Python distribution, contains more than 180 scientific packages
(such as Conda and Python) and their dependencies. It supports the Linux, Mac, and Windows
operating systems and provides package management and environment management functions.
This facilitates the coexistence and switchover of different Python versions, and installation of
various third-party packages. anaconda uses the Conda command to manage packages and
environment. It contains Python and related tools.

5.2.2 anaconda Installation


The installation procedure is as follows:
Step 1 Download anaconda.
Enter wget https://repo.continuum.io/archive/Anaconda2-5.1.0-Linux-x86_64.sh. The
operation is as follows.

Figure 5-8 Downloading the anaconda installation package

Figure 5-9 Completing the anaconda downloading

Step 2 Install anaconda.


Enter ll to go to the folder and view the directory. The result is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 37


Development Environment Establishment
Experimental Guide 5 Other Configurations

Figure 5-10 Going to the installation file directory

Enter bash Anaconda2-5.1.0-Linux-x86_64.sh, enter yes as promoted, agree with the license
agreement, and press Enter. Select the default installation path, write environment variables,
and enter yes. The result is as follows.

Figure 5-11 Completing anaconda installation

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 38


Development Environment Establishment
Experimental Guide 5 Other Configurations

Step 3 Validate the .bashrc file.


Enter source ~/.bashrc. The operation is as follows.

Figure 5-12 Validating the .bashrc file

Step 4 Perform environment verification.


Enter conda --version. The operation is as follows.

Figure 5-13 Checking the anaconda installation result

----End

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 39


HCIA-AI Huawei Certification Course

HCIA-AI
Python Programming Basics
Experimental Guide
Issue: 1.0

HUAWEI TECHNOLOGIES CO., LTD.


HCIA-AI Huawei Certification Course i

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

Huawei Proprietary and Confidential


Copyright © Huawei Technologies Co., Ltd.
HCIA-AI Huawei Certification Course 1

Introduction to Huawei Certification System

Based on cutting-edge technologies and professional training systems, Huawei certification meets the diverse AI
technology demands of various 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!
HCIA-AI Huawei Certification Course 2

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. After
understanding this document, you can perform simple Python basic programming, laying a
solid foundation for later AI development.

Contents
This experimental guide includes nine experiments, covering Python programming basics,
and is intended to help trainees and readers easily develop capabilities of developing AI.
 Experiment 1: Understand definition and operations of lists and tuples of Python.
 Experiment 2: Understand definition and operations of Python strings.
 Experiment 3: Understand definition and operations of Python dictionaries.
 Experiment 4: Understand definition and operations of conditional statements and
looping statements of Python.
 Experiment 5: Understand definition and operations of Python functions.
 Experiment 6: Understand definition and operations object-oriented programming of
Python.
 Experiment 7: Understand date and time operations of Python.
 Experiment 8: Understand definition and operations of regular expressions of Python.
 Experiment 9: Understand definition and operations of Python file manipulation.

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 the basic language editing capability.
 Have basic knowledge of data structures and database.

Experimental Environment
This experimental environment is compiled on Python 3.6.
HCIA-AI Huawei Certification Course 3

Contents

1 Lists and Tuples .......................................................................................................................... 5


1.1 Introduction to the Experiment............................................................................................................................... 5
1.1.1 About the Experiment ......................................................................................................................................... 5
1.1.2 Objectives of the Experiment .............................................................................................................................. 5
1.2 Experimental Tasks................................................................................................................................................ 5
1.2.1 Concepts ............................................................................................................................................................ 5
1.2.2 Experimental Operations on Lists........................................................................................................................ 5
1.2.3 Experimental Operations on Tuples ..................................................................................................................... 7

2 Strings ......................................................................................................................................... 9
2.1 Introduction to the Experiment............................................................................................................................... 9
2.1.1 About the Experiment ......................................................................................................................................... 9
2.1.2 Objectives of the Experiment .............................................................................................................................. 9
2.2 Experimental Tasks................................................................................................................................................ 9
2.2.1 Concepts ............................................................................................................................................................ 9
2.2.2 Experimental Operations..................................................................................................................................... 9

3 Dictionaries ............................................................................................................................... 11
3.1 Introduction to the Experiment.............................................................................................................................. 11
3.1.1 About the Experiment ........................................................................................................................................ 11
3.1.2 Objectives of the Experiment ............................................................................................................................. 11
3.2 Experimental Tasks............................................................................................................................................... 11
3.2.1 Concepts ........................................................................................................................................................... 11
3.2.2 Experimental Operations.................................................................................................................................... 11

4 Conditional and Looping Statements ...................................................................................... 13


4.1 Introduction to the Experiment..............................................................................................................................13
4.1.1 About the Experiment ........................................................................................................................................13
4.1.2 Objectives of the Experiment .............................................................................................................................13
4.2 Experimental Tasks...............................................................................................................................................13
4.2.1 Concepts ...........................................................................................................................................................13
4.2.2 Experimental Operations....................................................................................................................................14

5 Functions .................................................................................................................................. 15
5.1 Introduction to the Experiment..............................................................................................................................15
HCIA-AI Huawei Certification Course 4

5.1.1 About the Experiment ........................................................................................................................................15


5.1.2 Objectives of the Experiment .............................................................................................................................15
5.2 Experimental Tasks...............................................................................................................................................15
5.2.1 Concepts ...........................................................................................................................................................15
5.2.2 Experimental Operations....................................................................................................................................15

6 Object-Oriented Programming ............................................................................................... 20


6.1 Introduction to the Experiment..............................................................................................................................20
6.1.1 About the Experiment ........................................................................................................................................20
6.1.2 Objectives of the Experiment .............................................................................................................................20
6.2 Experimental Tasks...............................................................................................................................................20
6.2.1 Concepts ...........................................................................................................................................................20
6.2.2 Experimental Operations....................................................................................................................................21

7 Date and Time .......................................................................................................................... 24


7.1 Introduction to the Experiment..............................................................................................................................24
7.1.1 About the Experiment ........................................................................................................................................24
7.1.2 Objectives of the Experiment .............................................................................................................................24
7.2 Experimental Tasks...............................................................................................................................................24
7.2.1 Concepts ...........................................................................................................................................................24
7.2.2 Experimental Operations....................................................................................................................................24

8 Regular Expressions ................................................................................................................. 26


8.1 Introduction to the Experiment..............................................................................................................................26
8.1.1 About the Experiment ........................................................................................................................................26
8.1.2 Objectives of the Experiment .............................................................................................................................26
8.2 Experimental Tasks...............................................................................................................................................26
8.2.1 Concepts ...........................................................................................................................................................26
8.2.2 Experimental Operations....................................................................................................................................26

9 File Manipulation ..................................................................................................................... 30


9.1 Introduction to the Experiment..............................................................................................................................30
9.1.1 About This Experiment ......................................................................................................................................30
9.1.2 Objectives of the Experiment .............................................................................................................................30
9.2 Experimental Tasks...............................................................................................................................................30
9.2.1 Concepts ...........................................................................................................................................................30
9.2.2 Experimental Operations....................................................................................................................................30
HCIA-AI Huawei Certification Course 5

1 Lists and Tuples

1.1 Introduction to the Experiment


1.1.1 About the Experiment
This experiment introduces knowledge units about lists and tuples in Python, and related
operations on them.

1.1.2 Objectives of the Experiment


 Understand meanings of Python lists.
 Understand meanings of Python tuples.
 Grasp related operations on Python lists.
 Grasp related operations on Python tuples.

1.2 Experimental Tasks


1.2.1 Concepts
 Python lists. Lists include a series of ordered elements, and expressed in the form of [].
As lists are combined orderly, you can identify the position of elements for access.
Note: The list index starts from 0 forward and –1 backward.
 Python tuples. Tuples are similar to lists in Python, except for that elements of tuples are
unchangeable. Tuples are expressed in parentheses while lists in square brackets. It is
easy to create tuples, simply by adding elements in brackets and separating them with
commas.

1.2.2 Experimental Operations on Lists


This section describes related operations on lists.
1. Understand the difference between "append" and "extend".
"Append" adds data to the end of a list as a new element. Its arguments can be any object.
>>>x = [1, 2, 3]
>>>y = [4, 5]
HCIA-AI Huawei Certification Course 6

>>>x.append(y)
>>>print(x)
[1, 2, 3, [4, 5]]

The argument of "extend" must be an iterated object, which means that all elements of this
object are added to the end of list one by one.
>>>x = [1, 2, 3]
>>>y = [4, 5]
>>>x.extend(y)
>>>print(x)
[1, 2, 3, 4, 5]
# equal to
>>>for i in y:
>>>x.append(i)
>>>print(x)

2. Check whether the list is empty.


if len(items) == 0:
print("empty list")
or
if items == []:
print("empty list")
3. Copy a list.
Method one:
new_list = old_list[:]
Method two:
new_list = list(old_list)
Method three:
import copy
new_list = copy.copy(old_list)# copy
new_list = copy.deepcopy(old_list)# deep copy
4. Get the last element of a list.
Elements in an index list can be positive or negative numbers. Positive numbers mean
indexing from the left of list, while negative numbers mean indexing from the right of list.
There are two methods to get the last element.
>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[len(a)-1]
10
>>> a[-1]
10
5. Sort lists.
You can use two ways to sort lists. Python lists have a sort method and a built-in function
(sorted). You can sort complicated data types by specifying key arguments. You can sort lists
composed of dictionaries according to the age fields of elements in the dictionaries.
>>>items = [{'name': 'Homer', 'age': 39},
{'name': 'Bart', 'age': 10},
{"name": 'cater', 'age': 20}]
>>>items.sort(key=lambda item: item.get("age"))
>>>print(items)
HCIA-AI Huawei Certification Course 7

[{'age': 10, 'name': 'Bart'}, {'age': 20, 'name': 'cater'}, {'age': 39, 'name': 'Homer'}]
6. Remove elements from a list.
The "remove" method removes an element, and it removes only the element that appears for
the first time.
>>> a = [0, 2, 2, 3]
>>> a.remove(2)
>>> a
[0, 2, 3]
# ValueError will be returned if the removed element is not within the list.
>>> a.remove(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list·

"del" removes a certain element in a specified position.


"pop" is similar to "del", but "pop" can return the removed element.
7. Connect two lists.
>>>listone = [1, 2, 3]
>>>listtwo = [4, 5, 6]
>>>mergedlist = listone + listtwo
>>>print(mergelist)
[1, 2, 3, 4, 5, 6]

1.2.3 Experimental Operations on Tuples


This section describes related operations on tuples.
Another type of ordered lists is called tuples and is expressed in (). Similar to a list, a tuple
cannot be changed after being initialized. Elements must be determined when a tuple is
defined.
A tuple has no append() and insert() methods and cannot be assigned as another element. The
method for getting a tuple is similar to that for getting a list.
As tuples are unchangeable, code is more secure. Therefore, if possible, use tuples instead of
lists.
1. Define a tuple for an element.
t=(1,)

Note: In t=(1), t is not a tuple type, as the parentheses () can represent a tuple, or
mathematical formula. Python stipulates that in this case, () is a mathematical formula, and a
tuple with only one element must have a comma to eliminate ambiguity.
2. Define a changeable tuple.
>>> cn=('yi','er','san')
>>> en=('one','two','three')
>>> num=(1,2,3)
>>> tmp=[cn,en,num,[1.1,2.2],'language']
>>>print(tmp)
[('yi', 'er', 'san'), ('one', 'two', 'three'), (1, 2, 3), [1.1, 2.2], 'language']
>>>print(tmp[0])
('yi', 'er', 'san')
HCIA-AI Huawei Certification Course 8

>>>print(tmp[0][0])
yi
>>>print(tmp[0][0][0])
y
HCIA-AI Huawei Certification Course 9

2 Strings

2.1 Introduction to the Experiment


2.1.1 About the Experiment
This experiment mainly introduces related knowledge units about strings in Python, and
related operations on them.

2.1.2 Objectives of the Experiment


 Understand meanings of Python strings.
 Grasp related operations on Python strings.

2.2 Experimental Tasks


2.2.1 Concepts
 Strings of Python: A string is a sequence composed of zero or multiple characters, and it
is one of the six built-in sequences of Python. Strings are unchangeable in Python, which
are string constants in C and C++ languages.
 Expression of strings. Strings may be expressed in single quotes, double quotes, triple
quotes, or as escape characters and original strings.

2.2.2 Experimental Operations


1. Single quotes and double quotes
Strings in single quotes are equal to those in double quotes, and they are exchangeable.
>>> s = 'python string'
>>>print(s)
python string
>>> ss="python string"
>>>print(ss)
python string
>>> sss='python "Hello World"string'
>>>print(sss)
HCIA-AI Huawei Certification Course 10

python "Hello World"string


2. Long strings
Triple quotes can define long strings in Python as mentioned before. Long strings may have
output like:
>>>print('''this is a long string''')
this is a long string
3. Original strings
Original strings start with r, and you can input any character in original strings. The output
strings include backslash used by transference at last. However, you cannot input backslash at
the end of strings. For example:
>>> rawStr = r'D:\SVN_CODE\V900R17C00_TRP\omu\src'
>>>print(rawStr)
D:\SVN_CODE\V900R17C00_TRP\omu\src
4. Width, precision, and alignment of strings
To achieve the expected effects of strings in aspects of width, precision, and alignment, refer
to the formatting operator commands.
>>>print("%c" % 97)
a
>>>print("%6.3f" % 2.5)
2.500
>>>print("%+10x" % 10)
+a
>>>print("%.*f" % (4, 1.5))
1.5000
5. Connect and repeat strings
In Python, you can use "+" to connect strings and use "*" to repeat strings.
>>> s = 'I' + 'want' + 'Python' + '.'
>>>print(s)
IwantPython.
>>> ss='Python'*3
>>>print(ss)
PythonPythonPython
6. Delete strings
You can use "del" to delete a string. After being deleted, this object will no longer exist, and
an error is reported when you access this object again.
>>> ss='Python'*3
>>>print(ss)
PythonPythonPython
>>> del ss
>>>print(ss)
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
print(ssNameError: name 'ss' is not defined)
HCIA-AI Huawei Certification Course 11

3 Dictionaries

3.1 Introduction to the Experiment


3.1.1 About the Experiment
This experiment mainly introduces related knowledge units about dictionaries in Python, and
related operations on them.

3.1.2 Objectives of the Experiment


 Understand meanings of Python dictionaries.
 Grasp related operations on Python dictionaries.

3.2 Experimental Tasks


3.2.1 Concepts
 Python dictionary. A dictionary has a data structure similar to a mobile phone list, which
lists names and their associated information. In a dictionary, the name is called a "key",
and its associated information is called "value". A dictionary is a combination of keys
and values.
 Its basic format is as follows:
d = {key1 : value1, key2 : value2 }
 You can separate a key and a value with a colon, separate each key/value pair with a
comma, and include the dictionary in a brace.
 Some notes about keys in a dictionary: Keys must be unique, and must be simple objects
like strings, integers, floating numbers, and bool values.

3.2.2 Experimental Operations


1. Create a dictionary
A dictionary can be created in multiple manners, as shown below.
>>> a = {'one': 1, 'two': 2, 'three': 3}
>>>print(a)
HCIA-AI Huawei Certification Course 12

{'three': 3, 'two': 2, 'one': 1}


>>> b = dict(one=1, two=2, three=3)
>>>print(b)
{'three': 3, 'two': 2, 'one': 1}
>>> c = dict([('one', 1), ('two', 2), ('three', 3)])
>>>print(c)
{'three': 3, 'two': 2, 'one': 1}
>>> d = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>>print(d)
{'three': 3, 'two': 2, 'one': 1}
>>> e = dict({'one': 1, 'two': 2, 'three': 3})
>>>print(e)
{'one': 1, 'three': 3, 'two': 2}
>>>print(a==b==c==d==e)
True
2. dictcomp
"dictcomp" can build a dictionary from iterated objects that use key/value pairs as elements.
>>> data = [("John","CEO"),("Nacy","hr"),("LiLei","engineer")]
>>> employee = {name:work for name, work in data}
>>>print(employee)
{'LiLei': 'engineer', 'John': 'CEO', 'Nacy': 'hr'}
3. Dictionary lookup
Look up directly according to a key value.
>>>print(employee["John"])
CEO

If there is no matched key value in a dictionary, KeyError is returned.


>>>print(employee["Joh"])
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
print(employee["Joh"])
KeyError: 'Joh'

When you use dic[key] to look up a key value in a dictionary, it will return an error if there is
no such key value. However, if you use dic.get(key, default) to look up a key value, it will
return default if there is no such key value.
>>>print(employee.get("Nacy","UnKnown'"))
hr
>>>print(employee.get("Nac","UnKnown"))
UnKnown
HCIA-AI Huawei Certification Course 13

4 Conditional and Looping Statements

4.1 Introduction to the Experiment


4.1.1 About the Experiment
This experiment mainly introduces related knowledge and operations about conditional and
looping statements in Python.

4.1.2 Objectives of the Experiment


 Understand meanings of conditional and looping statements in Python.
 Understand related operations on conditional and looping statements in Python.

4.2 Experimental Tasks


4.2.1 Concepts
1. In Python, the "if" statement controls execution of a program, and its basic form is as
follows:
if judging condition:
Execute statement…
else:
Execute statement …

The following statements are executed if the judging condition is true (non-zero). There can
be multiple lines of execution statements, which can be indented to indicate the same range.
The "else" statement is optional, and can be executed when the condition is false.
2. Looping statement
There are a lot of changes in looping statements. Common statements include the "for"
statement and the "while" statement.
In "for" looping, the "for" statement should be followed by a colon. "for" looping is
performed in a way similar to iterating. In "while" looping, there is a judgment on condition
and then looping, like in other languages.
HCIA-AI Huawei Certification Course 14

4.2.2 Experimental Operations


1. "for" looping
>>> for i in range(0,10):
>>>print(i)
0
1
2
3
4
5
6
7
8
9
>>> a=[1,3,5,7,9]
>>> for i in a:
>>>print(i)
1
3
5
7
9
2. "while" looping
>>>while (a>100):
>>>print(a)
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
HCIA-AI Huawei Certification Course 15

5 Functions

5.1 Introduction to the Experiment


5.1.1 About the Experiment
This experiment mainly introduces related knowledge and operations about functions in
Python.

5.1.2 Objectives of the Experiment


 Understand meanings of Python functions.
 Grasp related operations on Python function.

5.2 Experimental Tasks


5.2.1 Concepts
Functions can raise modularity of applications and reuse of code. In Python, strings, tuples,
and numbers are unchangeable, while lists and dictionaries are changeable. For those
unchangeable types such as integers, strings, and tuples, only values are transferred during
function calling, without any impact on the objects themselves. For those changeable types,
objects are transferred during function calling, and external objects will also be impacted after
changes.

5.2.2 Experimental Operations


1. Common built-in functions
The "int" function can be used to convert other types of data into integers.
>>> int('123')
123
>>> int(12.34)
12
>>> float('12.34')
12.34
>>> str(1.23)
HCIA-AI Huawei Certification Course 16

'1.23'
>>> str(100)
'100'
>>> bool(1)
True
>>> bool('')
False
2. Function name
A function name is a reference to a function object, and it can be assigned to a variable, which
is equivalent to giving the function an alias.
>>> a = abs # Variable a points to function abs
>>> a(-1) # Therefore, the "abs" can be called by using "a"
1
3. Define functions
In Python, you can use the "def" statement to define a function, listing function names,
brackets, arguments in brackets and colons successively. Then you can edit a function in an
indentation block and use the "return" statement to return values.
We make an example by defining the "my_abs" function to get an absolute value.
>>> def my_abs(x):
if x>=0:
return x
else:
return –x

You can use the "pass" statement to define a void function, which can be used as a placeholder.
Change the definition of "my_abs" to check argument types, that is, to allow only arguments
of integers and floating numbers. You can check data types with the built-in function
isinstance().
def my_abs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x >= 0:
return x
else:
return –x
4. Keyword arguments
Changeable arguments allow you input zero or any number of arguments, and these
changeable arguments will be assembled into a tuple for function calling. While keyword
arguments allow you to input zero or any number of arguments, and these keyword arguments
will be assembled into a dictionary in functions.
def person(name, age, **kw):
print('name:', name, 'age:', age, 'other:', kw)

Function "person" receives keyword argument "kw" except essential arguments "name" and
"age". You can only input essential arguments when calling this function.
>>> person('Michael', 30)
name: Michael age: 30 other: {}

You can also input any number of keyword arguments.


HCIA-AI Huawei Certification Course 17

>>> person('Bob', 35, city='Beijing')


name: Bob age: 35 other: {'city': 'Beijing'}
>>> person('Adam', 45, gender='M', job='Engineer')
name: Adam age: 45 other: {'gender': 'M', 'job': 'Engineer'}

Similar to changeable arguments, you can assemble a dictionary and convert it into keyword
arguments as inputs.
>>> extra = {'city': 'Beijing', 'job': 'Engineer'}
>>> person('Jack', 24, city=extra['city'], job=extra['job'])
name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}

You can certainly simplify the above-mentioned complex function calling.


>>> extra = {'city': 'Beijing', 'job': 'Engineer'}
>>> person('Jack', 24, **extra)
name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}

**extra means transferring all key-values in this extra dictionary as key arguments into the
**kw argument of the function. kw will get a dictionary, which is a copy of the extra
dictionary. Changes on kw will not impact the extra dictionary outside the function.
5. Name keyword arguments
If you want to restrict names of keyword arguments, you can name keyword arguments. For
example, you can accept only "city" and "job" as keyword arguments. A function defined in
this way is as follows:
def person(name, age, *, city, job):
print(name, age, city, job)

Different from keyword argument "**kw", a special separator "*" is required to name a
keyword argument. Arguments after "*" are regarded as naming keyword arguments, which
are called as follows:
>>> person('Jack', 24, city='Beijing', job='Engineer')
Jack 24 Beijing Engineer

The special separator "*" is not required in the keyword argument after a changeable
argument in a function.
def person(name, age, *args, city, job):
print(name, age, args, city, job)

You need to input an argument name to name a keyword argument, which is different from
the position argument. An error will be returned during calling if no argument name is
introduced. In this case, the keyword argument can be default, as one way to simplify calling.
def person(name, age, *, city='Beijing', job):
print(name, age, city, job)

Because the keyword argument "city" has a default value, you do not need to input a
parameter of "city" for calling.
>>> person('Jack', 24, job='Engineer')
Jack 24 Beijing Engineer

When you name a keyword argument, "*" must be added as a special separator if there are no
changeable arguments. Python interpreter cannot identify position arguments and keyword
arguments if there is no "*".
HCIA-AI Huawei Certification Course 18

6. Argument combination
To define a function in Python, you can use required arguments, default arguments,
changeable arguments, keyword arguments and named keyword arguments. These five types
of arguments can be combined with each other.
Note: Arguments must be defined in the order of required arguments, default arguments,
changeable arguments, named keyword arguments, and keyword arguments.
For example, to define a function that includes the above-mentioned arguments:
def f1(a, b, c=0, *args, **kw):
print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)
def f2(a, b, c=0, *, d, **kw):
print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)

Python interpreter will input the matched arguments according to argument positions and
names automatically when calling a function.
>>> f1(1, 2)
a = 1 b = 2 c = 0 args = () kw = {}
>>> f1(1, 2, c=3)
a = 1 b = 2 c = 3 args = () kw = {}
>>> f1(1, 2, 3, 'a', 'b')
a = 1 b = 2 c = 3 args = ('a', 'b') kw = {}
>>> f1(1, 2, 3, 'a', 'b', x=99)
a = 1 b = 2 c = 3 args = ('a', 'b') kw = {'x': 99}
>>> f2(1, 2, d=99, ext=None)
a = 1 b = 2 c = 0 d = 99 kw = {'ext': None}

The most amazing thing is that you can call the above-mentioned function through a tuple and
a dictionary.
>>> args = (1, 2, 3, 4)
>>> kw = {'d': 99, 'x': '#'}
>>> f1(*args, **kw)
a = 1 b = 2 c = 3 args = (4,) kw = {'d': 99, 'x': '#'}
>>> args = (1, 2, 3)
>>> kw = {'d': 88, 'x': '#'}
>>> f2(*args, **kw)
a = 1 b = 2 c = 3 d = 88 kw = {'x': '#'}

Therefore, you can call a function through the forms similar to "func(*args, **kw)", no matter
how its arguments are defined.
7. Recursive function
You need to prevent a stack overflow when you use a recursive function. Functions are called
through the stack which is a data structure in computers. The stack will add a layer of stack
frames when a function is called, while the stack will remove a layer of stack frames when a
function is returned. As the size of a stack is limited, it will lead to a stack overflow if there
are excessive numbers of recursive calling of functions.
Solution to a stack overflow: tail recursion optimization
You can use tail recursion optimization to solve a stack flow. As tail recursion enjoys the same
effects with looping, you can take looping as a special tail recursion, which means to call
itself when the function is returned and exclude expressions in the "return" statement. In this
way, the compiler or interpreter can optimize tail recursion, making recursion occupying only
HCIA-AI Huawei Certification Course 19

one stack frame, no matter how many times the function is called. This eliminates the
possibility of stack overflow.
For the fact(n) function, because a multiplication expression is introduced in return n * fact(n - 1),
it is not tail recursion. To change it into tail recursion, more code is needed to transfer the
product of each step into a recursive function.
def fact(n):
return fact_iter(n, 1)
def fact_iter(num, product):
if num == 1:
return product
return fact_iter(num - 1, num * product)

It can be learned that return fact_iter(num - 1, num * product) returns only the recursive function
itself. num – 1 and num * product will be calculated before the function is called, without any
impact on the function.
HCIA-AI Huawei Certification Course 20

6 Object-Oriented Programming

6.1 Introduction to the Experiment


6.1.1 About the Experiment
This experiment mainly introduces related knowledge and operations about object-oriented
programming in Python.

6.1.2 Objectives of the Experiment


 Understand the meaning concerning object-oriented programming in Python.
 Grasp related operations concerning object-oriented programming in Python.

6.2 Experimental Tasks


6.2.1 Concepts
 Object-oriented programming
As a programming idea, Object Oriented Programming (OOP) takes objects as the basic
units of a program. An object includes data and functions that operate the data.
Process-oriented design (OOD) takes a program as a series of commands, which are a
group of functions to be executed in order. To simplify program design, OOD cuts
functions further into sub-functions. This reduces system complexity by cutting
functions into sub-functions.
OOP takes a program as a combination of objects, each of which can receive messages
from other objects and process these messages. Execution of a computer program is to
transfer a series of messages among different objects.
In Python, all data types can be regarded as objects, and you can customize objects. The
customized object data types are classes in object-orientation.
 Introduction to the object-oriented technology
− Class: A class refers to the combination of objects that have the same attributes and
methods. It defines the common attributes and methods of these objects in the
combination. Objects are instances of classes.
HCIA-AI Huawei Certification Course 21

− Class variable: Class variables are publicly used in the total instantiation, and they
are defined within classes but beyond function bodies. Class variables are not used
as instance variables.
− Data member: Class variables or instance variables process data related to classes
and their instance objects.
− Method re-writing: If the methods inherited from parent classes do not meet the
requirements of sub-classes, the methods can be re-written. Re-writing a method is
also called overriding.
− Instance variable: Instance variables are defined in methods and are used only for
the classes of current instances.
− Inheritance: Inheritance means that a derived class inherits the fields and methods
from a base class, and it allows taking the objects of derived class as the objects of
base classes. For example, a dog-class object drives from an animal-class object.
This simulates a "(is-a)" relationship (in the figure, a dog is an animal).
− Instantiation: It refers to creating instances for a class or objects for a class.
− Methods: functions defined in classes.
− Objects: data structure objects defined through classes. Objects include two data
members (class variable and instance variable), and methods.

6.2.2 Experimental Operations


1. Create and use a class
Create a dog class.
Each instance created based on a dog class stores name and age. We will assign capabilities of
sitting (sit () ) and rolling over (roll_over () ) as follows:
class Dog():
"""a simple try of simulating a dog"""
def init (self,name,age):
"""Initializeattribute: name and age"""
self.name = name
self.age = age
def sit(self):
"""Simulate sitting when a dog is ordered to do so"""
print(self.name.title()+"is now sitting")
def roll_over(self):
"""Simulate rolling over when a dog is ordered to do so"""
print(self.name.title()+"rolled over!")
2. Access attributes
Let us see a complete instance.
class Employee:
‘All employees base class’
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print("Total Employee %d" % Employee.empCount )
def displayEmployee(self):
HCIA-AI Huawei Certification Course 22

print("Name : ", self.name, ", Salary: ", self.salary)


"Create the first object of the employee class"
emp1 = Employee("Zara", 2000)
"Create the second object of the employee class"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print("Total Employee %d" % Employee.empCount)

Output following execution of the above-mentioned code:


Name : Zara ,Salary: 2000
Name : Manni ,Salary: 5000
Total Employee 2
3. Class inheritance
The major benefit of oriented-object programming is reuse of code. One way to reuse code is
the inheritance mechanism. Inheritance can be taken as setting relationships of parent classes
and child classes between classes.
Some features of class inheritance in Python.
 The construction (_init_() method) of the base class will be not auto-called, and it has to
be specially called in the construction of its derived classes.
 Class prefixes and self argument variables have to be added to the base class when its
methods are called. The self argument is not required when regular functions in classes
are called.
 Python always loops up the methods of the corresponding classes, and checks the base
class one method by one method only if the methods are not found in the derived classes.
(That is, Python searches for the calling method in this class first and then in the base
class).
 If an inheritance type lists more than one class, this inheritance is called
multi-inheritance.
class Parent: # Define the parent class
parentAttr = 100
def __init__(self):
print("Call parent class construction method")
def parentMethod(self):
print('Call parent class method')
def setAttr(self, attr):
Parent.parentAttr = attr
def getAttr(self):
print("Parent class attribute", Parent.parentAttr)
class Child(Parent): # Define a sub-class
def __init__(self):
print("Call sub-class construction method”)
def childMethod(self):
print(‘Call sub-class method‘)
c = Child() # Instantiate sub-class
c.childMethod() # Call sub-class method
c.parentMethod() # Call parent class method
c.setAttr(200) # Re-call parent class method - set attributes
c.getAttr() # Re-call parent class method - get attributes

The code will be executed to output the following results:


HCIA-AI Huawei Certification Course 23

Calling the child class construction method;


Calling the child class method;
Calling the parent class method;
Parent class attribute: 200.
4. Class attributes and methods
 Private attributes of classes:
__private_attrs: It starts with two underlines to indicate a private attribute, which cannot
be used outside a class or directly accessed. When it is used inside a class method, follow
the form of self.__private_attrs.
 Method of class
Inside a class, the def keyword can be used to define a method; unlike a regular function,
a class method must include the self argument, which has to be the first argument.
 Private method
__private_method: It starts with two underlines to indicate a private method, which
cannot be used outside a class. When it is used inside a class, follow the form of
self.__private_methods.
class JustCounter:
__secretCount = 0 # Private variable
publicCount = 0 # Public variable
def count(self):
self.__secretCount += 1
self.publicCount += 1
print(self.__secretCount)
counter = JustCounter()
counter.count()
counter.count()
print(counter.publicCount)
print(counter.__secretCount) # Error. Instance cannot access private variable
HCIA-AI Huawei Certification Course 24

7 Date and Time

7.1 Introduction to the Experiment


7.1.1 About the Experiment
This experiment mainly introduces related knowledge and operations about date and time in
Python.

7.1.2 Objectives of the Experiment


 Understand the meaning of date and time in Python.
 Grasp the basic operations on data and time in Python.

7.2 Experimental Tasks


7.2.1 Concepts
How to process date and time is a typical problem for Python. Python provides the time and
calendar modules to format date and time.
Time spacing is floating numbers with seconds as the unit. Each time stamp is expressed as
the time that has passed since the midnight of January 1st 1970.
The time module of Python has many functions to convert common date formats.

7.2.2 Experimental Operations


1. Get the current time
>>>import time
>>>localtime = time.localtime(time.time())
>>>print("Local time:”, localtime)
Output:
Local time: time.struct_time(tm_year=2018, tm_mon=4, tm_mday=28, tm_hour=10, tm_min=3, tm_sec=27,
tm_wday=3, tm_yday=98, tm_isdst=0)
2. Get the formatted time
HCIA-AI Huawei Certification Course 25

You can choose various formats as required, but the simplest function to get the readable time
mode is asctime():
>>>import time
>>>localtime = time.asctime( time.localtime(time.time()) )
>>>print("Local time :", localtime)

Output:
Local time: Thu Apr 7 10:05:21 2016
3. Format date
>>>import time
# Format into 2016-03-20 11:45:39
>>>print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# Format into Sat Mar 28 22:24:24 2016
>>>print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
# Turn format string into timestamp
>>>a = "Sat Mar 28 22:24:24 2016"
>>>print(time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")))

Output:
2016-04-07 10:25:09
Thu Apr 07 10:25:09 2016
1459175064.0
4. Get calendar of a month
The calendar module can process yearly calendars and monthly calendars using multiple
methods, for example, printing a monthly calendar.
>>>import calendar

>>>cal = calendar.month(2016, 1)
>>>print("output calendar of January 2016:”)
>>>print(cal)
Output:
The following is the calendar of April 2014
April 2014
Mo Tu We Th Fr Sa Su
1
2345678
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
HCIA-AI Huawei Certification Course 26

8 Regular Expressions

8.1 Introduction to the Experiment


8.1.1 About the Experiment
This experiment mainly introduces related knowledge and operations about regular
expressions in Python.

8.1.2 Objectives of the Experiment


 Understand the meaning of regular expressions in Python.
 Grasp the basic operations on regular expressions in Python.

8.2 Experimental Tasks


8.2.1 Concepts
Python regular expressions are special sequences of characters and they enable easy check on
whether a string matches a mode.
Python of version 1.5 and later has the new re module, which provides a Perl-style regular
expression mode.
The re module enables Python to have all regular expression functions.
The compile function creates a regular expression object based on a mode string and optional
flag arguments, and this object has a series of methods for matching and replacing regular
expressions.
The re module provides functions that have identical functions to these methods, and these
functions use a mode string as the first argument.
This chapter introduces the common Python functions that process regular expressions.

8.2.2 Experimental Operations


1. re.match function
HCIA-AI Huawei Certification Course 27

re.match tries to match a mode from the string start position. If no mode is matched from the
string start, match() returns none.
Function syntax:
re.match(pattern, string, flags=0)
Instance:
>>>import re
>>>print(re.match('www', 'www.runoob.com').span()) # Match at start
>>>print(re.match('com', 'www.runoob.com')) # Match not at start

Output:
(0, 3)
None
2. re.search method
re.search scans the entire string and returns the first successful match.
Function syntax:
re.search(pattern, string, flags=0)

Instance:
>>>import re
>>>line = "Cats are smarter than dogs"
>>>searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)
>>>if searchObj:
>>>print("searchObj.group() : ", searchObj.group())
>>>print("searchObj.group(1) : ", searchObj.group(1))
>>>print("searchObj.group(2) : ", searchObj.group(2))
>>>else:
>>>print("Nothing found!!")

Execution result of the above instance:


searchObj.group() : Cats are smarter than dogs
searchObj.group(1) : Cats
searchObj.group(2) : smarter
3. Differences between re.match and re.search
re.match only matches the string start. If the string start does not agree with the regular
expression, the matching fails and the function returns none. re.search matches the entire
string until finding a match.
>>>import re
>>>line = "Cats are smarter than dogs";
>>>matchObj = re.match( r'dogs', line, re.M|re.I)
>>>if matchObj:
>>>print("match --> matchObj.group() : ", matchObj.group())
>>>else:
>>>print("No match!!")
>>>matchObj = re.search( r'dogs', line, re.M|re.I)
>>>if matchObj:
>>>print("search --> matchObj.group() : ", matchObj.group())
>>>else:
>>>print("No match!!")
HCIA-AI Huawei Certification Course 28

Execution result of the above instance:


No match!!
search --> matchObj.group() : dogs
4. Index and replace
The re module of Python provides re.sub to replace matched items in strings.
Function syntax:
re.sub(pattern, repl, string, count=0, flags=0)

>>>import re
>>>phone = "2004-959-559 # This is an oversea telephone number"
# Delete Python comments in strings
>>>num = re.sub(r'#.*$', "", phone)
>>>print("The telephone number is", num)
# Delete non-number (-) strings
>>>num = re.sub(r'\D', "", phone)
>>>print("The telephone number is ", num)

Result:
The telephone number is: 2004-959-559
The telephone number is: 2004959559
5. re.compile function
The compile function compiles regular expressions and creates a regular expression (pattern)
object, which will be used by the match() and search() functions.
Function syntax:
re.compile(pattern[, flags])

>>>import re
>>> pattern = re.compile(r'\d+') # Match at least one number
>>> m = pattern.match('one12twothree34four') # Search head, no match
>>>print(m)
None
>>> m = pattern.match('one12twothree34four', 2, 10) # Match from ‘e’, no match
>>>print(m)
None
>>> m = pattern.match('one12twothree34four', 3, 10) # Match from ‘1’, matched
>>>print(m) # Return a match object
<_sre.SRE_Match object at 0x10a42aac0>
>>> m.group(0) # Ignorable 0
'12'
>>> m.start(0) # Ignorable 0
3
>>> m.end(0) # Ignorable 0
5
>>> m.span(0) # Ignorable 0
(3, 5)
6. findall
findall finds all strings that match regular expressions and returns a list. If there is no match, it
returns an empty list.
HCIA-AI Huawei Certification Course 29

Note: match and search match once, while findall matches all.
Function syntax:
findall(string[, pos[, endpos]])

>>>import re
>>>pattern = re.compile(r'\d+') # Search numbers
>>>result1 = pattern.findall('runoob 123 google 456')
>>>result2 = pattern.findall('run88oob123google456', 0, 10)
>>>print(result1)
>>>print(result2)

Output
['123', '456']
['88', '12']
7. re.finditer
Similar to findall, re.finditer finds all strings that match regular expressions, and returns them
as an iterator.
re.finditer(pattern, string, flags=0)

>>>import re
>>>it = re.finditer(r"\d+","12a32bc43jf3")
>>>for match in it:
>>>print(match.group())
Output:
12
32
43
3
8. re.split
The split method splits matched strings and returns a list. For example:
re.split(pattern, string[, maxsplit=0, flags=0])

Instance:
>>>import re
>>> re.split('\W+', 'runoob, runoob, runoob.')
['runoob', 'runoob', 'runoob', '']
>>> re.split('(\W+)', ' runoob, runoob, runoob.')
['', ' ', 'runoob', ', ', 'runoob', ', ', 'runoob', '.', '']
>>> re.split('\W+', ' runoob, runoob, runoob.', 1)
['', 'runoob, runoob, runoob.']
>>> re.split('a*', 'hello world') # Split will not split unmatched strings
['hello world']
HCIA-AI Huawei Certification Course 30

9 File Manipulation

9.1 Introduction to the Experiment


9.1.1 About This Experiment
This experiment mainly introduces related knowledge and operations about file
manipulation in Python.

9.1.2 Objectives of the Experiment


 Understand the meaning of file manipulation in Python.
 Grasp the basic operations on file manipulation in Python.

9.2 Experimental Tasks


9.2.1 Concepts
File manipulation is essential to programming languages, as information technologies would
be meaningless if data cannot be stored permanently. This chapter introduces common file
manipulation in Python.

9.2.2 Experimental Operations


1. Read keyboard input
Python provides two build-in functions to read a text line from the standard input, which a
keyboard by default. The two functions are raw_input and input.
raw_input( ) function:
>>>str = raw_input("Please input:")
>>>print("Your input is:", str)

This reminds your input of any strings and displays the same strings on the screen. When I
enter "Hello Python!", it outputs:
Please input: Hello Python!
Your input is: Hello Python!
HCIA-AI Huawei Certification Course 31

input( ) function:
The input([prompt]) and raw_input([prompt]) functions are similar, but the former can receive
a Python expression as the input and return the result.
>>>str = input("Please input:")
>>>print("Your input is: ", str)
Output:
Please input:[x*5 for x in range(2,10,2)]
Your input is: [10, 20, 30, 40]
2. Open and close files
Python provides essential functions and methods to manipulate files by default. You can use
the file object to do most file manipulations.
Open() function: You should open a file using the Python build-in open() function, and create
a file object, so that the related methods can call it to write and read.
# Open a file
>>>fo = open("foo.txt", "w")
>>>print("File name: “, fo.name)
>>>print("closed or not: ", fo.closed)
>>>print("access mode:”, fo.mode)
>>>print("space required at head and tail, or not: ", fo.softspace)

Output:
File name: foo.txt
Close or not: False
Access mode: w
Space required at head and tail, or not: 0

Close() function: For the file object, it refreshes any buffered data that has not been written
and closes the file. Then, the file cannot be written.
When a reference to a file object is re-assigned to another file, Python will close the previous
file. It is ideal to close a file using the close() function.
# Open a file
>>>fo = open("foo.txt", "w")
>>>print("File name: “, fo.name)
# Close the opened file
>>>fo.close()

Output:
File name: foo.txt
3. Write a file
write() function: It writes any string into an opened file. Note that Python strings can be
binary data, not just texts. This function will not add a line feed ('\n') at string ends.
# Open a file
>>>fo = open("foo.txt", "w")
>>>fo.write( "www.baidu.com!\nVery good site!\n")
# Close an opened file
>>>fo.close()
HCIA-AI Huawei Certification Course 32

The function above creates a foo.txt file, writes the received content into this file, and closes
the file. If you open this file, you will see:
www.baidu.com!
Very good site!
4. Read a file
Read() function: It reads strings from an opened file. Note that Python strings can be binary
data, not just texts.
# Open a file
>>>fo = open("foo.txt", "r+")
>>>str = fo.read(10)
>>>print("The read string is: ", str)
# Close an opened file
>>>fo.close()

Output:
The read string is: www.runoob.
5. Rename a file
The os module of Python provides a method to execute file processing operations, like
renaming and deleting files. To use this module, you have to import it first and then call
various functions.
rename(): It requires two arguments: current file name and new file name.
Function syntax:
os.rename(current_file_name, new_file_name)

>>>import os
# Rename file test1.txt to test2.txt
>>>os.rename( "test1.txt", "test2.txt" )
6. Delete a file
You can use the remove() method to delete a file, using the name of file to be deleted as an
argument.
Function syntax:
os.remove(file_name)

>>>import os
# Delete the existing file test2.txt
>>>os.remove("test2.txt")
HCIA-AI Huawei Certification Course

HCIA-AI
Math Basics
Experimental Guide
Version: 1.0

Huawei Technologies Co., Ltd.

1
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://huawei.com

2019-02-22 Huawei confidential. No spreading without permission. Page 2 of 32


HCNA-AI Experimental Guide INTERNAL

Contents

1 Basic Math Experiment ............................................................................................................ 5


1.1 Introduction ........................................................................................................................................................... 5
1.1.1 Contents ............................................................................................................................................................. 5
1.1.2 Framework ......................................................................................................................................................... 5
1.2 Implementation ..................................................................................................................................................... 5
1.2.1 ceil Implementation ............................................................................................................................................ 5
1.2.2 floor Implementation .......................................................................................................................................... 6
1.2.3 cos Implementation ............................................................................................................................................. 6
1.2.4 tan Implementation ............................................................................................................................................. 6
1.2.5 degrees Implementation ...................................................................................................................................... 7
1.2.6 exp Implementation ............................................................................................................................................ 7
1.2.7 fabs Implementation ........................................................................................................................................... 7
1.2.8 factorial Implementation ..................................................................................................................................... 7
1.2.9 fsum Implementation .......................................................................................................................................... 7
1.2.10 fmod Implementation ........................................................................................................................................ 8
1.2.11 log Implementation ........................................................................................................................................... 8
1.2.12 sqrt Implementation .......................................................................................................................................... 8
1.2.13 pi Implementation ............................................................................................................................................. 8
1.2.14 pow Implementation ......................................................................................................................................... 8
1.2.15 trunc Implementation ........................................................................................................................................ 9

2 Linear Algebra Experiment ................................................................................................... 10


2.1 Introduction ..........................................................................................................................................................10
2.1.1 Linear Algebra ...................................................................................................................................................10
2.1.2 Code Implementation .........................................................................................................................................10
2.2 Linear Algebra Implementation .............................................................................................................................10
2.2.1 Tensor Implementation ......................................................................................................................................10
2.2.2 Matrix Implementation ...................................................................................................................................... 11
2.2.3 Identity Matrix Implementation .......................................................................................................................... 11
2.2.4 reshape Operation ..............................................................................................................................................12
2.2.5 Transposition Implementation ............................................................................................................................13
2.2.6 Matrix Multiplication Implementation ................................................................................................................14
2.2.7 Matrix Corresponding Operation ........................................................................................................................15

2019-02-22 Huawei confidential. No spreading without permission. Page 3 of 32


HCNA-AI Experimental Guide INTERNAL

2.2.8 Inverse Matrix Implementation ..........................................................................................................................15


2.2.9 Eigenvalue and Eigenvector ...............................................................................................................................16
2.2.10 Determinant .....................................................................................................................................................16
2.2.11 Singular Value Decomposition .........................................................................................................................17
2.2.12 Linear Equation Solving ..................................................................................................................................18

3 Probability Theory-based Experiment ................................................................................ 19


3.1 Introduction ..........................................................................................................................................................19
3.1.1 Probability Theory .............................................................................................................................................19
3.1.2 Experiment ........................................................................................................................................................19
3.2 Probability Theory Implementation .......................................................................................................................19
3.2.1 Average Value Implementation ...........................................................................................................................19
3.2.2 Variance and Standard Deviation Implementation ...............................................................................................20
3.2.3 Standard Deviation Implementation ...................................................................................................................20
3.2.4 Covariance Implementation ...............................................................................................................................21
3.2.5 Correlation Coefficient ......................................................................................................................................21
3.2.6 Binomial Distribution Implementation ...............................................................................................................21
3.2.7 Poisson Distribution Implementation ..................................................................................................................23
3.2.8 Normal Distribution ...........................................................................................................................................24
3.2.9 beta Distribution ................................................................................................................................................25
3.2.10 Exponential Distribution ..................................................................................................................................26

4 Other Experiments ................................................................................................................. 28


4.1 Least Square Implementation ................................................................................................................................28
4.1.1 Algorithm ..........................................................................................................................................................28
4.1.2 Code Implementation .........................................................................................................................................28
4.2 Gradient Descent Implementation .........................................................................................................................30
4.2.1 Algorithm ..........................................................................................................................................................30
4.2.2 Code Implementation .........................................................................................................................................30

2019-02-22 Huawei confidential. No spreading without permission. Page 4 of 32


HCNA-AI Experimental Guide INTERNAL

1 Basic Math Experiment

1.1 Introduction
1.1.1 Contents
Basic mathematics knowledge, including algorithm design and numerical processing
knowledge, is widely applied in the Artificial Intelligence (AI) domain, especially in the field
of traditional machine learning and deep learning. The main purpose of this section is to
implement some common mathematical knowledge based on the Python language and basic
mathematics modules to provide basic support for AI learning.

1.1.2 Framework
This document mainly uses the math library, numpy library, and scipy library. The math
library, a standard library of Python, provides some common mathematical functions; the
numpy library, a numerical calculation and expansion library of Python, is mainly used to
handle issues such as linear algebra, random number generation, and Fourier Transform; the
scipy library is used to deal with issues such as statistics, optimization, interpolation, and
bonus points.

1.2 Implementation
Import corresponding modules in advance.
>>> import math
>>> import numpy as np

1.2.1 ceil Implementation


The value of ceil(x) is the minimum integer greater than or equal to x. If x is an integer, the
returned value is x.
# Code:
>>> math.ceil(4.01)
# Output:

2019-02-22 Huawei confidential. No spreading without permission. Page 5 of 32


HCNA-AI Experimental Guide INTERNAL

>>> 5
# Code:
>>> math.ceil(4.99)
# Output:
>>> 5

1.2.2 floor Implementation


The value of floor(x) is the maximum integer less than or equal to x. If x is an integer, the
returned value is x.
# Code:
>>> math.floor(4.1)
# Output:
>>> 4
# Code:
>>> math.floor(4.999)
# Output:
>>> 4

1.2.3 cos Implementation


The cos(x) parameter is the cosine of x, where x must be a radian (math.pi/4 is a radian,
indicating an angle of 45 degrees).
# Code:
>>> math.cos(math.pi/4)
# Output:
>>> 0.7071067811865476
# Code:
>>> math.cos(math.pi/3)
# Output:
>>> 0.5000000000000001

1.2.4 tan Implementation


The tan(x) parameter returns the tangent value of x (radian).
# Code:
>>> tan(pi/6)
# Output:
>>> 0.5773502691896257

2019-02-22 Huawei confidential. No spreading without permission. Page 6 of 32


HCNA-AI Experimental Guide INTERNAL

1.2.5 degrees Implementation


The degrees(x) parameter converts x from a radian to an angle.
# Code:
>>> math.degrees(math.pi/4)
# Output:
>>> 45.0
# Code:
>>> math.degrees(math.pi)
# Output:
>>> 180.0

1.2.6 exp Implementation


The exp(x) parameter returns math.e, that is, the x power of 2.71828.
# Code:
>>> math.exp(1)
# Output:
>>> 2.718281828459045

1.2.7 fabs Implementation


The fabs(x) parameter returns the absolute value of x.
# Code:
>>> math.fabs(-0.003)
# Output:
>>> 0.003

1.2.8 factorial Implementation


The factorial(x) parameter is the factorial of x.
# Code:
>>> math.factorial(3)
# Output:
>>> 6

1.2.9 fsum Implementation


The fsum(iterable) summarizes each element in the iterator.
# Code:

2019-02-22 Huawei confidential. No spreading without permission. Page 7 of 32


HCNA-AI Experimental Guide INTERNAL

>>> math.fsum([1,2,3,4])
# Output:
>>>10

1.2.10 fmod Implementation


The fmod(x, y) parameter obtains the remainder of x/y. The value is a floating-point number.
# Code:
>>> math.fmod(20,3)
# Output:
>>>2.0

1.2.11 log Implementation


The log([x, base]) parameter returns the natural logarithm of x. By default, e is the base
number. If the base parameter is fixed, the logarithm of x is returned based on the given base.
The calculation formula is log(x)/log(base).
# Code:
>>> math.log(10)
# Output:
>>> 2.302585092994046

1.2.12 sqrt Implementation


The sqrt(x) parameter indicates the square root of x.
# Code:
>>> math.sqrt(100)
# Output:
>>>10.0

1.2.13 pi Implementation
The pi parameter is a numeric constant, indicating the circular constant.
# Code:
>>> math.pi
# Output:
>>> 3.141592653589793

1.2.14 pow Implementation


The pow(x, y) parameter returns the x to the yth power, that is, x**y.
# Code:

2019-02-22 Huawei confidential. No spreading without permission. Page 8 of 32


HCNA-AI Experimental Guide INTERNAL

>>> math.pow(3,4)
# Output:
>>> 81.0

1.2.15 trunc Implementation


The trunc(x:Real) parameter returns the integer part of x.
# Code:
>>> math.trunc(6.789)
# Output:
>>> 6

2019-02-22 Huawei confidential. No spreading without permission. Page 9 of 32


HCNA-AI Experimental Guide INTERNAL

2 Linear Algebra Experiment

2.1 Introduction
2.1.1 Linear Algebra
Linear algebra is a mathematical branch widely used in various engineering technical
disciplines. Its concepts and conclusions can greatly simplify the derivation and expression of
AI formulas. Linear algebra can simplify complex problems so that we can perform efficient
mathematical operations.
In the context of deep learning, linear algebra is a mathematical tool that provides a technique
that helps us to operate arrays at the same time. Data structures like vectors and matrices can
store numbers and rules for operations such as addition, subtraction, multiplication, and
division.

2.1.2 Code Implementation


The numpy is a numerical processing module based on Python. It has powerful functions and
advantages in processing matrix data. As linear algebra mainly processes matrices, this
section is mainly based on the numpy. This section also uses the mathematical science library
scipy to illustrate equation solution.

2.2 Linear Algebra Implementation


Import corresponding modules in advance.
>>> import numpy as np
>>> import scipy as sp

2.2.1 Tensor Implementation


Generate a two-dimensional tensor whose elements are 0 and two dimensions are 3 and 4.
# Code:
>>> np.zeros((3,4))
# Output:

2019-02-22 Huawei confidential. No spreading without permission. Page 10 of 32


HCNA-AI Experimental Guide INTERNAL

>>> np.array([[ 0., 0., 0., 0.],


[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
Generate a three-dimensional random tensor whose three dimensions are 2, 3, and 4
respectively.
# Code:
>>> np.random.rand(2,3,4)
# Output:
>>> array([[[ 0.93187582, 0.4942617 , 0.23241437, 0.82237576],
[ 0.90066163, 0.30151126, 0.89734992, 0.56656615],
[ 0.54487942, 0.80242768, 0.477167 , 0.6101814 ]],

[[ 0.61176321, 0.11454075, 0.58316117, 0.36850871],


[ 0.18480808, 0.12397686, 0.22586973, 0.35246394],
[ 0.01192416, 0.5990322 , 0.34527612, 0.424322 ]]])

2.2.2 Matrix Implementation


In mathematics, a matrix is a set of complex numbers or real numbers arranged by a
rectangular array. A matrix is derived from a square array consisted of coefficients and
constants of an equation set.
Create a 3x3 zero matrix. The parameter of the zeros function in the matrix is of the tuple
type (3, 3).
# Code:
>>> np.mat(np.zeros((3,3)));
# Output:
>>> matrix([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])

2.2.3 Identity Matrix Implementation


Identity matrix is a square array whose diagonal elements are all 1 and other elements are 0.
# Code:
>>> np.eye(4)
# Output:
>>> array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],

2019-02-22 Huawei confidential. No spreading without permission. Page 11 of 32


HCNA-AI Experimental Guide INTERNAL

[ 0., 0., 1., 0.],


[ 0., 0., 0., 1.]])

2.2.4 reshape Operation


There is no ‘reshape’ operation in mathematics, but it is a very common operation in the
operation libraries such as the numpy and TensorFlow. The reshape operation is used to
change the dimension number of a tensor and size of each dimension. For example, a 10x10
image is directly saved as a sequence containing 100 elements. After the system reads the
image, you can transform the image from 1x100 to 10x10 through the reshape operation. The
example is as follows:
# Code:
# Generate a vector that contains integers from 0 to 11.
>>> x = np.arange(12)
>>> x
# Output:
>>> array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
# View the array size.
>>> x.shape
# Output:
>>>(12,)
# Convert the matrix x into a two-dimensional matrix, where the first dimension of the matrix
is 1.
>>> x = x.reshape(1,12)
>>> x
# Output:
>>> array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]])
>>> x.shape
# Output:
>>>(1, 12)
# Convert x to a 3x4 matrix.
>>> x = x.reshape(3,4)
>>> x
# Output:
>>> array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

2019-02-22 Huawei confidential. No spreading without permission. Page 12 of 32


HCNA-AI Experimental Guide INTERNAL

2.2.5 Transposition Implementation


The transposition of vectors and matrices is exchanging the row and column. For the
transposition of the tensors in three dimensions and above, you need to specify the
transposition dimension.
# Code:
## Generate a vector x containing five elements and transposes the vector.
>>> x = np.arange(5).reshape(1,-1)
>>> x
array([[0, 1, 2, 3, 4]])
>>> x.T
array([[0],
[1],
[2],
[3],
[4]])
## Generate a 3x4 matrix and transpose the matrix.
>>> A = np.arange(12).reshape(3,4)
>>> A
# Output:
>>> array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> A.T
# Output:
>>> array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
## Generate a 2x3x4 tensor.
>>> B = np.arange(24).reshape(2,3,4)
>>> B
# Output:
>>> array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],

2019-02-22 Huawei confidential. No spreading without permission. Page 13 of 32


HCNA-AI Experimental Guide INTERNAL

[ 8, 9, 10, 11]],

[[12, 13, 14, 15],


[16, 17, 18, 19],
[20, 21, 22, 23]]])
## Transpose the 0 and 1 dimensions of B.
>>> B.transpose(1,0,2)
# Output:
>>> array([[[ 0, 1, 2, 3],
[12, 13, 14, 15]],

[[ 4, 5, 6, 7],
[16, 17, 18, 19]],

[[ 8, 9, 10, 11],
[20, 21, 22, 23]]])

2.2.6 Matrix Multiplication Implementation


To multiply the matrix A and matrix B, the column quantity of A must be equal to the row
quantity of B.
# Code:
>>> A = np.arange(6).reshape(3,2)
>>> B = np.arange(6).reshape(2,3)
>>> A
# Output:
>>> array([[0, 1],
[2, 3],
[4, 5]])
>>> B
# Output:
>>> array([[0, 1, 2],
[3, 4, 5]])
# Matrix multiplication
>>> np.matmul(A,B)

2019-02-22 Huawei confidential. No spreading without permission. Page 14 of 32


HCNA-AI Experimental Guide INTERNAL

# Output:
>>> array([[ 3, 4, 5],
[ 9, 14, 19],
[15, 24, 33]])

2.2.7 Matrix Corresponding Operation


Matrix corresponding operations are operations for tensors of the same shape and size. For
example, adding, subtracting, multiplying, and dividing the elements with the same position
in two tensors.
# Code:
# Matrix creation
>>> A = np.arange(6).reshape(3,2)
# Matrix multiplication
>>> A*A
# Output:
>>> array([[ 0, 1],
[ 4, 9],
[16, 25]])
# Matrix addition
>>> A + A
# Output:
>>> array([[ 0, 2],
[ 4, 6],
[ 8, 10]])

2.2.8 Inverse Matrix Implementation


Inverse matrix implementation is applicable only to square matrices.
# Code:
>>> A = np.arange(4).reshape(2,2)
>>> A
# Output:
>>> array([[0, 1],
[2, 3]])
>>> np.linalg.inv(A)
# Output:

2019-02-22 Huawei confidential. No spreading without permission. Page 15 of 32


HCNA-AI Experimental Guide INTERNAL

>>> array([[-1.5, 0.5],


[ 1. , 0. ]])

2.2.9 Eigenvalue and Eigenvector


Obtain the eigenvalue and eigenvector of a matrix.
# Code:
>>> import numpy as np # Introduce the numpy module.
>>> x= np.diag(1, 2, 3) # Write the diagonal matrix x.
>>> x #Output the diagonal matrix x.
# Output:
>>> array([[1,0,0],
[0,2,0],
[0,0,3]])
>>> a,b= np.linalg.eig(x) # Assign the eigenvalue to a, and corresponding eigenvector to b.
>>> a
# Feature values: 1, 2, 3
# Output:
>>> array([1.,2.,3.])
>>> b
# Eigenvector
# Output:
>>> array([1.,0.,0.],
[0.,1.,0.],
[0.,0.,1.])

2.2.10 Determinant
Obtain the determinant of a matrix.
# Code:
>>> E = array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> F = array([[-1, 0, 1],
[ 2, 3, 4],
[ 5, 6, 7]])

2019-02-22 Huawei confidential. No spreading without permission. Page 16 of 32


HCNA-AI Experimental Guide INTERNAL

# Output:
>>> np.linalg.det(E)
>>> 6.6613381477509402e-16
# Output:
>>> np.linalg.det(F)
>>> 2.664535259100367e-15

2.2.11 Singular Value Decomposition


Create a matrix and perform singular value decomposition on the matrix.
# Code:
dataMat = [[1,1,1,0,0],
[2,2,2,0,0],
[1,1,1,0,0],
[5,5,5,0,0],
[1,1,0,2,2]]
>>> dataMat = mat(dataMat)
>>> U,Simga,VT = linalg.svd(dataMat)
>>> U
# Output:
>>> matrix([[ -1.77939726e-01, -1.64228493e-02, 1.80501685e-02,
9.53086885e-01, -3.38915095e-02, 2.14510824e-01,
1.10470800e-01],
[ -3.55879451e-01, -3.28456986e-02, 3.61003369e-02,
-5.61842993e-02, -6.73073067e-01, -4.12278297e-01,
4.94783103e-01],
[ -1.77939726e-01, -1.64228493e-02, 1.80501685e-02,
-2.74354465e-01, -5.05587078e-02, 8.25142037e-01,
4.57226420e-01],
[ -8.89698628e-01, -8.21142464e-02, 9.02508423e-02,
-1.13272764e-01, 2.86119270e-01, -4.30192532e-02,
-3.11452685e-01]])
>>> Simga
# Output:
>>>array([ 9.72140007e+00, 5.29397912e+00, 6.84226362e-01,

2019-02-22 Huawei confidential. No spreading without permission. Page 17 of 32


HCNA-AI Experimental Guide INTERNAL

1.52344501e-15, 2.17780259e-16])
>>> VT
# Output:
>>> matrix([[ -5.81200877e-01, -5.81200877e-01, -5.67421508e-01,
-3.49564973e-02, -3.49564973e-02],
[ 4.61260083e-03, 4.61260083e-03, -9.61674228e-02,
7.03814349e-01, 7.03814349e-01],
[ -4.02721076e-01, -4.02721076e-01, 8.17792552e-01,
5.85098794e-02, 5.85098794e-02],
[ -7.06575299e-01, 7.06575299e-01, -2.22044605e-16,
2.74107087e-02, -2.74107087e-02],
[ 2.74107087e-02, -2.74107087e-02, 2.18575158e-16,
7.06575299e-01, -7.06575299e-01]])

2.2.12 Linear Equation Solving


Solving a linear equation is simple because it requires only one function (scipy.linalg.solve).
An example is finding the solution of the following system of non-homogeneous linear
equations:
3x_1 + x_2 - 2x_3 = 5
x_1 - x_2 + 4x_3 = -2
2x_1 + 3x_3 = 2.5
# Code:
>>> from scipy.linalg import solve
>>> a = np.array([[3, 1, -2], [1, -1, 4], [2, 0, 3]])
>>> b = np.array([5, -2, 2.5])
>>> x = solve(a, b)
>>> x
# Output:
>>> [0.5 4.5 0.5]

2019-02-22 Huawei confidential. No spreading without permission. Page 18 of 32


HCNA-AI Experimental Guide INTERNAL

3 Probability Theory Experiment

3.1 Introduction
3.1.1 Probability Theory
Probability theory is a mathematical branch of studying the quantitative regularity of random
phenomena. A random phenomenon is different from a decisive phenomenon in that a
decisive phenomenon inevitably occurs under certain conditions.
The probability theory is a mathematical tool used to describe uncertainty. A large number of
AI algorithms build models using the probabilistic or inference information of samples.

3.1.2 Experiment
This section describes the knowledge points of probability and statistics, and mainly uses the
numpy and scipy frameworks.

3.2 Probability Theory Implementation


Import the corresponding modules in advance.
>>> import numpy as np
>>> import scipy as sp

3.2.1 Average Value Implementation


# Prepare the data.
>>> b = [1,3,5,6]
>>> ll = [[1,2,3,4,5,6],[3,4,5,6,7,8]]
# Code:
>>> np.mean(b)
# Output:
>>> 3.75

2019-02-22 Huawei confidential. No spreading without permission. Page 19 of 32


HCNA-AI Experimental Guide INTERNAL

>>> np.mean(ll) # Calculate the average value of all elements.


# Output:
>>> 4.5
>>> np.mean(ll,0) # Calculate the average value by column.
# Output:
>>> [2. 3. 4. 5. 6. 7.]
>>> np.mean(ll,1) # Calculate the average value by row.
# Output:
>>> [3.5 5.5]

3.2.2 Variance and Standard Deviation Implementation


# Prepare the data.
>>> b=[1,3,5,6]
>>> ll=[[1,2,3,4,5,6],[3,4,5,6,7,8]]
# Find the variance.
# Code:
>>> np.var(b)
# Output:
>>> 3.6875
# Code:
>>> np.var(ll[0])
# Output:
>>> 3.6874999999999996
# Code:
>>> np.var(ll,1)) # The second parameter is 1, indicating that the variance is calculated by
row.
# Output:
>>> [2.91666667 2.91666667]

3.2.3 Standard Deviation Implementation


# Prepare the data.
>>> b=[1,3,5,6]
>>> ll=[[1,2,3,4,5,6],[3,4,5,6,7,8]]
# Code:
>>> np.std(b)

2019-02-22 Huawei confidential. No spreading without permission. Page 20 of 32


HCNA-AI Experimental Guide INTERNAL

# Output:
>>> 1.920286436967152
# Code:
>>> np.std(ll)
# Output:
>>> 1.9790570145063195

3.2.4 Covariance Implementation


# Prepare the data.
>>> b=[1,3,5,6]
# Code:
>>> np.cov(b)
# Output:
>>> 4.916666666666666

3.2.5 Correlation Coefficient


# Prepare the data.
>>> vc=[1,2,39,0,8]
>>> vb=[1,2,38,0,8]
Complete implementation using a function.
# Code:
>>> np.corrcoef(vc,vb)
# Output:
>>> 4.916666666666667
Complete implementation through customization.
# Code:
>>> np.mean(np.multiply((vc-np.mean(vc)),(vb-np.mean(vb))))/(np.std(vb)*np.std(vc))
# Output:
>>> 4.916666666666666

3.2.6 Binomial Distribution Implementation


The random variable X, which complies with binomial distribution, indicates the number of
successful times (n-th and independent with the same distribution in the Bernoulli trial). The
success rate of each test is p.
# Code:
from scipy.stats import binom, norm, beta, expon

2019-02-22 Huawei confidential. No spreading without permission. Page 21 of 32


HCNA-AI Experimental Guide INTERNAL

import numpy as np
import matplotlib.pyplot as plt

binom_sim = binom.rvs(n=10, p=0.3, size=10000)


print('Data:',binom_sim)
print('Mean: %g' % np.mean(binom_sim))
print('SD: %g' % np.std(binom_sim, ddof=1))
plt.hist(binom_sim, bins=10, normed=True)
plt.xlabel(('x'))
plt.ylabel('density')
plt.show()
# Output:
Data: [2 4 3 ... 3 4 1]
Mean: 2.9821
SD: 1.43478
# The distribution figure is as follows.

2019-02-22 Huawei confidential. No spreading without permission. Page 22 of 32


HCNA-AI Experimental Guide INTERNAL

3.2.7 Poisson Distribution Implementation


A random variable X that obeys the Poisson distribution indicates the number of occurrences
of an event within a fixed time interval that meets the λ parameter. The parameter λ indicates
the rate of an event. Both the average value and variance of the random variable X are λ.
# Code:
import numpy as np
import matplotlib.pyplot as plt

x = np.random.poisson(lam=5, size=10000)
pillar = 15

a = plt.hist(x, bins=pillar, normed=True, range=[0, pillar], color='g', alpha=0.5)


plt.plot(a[1][0:pillar], a[0], 'r')
plt.grid()
plt.show()
# The distribution figure is as follows.

The preceding figure can be displayed only in a visual system. Trainees can perform the test in the local
environment.

2019-02-22 Huawei confidential. No spreading without permission. Page 23 of 32


HCNA-AI Experimental Guide INTERNAL

3.2.8 Normal Distribution


Normal distribution is continuous. Its function can obtain a value anywhere on the real line.
Normal distribution is described by two parameters: average value μ and standard deviation
σ of distribution.
# Code:
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt

mu = 0
sigma = 1
x = np.arange(-5, 5, 0.1)
y = norm.pdf(x, mu, sigma)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('density')
plt.show()
# The distribution figure is as follows.

2019-02-22 Huawei confidential. No spreading without permission. Page 24 of 32


HCNA-AI Experimental Guide INTERNAL

The preceding figure can be displayed only in a visual system. Trainees can perform the test in the local
environment.

3.2.9 beta Distribution


The beta distribution, continuous distribution with values between 0 and 1, is described by the
values of two morphological parameters α and β.
# Code:
from scipy.stats import beta
import numpy as np
import matplotlib.pyplot as plt

a = 0.5
b = 0.5
x = np.arange(0.01, 1, 0.01)
y = beta.pdf(x, a, b)
plt.plot(x, y)
plt.title('Beta: a=%.1f,b=%.1f' % (a, b))
plt.xlabel('x')
plt.ylabel('density')
plt.show()
# The distribution figure is as follows.

2019-02-22 Huawei confidential. No spreading without permission. Page 25 of 32


HCNA-AI Experimental Guide INTERNAL

The preceding figure can be displayed only in a visual system. Trainees can perform the test in the local
environment.

3.2.10 Exponential Distribution


Exponential distribution is continuous probability distribution, indicating the time interval of
independent random events (for example, time interval for entering the airport by passengers
or for calling the customer service center).
# Code:
from scipy.stats import expon
import numpy as np
import matplotlib.pyplot as plt

lam = 0.5
x = np.arange(0, 15, 0.1)
y = expon.pdf(x, lam)
plt.plot(x, y)
plt.title('Exponential: lam=%.2f' % lam)
plt.xlabel('x')
plt.ylabel('density')
plt.show()
# The distribution figure is as follows.

2019-02-22 Huawei confidential. No spreading without permission. Page 26 of 32


HCNA-AI Experimental Guide INTERNAL

The preceding figure can be displayed only in a visual system. Trainees can perform the test in the local
environment.

2019-02-22 Huawei confidential. No spreading without permission. Page 27 of 32


HCNA-AI Experimental Guide INTERNAL

4 Other Experiments

4.1 Least Square Implementation


4.1.1 Algorithm
The least square method, as the basis of the classification regression algorithm, has a long
history. It searches for the best function of the data through the least sum of square error. The
least square method can help easily obtain unknown parameters, and ensure that the square
sum of the error between the predicted data and the actual data is the smallest.

4.1.2 Code Implementation


# Code:
import numpy as np # Introduce the numpy.
import scipy as sp
import pylab as pl
from scipy.optimize import leastsq # Introduce the least square function.

n = 9 # Degree of polynomial

# Target function
def real_func(x):
return np.sin(2 * np.pi * x)

# Polynomial function
def fit_func(p, x):
f = np.poly1d(p)
return f(x)

2019-02-22 Huawei confidential. No spreading without permission. Page 28 of 32


HCNA-AI Experimental Guide INTERNAL

# Residual function
def residuals_func(p, y, x):
ret = fit_func(p, x) - y
return ret

x = np.linspace(0, 1, 9) # Randomly selected nine points as x.


x_points = np.linspace(0, 1, 1000) # Continuous points required for drawing the graph

y0 = real_func(x) # Target function


y1 = [np.random.normal(0, 0.1) + y for y in y0] # Function after the positive distribution noise
is added
p_init = np.random.randn(n) # Randomly initialize polynomial parameters.
plsq = leastsq(residuals_func, p_init, args=(y1, x))

print('Fitting Parameters: ', plsq[0]) # Output fitting parameters.

pl.plot(x_points, real_func(x_points), label='real')


pl.plot(x_points, fit_func(plsq[0], x_points), label='fitted curve')
pl.plot(x, y1, 'bo', label='with noise')
pl.legend()
pl.show()
# Output:
Fitting Parameters: [-1.22007936e+03 5.79215138e+03 -1.10709926e+04
1.08840736e+04
-5.81549888e+03 1.65346694e+03 -2.42724147e+02 1.96199338e+01
-2.14013567e-02]
# Visualized chart

2019-02-22 Huawei confidential. No spreading without permission. Page 29 of 32


HCNA-AI Experimental Guide INTERNAL

The preceding figure can be displayed only in a visual system. Trainees can perform the test in the local
environment.

4.2 Gradient Descent Implementation


4.2.1 Algorithm
Gradient descent (steepest descent) is the most common method for solving unconstrained
optimization problems. It is an iterative method. Each step mainly finds the gradient vector of
the target function and uses the negative gradient direction of the current position as the
search direction (the target function decreases the fastest in this direction, which is the
reason why it is called steepest descent method).
The gradient descent method has the following characteristics: The closer the function to the
target value, the smaller is the step and the slower is the decrease rate.

4.2.2 Code Implementation


# Code:
# Training set
# Each sample point has three components (x0, x1, and x2).
x = [(1, 0., 3), (1, 1., 3), (1, 2., 3), (1, 3., 2), (1, 4., 4)]
# y[i] (Output of the sample point)

2019-02-22 Huawei confidential. No spreading without permission. Page 30 of 32


HCNA-AI Experimental Guide INTERNAL

y = [95.364, 97.217205, 75.195834, 60.105519, 49.342380]


# Iteration threshold (When the difference between the two iteration loss functions is less than
the threshold, the iteration stops.)
epsilon = 0.0001
# Learning rate
alpha = 0.01
diff = [0, 0]
max_itor = 1000
error1 = 0
error0 = 0
cnt = 0
m = len(x)

# Parameter initialization
theta0 = 0
theta1 = 0
theta2 = 0

while True:
cnt += 1

# Parameter iteration calculation


for i in range(m):
# The fitting function is y = theta0 * x[0] + theta1 * x[1] +theta2 * x[2].
# Residual error calculation
diff[0] = (theta0 + theta1 * x[i][1] + theta2 * x[i][2]) - y[i]

# Gradient = diff[0] * x[i][j]


theta0 -= alpha * diff[0] * x[i][0]
theta1 -= alpha * diff[0] * x[i][1]
theta2 -= alpha * diff[0] * x[i][2]

# Loss function calculation


error1 = 0

2019-02-22 Huawei confidential. No spreading without permission. Page 31 of 32


HCNA-AI Experimental Guide INTERNAL

for lp in range(len(x)):
error1 += (y[lp]-(theta0 + theta1 * x[lp][1] + theta2 * x[lp][2]))**2/2

if abs(error1-error0) < epsilon:


break
else:
error0 = error1

print(' theta0: %f, theta1: %f, theta2: %f, error1: %f' % (theta0, theta1, theta2, error1) )

print('Done: theta0 : %f, theta1 : %f, theta2 : %f' % (theta0, theta1, theta2) )
print ('Number of iterations: %d' % cnt )
# Output:
theta0 : 2.782632, theta1 : 3.207850, theta2 : 7.998823, error1 : 5997.941160
theta0 : 4.254302, theta1 : 3.809652, theta2 : 11.972218, error1 : 3688.116951
theta0 : 5.154766, theta1 : 3.351648, theta2 : 14.188535, error1 : 2889.123934
theta0 : 5.800348, theta1 : 2.489862, theta2 : 15.617995, error1 : 2490.307286
theta0 : 6.326710, theta1 : 1.500854, theta2 : 16.676947, error1 : 2228.380594
theta0 : 6.792409, theta1 : 0.499552, theta2 : 17.545335, error1 : 2028.776801
……
theta0 : 97.717864, theta1 : -13.224347, theta2 : 1.342491, error1 : 58.732358
theta0 : 97.718558, theta1 : -13.224339, theta2 : 1.342271, error1 : 58.732258
theta0 : 97.719251, theta1 : -13.224330, theta2 : 1.342051, error1 : 58.732157
Done: theta0 : 97.719942, theta1 : -13.224322, theta2 : 1.341832
Number of iterations: 2608

2019-02-22 Huawei confidential. No spreading without permission. Page 32 of 32


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


HCIA-AI Huawei Certification Course

HCIA-AI
Image Recognition Programming
Experiment Guide
Version: 1.0

Huawei Technologies Co., Ltd.

1
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://huawei.com

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. i


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide Introduction to Huawei Certification System

Introduction to Huawei Certification System

Based on cutting-edge technologies and professional training systems, Huawei certification


meets the diverse AI technology demands of various 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-08-02) Copyright © Huawei Technologies Co., Ltd. ii


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide About This Document

About This Document

Brief Introduction
This document is an HCIA-AI certification training course. It is intended for trainees who are
preparing for HCIA-AI tests or readers who want to know about AI basics. After
understanding this document, you will be able to perform basic AI image recognition
programming.

Contents
This document contains one experiment (TensorFlow image recognition) to help trainees or
readers get started and build the basic AI programming capability.
The experiment includes image data preparation, library and module installation, and finally
image recognition realization.

Background Knowledge Required


To better master the contents of this course, the audience should:
Have basic Python language programming capabilities;
Have a basic understanding of data structures;
Have a basic understanding of AI.

Experiment Environment Overview

Python Development Tool


This experiment environment is developed and compiled based on the Python 3.6.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. iii


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide Contents

Contents

Introduction to Huawei Certification System ........................................................................... ii


About This Document ................................................................................................................... iii
1 Image Recognition Experiment Overview ............................................................................... 1
1.1 Experimental Environment ........................................................................................................................................... 1
1.1.1 Experimental Environment ........................................................................................................................................ 1
1.1.2 Main Framework ....................................................................................................................................................... 1
1.1.3 Application Framework List ...................................................................................................................................... 2
1.2 Data Set Introduction .................................................................................................................................................... 2
1.2.1 Data Set...................................................................................................................................................................... 2
1.2.2 Data Set Obtaining ..................................................................................................................................................... 2
1.3 CNN .............................................................................................................................................................................. 2
1.3.1 Structure..................................................................................................................................................................... 2
1.3.2 Overview ................................................................................................................................................................... 3
1.4 Experiment.................................................................................................................................................................... 3
1.4.1 Content....................................................................................................................................................................... 3
1.4.2 Step ............................................................................................................................................................................ 3
1.5 Summary ....................................................................................................................................................................... 4

2 Data Preparation for Image Recognition .................................................................................. 5


2.1 Obtaining Data .............................................................................................................................................................. 5
2.1.1 Entering the Development Environment.................................................................................................................... 5
2.1.2 Downloading Data ..................................................................................................................................................... 8
2.2 Viewing Data ................................................................................................................................................................ 9
2.2.1 Account Function ....................................................................................................................................................... 9
2.3 Summary ..................................................................................................................................................................... 10

3 Implementation of Image Recognition Based on the TensorFlow .................................... 11


3.1 Installing the scikit-image Framework ....................................................................................................................... 11
3.2 Image Recognition Implementation ............................................................................................................................ 12
3.2.1 Experiment Roadmap .............................................................................................................................................. 12
3.2.2 Module Importing .................................................................................................................................................... 13
3.2.3 Data Reading and Standardization ........................................................................................................................... 13

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. iv


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide Contents

3.2.4 Data Pre-processing ................................................................................................................................................. 14


3.2.5 Placeholder Defining ............................................................................................................................................... 15
3.2.6 CNN Construction ................................................................................................................................................... 15
3.2.7 Loss Function, Optimizer, and Verification Indicator Defining ............................................................................... 17
3.2.8 Model Training and Verification .............................................................................................................................. 18
3.2.9 Model Saving and Session Closing.......................................................................................................................... 19
3.3 Sample Image Test ...................................................................................................................................................... 20
3.3.1 Invoking the Model for Testing ............................................................................................................................... 20
3.4 Summary ..................................................................................................................................................................... 22

4 Image Recognition Effect Display ........................................................................................... 23


4.1 Script Test ................................................................................................................................................................... 23
4.1.1 Test Roadmap .......................................................................................................................................................... 23
4.1.2 Test Process.............................................................................................................................................................. 23
4.2 Summary ..................................................................................................................................................................... 30

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. v


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 1 Image Recognition Experiment Overview

1 Image Recognition Experiment Overview

1.1 Experimental Environment


1.1.1 Experimental Environment
The development environment used in this experiment is based on the established deep
learning environment (version: CPU). For details about how to set up the environment, see the
HCIA-AI Lab Environment Setup Experiment Manual. Because the size of the image data set
samples used by this experiment is small, the GPU is not used for model training and
iteration. In addition, you need to install the corresponding framework before the experiment
starts.

1.1.2 Main Framework


This experiment is mainly used to identify flower images. The following frameworks are
used:
scikit-image: mainly used for image processing. The native NumPy array is used as the image
object;
TensorFlow: mainly used to develop deep learning algorithms and construct a convolutional
neural network (CNN);
numpy: used for matrix and numerical value processing;
glob: used to search for a file path complying with a specific rule. This framework is similar
to the file search function in the Windows operating system;
OS: provides a large number of functions to process files and directories;
time: processes and converts time.
The following figure shows the module.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 1


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 1 Image Recognition Experiment Overview

1.1.3 Application Framework List


Python
Numpy
Scikit-image
Tensorflow
os
glob
time
Vim
Git

1.2 Data Set Introduction


1.2.1 Data Set
This document uses an open-source data set that contains five types of flowers: daisy (633
images), dandelion (898 images), roses (641 images), sunflowers (699 images), and tulips
(799 images). The 3670 images with a size of about 230 MB are saved in five directories.

1.2.2 Data Set Obtaining


 The data can be obtained from Huawei Cloud Object Storage Service (OBS).
To help trainees learn the HCIA-AI course, the OBS is enabled on Huawei cloud for
trainees to obtain the data set.
Link: https://data-certification.obs.cn-east-2.myhuaweicloud.com/ENG/HCIA-
AI/V1.0/image.zip
 The data can be downloaded from the following website:
Link: http://download.tensorflow.org/example_images/flower_photos.tgz

1.3 CNN
1.3.1 Structure
The CNN, one of the mainstream deep learning and neural network algorithms, is mainly used
for image recognition. The following figure shows the CNN structure.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 2


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 1 Image Recognition Experiment Overview

1.3.2 Overview
The CNN is an in-depth Forward Feedback Artificial Neural Network (FFANN), which has
been successfully applied in the field of image recognition. The network structure mainly
includes the input layer, convolutional layer, pooling layer, full connection layer, and output
layer.
Generally, the basic structure of the CNN includes two layers. The first layer is the feature
extraction layer: The input of each neuron is connected to the local acceptance field of the
previous layer, and the features of the local acceptance field are extracted. Once a local
feature is extracted, the location relationship between it and other features is determined. The
second layer is the feature mapping layer. Each computing layer of the network consists of
multiple feature mappings. Each feature mapping is a plane and the weights of all neurons on
the plane are the same. The feature mapping structure uses the sigmoid function that has the
smallest influence function kernel as the activation function of the convolutional network, so
that the feature mapping has the rotation invariance.

1.4 Experiment
1.4.1 Content
This experiment focuses on image recognition. Based on TensorFlow depth learning
framework and CNN, it aims to identify flower types using an open-source data set.

1.4.2 Step
The main steps of the experiment are as follows:
1. Importing frameworks
2. Preparing paths
3. Processing images
4. Defining placeholders
5. Defining the network structure
6. Defining the loss function and optimizer.
7. Model training and prediction
8. Model prediction

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 3


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 1 Image Recognition Experiment Overview

1.5 Summary
This chapter mainly introduces the environment and data set of the image recognition
experiment, briefly explains the CNN, and addresses the preparations based on the
TensorFlow framework.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 4


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 2 Data Preparation for Image Recognition

2 Data Preparation for Image Recognition

2.1 Obtaining Data


2.1.1 Entering the Development Environment
Step 1 Visit HUAWEI CLOUD official website.
Link: http://www.huaweicloud.com/.

Click Console in the upper right corner.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 5


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 2 Data Preparation for Image Recognition

Click Elastic Cloud Server. The server console page is displayed.

Click Remote Login to enter the environment.


Step 2 Enter the development environment.
The initial page is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 6


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 2 Data Preparation for Image Recognition

Enter the user name and password to log in to the system.

----End

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 7


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 2 Data Preparation for Image Recognition

2.1.2 Downloading Data


Step 1 Create the file directory HCIA-AI.
Run ll and view the existing file folders in the directory.

Run mkdir HCIA-AI and ll to create a file folder. Check the result.

Step 2 Switch to the HCIA-AI file folder.


Run cd HCIA-AI to download the data set.

Step 3 Download the data set from the OBS.


Run wget https://data-certification.obs.cn-east-2.myhuaweicloud.com/ENG/HCIA-
AI/V1.0/image.zip to download the data set.

----End

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 8


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 2 Data Preparation for Image Recognition

2.2 Viewing Data


2.2.1 Account Function
Step 1 View the downloaded package.
Run ll to check the package.

Step 2 Decompress the package.


Run unzip image.zip and view the existing file folders in the directory.

Run ll to check the decompressed files.

Step 3 View the data set.


Run cd flower_photos and ll to check the downloaded files.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 9


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 2 Data Preparation for Image Recognition

Run cd daisy and ll to view the data details.

The preceding jpg files are the sample pictures in the flower_photos data set.
----End

2.3 Summary
This chapter describes how to obtain the experiment data, download the experiment data, and
view related data sets to facilitate subsequent data operations.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 10


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

3 Implementation of Image Recognition


Based on the TensorFlow

3.1 Installing the scikit-image Framework


The scikit-image framework is required to process image data. The scikit-image framework is
not installed in the original development environment. Therefore, install the scikit-image
framework before the experiment starts. To install the scikit-image framework, perform the
following steps:
Step 1 Go to the main directory.
Run cd and ll.

Step 2 Install the scikit-image framework.


Run pip3 install scikit-image.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 11


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

Step 3 Check the installation result.


Run pip3 list.

----End

3.2 Image Recognition Implementation


3.2.1 Experiment Roadmap
1. This experiment classifies flower images by flower type. It is based on an open-source
data set flower_photos and an 11-layer deep CNN (including two convolutional layers,
two pooling layers, and two full connection layers). After data training is complete, one
image is extracted from each type of training data set for model verification.
2. This experiment complies with the standard development process. The involved steps
include data preparation, data preprocessing, variable and parameter defining, network
model construction, loss function and optimizer defining, model training and testing, and
model invoking.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 12


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

3.2.2 Module Importing


Import modules.
skimage: package for processing image data.
io: module for image data reading (imread) and output (imshow);
transform: module for changing the image size (using the resize function);
glob: package for searching for a file path that meets a specified rule (this approach is similar
to the file search in the Windows operating system);
os: module for processing files and directories. The operations include obtaining the files in
the current directory, deleting a specified file, changing the directory, and viewing the file
size;
Tensorflow: the most popular in-depth learning framework in the industry. It is widely applied
in fields such as image, voice, text, and target detection. The Tensorflow is also the core of the
experiment. It is used to define placeholders and variables, and create a CNN models;
numpy: scientific computing package based on python. It is used to process numerical
calculation in this experiment.
time: module for processing time data. In this experiment, the time module is used to return
the current time stamp and calculate the time required for running each epoch script.

3.2.3 Data Reading and Standardization


Step 1 Prepare the path.
In the following command, path indicates the data storage path and model_path indicates the
path for saving the model.

Step 2 Prepare the standard image parameters.


In the following command, w, h, and c are the length, width, and depth of the image
respectively. The image is in RGB format.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 13


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

Step 3 Perform data reading and standardization.


After the image data is processed, the images are in the 100x100x3 format. Map the images to
the tags and save the settings. Then, use the print() function to check the size of each image
and tag.

----End

3.2.4 Data Pre-processing


Step 1 Disrupt the original data sequence.
Use shuffle to interrupt the data set and prevent data distribution from affecting the model
results.

Step 2 Set the training set and verification set.


The creation ratio is 0.8, indicating that 80% of the data is used for model training and 20% is
used for model verification.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 14


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

----End

3.2.5 Placeholder Defining


Create placeholders x and y to perform model calculation for the CNN model to transmit
tensor data.

3.2.6 CNN Construction


Step 1 Create a CNN and the first hidden layer.
Define the CNN model. The three variables are input_tensor (input tensor), train (for
distinguishing the training process with the verification process), and regularizer (regular
expression).
For the first volume layer layer1-conv1, conv1_weights, conv1_biases, conv1, and relu1 are
the weight, offset, convolutional layer computing, and activation function respectively.

Step 2 Create the second hidden layer.


The second hidden layer is the first pooling layer, which is layer2-pool1. This layer can
preferentially narrow the size of the matrix, thereby reducing the parameters in the last full
connection layer. The pooling layer can also accelerate calculation and prevent overfitting.

Step 3 Create the third hidden layer.


The third hidden layer is the second volume layer, which is layer1-conv2.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 15


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

Step 4 Create the fourth hidden layer.


The fourth hidden layer is the second pooling layer, which is layer2-pool2.

Step 5 Create the fifth hidden layer.


Create the first full connection layer layer5-fc1 for the combination of the preceding features.

Step 6 Create the sixth hidden layer.


The sixth hidden layer is the second full connection layer layer5-fc2 for the combination of
the preceding features.

Step 7 View the overall network structure.


The entire model has six hidden layers: two convolutional layers, two pooling layers, and two
full connection layers.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 16


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

Step 8 Define regular terms.


This document mainly uses L2 regular terms to prevent over-fitting and improve model
generalization capabilities.

Step 9 View the model.


Check the structure of the final model result to prepare for the subsequent model test.

----End

3.2.7 Loss Function, Optimizer, and Verification


Indicator Defining
Step 1 Define the loss function.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 17


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

Calculate the sparse cross entropy between the prediction tag logits and original tag y_.

Step 2 Define the optimizer.


Use the adam optimizer.

Step 3 Define the optimization target.


Define the training operation and minimize the loss.

Step 4 Define the accuracy of verification indicators.


Define the accuracy function for model effect verification.

----End

3.2.8 Model Training and Verification


Step 1 Define the function for extracting data in batches.

Step 2 Prepare training parameters and perform model initialization.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 18


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

Step 3 Define the model operation on the training set and verification set, and print the results.
Train a total of five epoches. Each epoch needs to run on the training set and verification set.
Print the corresponding loss value and accuracy of two data sets as well as the duration of
each epoch operation.

----End

3.2.9 Model Saving and Session Closing


Step 1 Save the model.
Use the save function of the saver to save the model after the sess operation. The path is
model_path.

Step 2 Close the session.


After sess running ends, close the session.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 19


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

3.3 Sample Image Test


3.3.1 Invoking the Model for Testing
Step 1 Import modules.
Extract an image of the five types of images to test the model effect. The following is the path
and name of each image.

Step 2 Generate a type dictionary.


Generate a type dictionary for each flower type for type matching.

Step 3 Define the image standardization function.


Standardize the test images and convert the images into standard RGB images (100x100x3).

Step 4 Standard the test data.


Invoke the read_one_image function to standardize the test image.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 20


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

Step 5 Reload the model.


Reload the existing model to test the images.

Step 6 Output model parameters.


Output the parameters in the current calculation diagram tensor.

Step 7 Invoke the model.


Invoke the model for testing. Print the prediction matrix, and the subscript of the maximum
value of each row of the prediction matrix.

Step 8 Print the test result.


Print the prediction results of five test data records.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 21


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

----End

3.4 Summary
This chapter describes how to construct a CNN, define the model, set related parameters, and
test sample data based on the TensorFlow.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 22


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 4 Image Recognition Effect Display

4 Image Recognition Effect Display

4.1 Script Test


4.1.1 Test Roadmap
Perform the Python script test using the established experiment environment and VIM editor.

4.1.2 Test Process


Step 1 Access the HCIA-AI directory.
cd HCIA-AI

In the file directory, flower_photos is the data set and image.py is the image recognition
script.
Step 2 Open vim and create the Python script.
vim image_recog.py

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 23


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 4 Image Recognition Effect Display

Step 3 Edit the script.


Run i. Enter the edit mode and start editing.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 24


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 4 Image Recognition Effect Display

Step 4 Enter the following contents.


import tensorflow as tf
import numpy as np
……

Step 5 Save the script and exit.


Press ESC and run :wq!.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 25


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 4 Image Recognition Effect Display

Step 6 View the script content.


cat image_recog.py

cat image.py

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 26


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 4 Image Recognition Effect Display

Step 7 Perform the test.


python3 image_recog.py

Step 8 Perform the complete script test.


python3 image.py
The script is executed to read the image data.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 27


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 4 Image Recognition Effect Display

The output of each epoch training process is as follows.

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 28


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 4 Image Recognition Effect Display

The model result output is as follows.

----End

Issue 1.0 (2018-08-02) Copyright © Huawei Technologies Co., Ltd. 29


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide 4 Image Recognition Effect Display

4.2 Summary
This chapter describes how to recognize images based on data sets and test the model effect.
As shown in the figure, the accuracy is high.

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


HCIA-AI Huawei Certification Course

HCIA-AI
Speech Recognition Programming
Experiment Guide
Version: 1.0

Huawei Technologies Co., Ltd.

1
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 or 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.


Huawei Industrial Base
Bantian, Longgang
Address:
Shenzhen 518129
People's Republic of China

Website: http://e.huawei.com

Huawei Confidential
Copyright © Huawei Technologies Co., Ltd.
HCIA-AI Experiment Guide Page 2

Introduction to Huawei Certification System

Based on cutting-edge technologies and professional training systems, Huawei certification meets
the diverse AI technology demands of various 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, speech 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, speech 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 are 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, speech 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!
HCIA-AI Experiment Guide Page 3

Preface

Basic Introduction
This document is an HCIA-AI certification training course, and is intended to trainees who are
preparing for HCIA-AI tests or readers who want to know about AI basics. After understanding this
document, you can perform basic AI speech recognition programming.

Contents
This experiment guide includes one experiment, covering speech recognition, and is intended to help
trainees and readers complete the speech recognition experiment on their own.
Chapter 1 introduces the speech recognition experiment.
Chapter 2 describes preparations for the speech recognition model and relevant functions.
Chapter 3 describes code implementation of speech recognition and model training, and presents
training results.

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 the basic Python editing capability, and basic knowledge of data structures and AI.

Experimental Environment
Python Development Tool
This experimental environment is compiled based on Python 3.6.
HCIA-AI Experiment Guide Page 4

Contents

Preface ............................................................................................................................ 3
Basic Introduction .................................................................................................................................................3
Contents ...............................................................................................................................................................3
Knowledge Background of Readers ........................................................................................................................3
Experimental Environment .....................................................................................................................................3
1 Speech Recognition Experiment...................................................................................... 6
1.1 Introduction to the Experiment ..........................................................................................................................6
1.1.1 About the Experiment ....................................................................................................................................6
1.1.2 Objectives of the Experiment .........................................................................................................................6
1.1.3 Experiment Roadmap ....................................................................................................................................6
1.2 Experiment Content .........................................................................................................................................7
1.2.1 Dataset .........................................................................................................................................................7
1.2.2 Algorithm .....................................................................................................................................................7
2 Preparations for the Speech Recognition Model ............................................................... 1
2.1 Model Preparations ..........................................................................................................................................1
2.1.1 Introduction ..................................................................................................................................................1
2.1.2 Objective ......................................................................................................................................................1
2.1.3 Main Content ................................................................................................................................................1
2.2 Function Building ............................................................................................................................................2
2.2.1 Installing a Framework ..................................................................................................................................2
2.2.2 Creating a Script ...........................................................................................................................................2
2.2.3 Importing Modules ........................................................................................................................................5
2.2.4 Reading Data ................................................................................................................................................5
2.2.5 Converting Voice into MFCC .........................................................................................................................6
2.2.6 Converting MFCC into Matrix .......................................................................................................................7
2.2.7 Alignment Function .......................................................................................................................................8
2.2.8 Converting Text into Vector ...........................................................................................................................9
2.2.9 Converting Dense Matrix into Sparse Matrix ...................................................................................................9
2.2.10 Converting Vector into Text ........................................................................................................................10
2.2.11 BiRNN Model ...........................................................................................................................................10
2.2.12 Calling the CPU ........................................................................................................................................12
2.2.13 Saving and Viewing a Script .......................................................................................................................13
HCIA-AI Experiment Guide Page 5

2.3 Conclusion ....................................................................................................................................................14


3 Speech Recognition Implementation ............................................................................. 15
3.1 Experiment Process ........................................................................................................................................15
3.1.1 Experiment Roadmap ..................................................................................................................................15
3.1.2 Experiment Process .....................................................................................................................................15
3.2 Data Preparations ...........................................................................................................................................16
3.2.1 Importing Data ............................................................................................................................................16
3.2.2 Viewing Data ..............................................................................................................................................16
3.3 Speech Recognition Implementation ................................................................................................................18
3.3.1 Creating a Script .........................................................................................................................................18
3.3.2 Importing Modules ......................................................................................................................................21
3.3.3 Importing Defined Functions ........................................................................................................................21
3.3.4 Defining Relevant Parameters and Paths .......................................................................................................22
3.3.5 Importing and Viewing Voice Data ...............................................................................................................22
3.3.6 Processing Data in Batch .............................................................................................................................22
3.3.7 Defining Placeholders..................................................................................................................................23
3.3.8 Defining Operation......................................................................................................................................24
3.3.9 Defining a Loss Function and an Optimization Function .................................................................................24
3.3.10 Defining Decoding and Assessing Model Nodes ..........................................................................................24
3.3.11 Saving and Initializing the Model and Adding Checkpoints ...........................................................................24
3.3.12 Training the Model ....................................................................................................................................25
3.3.13 Completing Training ..................................................................................................................................26
3.3.14 Saving and Viewing a Script .......................................................................................................................26
3.4 Model Training ..............................................................................................................................................28
3.4.1 Training Results ..........................................................................................................................................28
3.5 Conclusion ....................................................................................................................................................30
HCIA-AI Experiment Guide Page 6

1 Speech Recognition Experiment

1.1 Introduction to the Experiment


1.1.1 About the Experiment
Speech recognition crosses subjects, and has been developed over 70 years. Thanks to the
development of the deep learning technology and breakthrough in computation capability, speech
recognition has entered the rapid development stage in the past 10 years, and corresponding products
start to serve the public. This document demonstrates a speech recognition experiment where the new
AI algorithm deep learning is used on the open-source voice dataset THCHS-30 of Tsinghua
University, to make trainees understand basics and practical application of speech recognition and AI
related content and applications.

1.1.2 Objectives of the Experiment


 Understand basics of speech recognition.
 Understand basic development process of speech recognition.
 Understand basics of a bidirectional recurrent neural network (BiRNN).

1.1.3 Experiment Roadmap


Conventional roadmap of speech recognition is as follows:
Step 1: Convert voice data into .wav format. The .wav file stores a file header and dots of sound
waveforms.
Step 2: Use the voice activity detection (VAD) technique to process the sound waveform data to
reduce interference to subsequent steps.
Step 3: Use mobile window functions for data framing.
Step 4: Convert the waveforms of the frame data using techniques such as extracting the
mel-frequency cepstral coefficient (MFCC), and then convert the frame data into multidimensional
vectors.
Step 5: Combine the MFCC frames into statuses. Generally, multiple frames correspond to one status.
Step 6: Combine the statuses into factors. Generally, three statuses constitute a factor.
Step 7: Combine the factors into words. Generally, multiple factors constitute a word.
Step 8: Combine the words into text.
AI roadmap: Compared with conventional speech recognition, AI algorithms extract core features,
optimize status and factor conversion, and transfer the work to the neural network algorithms for
HCIA-AI Experiment Guide Page 7

execution. Therefore, more quality and abundant features can be extracted, and man-made
interference can be minimized, which yields better results.
Though data has been optimized during preprocessing, a great number of preparations need to be
made. The preparation work is difficult, and therefore, you are required to learn and understand the
development process and solution roadmap in this stage.

1.2 Experiment Content


1.2.1 Dataset
1. THCHS-30 dataset was created in 2000 and 2001. The design goal was a supplemental data
resource for the 863 database, so that the Chinese utterance coverage can be maximally improved.
The database was named Tsinghua Continuous Mandarin Speech Database (TCMSD) originally, and
then renamed THCHS-30.
2. THCHS-30 involves more than 30 hours of speech signals recorded by a single carbon microphone
at the condition of silent office. Most of the participants are young colleague students, and all are
fluent in standard Mandarin. The sampling rate of the recording is 16 kHz, and the sample size is 16
bits.
3. The text of THCHS-30 was selected from a large volume of news, with the goal of augmenting the
863 database. The author selected 1000 sentences for recording. The recordings were split into four
groups according to the recording text: A (sentence ID from 1 to 250), B (sentence ID from 251 to
500), C (sentence ID from 501 to 750), and D (sentence ID from 751 to 1000). The 10,893 utterances
of groups A, B and C totaling 30 testees are combined as the training set, and the 2496 utterances of
group D totaling 10 testees are used as the test set.

1.2.2 Algorithm
1. BiRNN is short for bidirectional recurrent neural network. On a typical recurrent neural network,
status transmission is backward and unidirectional. In some cases, current output is in relationship
with the previous status and the next status. Therefore, a BiRNN is needed to resolve this problem.
For example, predicting a lacking word in a statement requires the previous content as well as
subsequent content. In this case, BiRNN works.
2. BiRNN is composed of two RNNs overlaid. At the time point t, input is provided for these two
opposite RNNs, and output is determined based on the status of the two unidirectional RNNs, as
shown in the following figure.
HCIA-AI Experiment Guide Page 1

2 Preparations for the Speech Recognition


Model

2.1 Model Preparations


2.1.1 Introduction
To implement modular development, code standardization, and subsequent model parameter calling,
this document divides code work into two parts: main function modules (for overall training iteration
and test of speech recognition) and encapsulation function modules (for assisting the main functions
in completing training tasks). This chapter describes how to develop scripts of an encapsulation
module. Main content includes file reading function, BiRNN function, and matrix-to-text function.

2.1.2 Objective
 Develop the modular function to facilitate optimization and adjustment.
 Reduce workload to improve focus.
 Standardize codes.
 Decrease reading difficulty.

2.1.3 Main Content


This chapter contains the following main functions:
 Read data: get_wavs_labels
 Extract data in batch: next_batch
 Convert voice to MFCC: audiofile_to_input_vector
 Convert MFCC to matrix: get_audio_and_transcriptch
 Convert text to vector: get_ch_label_v
 Alignment: pad_sequences
 Convert text to sparse matrix: sparse_tuple_from
 Convert sparse matrix to text: sparse_tuple_to_texts_ch and ndarry_to_text_ch
 BiRNN model: BiRNN_model
 Call the CPU: variable_on_cpu
HCIA-AI Experiment Guide Page 2

2.2 Function Building


2.2.1 Installing a Framework
This experiment needs a python_speech_features framework. Install this framework in advance. To
install the framework, do as follows:

Step 1 Run the following command to install python_speech_features:


pip install python_speech_features
Specific operation is as follows:

Step 2 Run the following command to view the installation result:


pip3 list
Specific operation is as follows:

2.2.2 Creating a Script


Step 1 Run the following command to create a folder:
HCIA-AI Experiment Guide Page 3

mkdir –p HCIA-AI/voice
Specific operation is as follows:

Step 2 Run the following commands to create a script:


Run the cd HCIA-AI/voice command to open the speech recognition folder. Specific operation is as
follows:

Run the vi yuyinmodules.py command. Specific operation is as follows:

The interface is as follows:


HCIA-AI Experiment Guide Page 4

Step 3 Enter the edit mode.


Press the key i to enter the edit mode and prepare for project development. Specific operation is as
follows:
HCIA-AI Experiment Guide Page 5

Enter a line of content, as shown in the previous figure.

2.2.3 Importing Modules


Import relevant modules for project development. The NumPy module processes data format
conversion, array splicing, data format reshaping, value calculation, and array generation. The
python_speech_features module is a framework used for speech recognition, and is used for MFCC
feature extraction in this experiment. The SciPy module is a scientific calculation framework on the
bottom layer, and is used for reading .wav voice data in this experiment. The OS module is a system
file operation framework of Python, and is used for processing folders and files in this experiment. As
the most popular deep learning framework currently, TensorFlow is used in this experiment for
building main models of the entire speech recognition model. The code is as follows:

2.2.4 Reading Data


The get_wavs_labels function reads data into a memory for related calculation. The wav_files
contains audio files, and the labels contain text corresponding to voice data. The returned results
include new voice dataset new_wav_files and text dataset labels. The code is as follows:
HCIA-AI Experiment Guide Page 6

Obtain a list of .wav files for


training.

Remove some small


files.

Obtain text corresponding to voice


data.

2.2.5 Converting Voice into MFCC


The audiofile_to_input_vector function extracts MFCC features from voice data, and converts them
into MFCC feature codes.
HCIA-AI Experiment Guide Page 7

# 139 slices

# Add ɵ for the first nine slices; mfcc


features

# Add ɵ for the last nine slices; mfcc


features

# Normally distribute the data, subtract an average value, and then divide the obtained result by a variance

The orig_inputs represents the converted MFCC codes, and the trans_inputs represents data after time
sequence expansion, with the for loop supplementing 0. The last two lines are used for data
standardization.

2.2.6 Converting MFCC into Matrix


The get_audio_and_transcriptch function converts MFCC feature codes into a matrix composed of
time sequence (column) and spectral feature coefficient (line).
HCIA-AI Experiment Guide Page 8

# txt_obj indicates files.

# txt_obj indicates labels.

The returned results mainly include voice matrix audio, voice matrix length audio_len, text vector
transcript, and text vector length transcript_len.

2.2.7 Alignment Function


In voice processing, each batch of voice time sequence data uses the same format, but features use
different formats. The pad_sequences function supplements 0 to the feature formats. The
pad_sequences function is used for supplementing 0 or truncation, which can be controlled using
parameters. Post indicates a post operation (supplementing 0 or truncation), and pre indicates a pre
operation (supplementing 0 or truncation).
HCIA-AI Experiment Guide Page 9

Input: audio data sequences


Output: sequence x after 0s are supplemented and length

2.2.8 Converting Text into Vector


The get_ch_lable_v function converts txt_label or text into vectors with the word_num_map
parameter.
# Convert characters first into
vectors.

The returned results include text labels or vectors corresponding to the data.

2.2.9 Converting Dense Matrix into Sparse Matrix


This function converts the dense matrix of original voice data into sparse matrix, preparing for
subsequent decoding process.
HCIA-AI Experiment Guide Page 10

2.2.10 Converting Vector into Text


Converting vectors into text is the last step of developing the speech recognition model. The following
two functions require word tables to be transmitted into and then convert the vectors into words based
on the index corresponding to the word tables.
The sparse_tuple_to_texts_ch function converts text vectors in the sparse matrix into text. The code is
as follows:

The ndarray_to_text_ch function converts text vectors in the dense matrix into text. The code is as
follows:

2.2.11 BiRNN Model


The BiRNN model is the core of speech recognition. This model calls the BiRNN algorithms of the
TensorFlow framework and encapsulates the algorithms into the BiRNN_model function.
The model is composed of one BiRNN layer and two full-connection layers, carries Dropout, and uses
ReLu for activation. The truncation value is 20. When initializing learning parameters, use the
standard difference of random_normal 0.05 and standard value of keep_dropout_rate 0.95.
HCIA-AI Experiment Guide Page 11

Step 1 Define parameters.


# Define relevant
parameters.

Step 2 Define the model, and process data.


Build the BiRNN model, and process input parameters. The code is as follows:

# Convert the input into time-first data.

# Convert the data into two-dimensional data and transmit it to the first layer.

Step 3 Define the previous three full-connection layers.

Step 4 Define the BiRNN network.


HCIA-AI Experiment Guide Page 12

Step 5 Define the last two full-connection layers.

# Apply the full-connection layers to softmax classification.

# Convert two-dimensional [n_steps*batch_size, n_character] into three-dimensional time-major [n_steps, batch_size, n_character].

The data shape change process of the entire model is complex, and you need to convert the input into
two-dimensional Tensor before it can be transmitted to the full-connection layers. When data is
transmitted into the BiRNN through the full-connection layers, the data needs to be converted into
three-dimensional Tensor. Since the BiRNN outputs two-dimensional data, the data needs to be
converted back to two-dimensional Tensor before entering the full-connection layers later. The final
output can be three-dimensional Tensor if needed.
So far, the BiRNN model is built.

2.2.12 Calling the CPU


This function is intended to state learning parameter variables and define the learning parameter
variables into the CPU so that the CPU memory operation is maximized.
HCIA-AI Experiment Guide Page 13

2.2.13 Saving and Viewing a Script


After the script is complete, save and view it.

Step 1 Save the script.


Press Esc to exit the edit mode. Use the key combination ":wq!" to save the script and exit the edit
environment. Specific operation is as follows:

Step 2 Run the following command to view the script:


cat yuyinmodules.py
Specific operation is as follows:
HCIA-AI Experiment Guide Page 14

2.3 Conclusion
This chapter mainly introduces how to build functions for speech recognition, facilitating subsequent
model parameter calling. Main function modules are used for overall training iteration and test of
speech recognition, and encapsulation function modules are used for assisting the main functions in
completing training tasks.
HCIA-AI Experiment Guide Page 15

3 Speech Recognition Implementation

3.1 Experiment Process


3.1.1 Experiment Roadmap
1. This experiment is intended to train the speech recognition model based on the THCHS-30 dataset
and BiRNN model, and assess the model accuracy.
2. The complete voice datasets are huge and do not apply to current teaching experiment environment,
and therefore, the datasets are filtered and only a small part of data is used for training to ensure that a
complete experiment is demonstrated in a short time. Trainees can conduct further test as they like, for
example, train the model based on all the datasets.
3. To reduce development and management difficulty and improve code standardization, this
experiment is divided into two parts and developed in two scripts: model preparation module and
model module. The former module supports the latter module.
4. The codes in this experiment are huge, so trainees are required to simply master the experiment
process and roadmap in this HCIA-AI stage.

3.1.2 Experiment Process


The experiment process is composed of the following steps:
1) Prepare training data.
2) Import modules.
3) Import defined functions.
4) Define parameters.
5) Preprocess data.
6) Define variables.
7) Define operation.
8) Define a loss function and an optimization function.
9) Prepare for training, and initialize the model.
10) Train the model.
11) Complete training.
HCIA-AI Experiment Guide Page 16

3.2 Data Preparations


3.2.1 Importing Data
Before starting the experiment, prepare datasets.

Step 1 Run the following command to open a folder:


cd HCIA-AI/voice
Specific operation is as follows:

Step 2 Run the following command to download datasets:


wget
https://data-certification.obs.cn-east-2.myhuaweicloud.com/ENG/HCIA-AI/V1.0/voice_full.zip
Specific operation is as follows:

3.2.2 Viewing Data


The datasets are compressed in .zip format. Before viewing the datasets, decompress them.

Step 1 Run the following command to decompress the datasets:


unzip voice_dataset.zip
Specific operation is as follows:

The decompression result is as follows:


HCIA-AI Experiment Guide Page 17

Step 2 Run the following command to view the datasets:


ll
Specific operation is as follows:

The datasets contain two data files data_thchs30 and data_thchs30_10. The data_thchs30 file contains
complete THCHS-30 datasets, and the data_thchs30_10 file contains selected datasets to adapt to the
limited sources used in this experiment. The latter file will be used in model training later.
Run the cd data_thchs30_10 command.
Specific operation is as follows:
HCIA-AI Experiment Guide Page 18

Run the cd train and ll commands.


Specific operation is as follows:

The train folder contains 10 .wav voice files, and the train.word.txt file includes text labels
corresponding to the voice data.

3.3 Speech Recognition Implementation


3.3.1 Creating a Script
We have developed functions in chapter 2. In this chapter, we start to develop a model. The specific
procedure is as follows:

Step 1 Run the following command to open a folder:


cd HCIA-AI/voice and ll
Specific operation is as follows:
HCIA-AI Experiment Guide Page 19

Step 2 Run the following command to create the script yuyinrecog.py:


vi yuyinrecog.py
Specific operation is as follows:

Specific output is as follows:

Press Enter to enter the edit interface, as shown in the following figure:
HCIA-AI Experiment Guide Page 20

Press the key i to enter the edit mode.


HCIA-AI Experiment Guide Page 21

3.3.2 Importing Modules


Import relevant modules to support subsequent development work. The NumPy module processes
data, so it can be seen in any relevant project. In this experiment, the module is used mainly for
reshaping data structures and calculating values. The Time module processes standard time in Python,
and is used for operating time data. In this experiment, the module is mainly used for calculating
experiment time. The TensorFlow platform is the most popular deep learning framework around the
world. In this experiment, the module is mainly used for building a neural network model to process
loss values between the sequence data output labels and standard labels and print out specific content
in the checkpoints of the training environment. The collections module provides many useful
collection classes.

3.3.3 Importing Defined Functions


Import edited and prepared functions for model development, including data reading function, MFCC
feature extraction and conversion function, matrix conversion function, vector-to-text conversion
function, and BiRNN model. The code is as follows:
HCIA-AI Experiment Guide Page 22

3.3.4 Defining Relevant Parameters and Paths


Step 1 Initialize images.
Clear stacking of the default images, and configure a global image as the default image. The code is as
follows:

Step 2 Configure paths.


Specify a path to store voice data and a path to store label data for model training.

3.3.5 Importing and Viewing Voice Data


Use the get_wavs_labels function to read and view data.

The returned results include the file name of the first voice data in the original training data, text
corresponding to the first voice data, and size of the training dataset.

3.3.6 Processing Data in Batch


Use the next_batch function to obtain data of a batch for training, use the get_audio_and_transcriptch
function to convert the voice data into training data in the next_batch function, and use the
pad_sequences function to supplement 0 to the data. For the label text, use the sparse_tuple_from
function to convert the text to a sparse matrix. The code is as follows:
HCIA-AI Experiment Guide Page 23

# If there are multiple files, their lengths should be uniform, and


can be truncated to the length of the longest file. If necessary,
add ɵ

Build a word table. The datasets used in this experiment contain 10 corpuses, only 212 Chinese
characters. Build a word table based on the characters so that the characters can be vectorized later.
The returned result is the size of the word table. The code is as follows:

# Word table

'Word table size'

The following code shows how to convert text vectors in the sparse matrix into text. The returned
result is "He swayed up and down the lane with the strength of his waist only, like a dolphin led by the
advantage of one head." The code is as follows:

# Calculate the number of MFCCs.


# For each time point, contain the number of samples in the context.

3.3.7 Defining Placeholders


After training data is prepared, start to train the model. First, define three placeholders. Input_tensor
indicates the input audio data, targets indicates the text corresponding to the audio data, hi indicates
the placeholder of a sparse matrix, and seq_length indicates the sequence length of the current batch
data. Keep_dropout is a parameter of the Dropout module. Specific operation is as follows:
HCIA-AI Experiment Guide Page 24

# The batch_size and max_stepsize cannot be changed once they are adjusted.
# Voice log filter bank or MFCC features

# Text

# Sequence length

3.3.8 Defining Operation


Since the core of this experiment is the BiRNN model, operate the BiRNN model defined in the
yuyinmodules.py script. Simply call the defined model. The code is as follows:

3.3.9 Defining a Loss Function and an Optimization Function


Speech recognition involves time sequence classification. Use the ctc_loss function as a loss function
to calculate the loss values, and call the function to calculate the mean value. Use the AdamOptimizer
as the optimizer, and set the learning rate to 0.001. The code is as follows:
# Call the ctc_loss function.

# Optimization function optimizer

3.3.10 Defining Decoding and Assessing Model Nodes


Use the ctc_beam_search_decoder function to decode the predicted result logits. A decoded result is
returned. The decoded result is an element array. Transmit the decoded[0] into the edit_distance
function and calculate the distance between targets. Based on the reduce_mean, the average error rate
of this model for current batch of data can be obtained. The code is as follows:

# Calculate the label error rate (accuracy).

3.3.11 Saving and Initializing the Model and Adding Checkpoints


So far, the model is built. Considering the large amount of training data and the long operation time,
add a checkpoint function. Before adding the function, define a class saver and use it for storing
checkpoint content. Set corresponding parameters and a path to store the model.
HCIA-AI Experiment Guide Page 25

# Generate a saver.

Initialize the model. The code is as follows:

# If no model is available, reinitialize it.

Checkpoint content is displayed. The code is as follows:

3.3.12 Training the Model


Set relevant parameters and start label. The code is as follows:

Step 1 Record the start time and training epoch.

# Sample set iteration times

Step 2 Print out the epoch.


Print out each epoch to display the training process vividly.
HCIA-AI Experiment Guide Page 26

Step 3 Start model iteration, and display results.


Start iteration training, print out relevant prediction results, and compare the prediction results with
the original results.

Step 4 Calculate the training duration, and save the model.


Calculate the training duration of each epoch, and return the epoch corresponding to the data, loss
function, iteration times, and consumed time. Meanwhile, save the training result of each epoch. The
code is as follows:

3.3.13 Completing Training


Print out the training duration of the model. The code is as follows:

Close the session. The training is complete.

3.3.14 Saving and Viewing a Script


After the model is developed, save and view the script.

Step 1 Save the script.


Press Esc to exit the edit mode. Use the key combination ":wq!" to save the script and exit the edit
environment. Specific operation is as follows:
HCIA-AI Experiment Guide Page 27

Step 2 Run the following command to view the script:


cat yuyinrecog.py
Specific operation is as follows:
HCIA-AI Experiment Guide Page 28

3.4 Model Training


3.4.1 Training Results
Step 1 Run the following command to run the script:
python3 yuyinrecog.py
Specific operation is as follows:

Step 2 View the running results.


During training:
HCIA-AI Experiment Guide Page 29

After training:

Nine corpuses, 212 Chinese characters are iterated for 50 epochs, consuming 153.74 minutes (about
2.5 hours). The value of the iteration times is 19 powers of 10. The error rate of the model is 26.5%.
The following figure shows the result of another version. Fifty corpuses consume 823.65 minutes
(about 13.7 hours), with the error rate being 14.5%. For the model result, see the text in red boxes.
Original indicates the original corpus, and Decoded indicates the corpus trained based on the model.
The two corpuses highly match.
HCIA-AI Experiment Guide Page 30

3.5 Conclusion
This experiment tests the speech recognition system under the TensorFlow framework, and is intended
to train the speech recognition model based on the THCHS-30 dataset and BiRNN model, and assess
the model accuracy.
HCIA-AI Huawei Certification Course

HCIA-AI
Man-Machine Dialogue Programming
Experiment Guide
Version: 1.0

Huawei Technologies Co., Ltd.

1
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 or 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 content, but all statements, information, and recommendations in this
document do not constitute a warranty of any kind, express or implied.

Huawei Technologies Co., Ltd.


Huawei Industrial Base
Bantian, Longgang
Address:
Shenzhen 518129
People's Republic of China

Website: http://e.huawei.com

华为专有和保密信息
版权所有 © 华为技术有限公司
Introduction to Huawei Certification System

Based on cutting-edge technologies and professional training systems, Huawei certification meets
the diverse AI technology demands of various 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, speech 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, speech 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, speech processing engineers and machine learning algorithm engineers.
Enterprises with HCIA-AI-certified engineers have the basic understanding of AI technology,
framework, and programming, and are 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, speech 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!

华为专有和保密信息
版权所有 © 华为技术有限公司
Preface

Basic Introduction
This document is an HCIA-AI certification training course, and is intended to trainees who are
preparing for HCIA-AI tests or readers who want to know about AI basics. After understanding this
document, you can perform basic AI man-machine dialogue programming.

Contents
This experiment guide includes one experiment, covering man-machine dialogue, and is intended to
help trainees complete the man-machine dialogue experiment on their own.
Chapter 1 introduces the man-machine dialogue experiment.
Chapter 2 describes how to preprocess dialogue corpus
Chapter 3 describes how to build the Seq2Seq model.
Chapter 4 describes how to train the model.
Chapter 5 describes how to test the model.

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 the basic Python editing capability, and basic knowledge of data structures and AI.

Experimental Environment
Python Development Tool
This experimental environment is compiled based on Python 3.6.

华为专有和保密信息
版权所有 © 华为技术有限公司
HCIA-AI Experiment Guide Page 1

Contents

Preface ............................................................................................................................ 3
Basic Introduction .................................................................................................................................................3
Contents ...............................................................................................................................................................3
Knowledge Background of Readers ........................................................................................................................3
Experimental Environment .....................................................................................................................................3
1 Man-machine Dialogue Experiment ................................................................................ 3
1.1 Introduction to the Experiment ..........................................................................................................................3
1.1.1 About the Experiment ....................................................................................................................................3
1.1.2 Objectives of the Experiment .........................................................................................................................3
1.1.3 Experiment Roadmap ....................................................................................................................................3
1.1.4 Dataset .........................................................................................................................................................4
1.1.5 Algorithm .....................................................................................................................................................5
2 Dialogue Corpus Preprocessing ...................................................................................... 6
2.1 Introduction .....................................................................................................................................................6
2.1.1 Preparation ...................................................................................................................................................6
2.1.2 Main Functions .............................................................................................................................................6
2.2 Corpus Preparation ..........................................................................................................................................6
2.2.1 Installing a Framework ..................................................................................................................................6
2.2.2 Downloading the Corpus ...............................................................................................................................7
2.2.3 Creating a Script to Preprocess the Corpus ......................................................................................................9
2.3 Preprocessing ................................................................................................................................................10
2.3.1 Importing Modules ......................................................................................................................................10
2.3.2 Defining the basic_tokenizer Function ..........................................................................................................11
2.3.3 Defining the fenci Function ..........................................................................................................................11
2.3.4 Defining the getRawFileList Function ...........................................................................................................11
2.3.5 Defining the get_ch_lable Function ..............................................................................................................12
2.3.6 Defining the get_ch_lable Function ..............................................................................................................12
2.3.7 Defining the build_dataset Function ..............................................................................................................13
2.3.8 Defining the create_vocabulary Function ......................................................................................................13
2.3.9 Defining the create_seq2seqfile Function ......................................................................................................14
2.3.10 Defining the splitFileOneline Function ........................................................................................................15
2.3.11 Defining the initialize_vocabulary Function .................................................................................................15
HCIA-AI Experiment Guide Page 2

2.3.12 Defining the sentence_to_ids Function ........................................................................................................15


2.3.13 Defining the textfile_to_idsfile Function .....................................................................................................15
2.3.14 Defining the textdir_to_idsdir Function .......................................................................................................16
2.3.15 Defining the ids2texts Function ..................................................................................................................16
2.3.16 Defining the Main Function .......................................................................................................................16
2.3.17 Running the Main Function ........................................................................................................................17
3 Seq2Seq Model Building .............................................................................................. 18
3.1 Introduction ...................................................................................................................................................18
3.1.1 Model Content ............................................................................................................................................18
3.1.2 Experiment Process .....................................................................................................................................18
3.2 Seq2Seq Model Building ................................................................................................................................18
3.2.1 Importing Modules ......................................................................................................................................18
3.2.2 Creating the Seq2SeqModel Class and Defining the Seq2Seq Model ...............................................................19
3.2.3 Defining the get_batch Function ...................................................................................................................21
3.2.4 Defining the Step Function...........................................................................................................................22
4 Model Training ............................................................................................................ 23
4.1 Experiment Process ........................................................................................................................................23
4.1.1 Experiment Roadmap ..................................................................................................................................23
4.1.2 Experiment Process .....................................................................................................................................23
4.2 Implementation ..............................................................................................................................................23
4.2.1 Importing Modules ......................................................................................................................................23
4.2.2 Preparing Parameters ...................................................................................................................................24
4.2.3 Defining the getdialogInfo Function .............................................................................................................24
4.2.4 Creating the createModel Model ...................................................................................................................25
4.2.5 Importing Modules ......................................................................................................................................25
4.2.6 Defining the Main Function .........................................................................................................................26
4.2.7 Executing the Main Function .......................................................................................................................27
5 Model Test .................................................................................................................. 29
5.1 Experiment Process ........................................................................................................................................29
5.1.1 Experiment Roadmap ..................................................................................................................................29
5.1.2 Experiment Process .....................................................................................................................................29
5.2 Data Preparation ............................................................................................................................................29
5.2.1 Importing Modules ......................................................................................................................................29
5.2.2 Preparing Parameters ...................................................................................................................................29
5.2.3 Defining the getdialogInfo Function .............................................................................................................30
5.2.4 Defining the createModel .............................................................................................................................30
5.2.5 Defining the Main Function .........................................................................................................................31
5.2.6 Executing the Main Function .......................................................................................................................31
HCIA-AI Experiment Guide Page 3

1 Man-machine Dialogue Experiment

1.1 Introduction to the Experiment


1.1.1 About the Experiment
Man-machine dialogue, also referred to as a chatting robot, is a popular branch in the AI field. Man
refers to people, and machine refers to any entity that can respond to individual questions intelligently,
for example, a script, an app, and a robot. Man-machine dialogue is used mainly in customer service,
entertainment, guiding, e-commerce, and government affairs for replacing repetitive labor, reducing
costs, and improving efficiency.
This experiment processes Seq2Seq tasks using the currently popular deep learning algorithm
recurrent neural network (RNN) and the most popular deep learning framework TensorFlow, resolves
the man-machine dialogue in the Seq2Seq tasks. The datasets used in this experiment are several
groups of dialogues compiled by engineers. Though the data amount is small, it can help achieve
required learning effect.
This experiment contains four Python scripts: preprocessing.py for data processing, seq2seq.py for
building a dialogue model, train.py for training the model, and test.py for testing the model. In the
first script, a great number of functions are defined to process original data. In the second script, the
content is originated from the source codes of the earlier TensorFlow. After the source codes are
changed with the TensorFlow upgrade, no corresponding content can be found from the current
version. The source codes are simply copied here.
This experiment involves a great amount of professional knowledge, and code compilation is a
difficult task. To meet the requirements of most trainees, the experiment attaches importance to the
development process and roadmap.

1.1.2 Objectives of the Experiment


 Understand basics of man-machine dialogue.
 Understand basic development process of man-machine dialogue.
 Understand basics of the Seq2Seq model.

1.1.3 Experiment Roadmap


Man-machine dialogue experiment roadmaps fall into two types: search type and generative type.
The search type is simple and clear, and boasts high accuracy and speed. However, a great amount of
preparation work needs to be made so that as much material as possible can be searched out. Under
current big data environment, this type applies to specific scenario. The generative type combines
currently popular algorithms and technologies, making dialogues open, breaking the bottle of finding
an answer in the search type, and simulating a person to answer questions.
Roadmap:
1. Obtain dialogue corpus.
2. Preprocess dialogue corpus.
3. Build a Seq2Seq model.
HCIA-AI Experiment Guide Page 4

4. Train the model.


5. Test the model.

1.1.4 Dataset
The dialogue corpus used in this experiment is compiled by engineers, containing two .txt files,
totaling 12 sentences and six dialogue groups. The following figure shows the two files.
Nam
Type Size
e
.txt file
.txt file

The content in the one.txt file is as follows:

one.txt
Format
File (F) Edit (E) View (V) Help (H)
(O)

Hi
Hi
Have you had your dinner?
Yes
What did you eat?
Salmon
Any else? Could you give me some?
No. I have not had enough.

The content in the two.txt file is as follows:


HCIA-AI Experiment Guide Page 5

two.txt
Help
File (F) Edit (E) Format (O) View (V)
(H)
How old are you?
Guess
Younger than 20
Bingo! I am 18.

1.1.5 Algorithm
A task which is processed in a sequence and mapped to another sequence is a Seq2Seq task. A
sequence can be a string. Common Seq2Seq tasks include machine translation, part-of-speech tagging,
speech recognition, and man-machine dialogue that this experiment mainly talks about
In this experiment, the LSTM and Encoder-Decoder algorithms are used.
Long Short-Term Memory (LSTM) is a recurrent neural network (RNN) processing and predicting
major events with long interval and delay in the time sequence. https://baike.baidu.com/item/time
sequence LSTM has been applied in the scientific and technological field. LSTM-based systems can
be used to process tasks such as machine translation, robot control, speech recognition, and chatting
robot.
Encoder-Decoder is the basic framework of Seq2Seq, and includes the encoder, decoder, and
intermediate status vector connecting them. By learning the input, the encoder encodes the input as a
status vector S with fixed size, and then transfers the S to the decoder. By learning the status vector S,
the decoder gives output.

Semantic
coding C
HCIA-AI Experiment Guide Page 6

2 Dialogue Corpus Preprocessing

2.1 Introduction
2.1.1 Preparation
 Create a folder for storing project content.
 Download datasets from the OBS service of Huawei cloud object storage service.
 Create sub-folders in the project folder to meet different requirements.

2.1.2 Main Functions


This chapter contains the following main functions:
 Separate sentences: basic_tokenizer()
 Divide Chinese characters: fenci()
 Obtain a file list: getRawFileList()
 Read divided Chinese Characters: get_ch_lable()
 Obtain text content: get_ch_path_text()
 Build datasets: build_dataset()
 Create a vocabulary: create_vocabulary()
 Create sequence files: create_seq2seqfile()
 Divide text: splitFileOneline()
 Initialize the vocabulary: initialize_vocabulary()
 Convert sentences into IDS: sentence_to_ids()
 Convert files into IDS: textfile_to_idsfile()
 Convert files into IDS in batch: textdir_to_idsdir()
 Convert IDS into files: ids2texts()

2.2 Corpus Preparation


2.2.1 Installing a Framework
This experiment needs a Jieba framework. Install this framework in advance. To install the framework,
do as follows:

Step 1 Run the following command to install Jieba:


pip install jieba
Specific operation is as follows:
HCIA-AI Experiment Guide Page 7

Step 2 Run the following command to view the installation result:


pip3 list
Specific operation is as follows:

2.2.2 Downloading the Corpus


Step 1 Download the corpus.
HCIA-AI Experiment Guide Page 8

Open the HCIA-AI folder, and run the wget


https://data-certification.obs.cn-east-2.myhuaweicloud.com/ENG/HCIA-AI/V1.0/chatbot.zip
command to download the corpus. Specific operation is as follows:

Step 2 Run the following command to decompress the .zip file:


unzip chatbot.zip
Specific operation is as follows:

Step 3 Run the following command to view the content:


ll. The result is as follows:
HCIA-AI Experiment Guide Page 9

Open the decompressed folder. The chatbot folder contains a data folder for storing data and a
myjiebadict.txt file for dividing Chinese characters. Open the data folder. The folder contains four
sub-folders and one .txt file. The four sub-folders store checkpoints, dialogue corpus, question IDS
files, and answer IDS files. The dictch.txt file is used for building a vocabulary.

2.2.3 Creating a Script to Preprocess the Corpus


Run the command to create a script, and press the key i to enter the edit mode. The result is as follows:
HCIA-AI Experiment Guide Page 10

2.3 Preprocessing
2.3.1 Importing Modules
Import relevant modules for project development.
 The sys module is responsible for interaction between the program and Python interpreter, and
provides a series of functions and variables for controlling the operation environment of Python;
in this experiment, it is used for refreshing data.
 The OS module is responsible for interaction between the program and OS, and provides an
interface for accessing the OS bottom layer; in this experiment, it is used for operating system
files, for example, returning a file list and multipath combination.
 The matplotlib is a 2D graphics library of Python, and generates publishing-level graphics using
hard copy formats in cross-platform interactive environment, and supports different types of 2D
graphics to be generated using a small number of codes.
 The matplotlib.pyplot is a function collection with command style, and looks like MATLAB.
 The NumPy is an open-source value calculation package of Python; in this experiment, it is used
for processing matrices, for example, creating a matrix, reshaping its structure, and calculating
convergence.
 The tensorFlow.python.platform.gfile provides file object APIs like Python for operating files.
 The random module mainly generates random data; in this experiment, it is used to disorder the
original data sequence.
 The re module is a regular expression module of Python; in this experiment, it is used for
replacement and division.
 The collections module provides many useful collection classes; in this experiment, the counter
class of the collections module is used to track the occurrence times of values.
HCIA-AI Experiment Guide Page 11

 The jieba module is a Python-based Chinese character division tool; in this experiment, it is used
to import files and divide Chinese characters.
The code is as follows:

2.3.2 Defining the basic_tokenizer Function


The basic_tokenizer () function divides a single sentence using specific separators, including: ",", ".",
"‘", and "’". The code is as follows:

2.3.3 Defining the fenci Function


The fenci () function divides the training data. The code is as follows:

2.3.4 Defining the getRawFileList Function


The getRawFileList () function obtains a file list, and returns file paths and file names. The code is as
follows:
HCIA-AI Experiment Guide Page 12

2.3.5 Defining the get_ch_lable Function


The get_ch_lable () function reads Chinese characters in a file after they are divided. The code is as
follows:

2.3.6 Defining the get_ch_lable Function


The get_ch_path_text () function obtains content from text files. Call the getRawFileList function to
obtain file paths and names, assign values of the file paths and names to the text_files, and view the
training data paths and names. In case of a large file volume, shuffle the file sequence to traverse each
file in the folder, call the get_ch_lable function to obtain specific content and content length of each
file, and assign the value of the file content to the training_data and the content length to the
training_datasz. Add the processed training_ci to the labels list.
The code is as follows:
# Create the get_ch_path_text function to obtain the content of the text file.
# Call the getRawFileList function to obtain file path and name information, and
assign values of the file paths and names to the text_files.
# Define an empty list for storing processed file content.
# Define a list that contains only the element 0.

# View the two training data paths and names. In case of a large file
volume, shuffle the file sequence.
# Traverse each file in the folder.
# Call the get_ch_lable function to obtain specific content and content length of each file, and assign
the value of the file content to the training_data and the content length to the training_datasz.
# Convert the file content into the array training_ci.
# Convert the arrany training_ci into
# Add the processed training_ci to the labels list.

# The length of this group of content is equal to the length of the last group of
content plus the length of this group (initial content length is 0).
# Call the list.extend() function to add multiple values of another sequence to the end of the list.
# Output the iterative length of every line of content in each text_file.
# Output the content and content length of all files.

The result is as follows:


HCIA-AI Experiment Guide Page 13

2.3.7 Defining the build_dataset Function


The build_dataset () function builds training datasets. The sequence number, frequency, dictionary,
and reverse dictionary of a group of content are returned.
The code is as follows:
# System character, which needs to be added when a dictionary is created to assist in
marking
# Padded and occupied in the bucket mechanism
# Start of a decoded result input
# End of an output result
# Replaced with the character which is not found from the dictionary
# Replaced with a digital character. It is not a system character.

2.3.8 Defining the create_vocabulary Function


The create_vocabulary () function builds a vocabulary. Call the get_ch_path_text function. The file
content and content length of the raw_data_dir are returned. Traverse the file content of each line, and
add the content to the all_words vocabulary created previously. Call the build_dataset function to
create a dataset, input all elements, and set the maximum size of the vocabulary. The sequence number,
content, dictionary, and reverse dictionary of each element are returned.
The code is as follows:
HCIA-AI Experiment Guide Page 14

# Call the build_dataset function to create a dataset, input all elements, and set the maximum size of
the vocabulary. The sequence number, content, dictionary, and reverse dictionary of each element are
returned

The result is as follows:

Number of lines:

Number of Chinese characters: 34


Number of Chinese characters: 16

Number of Chinese characters: 50

2.3.9 Defining the create_seq2seqfile Function


The create_seq2seqfile () function places the question and answer IDS data into different files for
subsequent model training.
The code is as follows:

The result is as follows:


HCIA-AI Experiment Guide Page 15

2.3.10 Defining the splitFileOneline Function


The splitFileOneline () function divides the read dialogue text by one line of question and one line of
answer. The questions and answers are stored in two separate files. The training_data indicates the
total data, and the textssz indicates the accumulative length of each line.
The code is as follows:

2.3.11 Defining the initialize_vocabulary Function


The initialize_vocabulary () function initializes the vocabulary. Determine whether a directory or path
is available. The vocabulary_file can be a directory or a file path. If any, the function returns True;
otherwise, the function returns False. Open the vocabulary file in read-only mode. The
vocabulary_path is the file storage path. Read all lines of the vocabulary file, and add the lines to the
rev_vocab. Traverse the content of each line in the rev_vocab, call the strip function to remove all
specific characters (space by default), and call the enumerate function to serialize the rev_vocab and
then traverse the vocabulary. Exchange the elements with sequence, and then copy them to the vocab.
A value is returned.
The code is as follows:

2.3.12 Defining the sentence_to_ids Function


The sentence_to_ids () function converts sentences into IDSs. Call the basic_tokenizer function to
process the sentences, and then call the fenci function to traverse the content of the divided sentences.
Call the dict.get () function. The value of a specified key is returned. If the value is not available in the
dictionary, a default value is returned. Otherwise, the key value is returned.
The code is as follows:

2.3.13 Defining the textfile_to_idsfile Function


The textfile_to_idsfile () function converts a text file into an IDS file. Open the data_file_name file
and target_file_name in binary read-only mode, and traverse the content in the data_file. Call the
HCIA-AI Experiment Guide Page 16

sentence_to_ids function to convert each line of sentence (decoded first) into an IDS, traverse and
serialize the IDS in each sentence, and then write the IDS with space into the ids_file.
The code is as follows:

2.3.14 Defining the textdir_to_idsdir Function


The get_ch_path_text () function converts text files into IDS files in batch. Call the getRawFileList
function to obtain file path and name information, call the zip function to process data and use the
processed data as function parameters, and pack the elements in the data as tuples. A list composed of
these tuples is returned. View the detailed file paths and names of the combined files, and call the
textfile_to_idsfile function to convert the text files into IDS files in batch.
The code is as follows:

2.3.15 Defining the ids2texts Function


The ids2texts () function converts IDS files into text files.
The code is as follows:

2.3.16 Defining the Main Function


The main () function is the core of Python scripts and a final target of each task. Add the combined
vocabulary file names into the create_vocabulary function to create a vocabulary, and call the
splitFileOneline function to divide the vocabulary by one line of question and one line of answer. The
questions and answers are stored in two separate files. View the size of the two files.
The code is as follows:
HCIA-AI Experiment Guide Page 17

# Define the main function.

The result is as follows:

2.3.17 Running the Main Function


The __name__ indicates the current module name. When the module is run directly, the module name
is __main__. That is, when the module is run directly, the following code block will be run. When the
module is imported, the code block is not run.
The code is as follows:

The result is as follows:

Number of
lines:
Number
Number of
of Chinese
Chinese characters:
characters: 34
16

Number of Chinese characters: 50


HCIA-AI Experiment Guide Page 18

3 Seq2Seq Model Building

3.1 Introduction
3.1.1 Model Content
1. Due to TensorFlow upgrade, the Seq2Seq model of the original dialogue module has been changed
significantly. Therefore, the original module is not found from the new TensorFlow. In this experiment,
the original Seq2Seq codes in the earlier TensorFlow are copied here.
2. A class is defined here, including the Seq2Seq model which the attention mechanism and a plurality
of buckets are added into, the get_batch function which extracts data in batch, and the step function
which injects data.
3. The codes in this experiment are huge, so trainees are required to simply master the experiment
process and roadmap in this HCIA-AI stage.

3.1.2 Experiment Process


1. Define the Seq2SeqModel class.
2. Define the Seq2Seq model.
a) Define a loss function.
b) Define the seq2seq_f model.
c) Inject data.
d) Conduct reverse spreading and optimization.
3. Define the get_batch function.
4. Define the step function.

3.2 Seq2Seq Model Building


3.2.1 Importing Modules
Import third-party modules.
HCIA-AI Experiment Guide Page 19

3.2.2 Creating the Seq2SeqModel Class and Defining the Seq2Seq Model
Define three functions under the class: __init__, get_batch, and step.

Step 1 Create a class.

The Seq2Seq model carries the attention mechanism and a plurality of buckets.
The class implements an encoder composed of a multilayer perceptron and a decoder having the
attention mechanism.

Step 2 State variables.

The Seq2Seq model carries the attention mechanism and a plurality of buckets.
The class implements an encoder composed of a multilayer perceptron and a decoder having the attention
mechanism.

# State the private attributes.


# Original vocabulary size
# Tone of the target vocabulary
# An I/O list. I indicates the maximum length of input, and O indicates the maximum length of output.
# Number of units on each hidden layer
# Number of hidden layers
#
# Threshold of an interception gradient
# Data size of a batch
# Initial learning rate
# Attenuation parameter of decay learning rate
# Whether to use LSTM cells. True indicates Yes, and False indicates that GRU cells are used.
# Number of sampled softmax
# True indicates forward spreading, and False indicates forward + reverse spreading.
# Type of interval variables

# 0 by default
HCIA-AI Experiment Guide Page 20

Step 3 Calculate the output projection.

# The sampled softmax is useful when the sample number is smaller than the vocabulary
size.

Step 4 Define a loss function.


# Calculate the model
output.

Step 5 Define the Seq2Seq model.

Step 6 Inject data.


HCIA-AI Experiment Guide Page 21

# Inject data.

# Move the decoder by one digit to obtain targets.

Step 7 Define the running direction and loss.

# Encoded input tensor


# Decoded input tensor

# Call the seq2seq_f model.


# Call the defined loss
function.

# Encoded input tensor


# Decoded input tensor

# Call the seq2seq_f model.


# Call the defined loss
function.

Step 8 Conduct reverse spreading and optimization.


# Calculate the gradient by reverse spreading, and update the parameters through the
optimizer. # A variable list that needs training is
returned.

Step 9 Obtain the global variables, and save the model.

3.2.3 Defining the get_batch Function


Sample data obtained in batch cannot be used directly, and it needs conversion. For example,
determine the input and output size based on the size corresponding to a bucket_id, pad data based on
the input and output size, and then use padding data with the first word being "_GO" as the input of
the encoder.
HCIA-AI Experiment Guide Page 22

# Select a batch of data at random and use the data as the input of the encoder and decoder.
# Pad the data if necessary, reverse the input sequence of the encoder, and add a "GO" to the decoder.

# Padding
# Reversal
# Add an extra "GO" to the input data of the decoder, and pad the data.

3.2.4 Defining the Step Function


The step function injects data.

# Session in the TensorFlow


# Inject the list of input data of the encoder.
# Inject the list of input data of the decoder.
# Inject the list of input data of the target_weights.

# Perform forward spreading or forward + reverse spreading.

# Define injection of the input data.

# Define injection of the output data.

# In case of forward spreading (training), the gradient norm and loss that need update are output.

# In case of forward + reverse spreading (test), the gradient does not need to be output. Only the loss and decoder size sequence are output.
HCIA-AI Experiment Guide Page 23

4 Model Training

4.1 Experiment Process


4.1.1 Experiment Roadmap
After the data preprocessing is complete and the Seq2Seq model is built, train the model. The model
training process includes module import, data preparation, model preparation, model training, and
model storage.

4.1.2 Experiment Process


The experiment process includes the following steps:
1) Import modules.
2) Prepare parameters.
3) Prepare data (including file path and data).
4) Define the model.
5) Train the model.
6) Save the model.

4.2 Implementation
4.2.1 Importing Modules
Import relevant modules for running scripts. The math module is the mathematical knowledge module
of Python and used for processing mathematical operation. The six.moves module is used for shielding
function position changes in Python 2 and Python 3. The preprocessing module and seq_2_seq_model
module are defined previously, and are used for preprocessing data and building a model, respectively.
The code is as follows:
HCIA-AI Experiment Guide Page 24

# Defined data preprocessing module


# Defined Seq2Seq model

4.2.2 Preparing Parameters


Parameter meanings and functions can be viewed from the notes in the following figure.
The code is as follows:
# Clear the default graphics stacking, and reset the global
default graphics.

# Checkpoint storage node


#
# Dropout probability
# Gradient tailoring
# Size of data extracted in batch
# Number of hidden layers
# Learning rate
# Attenuation factor of learning rate
# Number of neurons on the hidden layers
# Understanding of checkpoint storage
# Buckets

4.2.3 Defining the getdialogInfo Function


The getdialogInfo() function obtains dialogue corpus data. The vocabulary size, reverse vocabulary
size, path of the question training corpus, and path of the answer training corpus are returned.
The code is as follows:
HCIA-AI Experiment Guide Page 25

4.2.4 Creating the createModel Model


Define a new Seq2Seq model based on the Seq2SeqModel model created previously. Determine
whether the model is available; and f no, train a model and then read lines.
The code is as follows:

# Obtain the model saved


the last time.

# Global initialization

4.2.5 Importing Modules


Read corpus data from the corpus data path to generate datasets of corresponding format for model
training.
The code is as follows:
HCIA-AI Experiment Guide Page 26

4.2.6 Defining the Main Function


Step 1 Create the main function and checkpoint path.
The code is as follows:

# If the current checkpoint path does not exist,


a checkpoint path is generated automatically.
# View the current checkpoint path.

Step 2 Generate a session and file path for training, and prepare data.
The code is as follows:

# Print out the current bucket size.

Step 3 Prepare buckets and parameters.


The code is as follows:

Step 4 Start to train the model.


The code is as follows:

# Start to train the model.

Step 5 Save the checkpoint and test the data.


The code is as follows:
HCIA-AI Experiment Guide Page 27

# Save the checkpoint and test the data.

# Decay learning rate

# Save the checkpoint.

Step 6 Output the training process.


The code is as follows:

Input
Output

"Result"

4.2.7 Executing the Main Function


Execute the defined process and main function.
The code is as follows:

The __name__ indicates the current module name. When the module is run directly, the module name
is __main__. That is, when the module is run directly, the following code block will be run. When the
module is imported, the code block is not run.
The execution result is as follows:
HCIA-AI Experiment Guide Page 28

Input
Output

Input
Output

Input
Output

Input
HCIA-AI Experiment Guide Page 29

5 Model Test

5.1 Experiment Process


5.1.1 Experiment Roadmap
1. After the data preprocessing is complete and the Seq2Seq model is built, perform a man-machine
dialogue to test the trained CKPT model.
2. Since the corpus amount is small, select the words that are available in the corpus. The words that
are not available in the corpus will be replaced by the set keyword _PAD.

5.1.2 Experiment Process


The experiment process includes the following steps:
1. Import modules.
2. Prepare parameters.
3. Prepare data.
4. Define the model.
5. Save the model.

5.2 Data Preparation


5.2.1 Importing Modules
Before starting the experiment, prepare datasets.

5.2.2 Preparing Parameters


Before the test, prepare relevant parameters.
HCIA-AI Experiment Guide Page 30

5.2.3 Defining the getdialogInfo Function


The getdialogInfo() function reads data and prepares datasets.

5.2.4 Defining the createModel


Define a Seq2Seq model based on the Seq2Seq_Model model created previously.
HCIA-AI Experiment Guide Page 31

5.2.5 Defining the Main Function


Define the main function and execution process.
The code is as follows:

5.2.6 Executing the Main Function


The code is as follows:

The execution result is as follows:


HCIA-AI Experiment Guide Page 32

Hi
Hi
How old are you?
Guess
What did you eat?
I ate
Hm

You might also like