0% found this document useful (0 votes)
253 views124 pages

HCIA-AI V1.0 Lab Guide

Uploaded by

multe123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
253 views124 pages

HCIA-AI V1.0 Lab Guide

Uploaded by

multe123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 124

Huawei HCIA-AI Series Training

HCIA-AI V1.0
HUAWEI Cloud Modelarts Guide

Issue: 1.0

HUAWEI TECHNOLOGIES CO., LTD.


Copyright © Huawei Technologies Co., Ltd. 2019. 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: https://www.huawei.com/
Email: [email protected]

2020-04-15 Copyright © Huawei Technologies Co., Ltd. ii

ii
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!

2020-04-15 Copyright © Huawei Technologies Co., Ltd. iii

iii
2020-04-15 Copyright © Huawei Technologies Co., Ltd. iv

iv
Content
Introduction to Huawei Certification System.................................................................. iii
1 HUAWEI Cloud Modelarts Guide ................................................................................... 6
1.1 Access HUAWEI Cloud .......................................................................................................................... 6
1.2 Log in HUAWEI Cloud ............................................................................................................................ 6
1.3 Enter the “Modelarts” ............................................................................................................................. 7

2020-04-15 Copyright © Huawei Technologies Co., Ltd. v

v
HCIA-AI Python Programming Basics Experiment Guide

1 HUAWEI Cloud Modelarts Guide

1.1 Access HUAWEI Cloud

Please use Google browser and visit the Huawei cloud:https://www.huaweicloud.com/en-us/, and then
choose English version of China station.

1.2 Log in HUAWEI Cloud


Login in the Huawei cloud official website by ‘IAM User login’.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 6


HCIA-AI Python Programming Basics Experiment Guide

And then, use your account to log in the HUAWEI Cloud.

1.3 Enter the “Modelarts”


Click “ModelArts” to enter the interface.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 7


HCIA-AI Python Programming Basics Experiment Guide

Click “Try Now”.

Choose Beijing4.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 8


HCIA-AI Python Programming Basics Experiment Guide

Click “DevEnviron” and click “Notebooks”.

We have created the notebooks, so you don’t need to add access key. Click “Cancel” and find the
notebook with your account name.

Start your notebook.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 9


HCIA-AI Python Programming Basics Experiment Guide

Choose the “Auto Stop Time” according to your need.

Wait a minute until your notebook is running, and then open your notebook.

When you enter the Jupyter interface, you have entered the experimental environment.

In this course, we use the TensorFlow-1.8 version, so you can create the TensorFlow-1.8 and complete
one experiment.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 10


HCIA-AI Python Programming Basics Experiment Guide

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 11


Huawei HCIA-AI Series Training

HCIA-AI V1.0
Python Programming Basics
Experiment Guide

Issue: 1.0

HUAWEI TECHNOLOGIES CO., LTD.


Copyright © Huawei Technologies Co., Ltd. 2019. 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: https://www.huawei.com/
Email: [email protected]

2020-04-15 Copyright © Huawei Technologies Co., Ltd. ii

ii
HCIA-AI V1.0 Python 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 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!

2020-04-15 Copyright © Huawei Technologies Co., Ltd. iii


HCIA-AI V1.0 Python Programming Basics Experiment Guide

2020-04-15 Copyright © Huawei Technologies Co., Ltd. iv


HCIA-AI V1.0 Python Programming Basics Experiment Guide

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 covers Python programming basics, and is intended to help
trainees and readers easily develop capabilities of developing AI.

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.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. v


HCIA-AI V1.0 Python Programming Basics Experiment Guide

Content
Introduction to Huawei Certification System.................................................................. iii
1 Python Programming Basics ......................................................................................... 7
1.1 Experiment Overview ............................................................................................................................. 7
1.1.1 Experiment Introduction....................................................................................................................... 7
1.1.2 Objective............................................................................................................................................. 7
1.2 Experiment Code ................................................................................................................................... 7
1.2.1 Hello World ......................................................................................................................................... 7
1.2.2 Data Type: Number ............................................................................................................................. 7
1.2.3 Data Type: Character String ................................................................................................................ 8
1.2.4 Data Type: List .................................................................................................................................... 9
1.2.5 Data Type: Tuple ................................................................................................................................10
1.2.6 Data Type: Dictionary .........................................................................................................................10
1.2.7 Data Type: Set ...................................................................................................................................10
1.2.8 Deep Copy and Shallow Copy ............................................................................................................ 11
1.2.9 if Statement........................................................................................................................................ 11
1.2.10 Loop Statement ................................................................................................................................12
1.2.11 Customizing a Function ....................................................................................................................13
1.2.12 Object-oriented Programming...........................................................................................................14
1.2.13 Standard Library Usage....................................................................................................................16
1.2.14 I/O Operations..................................................................................................................................20
1.2.15 Multiple Tasks...................................................................................................................................21
1.2.16 Regular Expressions ........................................................................................................................23

2020-04-15 Copyright © Huawei Technologies Co., Ltd. vi


HCIA-AI Python Programming Basics Experiment Guide

1 Python Programming Basics

1.1 Experiment Overview


1.1.1 Experiment Introduction
This experiment introduces the basics of Python 3, helping you quickly master the
basic data types, basic syntax, object-oriented programming, and file operations of
Python.

1.1.2 Objective
The following experiments help you master the Python programming language and
lay a foundation for the AI experiment in the future.

1.2 Experiment Code


1.2.1 Hello World
#The first Python program generates "hello world".
print ('hello world') # Generate "hello world".
print("hello world") # Generate "hello world". The output is the same when single and double
quotation marks are carried in input.

1.2.2 Data Type: Number


#You need to be familiar with the basic operations of numbers in Python. Note that
Boolean operations in Python use the keyword and/or/not instead of the operator.
print(True+False)# The output is 1. By default, True indicates 1, and False indicates 0.
print(True or False)# If True is displayed, enter or or perform the OR operation.
print(5//2)# The output is 2, and // is the rounding operator.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 7


HCIA-AI Python Programming Basics Experiment Guide

print(5%2)# The output is 1, and % is the modulo operator.


print(3**2) # The output is 9, and ** indicates the power operation.
print(5+1.6) # The output is 6.6. By default, the sum of numbers of different precisions is the number
of the highest precision type.

1.2.3 Data Type: Character String


Step 1 Basic operations on character strings:
S = 'python' # Assign value python to variable S.
# len(obj): Return the object length.
print(len(S)) # Output: 6
print(S[0], S[1], S[-1]) # The output is pyn. Elements are obtained by index.
print (S+' 1', S*2) # The output is python1 pythonpython, which indicates mergence and
duplication.

Step 2 Unchangeability of character strings:


S = 'python' # Assign value python to variable S.
S[0] = 'Z' # The program is abnormal.
S1 = 'Z' +S[1:]# New character string Zython is generated and assigned to S1.
print("S:%s, S1:%s"%(S, S1))# Output: S:python, S1:Zython

Step 3 Common operations on character strings:


S = "python" # Assign value python to variable S.
# str.split(str="", num=-1): The character string is split by separator. If the num parameter has a value,
divide the character string into num+1 substrings. The value -1 indicates that all strings are split.
print (S.split('h'))# The output is ['pyt','on']. The character string is split by h.
# str.replace(old, new[, max]): A character string generated after the old character string is replaced
with the new character string is returned. If the third parameter max is specified, the number of times
that the old string is replaced with the new string cannot exceed the value of max.
print (S.replace('py', 'PY'))# In the character string, py is replaced with PY.
# str.upper(): Return the value after lowercase letters are converted to uppercase letters.
print(S.upper()) # PYTHON
# str.lower(): Return the value after uppercase letters are converted to lowercase letters.
print ('PYTHON'.lower())# The output is python because all uppercase letters are converted to
lowercase letters.
line= 'aa,bb,ccc,dd\n' # \n is a newline character.
# str.join(sequence): sequence indicates a sequence to be joined. In the output, the new string
generated after the elements in the specified character join the sequence is returned.
print (' '.join([' life ',' is ',' short ']))# The output is life is short. The join function is used for
concatenating character strings.
hw12= '%s %s %d' % ('hello', 'world', 12) # Format the character string.
print(hw12)# Output: hello world 12

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 8


HCIA-AI Python Programming Basics Experiment Guide

----End

1.2.4 Data Type: List


Common operations on lists:

Input:
animals = ['cat', 'dog', 'monkey']
# list.append(obj): Add a new object to the end of a list.
animals.append ('fish') # Append an element.
print(animals)# Output: ['cat', 'dog', 'monkey', ‘fish’]
# list.remove(obj): Remove the first match for a value in the list.
animals.remove ('fish') # Delete element fish.
print(animals)# Output: ['cat', 'dog', 'monkey']
# list.insert(index, obj): Insert a specified object to a specified position in the list. The index indicates
the position.
animals.insert (1, 'fish') # Insert element fish at subscript 1.
print(animals)# Output: ['cat', ‘fish’, 'dog', 'monkey']
# list.pop([index=-1]): Remove the element (the last element by default) corresponding to the
subscript in the list. The index indicates the subscript.
animals.pop(1)# Delete the element whose subscript is 1.
print(animals)# Output: ['cat', 'dog', 'monkey']
# Traverse and obtain the elements and indexes.
# enumerate(sequence): Return an index sequence consisting of a data object that can be traversed
and list the data and subscripts. This function is usually used in the for loop.
for i in enumerate(animals):
print(i)# Index consisting of the element subscript and element
Output:
(0, cat)
(1, dog)
(2, monkey)
# List derivation.
squares = [x*2 for x in animals]# Generate a list of elements that comply with rules in batches.
print(squares) #['catcat ', 'dogdog ', 'monkeymonkey ']
list1 = [12,45,32,55]
# list.sort(cmp=None, key=None, reverse=False): The cmp parameter is an optional parameter. If
this parameter is specified, the method specified by this parameter is used for sorting. The key
parameter is an element used for comparison, reverse indicates the sorting rule, and False
indicates the ascending order.
list1.sort()# Sort the list.
print(list1)# Output: [12,32,45,55]
# list.reverse(): Elements in the reverse list.
list1.reverse()# Reverse the list.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 9


HCIA-AI Python Programming Basics Experiment Guide

print(list1)# Output: [55,45,32,12]

1.2.5 Data Type: Tuple


Common operations on tuples:
T=(1,2,3) # Create a tuple.
print(T+(4,5)) # Combine tuples. The output is (1, 2, 3, 4, 5).
t=(42,) # A tuple with only one element, which is different from a number.
tuple1 = (12,45,32,55,[1,0,3])# Create a tuple.
tuple1[0] = "good"# The program is abnormal, and the tuple is unchangeable.
tuple1[4][0] = 2 # Elements that can be changed in a tuple are changeable.
print(tuple1) # (12,45,32,55,[2,0,3])

1.2.6 Data Type: Dictionary


Common operations on dictionaries:
# Three value assignment operations on dictionaries.
x = {'food':'Spam','quantity':4,'color':'pink'}
X =dict(food='Spam',quantity=4, color='pink')
x = dict([("food", "Spam"),("quantity", "4"),("color","pink")])
# dict.copy(): Copy data.
d =x.copy()
d['color'] = 'red'
print(x) # {'food':'Spam','quantity':4,'color':'pink'}
print(d) # {'food':'Spam','quantity':4,'color':'red'}
# Element access.
print (d ['name']) # Obtain the error information.
print(d.get('name')) # Output: None
print(d.get('name','The key value does not exist.')) # Output: The key value does not exist.
print(d.keys()) # Output: dict_keys(['food', 'quantity', 'color'])
print(d.values())# Output: dict_values(['Spam', 4, 'red'])
print(d.items())
# Output: dict_items([('food', 'Spam'), ('quantity', 4), ('color', 'red')])
d.clear()# Clear all data in the dictionary.
print(d)# Output: {}
del(d)# Delete the dictionary.
print(d)# The program is abnormal, and a message is displayed, indicating that d is not defined.

1.2.7 Data Type: Set


Common operations on sets:
sample_set = {'Prince', 'Techs'}
print('Data' in sample_set) # The output is False. in is used to check whether an element exists in the

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 10


HCIA-AI Python Programming Basics Experiment Guide

set.
# set.add(obj): Add an element to a set. If the element to be added already exists in the set, no
operation is performed.
sample_set.add('Data') # Add element Data to the set.
print(sample_set)# Output: {'Prince', 'Techs', 'Data'}
print(len(sample_set)) # Output: 3
# set.remove(obj): Remove a specified element from a set.
sample_set.remove('Data') # Delete element Data.
print(sample_set) # {'Prince', 'Techs'}
list2 = [1,3,1,5,3]
print(list(set(list2)))# The output is [1,3,5]. The uniqueness of the set elements is used to deduplicate
the list.
sample_set = frozenset(sample_set)# Unchangeable set.

1.2.8 Deep Copy and Shallow Copy


The copy module in Python is used to implement deep copy.
import copy
Dict1 = {'name':'lee', 'age':89, 'num':[1,2,8]}# Create a dictionary.
Dict_copy = Dict1.copy()# Shallow copy.
Dict_dcopy = copy.deepcopy(Dict1)# Deep copy.
Dict1['num'][1] = 6# Change the value of the nested list in the raw data.
print('Dict1:'+str(Dict1)+"\n",' Dict_copy:'+ str(Dict_copy)+"\n",' Dict_dcopy:'+ str(Dict_dcopy))

Output:
Dict1:{‘name’:’lee’, ‘age’:89, ‘num’:[1,6,8]}
Dict_copy :{'name':'lee', 'age':89, 'num':[1,6,8]}# The shallow copy data is modified.
Dict_dcopy :{'name':'lee', 'age':89, 'num':[1,2,8]}# The deep copy data is not modified.

1.2.9 if Statement
You can use the if statement in Python to determine the level of a score input by a
user.
# Determine the entered score.
# input(): Receive input data.
score = input("Please enter your score.") # The input function receives input, which is a character
string.
# try:… except Exception:… is a Python statement used to capture exceptions. If an error occurs in
the statement in the try statement, the except statement will be executed.
try:
score = float(score)# Convert the score to a number.
if 100>=score>=90: # Check whether the entered value
is greater than the score of a level.
print("Excellent") # Generate the level when conditions are met.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 11


HCIA-AI Python Programming Basics Experiment Guide

elif 90 > score >= 80:


print("Good")
elif 80>score>0:
print("Medium")
else:
print("Bad")
except Exception:
print("Enter a correct score.")

1.2.10 Loop Statement


Step 1 for loop:
Use the for loop statement to generate a multiplication table.
for i in range(1,10):# Define the outer loop.
for j in range(1,i+1):# Define the inner loop.
# Format the output character string to align the generated result. The end attribute is set to /n
by default.
print("%d*%d=%2d"%(i,j,i*j), end=" ")
print()

Output:
1*1= 1
2*1= 2 2*2= 4
3*1= 3 3*2= 6 3*3= 9
4*1= 4 4*2= 8 4*3=12 4*4=16
5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25
6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

Step 2 while loop:


When the condition is met, the statement block is executed cyclically. To end the loop,
use break or continue.
# while loop
i = 0# Create variable i.
while i<9: # Set a condition for the loop.
i+=1 # The value of i increases by 1 in each loop.
if i == 3: # Check whether the conditions are
met.
print("Exit this loop.")
continue# Execute continue to exit the current loop.
if i == 5:

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 12


HCIA-AI Python Programming Basics Experiment Guide

print("Exit the current big loop.")


break# Exit the current big loop.
print(i)

Output:
1
2
Exit the current loop.
4
Exit the current big loop.

----End

1.2.11 Customizing a Function


Step 1 Define a function.
Define a function to return a sequence. Each number in the sequence is the sum of
the former two numbers (Fibonacci sequence).
def fibs(num): # Position parameter.
result = [0,1]# Create a list to store the sequence value.
for i in range(2, num):# Cycle num-2 times.
a = result[i-1] + result[i-2]
result.append(a)# Append the value to the list.
return result# Return the list.
fibs(5)

Output:
[0, 1, 1, 2, 3]

Step 2 Generate parameters.


The function can be customized to generate parameters transferred in different ways.
def hello(greeting='hello',name='world'): # Default parameters.
print('%s, %s!' % (greeting, name))# Format the output.
hello() # hello, world Default parameter.
hello ('Greetings') # The Greetings and world parameters are position parameters.
hello ('Greetings', 'universe') # The Greetings and universe parameters are position parameters.
hello (name= 'Gumby') # The hello and Gumby parameters are keyword parameters.

Output:
hello, world!
Greetings, world!
Greetings, universe!
hello, Gumby!

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 13


HCIA-AI Python Programming Basics Experiment Guide

----End

1.2.12 Object-oriented Programming


Step 1 Create and use classes.
Create the Dog class.
Each instance created based on the Dog class stores the name and age. We will give
each dog the sit () and roll_over () abilities.
class Dog():
"""A simple attempt to simulate a dog"""
def __init__ (self,name,age):
"""Initialize the name and age attributes."""
self.name = name
self.age = age
def sit(self):
"""Simulate a dog sitting when ordered."""
print(self.name.title()+" is now sitting")
def roll_over(self):
"""Simulate a dog rolling over when ordered."""
print(self.name.title()+" rolled over!")

Instantiate a class.
dog = Dog ("Husky",2)
dog.sit()
dog.roll_over()

Output:
Husky is now sitting
Husky rolled over!

Step 2 Access attributes.


class Employee:
'Base class of all employees'
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):
print("Name : ", self.name, ", Salary: ", self.salary)
# Create the first object of the Employee class.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 14


HCIA-AI Python Programming Basics Experiment Guide

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:
Name : Zara ,Salary: 2000
Name : Manni ,Salary: 5000
Total Employee 2

Step 3 Inherit a class.


One of the main benefits of object-oriented programming is code reuse, which is
achieved through inheritance. Inheritance can be understood as the type and subtype
relationships between classes.
class Parent: # Define the parent class.
parentAttr = 100
def __init__(self):
print("Invoke the parent class to construct a function.")
def parentMethod(self):
print('Invoke a parent class method.')
def setAttr(self, attr):
Parent.parentAttr = attr
def getAttr(self):
print("Parent attribute:", Parent.parentAttr)
class Child(Parent): # Define a child class.
def __init__(self):
print("Invoke a child class to construct a method.")
def childMethod(self):
print('Invoke a child method.')
c = Child() # Instantiate a child class.
c.childMethod() # Invoke the method of a child class.
c.parentMethod() # Invoke the method of a parent class.
c.setAttr(200) # Invoke the method of the parent class again to set the attribute value.
c.getAttr() # Invoke the method of the parent class again to obtain the attribute value.

Output:
Method constructed by invoking a child class
The method of a child class is invoked.
The method of a parent class is invoked.
Parent attribute: 200

Step 4 Class attributes and methods.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 15


HCIA-AI Python Programming Basics Experiment Guide

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) # An error is reported, indicating that the instance cannot access
private variables.

Output:
1

---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)

<ipython-input-4-ee25477dd398> in <module>

10 counter.count()

11 print(counter.publicCount)

---> 12 print(counter.__secretCount) # An error is reported, indicating that the instance

AttributeError: 'JustCounter' object has no attribute '__secretCount'

----End

1.2.13 Standard Library Usage


Step 1 sys
sys.exit([n]): This method can be used to exit the current program. If the value of n is 0,
the program exits normally; if the value of n is not 0, the program exits abnormally.
import sys
for i in range(100):
print(i)
if i ==5:
sys.exit(0)

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 16


HCIA-AI Python Programming Basics Experiment Guide

Output:
0
1
2
3
4
5
An exception has occurred, use %tb to see the full traceback.

sys.path: Obtain the module search path.


sys.path

Output:
['D:\\python3.6\\python36.zip',
'D:\\python3.6\\DLLs',
'D:\\python3.6\\lib',
'D:\\python3.6',
'',
'D:\\python3.6\\lib\\site-packages',
'D:\\python3.6\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\xxx\\.ipython']

sys.platform: Obtain the current system platform.


sys.platform

Output:
'win32'

sys.argv: Transfer parameters from outside of the program to the program. The
parameters are transferred in list format. The first parameter is the current file name.
Create the .py file test.py (in the current folder or on the desktop) and write the
following code:
print(sys.argv[1])

Switch to the file path in the command line and run the program.
python test.py hello

Output:
hello

Step 2 os
import os
# os.getpid() Obtain the current process ID.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 17


HCIA-AI Python Programming Basics Experiment Guide

print("ID of the current process:", os.getpid())

# os.getppid(): Obtain the ID of the current parent process.


print(" ID of the current parent process:", os.getppid())

# os.getcwd(): Obtain the current path.


cwd = os.getcwd()
print(" The current path is:",cwd)

# os.chdir(path): Change the current working directory.


#os.chdir("C:\\")
#print("The modified path is:", os.getcwd())

# os.listdir(): Return all files in the directory.


#print("Files in the current directory:", os.listdir(cwd))

# os.walk(): Export all files in the current path.


for root, dirs, files in os.walk(cwd, topdown=False):
for name in files:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))

Output:
ID of the current process: 7580

ID of the current parent process: 18220

The current path is: D:\TensorFlow\IA1.0

The modified path is: C:\

Files in the current directory: ['.ipynb_checkpoints', 'test.py', 'Untitled.ipynb']

D:\TensorFlow\IA1.0\.ipynb_checkpoints\Untitled-checkpoint.ipynb

D:\TensorFlow\IA1.0\test.py

D:\TensorFlow\IA1.0\Untitled.ipynb

D:\TensorFlow\IA1.0\.ipynb_checkpoints

os.path module: Obtain file attributes.


import os
# os.path.abspath(path): Return the absolute path.
print("The absolute path of text.txt is:",os.path.abspath("text.txt")) # The text.txt file is a file in the
current folder. (In the previous experiment, the current path is changed to C:\, and you need to
switch back to the original path.)

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 18


HCIA-AI Python Programming Basics Experiment Guide

# os.path.exists(path): If the file exists, True is returned; if the file does not exist, False is returned.
print("Whether the text.txt file exists: ",os.path.exists("text.txt"))

# os.path.getsize(path): Return the file size. If the file does not exist, an error is returned.
print("Size of the text.txt file: ",os.path.getsize("text.txt"))

# os.path.isfile(path): Check whether the path is a file.


print("Whether text.txt is a file:",os.path.isfile("text.txt"))

# os.path.isdir(path): Check whether the path is a folder.


print("Whether text.txt is a folder:",os.path.isdir("text.txt"))

Output:
Absolute path of text.txt is D:\python project\text.txt
Whether text.txt exists: True
Size of the text.txt file: 0
Whether text.txt is a file: True
Whether text.txt is a folder: False

Step 3 time
import time
# time.time(): Obtain the current timestamp.
time_now = time.time()
print("Timestamp:",time_now)

# time.localtime(): Obtain the time tuple.


localtime = time.localtime(time_now)
print("The local time is:", localtime)

# time.asctime(): Obtain formatted time.


localtime = time.asctime(localtime)
print("The local time is:", localtime)

#time.strftime(format[, t]): Receive the time tuple and return the local time expressed in a readable
string, in the format specified by the format parameter.
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

Output:
Timestamp: 1555950340.4777014
Local time: time.struct_time(tm_year=2019, tm_mon=4, tm_mday=23, tm_hour=0, tm_min=25,
tm_sec=40, tm_wday=1, tm_yday=113, tm_isdst=0)
Local time: Tue Apr 23 00:25:40 2019

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 19


HCIA-AI Python Programming Basics Experiment Guide

2019-04-23 00:25:40

----End

1.2.14 I/O Operations


Step 1 Write data to a file.
f = open("text.txt", 'w') # Open the text.txt file. If the file does not exist, a new file will be created.
Str = input("Please enter the content to be written:")
f.write(Str)
f.close()

Output:

Figure 1-1 Content to be written

Figure 1-2 File content

Step 2 Read file data.


f = open("text.txt", 'r')
print(f.read(6))# Read six characters and move the cursor six characters backward.
print(f.read())# Read from the current position of the cursor to the end.
f.close()

Output:
python
file operation

Step 3 Use the context manager to operate the file.


# Use the with statement to write file data.
with open("text1.txt", 'w') as f:
f.write("python file operation")
# Use the with statement to read the file content.
with open("text1.txt", 'r') as f:

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 20


HCIA-AI Python Programming Basics Experiment Guide

print(f.read())

Output:
python file operation

Step 4 Use the os module to operate the file.


import os
os.rename("text.txt","text0.txt")# Rename the file.
os.remove("text1.txt")# Delete the file.

----End

1.2.15 Multiple Tasks


Step 1 Multiple threads
Use multiple threads to execute tasks.
import threading
from time import sleep,ctime
def work1():
for i in range(3):
print("work1 is being executed...%d"%i)
sleep(1)

def work2():
for i in range(3):
print("work2 is being executed...%d"%i)
sleep(1)

if __name__ == '__main__':
print('---Start---:%s'%ctime())

t1 = threading.Thread(target=work1)# Thread 1
t2 = threading.Thread(target=work2)# Thread 2
# Start the thread.
t1.start()
t2.start()

sleep(5)
print('---End---:%s' %ctime())

Output:
---Start---:Mon Apr 15 10:55:16 2019
work1 is being executed...0

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 21


HCIA-AI Python Programming Basics Experiment Guide

work2 is being executed...0


work1 is being executed...1
work2 is being executed...1
work1 is being executed...2
work2 is being executed...2
---End---:Mon Apr 15 10:55:21 2019

Thread synchronization:
import threading
import time
g_num = 0
def test1(num):
global g_num # Use the global variable g_num.
for i in range(num):
mutex.acquire() # Lock
g_num += 1
mutex.release() # Unlock
print("---test1---g_num=%d"%g_num)

def test2(num):
global g_num
for i in range(num):
mutex.acquire() # Lock
g_num += 1
mutex.release() # Unlock

print("---test2---g_num=%d"%g_num)

# Create a mutual exclusion lock.


# By default, the global variable is not locked. You can delete the lock and view the resource
contention result.
mutex = threading.Lock()

# Create two threads and add 1000000 times for g_num.


p1 = threading.Thread(target=test1, args=(1000000,))
p1.start()

p2 = threading.Thread(target=test2, args=(1000000,))
p2.start()

# Wait for the calculation to be complete.


time.sleep(5)

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 22


HCIA-AI Python Programming Basics Experiment Guide

print("The final result after operations are performed on the same global variable by two
threads:%s" % g_num)

Output:
---test2---g_num=1971982---test1---g_num=2000000

After two threads operate the same global variable, the final result is as follows: 2000000

1.2.16 Regular Expressions


Step 1 re.match function
The re.match function attempts to match a pattern from the start position of a string. If
the match is not successful, match() returns none.
Function syntax:
re.match(pattern, string, flags=0)

Example:
import re
print(re.match('www', 'www.huawei.com').span()) # Match at the start position.
print(re.match('com', 'www.huawei.com')) # Not match at the start position.

Output:
(0, 3)
None

Step 2 re.search method


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

Example:
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!!" )

Output:
searchObj.group() : Cats are smarter than dogs

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 23


HCIA-AI Python Programming Basics Experiment Guide

searchObj.group(1) : Cats
searchObj.group(2) : smarter

Step 3 Retrieval and replacement


The re module of Python provides re.sub function to replace the matching items in the
string.
Syntax:
re.sub(pattern, repl, string, count=0, flags=0)

Example:
import re
phone = "2019-0101-000 # This is a phone number."
# Delete the Python comment in the character string.
num = re.sub(r'#.*$', "", phone)
print("The phone number is: ", num)
# Delete the hyphens from the phone number.
num = re.sub(r'\D', "", phone)
print("The phone number is: ", num)

Output:
The phone number is: 2019-0101-000
The phone number is: 20190101000

Step 4 re.compile function


The compile function is used to compile regular expressions and generate a regular
expression object (pattern) for the match() and search() functions.
The syntax format is as follows:
re.compile(pattern[, flags])

Example:
import re
pattern = re.compile(r'\d+') # At least one digit is matched.
n = pattern.match ('one12twothree34four') # The information is not found from the header.
print(n)

m = pattern.search ('one12twothree34four') # Match from the position of 'e'. No match is found.


print(m)
print(m.group())

Output:
None
<_sre.SRE_Match object; span=(3, 5), match='12'>
12

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 24


HCIA-AI Python Programming Basics Experiment Guide

Step 5 re.split()
The split method splits a character string based on the matched substring and returns
a list. The usage of the method is as follows:
re.split(pattern, string[, maxsplit=0, flags=0])

Example:
import re
s = re.split('\W+', 'www.huawei.com')
print(s)

Output:
['www', 'huawei', 'com']

----End

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 25


Huawei HCIA-AI Series Training

HCIA-AI V1.0
AI Mathematics
Experiment Guide
Issue: 1.0

HUAWEI TECHNOLOGIES CO., LTD.


Copyright © Huawei Technologies Co., Ltd. 2019. 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: https://www.huawei.com/
Email: [email protected]

2020-04-15 Copyright © Huawei Technologies Co., Ltd. ii


错误!未知的文档属性名称

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!

2020-04-15 Copyright © Huawei Technologies Co., Ltd. iii


HCIA-AI V1.0 AI Mathematics Experiment Guide

2020-04-15 Copyright © Huawei Technologies Co., Ltd. iv


HCIA-AI V1.0 AI Mathematics Experiment Guide

Contents
1 Experiment Overview ..................................................................................................... 7
1.1 Experiment Introduction ......................................................................................................................... 7
1.2 Description............................................................................................................................................. 7
1.3 Skill Requirements ................................................................................................................................. 7
1.4 Experiment Environment Overview......................................................................................................... 8

2 Basic Mathematics Experiment ..................................................................................... 9


2.1 Introduction ............................................................................................................................................ 9
2.1.1 Content ............................................................................................................................................... 9
2.1.2 Frameworks ........................................................................................................................................ 9
2.2 Implementation ...................................................................................................................................... 9
2.2.1 ceil Implementation ............................................................................................................................. 9
2.2.2 floor Implementation ...........................................................................................................................10
2.2.3 degrees Implementation .....................................................................................................................10
2.2.4 exp Implementation ............................................................................................................................10
2.2.5 fabs Implementation ........................................................................................................................... 11
2.2.6 factorial Implementation ..................................................................................................................... 11
2.2.7 fsum Implementation .......................................................................................................................... 11
2.2.8 fmod Implementation .......................................................................................................................... 11
2.2.9 log Implementation ............................................................................................................................. 11
2.2.10 sqrt Implementation ..........................................................................................................................12
2.2.11 pi Implementation .............................................................................................................................12
2.2.12 pow Implementation .........................................................................................................................12

3 Linear Algebra Experiment .......................................................................................... 13


3.1 Introduction ...........................................................................................................................................13
3.1.1 Linear Algebra ....................................................................................................................................13
3.1.2 Code Implementation .........................................................................................................................13
3.2 Linear Algebra Implementation ..............................................................................................................13
3.2.1 Reshape Operation ............................................................................................................................13
3.2.2 Transpose Implementation .................................................................................................................14
3.2.3 Matrix Multiplication Implementation ...................................................................................................15
3.2.4 Matrix Operations ...............................................................................................................................16
3.2.5 Inverse Matrix Implementation............................................................................................................16
3.2.6 Eigenvalue and Eigenvector ...............................................................................................................17
3.2.7 Determinant .......................................................................................................................................18
3.2.8 Singular Value Decomposition ............................................................................................................18
3.2.9 Application Scenario of Singular Value Decomposition: Image Compression .......................................21
3.2.10 Solving a System of Linear Equations...............................................................................................23

4 Probability Theory Experiment .................................................................................... 25

2020-04-15 Copyright © Huawei Technologies Co., Ltd. v


HCIA-AI V1.0 AI Mathematics Experiment Guide

4.1 Introduction ...........................................................................................................................................25


4.1.1 Probability Theory ..............................................................................................................................25
4.1.2 Experiment Overview .........................................................................................................................25
4.2 Probability Theory Implementation ........................................................................................................25
4.2.1 Mean Value Implementation ...............................................................................................................25
4.2.2 Variance Implementation ....................................................................................................................26
4.2.3 Standard Deviation Implementation ....................................................................................................26
4.2.4 Covariance Implementation ................................................................................................................27
4.2.5 Correlation Coefficient ........................................................................................................................27
4.2.6 Binomial Distribution Implementation ..................................................................................................27
4.2.7 Poisson Distribution Implementation ...................................................................................................28
4.2.8 Normal Distribution.............................................................................................................................29
4.3 Application Scenario.................................................................................................... 错误!未定义书签。
4.3.1 Adding Noise to Images............................................................................................ 错误!未定义书签。

5 Optimization Experiment .............................................................................................. 31


5.1 Implementation of the Least Squares Method ........................................................................................31
5.1.1 Algorithm ............................................................................................................................................31
5.1.2 Case Introduction ...............................................................................................................................31
5.1.3 Code Implementation .........................................................................................................................32
5.2 Gradient Descent Implementation .........................................................................................................33
5.2.1 Algorithm ............................................................................................................................................33
5.2.2 Case Introduction ...............................................................................................................................33
5.2.3 Code Implementation .........................................................................................................................34

2020-04-15 Copyright © Huawei Technologies Co., Ltd. vi


HCIA-AI V1.0 AI Mathematics Experiment Guide

1 Experiment Overview

1.1 Experiment Introduction


This course will introduce the implementation of basic mathematics experiments
based on Python, including basic operators, linear algebra, probability theory, and
optimization. Upon completion of this course, you will be able to master the
implementation of basic mathematical methods based on Python and apply them to
actual projects to solve business problems.
After completing this experiment, you will be able to:
 Master how to use Python to implement basic operators.
 Master how to use Python to implement calculation related to linear algebra and
probability theory.
 Master the implementation of Python optimization.

1.2 Description
This document describes five experiments:
 Experiment 1: basic mathematics experiment
 Experiment 2: implementation of operators related to linear algebra
 Experiment 3: implementation of statistical distribution of the probability theory
 Experiment 4: implementation of the least squares method
 Experiment 5: implementation of gradient descent

1.3 Skill Requirements


This course is a basic mathematics course based on Python. Before starting this
experiment, you are expected to master knowledge about the basic linear algebra,
probability theory, and optimization.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 7


HCIA-AI V1.0 AI Mathematics Experiment Guide

1.4 Experiment Environment Overview


 Use a PC running the Windows 7/Windows 10 64-bit operating system. The PC
must be able to access the Internet.
 Download and install Anaconda 3 4.4.0 or a later version based on the operating
system version.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 8


HCIA-AI V1.0 AI Mathematics Experiment Guide

2 Basic Mathematics Experiment

2.1 Introduction
2.1.1 Content
The basic mathematics knowledge is widely used in data mining, especially in
algorithm design and numerical processing. The main purpose of this section is to
implement some basic mathematical algorithms based on the Python language and
basic mathematics modules, laying a foundation for learning data mining.

2.1.2 Frameworks
This document mainly uses the math library, NumPy library, and SciPy library. The
math library is a standard library of Python and provides some common mathematical
functions. The NumPy library is an extended library of Python, used for numerical
calculation. It can solve problems about linear algebra, random number generation,
and Fourier transform. The SciPy library is used to handle problems related to
statistics, optimization, interpolation, and integration.

2.2 Implementation
Import libraries:
import math
import numpy as np

2.2.1 ceil Implementation


The ceil(x) function obtains the minimum integer greater than or equal to x. If x is an
integer, the returned value is x.
Input:
math.ceil(4.01)

Output:
5

Input:

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 9


HCIA-AI V1.0 AI Mathematics Experiment Guide

math.ceil(4.99)

Output:
5

2.2.2 floor Implementation


The floor(x) function obtains the maximum integer less than or equal to x. If x is an
integer, the returned value is x.
Input:
math.floor(4.1)

Output:
4

Input:
math.floor(4.999)

Output:
4

2.2.3 degrees Implementation


The degrees(x) function converts x from a radian to an angle.
Input:
math.degrees(math.pi/4)

Output:
45.0

Input:
math.degrees(math.pi)

Output:
180.0

2.2.4 exp Implementation


The exp(x) function returns math.e, that is, 2.71828 to the power of x.
Input:
math.exp(1)

Output:
2.718281828459045

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 10


HCIA-AI V1.0 AI Mathematics Experiment Guide

2.2.5 fabs Implementation


The fabs(x) function returns the absolute value of x.
Input:
math.fabs(-0.003)

Output:
0.003

2.2.6 factorial Implementation


The factorial(x) function returns the factorial of x.
Input:
math.factorial(3)

Output:
6

2.2.7 fsum Implementation


The fsum(iterable) function summarizes each element in the iterator.
Input:
math.fsum([1,2,3,4])

Output:
10

2.2.8 fmod Implementation


The fmod(x, y) function obtains the remainder of x/y. The value is a floating-point
number.
Input:
math.fmod(20,3)

Output:
2.0

2.2.9 log Implementation


The log([x, base]) function returns the natural logarithm of x. By default, e is the base
number. If the base parameter is specified, the logarithm of x is returned based on
the given base. The calculation formula is log(x)/log(base).
Input:

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 11


HCIA-AI V1.0 AI Mathematics Experiment Guide

math.log(10)

Output:
2.302585092994046

2.2.10 sqrt Implementation


The sqrt(x) function returns the square root of x.
Input:
math.sqrt(100)

Output:
10.0

2.2.11 pi Implementation
pi is a numerical constant, indicating the circular constant.
Input:
math.pi

Output:
3.141592653589793

2.2.12 pow Implementation


The pow(x, y) function returns the x to the power of y, that is, x**y.
Input:
math.pow(3,4)

Output:
81.0

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 12


HCIA-AI V1.0 AI Mathematics Experiment Guide

3 Linear Algebra Experiment

3.1 Introduction
3.1.1 Linear Algebra
Linear algebra is a discipline widely used in various engineering fields. The concepts
and conclusions of linear algebra can greatly simplify the derivations and expressions
of data mining formulas. Linear algebra can simplify complex problems so that we
can perform efficient mathematical operations.
Linear algebra is a mathematical tool. It not only provides the technology for array
operations, but also provides data structures such as vectors and matrices to store
numbers and rules for addition, subtraction, multiplication, and division.

3.1.2 Code Implementation


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 NumPy. The mathematical science library
SciPy is also used to illustrate equation solution in this section.

3.2 Linear Algebra Implementation


Import libraries:
import numpy as np
import scipy as sp

3.2.1 Reshape Operation


There is no reshape operation in mathematics, but it is a very common operation in
the NumPy operation library. 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 image is read, it can

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 13


HCIA-AI V1.0 AI Mathematics Experiment Guide

be transformed from 1x100 to 10x10 through the reshape operation. The following is
an example:
Input:
Generate a vector that contains integers from 0 to 11.
x = np.arange(12)
print(x)

Output:
[ 0 1 2 3 4 5 6 7 8 9 10 11]

View the array size.


x.shape

Output:
(12,)

Convert x into a two-dimensional matrix, where the first dimension of the matrix is 1.
x = x.reshape(1,12)
print(x)

Output:
[[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]

View the array size.


x.shape

Output:
(1, 12)

Convert x to a 3x4 matrix.


x = x.reshape(3,4)
print(x)

Output:
[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]

3.2.2 Transpose Implementation


The transpose of vectors and matrices is to switch the row and column indices. For
the transpose of tensors of three dimensions and above, you need to specify the
transpose dimension.
Input:

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 14


HCIA-AI V1.0 AI Mathematics Experiment Guide

Generate a 3x4 matrix and transpose the matrix.


A = np.arange(12).reshape(3,4)
print(A)

Output:
[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]

Input:
A.T

Output:
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])

3.2.3 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.
Input:
A = np.arange(6).reshape(3,2)
B = np.arange(6).reshape(2,3)
print(A)

Output:
[[0 1]
[2 3]
[4 5]]

Input:
print(B)

Output:
[[0, 1, 2],
[3, 4, 5]]

Matrix multiplication:
np.matmul(A,B)

Output:
array([[ 3, 4, 5],

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 15


HCIA-AI V1.0 AI Mathematics Experiment Guide

[ 9, 14, 19],
[15, 24, 33]])

3.2.4 Matrix Operations


Element operations are operations on matrices of the same shape. For example,
element operations indicate the addition, subtraction, division, and multiplication
operations on elements with the same position in two matrices.
Input:
Matrix creation:
A = np.arange(6).reshape(3,2)

Matrix multiplication:
print(A*A)

Output:
array([[ 0, 1],
[ 4, 9],
[16, 25]])

Matrix addition:
print(A + A)

Output:
array([[ 0, 2],
[ 4, 6],
[ 8, 10]])

3.2.5 Inverse Matrix Implementation


Inverse matrix implementation is applicable only to square matrices.
Input:
A = np.arange(4).reshape(2,2)
print(A)

Output:
array([[0, 1],
[2, 3]])

Inverse matrix:
np.linalg.inv(A)

Output:
array([[-1.5, 0.5],

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 16


HCIA-AI V1.0 AI Mathematics Experiment Guide

[ 1. , 0. ]])

3.2.6 Eigenvalue and Eigenvector


This section describes how to obtain the matrix eigenvalues and eigenvectors and
implement visualization.
Input:
#Import libraries:
from scipy.linalg import eig
import numpy as np
import matplotlib.pyplot as plt

#Obtain the eigenvalue and eigenvector:


A = [[1, 2],# Generate a 2x2 matrix.
[2, 1]]
evals, evecs = eig(A) # Calculate the eigenvalue (evals) and eigenvector (evecs) of A.
evecs = evecs[:, 0], evecs[:, 1]

#The plt.subplots() function returns a figure instance named fig and an AxesSubplot
instance named ax. The fig parameter indicates the entire figure, and ax indicates
the coordinate axis. Plotting:
fig, ax = plt.subplots()

#Make the coordinate axis pass the origin.


for spine in ['left', 'bottom']:# Make the coordinate axis in the lower left corner pass the origin.
ax.spines[spine].set_position('zero')

#Draw a grid:
ax.grid(alpha=0.4)

#Set the coordinate axis ranges.


xmin, xmax = -3, 3
ymin, ymax = -3, 3
ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax))

#Draw an eigenvector. Annotation is to use an arrow that points to the content to be


explained and add a description. In the following code, s indicates the input, xy
indicates the arrow direction, xytext indicates the text location, and arrowprops
uses arrowstyle to indicate the arrow style or type.
for v in evecs:
ax.annotate(s="", xy=v, xytext=(0, 0),
arrowprops=dict(facecolor='blue',
shrink=0,
alpha=0.6,
width=0.5))

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 17


HCIA-AI V1.0 AI Mathematics Experiment Guide

#Draw the eigenspace:


x = np.linspace(xmin, xmax, 3)# Return evenly spaced numbers over a specified interval.
for v in evecs:
a = v[1] / v[0] # Unit vector in the eigenvector direction.
ax.plot(x, a * x, 'r-', lw=0.4)# The lw parameter indicates the line thickness.
plt.show()

Figure 3-1 Visualized chart

Illustration: The vectors with the blue arrow are eigenvectors, and the space formed
by the two red lines is the eigenspace.

3.2.7 Determinant
This section describes how to obtain the determinant of a matrix.
Input:
E = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print(np.linalg.det(E))

Output:
0.0

3.2.8 Singular Value Decomposition


Case introduction:
If we are a news provider, we need to push a large number of articles from different
fields to users every day. However, the quality of these articles varies. To improve
user experience, we need to classify a lot of articles and select better articles for

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 18


HCIA-AI V1.0 AI Mathematics Experiment Guide

users. So the question is, what method can be used to automatically classify the
articles? The following describes a simple approach. Generally, the title of an article
consists of a few core concept words, which represent the subject of the article.
Therefore, we can determine whether two articles belong to the same type based on
the similarity between titles. The similarity between titles depends on the similarity
between words. Then how do we check the similarity between words in the titles? We
can use singular value decomposition to solve this problem. Assume that there are
eight titles. The keywords of the titles are as follows:
title_1 = ["dad","dad","stock"]
title_2 = ["books","books","value","estate"]
title_3 = ["books","decomposition"]
title_4 = ["stock"]
title_5 = ["dad"]
title_6 = ["value","singular","decomposition"]
title_7 = ["dad","singular"]
title_8 = ["singular","estate","decomposition"]

Input:
#Import modules:
import numpy as np
import matplotlib.pyplot as plt

#Enter keywords:
words = ["books","dad","stock","value","singular","estate","decomposition"]

#Assume that there are eight titles and seven keywords. Record the number of times
each keyword appears in each title to obtain matrix X. In matrix X, each row indicates
a title, each column indicates a keyword, and each element in the matrix indicates
the number of times a keyword appears in a title.
X=np.array([[0,2,1,0,0,0,0],[2,0,0,1,0,1,0],[1,0,0,0,0,0,1],[0,0,1,0,0,0,0],[0,1,0,0,0,0,0],[0,0,0,1,1,0,1],
[0,1,0,0,1,0,0],[0,0,0,0,1,1,1]])

#Singular value decomposition:


U,s,Vh=np.linalg.svd(X)

#Output the left singular matrix U and its shape:


print("U=",U)
print("U.shape",U.shape)

Output:
U= [[-1.87135757e-01 -7.93624528e-01 2.45011855e-01 -2.05404352e-01
-3.88578059e-16 5.75779114e-16 -2.57394431e-01 -4.08248290e-01]
[-6.92896814e-01 2.88368077e-01 5.67788037e-01 2.22142537e-01
2.54000254e-01 -6.37019839e-16 -2.21623012e-02 2.05865892e-17]
[-3.53233681e-01 1.22606651e-01 3.49203461e-02 -4.51735990e-01
-7.62000762e-01 1.27403968e-15 2.72513448e-01 3.80488702e-17]

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 19


HCIA-AI V1.0 AI Mathematics Experiment Guide

[-2.61369658e-02 -1.33189110e-01 7.51079037e-02 -6.44727454e-01


5.08000508e-01 1.77635684e-15 3.68146235e-01 4.08248290e-01]
[-8.04993957e-02 -3.30217709e-01 8.49519758e-02 2.19661551e-01
-2.54000254e-01 -4.81127681e-16 -3.12770333e-01 8.16496581e-01]
[-3.95029694e-01 1.56123876e-02 -5.28290830e-01 -6.82340484e-02
1.27000127e-01 -7.07106781e-01 -2.09360158e-01 1.55512464e-17]
[-2.02089013e-01 -3.80395849e-01 -2.12899198e-01 4.80790894e-01
8.04483689e-16 -1.60632798e-15 7.33466480e-01 1.76241226e-16]
[-3.95029694e-01 1.56123876e-02 -5.28290830e-01 -6.82340484e-02
1.27000127e-01 7.07106781e-01 -2.09360158e-01 -1.23226632e-16]]
U.shape (8, 8)

#Output the singular matrix and its shape:


print("s=",s)
print("s.shape",s.shape)

Output:

the results in descending order, where each singular value corresponds to a left
singular vector and a right singular vector:
s= [2.85653844 2.63792139 2.06449303 1.14829917 1. 1.
0.54848559]
s.shape (7,)

#Output the right singular matrix Vh and its shape:


print("Vh",Vh)
print("Vh.shape",Vh.shape)

Output:
Vh [[-6.08788345e-01 -2.29949618e-01 -7.46612474e-02 -3.80854846e-01

-3.47325416e-01 -3.80854846e-01 -4.00237243e-01]


[ 2.65111314e-01 -8.71088358e-01 -3.51342402e-01 1.15234846e-01
-1.32365989e-01 1.15234846e-01 5.83153945e-02]
[ 5.66965547e-01 1.75382762e-01 1.55059743e-01 1.91316736e-02
-6.14911671e-01 1.91316736e-02 -4.94872736e-01]
[-6.48865369e-03 2.52237176e-01 -7.40339999e-01 1.34031699e-01
2.99854608e-01 1.34031699e-01 -5.12239408e-01]
[-2.54000254e-01 -2.54000254e-01 5.08000508e-01 3.81000381e-01
2.54000254e-01 3.81000381e-01 -5.08000508e-01]
[ 0.00000000e+00 -7.68640544e-16 2.33583082e-15 -7.07106781e-01
-1.21802199e-15 7.07106781e-01 1.91457709e-15]
[ 4.16034348e-01 -1.71550021e-01 2.01922906e-01 -4.22112199e-01
5.73845817e-01 -4.22112199e-01 -2.66564648e-01]]

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 20


HCIA-AI V1.0 AI Mathematics Experiment Guide

Vh.shape (7, 7)

#Set the coordinate axis ranges.


plt.axis([-0.8,0.2,-0.8,0.8])

#Each keyword is represented by a vector of 1x8. Now, the vector is reduced to a


vector of 1x2 for visualization.
for i in range(len(words)):
plt.text(U[i,0],U[i,1],words[i])
plt.show()

Figure 3-2 Visualization result

Illustration: After obtaining the visualized two-dimensional result, we can group


keywords, such as singular, value, and decomposition, which are close to each
other. The words stock and estate often appear at the same time.
After the word vector representation is obtained, we can calculate the similarity
between words by selecting a vector distance (for example, Euclidean metric or
Manhattan distance) calculation manner and obtain the similarity between article
titles based on some policies (for example, a sum or a mean value of the word pair
similarities). Then we can classify articles based on the similarity between article
titles, which can approximately represent the similarity between article contents.

3.2.9 Application Scenario of Singular Value Decomposition:


Image Compression
A grayscale image can be regarded as a matrix. If singular value decomposition is
performed on such a matrix, singular values of the singular value matrix are arranged
in descending order. A singular vector with a larger singular value can save more

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 21


HCIA-AI V1.0 AI Mathematics Experiment Guide

information, but the singular values usually attenuate quickly. Therefore, the first K
singular values and corresponding singular vectors include most information in the
image. As a result, an image formed by the first K singular values and their singular
vectors can achieve basically the same definition as the original image, but the data
amount is greatly reduced. In this way, image data compression can be implemented.
Input:
import numpy as np
from pylab import *
import matplotlib.pyplot as plt

# Read and save the grayscale image.


img = imread('lena.jpg')[:]
plt.savefig('./lena_gray')
plt.gray()
# Draw a grayscale image.
plt.figure(1)
plt.imshow(img)

Output:

Figure 3-3

# Read and print the image length and width.


m,n = img.shape
print(np.shape(img))
Output:
(512, 512)

# Perform singular value decomposition on the image matrix.


U,sigma,V = np.linalg.svd(img)
# Print the singular value shape.
print(np.shape(sigma))
Output:

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 22


HCIA-AI V1.0 AI Mathematics Experiment Guide

(512,)

# Arrange singular values into a diagonal matrix.


sigma = resize(sigma, [m,1])*eye(m,n)
# Use the first K singular values and their singular vectors for image compression.
k= 100
# Create an image with the first K singular values and their singular vectors.
img1 = np.dot(U[:,0:k],np.dot(sigma[0:k,0:k],V[0:k,:]))
plt.figure(2)
# Print the compressed image.
plt.imshow(img1)
plt.show()

Figure 3-4

3.2.10 Solving a System of Linear Equations


Solving a system of linear equations is simple because it requires only one function
(scipy.linalg.solve).
Case introduction: There are 3 kinds of fruits with unknown prices: apples, bananas,
grapes. Tom has spent CNY10 buying 10 kg apples, 2 kg bananas, and 5 kg grapes.
Lily has spent CNY8 buying 4 kg apples, 4 kg bananas, and 2 kg grapes. Tom has
spent CNY5 buying 2 kg apples, 2 kg bananas, and 2 kg grapes. How much does it
cost to buy 1 kg apples, bananas, and grapes, respectively?
Based on the known conditions, the following equations can be constructed, where
x_1, x_2, and x_3 are the prices of apples, bananas, and grapes, respectively. The
purpose is to calculate the values of x_1, x_2, and x_3.
10x_1 + 2x_2 + 5x_3 = 10
4x_1 + 4x_2 + 2x_3 = 8
2x_1 + 2x_2 + 2x_3 = 5
Input:

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 23


HCIA-AI V1.0 AI Mathematics Experiment Guide

import numpy as np
from scipy.linalg import solve
a = np.array([[10, 2, 5], [4, 4, 2], [2, 2, 2]])
b = np.array([10,8,5])
x = solve(a, b)
print(x)

Output of print(x):
array([0.25 1.25 1. ])

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 24


HCIA-AI V1.0 AI Mathematics Experiment Guide

4 Probability Theory Experiment

4.1 Introduction
4.1.1 Probability Theory
Probability theory is a branch of mathematics concerned with the quantitative
regularity of random phenomena. A random phenomenon is a situation in which we
know what outcomes could happen, but we do not know which particular outcome did
or will happen, while a decisive phenomenon is a situation in which a result inevitably
occurs under certain conditions.
The probability theory is a mathematical tool used to describe uncertainties. A large
number of data mining algorithms build models based on the sample probabilistic
information or through inference.

4.1.2 Experiment Overview


This section describes the knowledge of probability and statistics, and mainly uses
the NumPy and SciPy frameworks.

4.2 Probability Theory Implementation


Import libraries:
import numpy as np
import scipy as sp

4.2.1 Mean Value Implementation


Input:
#Data preparation:
ll = [[1,2,3,4,5,6],[3,4,5,6,7,8]]

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 25


HCIA-AI V1.0 AI Mathematics Experiment Guide

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

Output:
4.5

Input:
np.mean(ll,0) # Calculate the mean value by column. The value 0 indicates the column vector.

Output:
array([2., 3., 4., 5., 6., 7.])

Input:
np.mean(ll,1) # Calculate the mean value by row. The value 1 indicates the row vector.

Output:
array([3.5, 5.5])

4.2.2 Variance Implementation


#Data preparation:
b=[1,3,5,6]
ll=[[1,2,3,4,5,6],[3,4,5,6,7,8]]

#Calculate the variance:


np.var(b)

Output:
3.6875

Input:
np.var(ll,1) # The value of the second parameter is 1, indicating that the variance is calculated
by row.

Output:
[2.91666667 2.91666667]

4.2.3 Standard Deviation Implementation


Input:
#Data preparation:
ll=[[1,2,3,4,5,6],[3,4,5,6,7,8]]
np.std(ll)

Output:
1.9790570145063195

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 26


HCIA-AI V1.0 AI Mathematics Experiment Guide

4.2.4 Covariance Implementation


Input:
#Data preparation:
b=[1,3,5,6]
np.cov(b)

Output:
4.916666666666666

4.2.5 Correlation Coefficient


Input:
#Data preparation:
vc=[1,2,39,0,8]
vb=[1,2,38,0,8]

#Function-based implementation:
np.corrcoef(vc,vb)

Output:
array([[1. , 0.99998623],
[0.99998623, 1. ]])

4.2.6 Binomial Distribution Implementation


The random variable X, which complies with binomial distribution, indicates the
number of successful times in n times of independent and identically distributed
Bernoulli experiments. The success probability of each experiment is p.
Input:
from scipy.stats import binom, norm, beta, expon
import numpy as np
import matplotlib.pyplot as plt

# The n and p parameters indicate the success times and probability in the binomial formula,
respectively, and size indicates the number of sampling times.
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))
# Generate a histogram. The x parameter indicates the data distribution of each bin, corresponding
to the x axis. The bins parameter indicates the number of bars in total. The normed parameter
indicates whether to perform normalization. By default, the sum of the percentages of all bars is 1.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 27


HCIA-AI V1.0 AI Mathematics Experiment Guide

plt.hist(binom_sim, bins=10, normed=True)


plt.xlabel(('x'))
plt.ylabel('density')
plt.show()

Output:
Data: [2 4 3 ... 3 4 1]# 10,000 numbers that comply with the binomial distribution.
Mean: 2.9821
SD: 1.43478

The following figure shows the binomial distribution.

Figure 4-1

4.2.7 Poisson Distribution Implementation


A random variable X that complies with the Poisson distribution indicates the number
of occurrences of an event within a fixed time interval with the λ parameter. The λ
parameter indicates the occurrence probability of an event. Both the mean value and
variance of the random variable X are λ.
Input:
import numpy as np
import matplotlib.pyplot as plt

# Generate 10,000 numbers that comply with the Poisson distribution where the value of lambda is
2.
X= np.random.poisson(lam=2, size=10000)

a = plt.hist(X, bins=15, normed=True, range=[0, 15])

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 28


HCIA-AI V1.0 AI Mathematics Experiment Guide

# Generate grids.
plt.grid()
plt.show()

The following figure shows the Poisson distribution.

Figure 4-2

4.2.8 Normal Distribution


Normal distribution is a continuous probability distribution. Its function can obtain a
value anywhere on the curve. Normal distribution is described by two parameters: μ
and σ, which indicate the mean value and standard deviation, respectively.
Input:
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt

mu = 0
sigma = 1
# Distribution sampling points.
x = np.arange(-5, 5, 0.1)
# Generate normal distribution that complies with mu and sigma.
y = norm.pdf(x, mu, sigma)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('density')
plt.show()

The following figure shows the distribution.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 29


HCIA-AI V1.0 AI Mathematics Experiment Guide

Figure 4-3

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 30


HCIA-AI V1.0 AI Mathematics Experiment Guide

5 Optimization Experiment

5.1 Implementation of the Least Squares Method


5.1.1 Algorithm
The least squares method, as the basis of the classification regression algorithm, has
a long history. It seeks the best function that matches data by minimizing the error
sum of squares. The least squares method can easily obtain unknown parameters,
and minimize the sum of the squared errors between the predicted data and the
actual data.

5.1.2 Case Introduction


Assume that we need to predict the price of a stock based on historical data.
According to past experience, the price of a stock fluctuates in a sinusoidal manner
with time. Therefore, we want to use a polynomial y = 𝜃𝑛 𝑥 𝑛 + 𝜃𝑛−1 𝑥 𝑛−1 + … +
𝜃1 𝑥 0 + 𝜃0 𝑥 0 to represent the relationship, where y indicates the stock price, x
indicates the time, and the 𝜃𝑖 (𝑖 = 0, … … , 𝑛) parameter is unknown. Once the 𝜃𝑖 (𝑖 =
0, … … , 𝑛) parameter is determined, we can estimate the stock price of any time point
according to the formula. How can we obtain a better 𝜃𝑖 (𝑖 = 0, … … , 𝑛) parameter
based on the historical data to make the expression comply with the actual condition
as much as possible? The most direct method is to use the time in historical data in
the polynomial to predict the stock price 𝑦̂. If predicted price 𝑦̂ is close to the actual
price y, the polynomial can almost reflect the actual condition. How do we express
the difference between the stock price obtained by the polynomial and the actual
stock price? One common way is to use the square of the difference between the two
1
prices, that is, using the function 2 (𝑦̂ − 𝑦)2. However, we have a large amount of
historical data. A good polynomial should be optimal in the overall data, not just at a
1 1
specific data point. We can use 𝑚 ∑𝑚 ̂𝑖 − 𝑦𝑖 )2 to represent the overall quality,
𝑖=1 2 (𝑦
where m indicates the number of historical data points. Then the problem is simplified
into an optimization problem, that is, to obtain 𝜃𝑖 (𝑖 = 0, … … , 𝑛) that can minimize the
1 1
value of 𝑚 ∑𝑚 ̂𝑖 − 𝑦𝑖 )2. The following first creates sinusoidal data with noise,
𝑖=1 2 (𝑦
which is regarded as the historical data. Then the least squares method is used to
1 1
obtain 𝜃𝑖 (𝑖 = 0, … … , 𝑛) that minimizes the value of 𝑚 ∑𝑚 ̂𝑖 − 𝑦𝑖 )2 based on the
𝑖=1 2 (𝑦
historical data.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 31


HCIA-AI V1.0 AI Mathematics Experiment Guide

5.1.3 Code Implementation


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

n = 9 # Degree of the polynomial.

#Define a target function:


def real_func(x):
# Target function: sin(2*pi*x)
return np.sin(2 * np.pi * x)
#Define a polynomial function and use the polynomial to fit data:
def fit_func(p, x):
f = np.poly1d(p)
return f(x)

#Define a residual function. The value of the function is the difference between the
polynomial fitting result and the actual value.
def residuals_func(p, y, x):
ret = fit_func(p, x) - y
return ret

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


x_points = np.linspace(0, 1, 1000) # Continuous points required for drawing a graph.
y0 = real_func(x) # Target function.
y1 = [np.random.normal(0, 0.1) + y for y in y0] # Function obtained after the noise that complies
with normal distribution is added to the target function.
p_init = np.random.randn(n) # Randomly initialize the polynomial parameters.
# Invoke the leastsq function of scipy.optimize to minimize the sum of the squared errors to find the
optimal matching function.
# The func function is a residual function, and x0 is the initial parameter value. Package parameters
except the initialization parameter in the residual function into args.
plsq = leastsq(func=residuals_func, x0=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()

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 32


HCIA-AI V1.0 AI Mathematics Experiment Guide

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]

Figure 5-1 Visualized chart

5.2 Gradient Descent Implementation


5.2.1 Algorithm
Gradient descent is a first-order iterative optimization algorithm for finding the
minimum of a function. The operation of each step is to solve the gradient vectors of
the target function. The gradient direction negative to the current position is used as
the search direction (as the target function descends the most quickly in this
direction, the gradient descent method is also called the steepest descent method).
The gradient descent method has the following characteristics: If the function is
closer to the target value, the step is smaller, and the descent speed is slower.

5.2.2 Case Introduction


Currently, the real estate industry is popular. How do we predict the price of a house?
According to experience, the house price y is determined by three key factors:
house area x0, floor area ratio x1, and afforested area x2. The relationship between
the house price and key factors is linear. That is, the relationship between the house
price and key factors can be described through the following formula: 𝑦 = 𝜃0 * x0 +
𝜃1 * x1 + 𝜃2 * x2, where 𝜃0 , 𝜃1 , and 𝜃2 are unknown. Once the three values are

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 33


HCIA-AI V1.0 AI Mathematics Experiment Guide

determined, we can estimate the price of any house according to the formula. In
order to obtain 𝜃0 , 𝜃1 , and 𝜃2 , we have found a copy of the city's house prices and
data about the house areas, floor area ratios, and afforested areas. How do we use
the data to help us obtain 𝜃0 , 𝜃1 , and 𝜃2 ? To evaluate the quality of 𝜃0 , 𝜃1 , and 𝜃2 ,
we can compare the value of 𝑦𝑝𝑟𝑒 (obtained through the formula 𝜃0 *s + 𝜃1 *r + 𝜃2 *d
based on most of the actual data) with that of the real house price y. If the values are
similar, the 𝜃0 , 𝜃1 , and 𝜃2 are high-quality data. Similar to the case in 5.1.2 Case
Introduction where we have obtained a large amount of known data, a good
polynomial should be optimal in the overall data rather than just at some data points.
1 1 2
Therefore, ∑𝑚 𝑖=1 (𝑦𝑖 − 𝑦(𝑝𝑟𝑒,𝑖) ) is used to show the overall quality, and m
𝑚 2
indicates the number of known data points. Then the problem is simplified into an
optimization problem, that is, to obtain 𝜃0 , 𝜃1 , and 𝜃2 that can minimize the value of
1 𝑚 1 2
∑ (𝑦𝑖 − 𝑦(𝑝𝑟𝑒,𝑖) ) . The following is an example. We have five copies of the
𝑚 𝑖=1 2
actual data.

House Price House Area Floor Area Ratio Afforested Area


95.364 1 0 3
97.217205 1 1 3
75.195834 1 2 3
60.105519 1 3 2
49.342380 1 4 4

The following describes how to use the gradient descent algorithm to obtain the
1 1 2
optimal 𝜃0 , 𝜃1 , and 𝜃2 to minimize the value of 𝑚 ∑𝑚
𝑖=1 2 (𝑦𝑖 − 𝑦(𝑝𝑟𝑒,𝑖) ) .

5.2.3 Code Implementation


Input:
#There are five samples in the training set (x,y), and each sample point has three
components (x0,x1,x2)
x = [(1, 0., 3), (1, 1., 3), (1, 2., 3), (1, 3., 2), (1, 4., 4)]
y = [95.364, 97.217205, 75.195834, 60.105519, 49.342380] # Output corresponding to the y[i]
sample point.
epsilon = 0.0001 # Iteration threshold. (When the difference between the two iteration loss
functions is less than the threshold, the iteration stops.)
alpha = 0.01 # Learning rate.
diff = [0, 0]
max_itor = 1000
error1 = 0
error0 = 0
cnt = 0
m = len(x)
# Initialize parameters.

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 34


HCIA-AI V1.0 AI Mathematics Experiment Guide

theta0 = 0
theta1 = 0
theta2 = 0
while True:
cnt += 1

# Parameter iteration calculation.


for i in range(m):
# The fitting function is as follows: y = theta0 * x[0] + theta1 * x[1] +theta2 * x[2]
# Calculate the residual value (Value of the fitting function – Actual value).
diff[0] = (theta0* x[i][0] + theta1 * x[i][1] + theta2 * x[i][2]) - y[i]
# Gradient = diff[0] * x[i][j]. Update parameters based on the value (Step x Gradient).
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
for lp in range(len(x)):
error1 += (y[lp]-(theta0* x[lp][0] + theta1 * x[lp][1] + theta2 * x[lp][2]))**2/2
# If the difference between two iteration loss functions is less than the threshold, the iteration
stops and the loop ends.
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 )

As parameters are updated continuously based on the gradient descent algorithm,


the error becomes smaller and smaller until the loop ends. The result is as follows:
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

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 35


HCIA-AI V1.0 AI Mathematics Experiment Guide

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

2020-04-15 Copyright © Huawei Technologies Co., Ltd. 36


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 Contents

Contents

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.2 Experimental Process ............................................................................................................................ 1
1.2.1 Setting Encoding Declarations ............................................................................................................. 1
1.2.2 Importing a Module ............................................................................................................................. 1
1.2.3 Defining Variables ............................................................................................................................... 1
1.2.4 Creating a Session .............................................................................................................................. 2
1.2.5 Closing the Session............................................................................................................................. 2
1.2.6 Experimental Results .......................................................................................................................... 2
1.3 Instance Description............................................................................................................................... 2

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

3 Matrix Multiplication ....................................................................................................... 5


3.1 Introduction to the Experiment ................................................................................................................ 5
3.1.1 About the Experiment .......................................................................................................................... 5
3.1.2 Objectives of the Experiment ............................................................................................................... 5
3.1.3 Experiment Content............................................................................................................................. 5
3.2 Experimental Process ............................................................................................................................ 5
3.2.1 Setting Encoding Declarations ............................................................................................................. 5

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


TensorFlow Programming Basics
Experiment Guide Contents

3.2.2 Importing a Module ............................................................................................................................. 5


3.2.3 Starting a TensorFlow Default Session................................................................................................. 5
3.2.4 Creating Matrix Variables .................................................................................................................... 5
3.2.5 Defining a Constant Matrix .................................................................................................................. 6
3.2.6 Initializing Global Variables .................................................................................................................. 6
3.2.7 Matrix Multiplication............................................................................................................................. 6
3.2.8 Experimental Results .......................................................................................................................... 6
3.3 Instance Description............................................................................................................................... 7

4 Definition of Variables .................................................................................................... 8


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

5 Visualization of TensorBoard....................................................................................... 10
5.1 Introduction to the Experiment ...............................................................................................................10
5.1.1 About the Experiment .........................................................................................................................10
5.1.2 Objectives of the Experiment ..............................................................................................................10
5.1.3 Experiment Content............................................................................................................................10
5.2 Experimental Process ...........................................................................................................................10
5.2.1 Setting Encoding Declarations ............................................................................................................10
5.2.2 Importing a Module ............................................................................................................................10
5.2.3 Generating Analog Data ..................................................................................................................... 11
5.2.4 Resetting the Computation Graph....................................................................................................... 11
5.2.5 Creating a Model ................................................................................................................................ 11
5.2.6 Creating a Forward Structure.............................................................................................................. 11
5.2.7 Reverse Optimization ......................................................................................................................... 11
5.2.8 Initializing Variables ............................................................................................................................12
5.2.9 Starting a Session ..............................................................................................................................12
5.2.10 Writing Data to the Model ....................................................................................... 错误!未定义书签。
5.2.11 Graphical Display ................................................................................................... 错误!未定义书签。
5.2.12 Experimental Results........................................................................................................................13
5.3 Instance Description.................................................................................................... 错误!未定义书签。

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


TensorFlow Programming Basics
Experiment Guide Contents

6 Graphic Operation ........................................................................................................ 15


6.1 Introduction to the Experiment ...............................................................................................................15
6.1.1 About the Experiment .........................................................................................................................15
6.1.2 Objectives of the Experiment ..............................................................................................................15
6.1.3 Experiment Content............................................................................................................................15
6.2 Experimental Process ...........................................................................................................................15
6.2.1 Creating a Graph................................................................................................................................15
6.2.2 Getting the Tensor ..............................................................................................................................16
6.2.3 Getting an Operation ..........................................................................................................................16
6.2.4 Getting All Lists ..................................................................................................................................16
6.2.5 Getting an Object ...............................................................................................................................17
6.2.6 Experimental Results .........................................................................................................................17
6.3 Instance Description..............................................................................................................................18

7 Linear Regression: Housing Price Forecast............................................................... 19


7.1 Introduction to the Experiment ...............................................................................................................19
7.1.1 About the Experiment .........................................................................................................................19
7.1.2 Objectives of the Experiment ..............................................................................................................19
7.1.3 Experiment Content............................................................................................................................19
7.2 Experimental Process ...........................................................................................................................19
7.2.1 Setting Encoding Declarations ............................................................................................................19
7.2.2 Importing a Module ............................................................................................................................19
7.2.3 Importing Data ...................................................................................................................................20
7.2.4 Experimental Results .........................................................................................................................20
7.2.5 Defining Parameters...........................................................................................................................20
7.2.6 Defining Placeholders ........................................................................................................................20
7.2.7 Creating a Structure of Forward Propagation ......................................................................................21
7.2.8 Initializing Variables ............................................................................................................................21
7.2.9 Enabling Circulation ...........................................................................................................................21
7.2.10 Displaying Training Results ..............................................................................................................21
7.2.11 Experimental Results ........................................................................................................................22
7.3 Instance Description..............................................................................................................................22

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


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

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


TensorFlow Programming Basics
Experiment Guide 1 Hello, TensorFlow!

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.

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. 2


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

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


TensorFlow Programming Basics
Experiment Guide 2 Session

print ("Add: %i" % sess.run(a+b)) #Calculate and output the sum.


print ("Multiply: %i" % sess.run(a*b)) #Calculate and output the product.

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. 4


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

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


TensorFlow Programming Basics
Experiment Guide 3 Matrix Multiplication

#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]])

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:

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


TensorFlow Programming Basics
Experiment Guide 3 Matrix Multiplication

[[7.3619137]]

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


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.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")

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


TensorFlow Programming Basics
Experiment Guide 4 Definition of Variables

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

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. 9


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.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":[] }

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


TensorFlow Programming Basics
Experiment Guide 5 Visualization of TensorBoard

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.
#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))

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


TensorFlow Programming Basics
Experiment Guide 5 Visualization of TensorBoard

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.
summary_writer = tf.summary.FileWriter('log/mnist_with_summaries',sess.graph)
#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})

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


TensorFlow Programming Basics
Experiment Guide 5 Visualization of TensorBoard

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}))
#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')
#Test on model results:
print("x=0.2, z=", sess.run(z, feed_dict={X: 0.2}))
plt.legend()
plt.show()

plotdata["avgloss"] = moving_average(plotdata["loss"])
plt.figure(1)
plt.subplot(211)
plt.plot(plotdata["batchsize"], plotdata["avgloss"], 'b--')
plt.xlabel('Minibatch number')
plt.ylabel('Loss')
plt.title('Minibatch run vs. Training loss')

plt.show()

5.2.10 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]

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


TensorFlow Programming Basics
Experiment Guide 5 Visualization of TensorBoard

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.105398916 W= [2.1561167] b= [-0.0306179]
cost: 0.105398916
x=0.2, z= [0.40060547]

Figure 5-1 Visualization of TensorBoard 1

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


TensorFlow Programming Basics
Experiment Guide 6 Graphic Operation

6 Graphic Operation

6.1 Introduction to the Experiment


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

6.1.2 Objectives of the Experiment


Understand graphic operations.

6.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.

6.2 Experimental Process


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

6.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)

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


TensorFlow Programming Basics
Experiment Guide 6 Graphic Operation

g2 = tf.get_default_graph()
print(g2)

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

6.2.2 Getting the Tensor


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

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


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)

6.2.4 Getting All Lists


# 4 Get all lists.

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


TensorFlow Programming Basics
Experiment Guide 6 Graphic Operation

#Return the list of operating nodes in the graph.


tt2 = g.get_operations()
print(tt2)

6.2.5 Getting an Object


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

6.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)
exampleop
name: "exampleop"
op: "MatMul"
input: "Const"
input: "Const_1"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "transpose_a"
value {
b: false
}

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


TensorFlow Programming Basics
Experiment Guide 6 Graphic Operation

}
attr {
key: "transpose_b"
value {
b: false
}
}

[[7.]]
Tensor("exampleop:0", shape=(1, 1), dtype=float32)
[<tf.Operation 'Const' type=Const>]
Tensor("Const:0", shape=(), dtype=float32)
________________________

6.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. 18


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

7 Linear Regression:
Housing Price Forecast

7.1 Introduction to the Experiment


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

7.1.2 Objectives of the Experiment


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

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

7.2 Experimental Process


7.2.1 Setting Encoding Declarations
# coding:utf-8

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

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


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

import numpy as np
import matplotlib.pyplot as plt

7.2.3 Importing Data


The data in this experiment comes from the bj_housing.csv file.
Read data:
# import data
train = pd.read_csv(r"bj_housing.csv")
train = train[train['Area'] < 300]
print(len(train))
print(train.head())

7.2.4 Experimental Results


9959
Area Value Room Living School Year Floor
0 128 370 3 1 1 2004 21
1 68 330 1 2 1 2000 6
2 125 355 3 2 0 2003 5
3 129 278 2 2 0 2005 16
4 118 340 3 2 0 2003 6

7.2.5 Defining Parameters


train_X = train['Area'].values.reshape(-1,1)
train_Y = train['Value'].values.reshape(-1,1)
n_samples = train_X.shape[0]
learning_rate = 2
# set learning rate
training_epochs = 100
# set display_step
display_step = 10

7.2.6 Defining Placeholders


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

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


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

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


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

7.2.7 Creating a Structure of Forward Propagation


#Create a structure of forward propagation.
#pred = W * X + b
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)

7.2.8 Initializing Variables


#Enable Init.
init = tf.global_variables_initializer()

7.2.9 Enabling Circulation


#Enable circulation to start training.
with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
sess.run(optimizer, feed_dict = {X: train_X, Y:train_Y})
if (epoch)%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))
print("Optimization Finished!")
print("--------------------------------------------------")
training_cost = sess.run(cost, feed_dict = {X: train_X, Y:train_Y})
w_final = sess.run(W)
b_final = sess.run(b)
print("Training cost = ", training_cost , "W=", w_final, "b=",b_final,'\n')

7.2.10 Displaying Training Results


import matplotlib.pyplot as plt

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


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

# show the plot


plt.plot(train_X, train_Y, 'ro', label="Original data")
plt.plot(train_X, w_final * train_X + b_final, label="Fitted line")
plt.legend()
plt.show()

7.2.11 Experimental Results

...

7.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. 22


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

2 Data Preparation for Image Recognition ...................................................................... 4


2.1 Obtaining Data ....................................................................................................................................... 4
2.1.1 Entering the Development Environment............................................................................................... 4
2.1.2 Downloading Data ............................................................................................................................... 4
2.2 Viewing Data.......................................................................................................................................... 5
2.2.1 Account Function................................................................................................................................. 5
2.3 Summary ............................................................................................................................................... 6

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


3.1 Image Recognition Implementation ........................................................................................................ 7
3.1.1 Experiment Roadmap.......................................................................................................................... 7
3.1.2 Module Importing ................................................................................................................................ 7
3.1.3 Data Reading and Standardization ...................................................................................................... 8
3.1.4 Data Pre-processing............................................................................................................................ 9
3.1.5 Placeholder Defining ..........................................................................................................................10
3.1.6 CNN Construction ..............................................................................................................................10
3.1.7 Loss Function, Optimizer, and Verification Indicator Defining ..............................................................12
3.1.8 Model Training and Verification...........................................................................................................13
3.1.9 Model Saving and Session Closing.....................................................................................................14
3.2 Sample Image Test ...............................................................................................................................15
3.2.1 Invoking the Model for Testing ............................................................................................................15

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


Image Recognition Programming

Experimental GuideImage Recognition Programming

Experimental Guide

3.3 Summary ..............................................................................................................................................18

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.

1.1.3 Application Framework List


Python
Numpy
Scikit-image
Tensorflow

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

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://computer-version-dataset.obs.cn-north-
4.myhuaweicloud.com/flower_photos.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

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. 3


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 Open the notebook and open a terminal.

----End

2.1.2 Downloading Data


Step 1 Enter the “work”.
Run “ls” to view the files.

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

Run “cd work” to enter the “work”.

Step 2 Download the data set from the OBS.


Run wget https://hciaai.obs.cn-north-
4.myhuaweicloud.com:443/flower_photos.zip to download the data set.

----End

2.2 Viewing Data


2.2.1 Account Function
Step 1 View the downloaded package.
Run “ls” to check the package.

Step 2 Decompress the package.


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

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

Run “ls” to check the decompressed files.

Step 3 View the data set.


Run cd flower_photos and ls to check the downloaded files.

Run cd daisy and ls 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. 6


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 Image Recognition Implementation


3.1.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.

3.1.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.

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


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

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.
Input:
# -*- coding:uft-8
from skimage import io,transform
import glob
import os
import tensorflow as tf
import numpy as np
import time

3.1.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.
Input:
# the directory of data
path = './flower_photos/'
#path = 'D:/AI/HCIA/Abroad/Lab/Data/flower_photos/'
# the saved modle directory
model_path = './model/'
# the name of modle
model_name = 'CNN_model'
# tensorboard dir
tb_dir = './tbdir'

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.
Input:
# scales the size of the picture:100*100
w = 100
h = 100

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


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

c = 3 # channels

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.
Input:
#read images
def read_img(path):
cate=[path+x for x in os.listdir(path) if os.path.isdir(path+x)]
print(cate)
imgs=[]
labels=[]
for idx,folder in enumerate(cate):
for im in glob.glob(folder+'/*.jpg'):
print('reading the images:%s'%(im))
img=io.imread(im)
img=transform.resize(img,(w,h))
imgs.append(img)
labels.append(idx)
return np.asarray(imgs,np.float32),np.asarray(labels,np.int32)
data,label=read_img(path)

----End

3.1.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.
Input:
num_example=data.shape[0]
arr=np.arange(num_example)
np.random.shuffle(arr)
data=data[arr]

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


Image Recognition Programming

Experimental GuideImage Recognition Programming


3 Implementation of Image Recognition Based on the
Experimental Guide TensorFlow

label=label[arr]

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.
Input:
ratio=0.8
s=np.int(num_example*ratio)
x_train=data[:s]
y_train=label[:s]
x_val=data[s:]
y_val=label[s:]
print(len(x_train))
print(len(x_val))

----End

3.1.5 Placeholder Defining


Create placeholders x and y to perform model calculation for the CNN model to
transmit tensor data.
Input:
#placeholder
x=tf.placeholder(tf.float32,shape=[None,w,h,c],name='x')
y_=tf.placeholder(tf.int32,shape=[None,],name='y_')

3.1.6 CNN Construction


Step 1 Create a CNN which has 11 layers.
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).
The structure of this CNN model is convolutional layer--pooling layer-- convolutional
layer--pooling layer-- convolutional layer--pooling layer-- convolutional layer--pooling
layer—fully connected layer-- fully connected layer-- fully connected layer.
Input:

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

Step 2 Define regular terms.


This document mainly uses L2 regular terms to prevent over-fitting and improve
model generalization capabilities.
Input:
regularizer = tf.contrib.layers.l2_regularizer(0.0001)
Step 3 View the model.
Check the structure of the final model result to prepare for the subsequent model
test.
Input:
logits = inference(x,False,regularizer)
logits_eval = tf.nn.softmax(logits, name='logits_eval')
----End

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 4 Save calculation graph.


Input:
sess=tf.Session()
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter(tb_dir, sess.graph)

3.1.7 Loss Function, Optimizer, and Verification


Indicator Defining
Step 1 Define the loss function.
Calculate the sparse cross entropy between the prediction tag logits and original tag
y_.
Input:
#loss function
loss=tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y_)
loss_reg = tf.add_n(tf.get_collection('losses'))
loss = loss + loss_reg

Step 2 Define the optimizer.


Use the adam optimizer. Define the training operation and minimize the loss
Input:
#optimizer
train_op=tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

Step 3 Define the accuracy of verification indicators.


Define the accuracy function for model effect verification.
Input:
writer = tf.summary.FileWriter( tb_dir, sess.graph)
#the correct number
correct_prediction = tf.equal(tf.cast(tf.argmax(logits,1),tf.int32), y_)
#accurate
acc= tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
----End

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.1.8 Model Training and Verification


Step 1 Define the function for extracting data in batches.
Input:
#get a batch data
def minibatches(inputs=None, targets=None, batch_size=None, shuffle=False):
assert len(inputs) == len(targets)
if shuffle:
indices = np.arange(len(inputs))
np.random.shuffle(indices)
for start_idx in range(0, len(inputs) - batch_size + 1, batch_size):
if shuffle:
excerpt = indices[start_idx:start_idx + batch_size]
else:
excerpt = slice(start_idx, start_idx + batch_size)
yield inputs[excerpt], targets[excerpt]

Step 2 Prepare training parameters and perform model initialization.


Input:
n_epoch=5
batch_size=32
display_step = 10
saver=tf.train.Saver()
sess=tf.Session()
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter(tb_dir, sess.graph)

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.
Input:
for epoch in range(n_epoch):

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

start_time = time.time()
#training
train_loss, train_acc, n_batch = 0, 0, 0
for x_train_a, y_train_a in minibatches(x_train, y_train, batch_size,
shuffle=True):
_,err,ac=sess.run([train_op,loss,acc], feed_dict={x: x_train_a, y_: y_train_a})
train_loss += err; train_acc += ac; n_batch += 1
if n_batch % display_step == 1:
print(" batch acc: %f batch loss: %f" %
(np.sum(ac),np.sum(err)))
print(" train loss: %f" % (np.sum(train_loss)/ n_batch))
print(" train acc: %f" % (np.sum(train_acc)/ n_batch))

#validation
val_loss, val_acc, n_batch = 0, 0, 0
for x_val_a, y_val_a in minibatches(x_val, y_val, batch_size, shuffle=False):
err, ac = sess.run([loss,acc], feed_dict={x: x_val_a, y_: y_val_a})
val_loss += err; val_acc += ac; n_batch += 1
print(" validation loss: %f" % (np.sum(val_loss)/ n_batch))
print(" validation acc: %f" % (np.sum(val_acc)/ n_batch))
print('-------------------------------------------------------')

----End

3.1.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.
Input:
saver.save(sess, model_path+model_name, global_step=epoch)

Step 2 Close the session.


After sess running ends, close the session.
Input:

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

sess.close()
model_file=tf.train.latest_checkpoint(model_path)
print(model_file)

3.2 Sample Image Test


3.2.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.
Input:
from skimage import io,transform
import tensorflow as tf
import numpy as np
# the directory of data
path = './flower_photos/'
# the saved modle directory
model_path = './model/'
# the name of modle
model_name = 'CNN_model-4'
#test images
path1 = "daisy/5547758_eea9edfd54_n.jpg"
path2 = "roses/394990940_7af082cf8d_n.jpg"
path3 = "dandelion/7355522_b66e5d3078_m.jpg"
path4 = "tulips/10791227_7168491604.jpg"
path5 = "sunflowers/6953297_8576bf4ea3.jpg"

path1 = path+path1
path2 = path+path2
path3 = path+path3
path4 = path+path4
path5 = path+path5

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 2 Define the image standardization function.


Standardize the test images and convert the images into standard RGB images
(100x100x3).
Input:
flower_dict = {0:'dasiy',1:'dandelion',2:'roses',3:'sunflowers',4:'tulips'}
w=100
h=100
c=3
#read test image and resize the images
def read_one_image(path):
img = io.imread(path)
img = transform.resize(img,(w,h),mode='constant')
return np.asarray(img)
Step 3 Standard the test data.
Invoke the read_one_image function to standardize the test image.
Input:
# prepare the data
data = []
data1 = read_one_image(path1)
data2 = read_one_image(path2)
data3 = read_one_image(path3)
data4 = read_one_image(path4)
data5 = read_one_image(path5)
data.append(data1)
data.append(data2)
data.append(data3)
data.append(data4)
data.append(data5)
Step 4 Print the test result.
Print the prediction results of five test data records.
Input:
# start the session
with tf.Session() as sess:

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

#latrest model path


model_file=tf.train.latest_checkpoint(model_path)
# import computive graph
saver = tf.train.import_meta_graph(model_file+'.meta')
# restore weight ingformation
saver.restore(sess, model_file)
graph = tf.get_default_graph()
# calculate the input nodes in graph
x = graph.get_tensor_by_name("x:0")
# feed information
feed_dict = {x:data}
# calculate the output nodes in graph
logits = graph.get_tensor_by_name("logits_eval:0")
# run the session
classification_result = sess.run(logits,feed_dict)
#diplay 4 decimals
np.set_printoptions(precision=4, suppress=True)
#print the prediction
print(classification_result)
#print the maximum in the matrix
print(tf.argmax(classification_result,1).eval())
#Match category of flowers
output = []
output = tf.argmax(classification_result,1).eval()
name=["fisrt","second","third","fourth","fifth"]
for i in range(len(output)):
print("The prediction of %s flower is:" %(name[i])+flower_dict[output[i]])

----End

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

3.3 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. 18

You might also like