HCIA-AI V1.0 Lab Guide
HCIA-AI V1.0 Lab Guide
HCIA-AI V1.0 Lab Guide
HCIA-AI
Development Environment
Establishment
Experimental Guide
Version: 1.0
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.
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.
Website: http://e.huawei.com
Preface
Brief Introduction
This document is an HCIA-AI certification training course, intended to trainees who are
preparing for HCIA-AI tests or readers who want to know about AI basics. After
understanding this document, you can perform simple Python basic programming, laying a
solid foundation for later AI development.
Contents
This experimental guide includes five experiments, covering AI environment setup, and is
intended to help trainees and readers easily develop capabilities of developing AI.
Experiment 1 — Introduction to the AI experiment environment: Understand the software and
framework required for establishing an HCIA-AI environment.
Experiment 2 — How to apply for and activate Huawei cloud services: Understand Huawei
Cloud service platform, register with the platform, and get familiar with related operations.
Experiment 3 — Environment establishment: Install related software modules, such as Python,
pip, NumPy, Pandas, and TensorFlow.
Experiment 4 — Test cases: Understand how to compile scripts in real time on the Elastic
Cloud Server (ECS) or download existing scripts from GitHub for testing.
Experiment 5 — AI experiment environment configuration: Understand the installation and
deployment of Gnome and anaconda.
Experimental Environment
Python Development Tool
This experiment environment is developed and compiled based on the Python 3.6.
Contents
Preface......................................................................................................................................... iii
1 Introduction to HCIA-AI Experiment Environment ........................................................... 1
1.1 Introduction to the Experiment............................................................................................................................... 1
1.1.1 About the Experiment ......................................................................................................................................... 1
1.1.2 Objectives of the Experiment .............................................................................................................................. 1
1.1.3 Required Software and Frameworks .................................................................................................................... 1
1.2 Experimental Tasks................................................................................................................................................ 2
1.2.1 Experiment Guideline ......................................................................................................................................... 2
1.2.2 Experiment Procedure......................................................................................................................................... 2
1.3 Result Verification ................................................................................................................................................. 2
1.3.1 Viewing Information About the Huawei Cloud Environment ............................................................................... 2
1.3.2 Viewing Configuration Information About the Experiment Environment ............................................................. 3
1.4 Questions .............................................................................................................................................................. 3
5 Other Configurations............................................................................................................. 34
5.1 GUI-based GNOME Installation ...........................................................................................................................34
5.1.1 Introduction to GNOME ....................................................................................................................................34
5.1.2 GNOME Installation ..........................................................................................................................................34
5.2 anaconda Installation ............................................................................................................................................37
5.2.1 Introduction to anaconda ....................................................................................................................................37
5.2.2 anaconda Installation .........................................................................................................................................37
1.4 Questions
When you apply for an elastic cloud service, which server is suitable for calculating
traditional machine learning iterative tasks, and which server is suitable for deep learning
tasks?
When establishing the environment, how can you install the Python framework without using
pip?
After the experimental tasks are completed and the server is shut down, are the occupied
resources, including the storage, IP address, and bandwidth, continue to be charged?
Step 2 Click Register in the upper right corner on the home page.
Click Register in the upper right corner to register an account. The following page is
displayed.
2.3.2 Procedure
Step 1 Log in to Huawei Cloud official website.
Visit https://www.huaweicloud.com/en-us/ and choose Products > Computing > Elastic
Cloud Server. The following page is displayed.
Select I have read and agree to Huawei Image Disclaimer and click Submit.
You can click Start, Stop, Restart, Delete, Remote Login, or More to perform related
operations on the service you applied for. Click Remote Login to go to the application
environment.
2.4.2 Procedure
Step 1 Log in to Huawei Cloud official website.
Visit http://www.huaweicloud.com/. The following page is displayed.
Click Console in the upper right corner of the home page. The following page is displayed.
Choose Elastic Cloud Server. On the page that is displayed, select an elastic cloud server, as
shown in the following figure.
Click Delete, select "Release the EIPs bound to the following ECSs" and "Delete the data
disks attached to the following ECSs", and click OK to delete an elastic cloud server. The
operation page is as follows.
Operations such as start, stop, and restart on other servers are similar.
----End
Enter tar -xvJf Python-3.6.4.tar.xz to decompress the installation package. The result is as
follows.
Go to the folder where the decompressed file package is stored and check whether the
package is decompressed successfully.
Enter make install to compile the source code. The result is as follows.
----End
The installation might take a long time. You can enter nohup pip3 install numpy to install
the NumPy in the background. The result is as follows.
----End
When the Pandas is installed, its dependent framework packages, including python-dateutil
and pytz, are automatically installed as well.
The installation might take a long time. You can enter nohup pip3 install pandas to install
the Pandas in the background. The result is as follows.
----End
The installation might take a long time. You can enter the command nohup pip3 install
tensorflow to install the TensorFlow in the background. The result is as follows.
----End
4 Case Test
import tensorflow as tf
import numpy as np
sess.close()
-over-
----End
Enter cd HCIA-AI and ll to switch to the corresponding folder and view the files. The result
is as follows.
5 Other Configurations
Press Ctrl+Alt+F2 to switch to the DOS page. Enter init 5 in the command line to switch to
the GUI.
----End
Enter bash Anaconda2-5.1.0-Linux-x86_64.sh, enter yes as promoted, agree with the license
agreement, and press Enter. Select the default installation path, write environment variables,
and enter yes. The result is as follows.
----End
HCIA-AI
Python Programming Basics
Experimental Guide
Issue: 1.0
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.
Website: http://e.huawei.com
Based on cutting-edge technologies and professional training systems, Huawei certification meets the diverse AI
technology demands of various clients. Huawei is committed to providing practical and professional technical
certification for our clients.
HCIA-AI V1.0 certification is intended to popularize AI and help understand deep learning and Huawei Cloud EI,
and learn the basic capabilities of programming based on the TensorFlow framework, as a motive to promote
talent training in the AI industry.
Content of HCIA-AI V1.0 includes but is not limited to: AI overview, Python programming and experiments,
mathematics basics and experiments, TensorFlow introduction and experiments, deep learning pre-knowledge,
deep learning overview, Huawei cloud EI overview, and application experiments for image recognition, voice
recognition and man-machine dialogue.
HCIA-AI certification will prove that you systematically understand and grasp Python programming, essential
mathematics knowledge in AI, basic programming methods of machine learning and deep learning platform
TensorFlow, pre-knowledge and overview of deep learning, overview of Huawei cloud EI, basic programming for
image recognition, voice recognition, and man-machine dialogue. With this certification, you have required
knowledge and techniques for AI pre-sales basic support, AI after-sales technical support, AI products sales, AI
project management, and are qualified for positions such as natural language processing (NLP) engineers, image
processing engineers, voice processing engineers and machine learning algorithm engineers.
Enterprises with HCIA-AI-certified engineers have the basic understanding of AI technology, framework, and
programming, and capable of leveraging AI, machine learning, and deep learning technologies, as well as the
open-source TensorFlow framework to design and develop AI products and solutions like machine learning, image
recognition, voice recognition, and man-machine dialogue.
Huawei certification will help you open the industry window and the door to changes, standing in the forefront of
the AI world!
HCIA-AI Huawei Certification Course 2
Preface
Brief Introduction
This document is an HCIA-AI certification training course, intended to trainees who are
preparing for HCIA-AI tests or readers who want to know about AI basics. After
understanding this document, you can perform simple Python basic programming, laying a
solid foundation for later AI development.
Contents
This experimental guide includes nine experiments, covering Python programming basics,
and is intended to help trainees and readers easily develop capabilities of developing AI.
Experiment 1: Understand definition and operations of lists and tuples of Python.
Experiment 2: Understand definition and operations of Python strings.
Experiment 3: Understand definition and operations of Python dictionaries.
Experiment 4: Understand definition and operations of conditional statements and
looping statements of Python.
Experiment 5: Understand definition and operations of Python functions.
Experiment 6: Understand definition and operations object-oriented programming of
Python.
Experiment 7: Understand date and time operations of Python.
Experiment 8: Understand definition and operations of regular expressions of Python.
Experiment 9: Understand definition and operations of Python file manipulation.
Experimental Environment
This experimental environment is compiled on Python 3.6.
HCIA-AI Huawei Certification Course 3
Contents
2 Strings ......................................................................................................................................... 9
2.1 Introduction to the Experiment............................................................................................................................... 9
2.1.1 About the Experiment ......................................................................................................................................... 9
2.1.2 Objectives of the Experiment .............................................................................................................................. 9
2.2 Experimental Tasks................................................................................................................................................ 9
2.2.1 Concepts ............................................................................................................................................................ 9
2.2.2 Experimental Operations..................................................................................................................................... 9
3 Dictionaries ............................................................................................................................... 11
3.1 Introduction to the Experiment.............................................................................................................................. 11
3.1.1 About the Experiment ........................................................................................................................................ 11
3.1.2 Objectives of the Experiment ............................................................................................................................. 11
3.2 Experimental Tasks............................................................................................................................................... 11
3.2.1 Concepts ........................................................................................................................................................... 11
3.2.2 Experimental Operations.................................................................................................................................... 11
5 Functions .................................................................................................................................. 15
5.1 Introduction to the Experiment..............................................................................................................................15
HCIA-AI Huawei Certification Course 4
>>>x.append(y)
>>>print(x)
[1, 2, 3, [4, 5]]
The argument of "extend" must be an iterated object, which means that all elements of this
object are added to the end of list one by one.
>>>x = [1, 2, 3]
>>>y = [4, 5]
>>>x.extend(y)
>>>print(x)
[1, 2, 3, 4, 5]
# equal to
>>>for i in y:
>>>x.append(i)
>>>print(x)
[{'age': 10, 'name': 'Bart'}, {'age': 20, 'name': 'cater'}, {'age': 39, 'name': 'Homer'}]
6. Remove elements from a list.
The "remove" method removes an element, and it removes only the element that appears for
the first time.
>>> a = [0, 2, 2, 3]
>>> a.remove(2)
>>> a
[0, 2, 3]
# ValueError will be returned if the removed element is not within the list.
>>> a.remove(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list·
Note: In t=(1), t is not a tuple type, as the parentheses () can represent a tuple, or
mathematical formula. Python stipulates that in this case, () is a mathematical formula, and a
tuple with only one element must have a comma to eliminate ambiguity.
2. Define a changeable tuple.
>>> cn=('yi','er','san')
>>> en=('one','two','three')
>>> num=(1,2,3)
>>> tmp=[cn,en,num,[1.1,2.2],'language']
>>>print(tmp)
[('yi', 'er', 'san'), ('one', 'two', 'three'), (1, 2, 3), [1.1, 2.2], 'language']
>>>print(tmp[0])
('yi', 'er', 'san')
HCIA-AI Huawei Certification Course 8
>>>print(tmp[0][0])
yi
>>>print(tmp[0][0][0])
y
HCIA-AI Huawei Certification Course 9
2 Strings
3 Dictionaries
When you use dic[key] to look up a key value in a dictionary, it will return an error if there is
no such key value. However, if you use dic.get(key, default) to look up a key value, it will
return default if there is no such key value.
>>>print(employee.get("Nacy","UnKnown'"))
hr
>>>print(employee.get("Nac","UnKnown"))
UnKnown
HCIA-AI Huawei Certification Course 13
The following statements are executed if the judging condition is true (non-zero). There can
be multiple lines of execution statements, which can be indented to indicate the same range.
The "else" statement is optional, and can be executed when the condition is false.
2. Looping statement
There are a lot of changes in looping statements. Common statements include the "for"
statement and the "while" statement.
In "for" looping, the "for" statement should be followed by a colon. "for" looping is
performed in a way similar to iterating. In "while" looping, there is a judgment on condition
and then looping, like in other languages.
HCIA-AI Huawei Certification Course 14
5 Functions
'1.23'
>>> str(100)
'100'
>>> bool(1)
True
>>> bool('')
False
2. Function name
A function name is a reference to a function object, and it can be assigned to a variable, which
is equivalent to giving the function an alias.
>>> a = abs # Variable a points to function abs
>>> a(-1) # Therefore, the "abs" can be called by using "a"
1
3. Define functions
In Python, you can use the "def" statement to define a function, listing function names,
brackets, arguments in brackets and colons successively. Then you can edit a function in an
indentation block and use the "return" statement to return values.
We make an example by defining the "my_abs" function to get an absolute value.
>>> def my_abs(x):
if x>=0:
return x
else:
return –x
You can use the "pass" statement to define a void function, which can be used as a placeholder.
Change the definition of "my_abs" to check argument types, that is, to allow only arguments
of integers and floating numbers. You can check data types with the built-in function
isinstance().
def my_abs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x >= 0:
return x
else:
return –x
4. Keyword arguments
Changeable arguments allow you input zero or any number of arguments, and these
changeable arguments will be assembled into a tuple for function calling. While keyword
arguments allow you to input zero or any number of arguments, and these keyword arguments
will be assembled into a dictionary in functions.
def person(name, age, **kw):
print('name:', name, 'age:', age, 'other:', kw)
Function "person" receives keyword argument "kw" except essential arguments "name" and
"age". You can only input essential arguments when calling this function.
>>> person('Michael', 30)
name: Michael age: 30 other: {}
Similar to changeable arguments, you can assemble a dictionary and convert it into keyword
arguments as inputs.
>>> extra = {'city': 'Beijing', 'job': 'Engineer'}
>>> person('Jack', 24, city=extra['city'], job=extra['job'])
name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}
**extra means transferring all key-values in this extra dictionary as key arguments into the
**kw argument of the function. kw will get a dictionary, which is a copy of the extra
dictionary. Changes on kw will not impact the extra dictionary outside the function.
5. Name keyword arguments
If you want to restrict names of keyword arguments, you can name keyword arguments. For
example, you can accept only "city" and "job" as keyword arguments. A function defined in
this way is as follows:
def person(name, age, *, city, job):
print(name, age, city, job)
Different from keyword argument "**kw", a special separator "*" is required to name a
keyword argument. Arguments after "*" are regarded as naming keyword arguments, which
are called as follows:
>>> person('Jack', 24, city='Beijing', job='Engineer')
Jack 24 Beijing Engineer
The special separator "*" is not required in the keyword argument after a changeable
argument in a function.
def person(name, age, *args, city, job):
print(name, age, args, city, job)
You need to input an argument name to name a keyword argument, which is different from
the position argument. An error will be returned during calling if no argument name is
introduced. In this case, the keyword argument can be default, as one way to simplify calling.
def person(name, age, *, city='Beijing', job):
print(name, age, city, job)
Because the keyword argument "city" has a default value, you do not need to input a
parameter of "city" for calling.
>>> person('Jack', 24, job='Engineer')
Jack 24 Beijing Engineer
When you name a keyword argument, "*" must be added as a special separator if there are no
changeable arguments. Python interpreter cannot identify position arguments and keyword
arguments if there is no "*".
HCIA-AI Huawei Certification Course 18
6. Argument combination
To define a function in Python, you can use required arguments, default arguments,
changeable arguments, keyword arguments and named keyword arguments. These five types
of arguments can be combined with each other.
Note: Arguments must be defined in the order of required arguments, default arguments,
changeable arguments, named keyword arguments, and keyword arguments.
For example, to define a function that includes the above-mentioned arguments:
def f1(a, b, c=0, *args, **kw):
print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)
def f2(a, b, c=0, *, d, **kw):
print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)
Python interpreter will input the matched arguments according to argument positions and
names automatically when calling a function.
>>> f1(1, 2)
a = 1 b = 2 c = 0 args = () kw = {}
>>> f1(1, 2, c=3)
a = 1 b = 2 c = 3 args = () kw = {}
>>> f1(1, 2, 3, 'a', 'b')
a = 1 b = 2 c = 3 args = ('a', 'b') kw = {}
>>> f1(1, 2, 3, 'a', 'b', x=99)
a = 1 b = 2 c = 3 args = ('a', 'b') kw = {'x': 99}
>>> f2(1, 2, d=99, ext=None)
a = 1 b = 2 c = 0 d = 99 kw = {'ext': None}
The most amazing thing is that you can call the above-mentioned function through a tuple and
a dictionary.
>>> args = (1, 2, 3, 4)
>>> kw = {'d': 99, 'x': '#'}
>>> f1(*args, **kw)
a = 1 b = 2 c = 3 args = (4,) kw = {'d': 99, 'x': '#'}
>>> args = (1, 2, 3)
>>> kw = {'d': 88, 'x': '#'}
>>> f2(*args, **kw)
a = 1 b = 2 c = 3 d = 88 kw = {'x': '#'}
Therefore, you can call a function through the forms similar to "func(*args, **kw)", no matter
how its arguments are defined.
7. Recursive function
You need to prevent a stack overflow when you use a recursive function. Functions are called
through the stack which is a data structure in computers. The stack will add a layer of stack
frames when a function is called, while the stack will remove a layer of stack frames when a
function is returned. As the size of a stack is limited, it will lead to a stack overflow if there
are excessive numbers of recursive calling of functions.
Solution to a stack overflow: tail recursion optimization
You can use tail recursion optimization to solve a stack flow. As tail recursion enjoys the same
effects with looping, you can take looping as a special tail recursion, which means to call
itself when the function is returned and exclude expressions in the "return" statement. In this
way, the compiler or interpreter can optimize tail recursion, making recursion occupying only
HCIA-AI Huawei Certification Course 19
one stack frame, no matter how many times the function is called. This eliminates the
possibility of stack overflow.
For the fact(n) function, because a multiplication expression is introduced in return n * fact(n - 1),
it is not tail recursion. To change it into tail recursion, more code is needed to transfer the
product of each step into a recursive function.
def fact(n):
return fact_iter(n, 1)
def fact_iter(num, product):
if num == 1:
return product
return fact_iter(num - 1, num * product)
It can be learned that return fact_iter(num - 1, num * product) returns only the recursive function
itself. num – 1 and num * product will be calculated before the function is called, without any
impact on the function.
HCIA-AI Huawei Certification Course 20
6 Object-Oriented Programming
− Class variable: Class variables are publicly used in the total instantiation, and they
are defined within classes but beyond function bodies. Class variables are not used
as instance variables.
− Data member: Class variables or instance variables process data related to classes
and their instance objects.
− Method re-writing: If the methods inherited from parent classes do not meet the
requirements of sub-classes, the methods can be re-written. Re-writing a method is
also called overriding.
− Instance variable: Instance variables are defined in methods and are used only for
the classes of current instances.
− Inheritance: Inheritance means that a derived class inherits the fields and methods
from a base class, and it allows taking the objects of derived class as the objects of
base classes. For example, a dog-class object drives from an animal-class object.
This simulates a "(is-a)" relationship (in the figure, a dog is an animal).
− Instantiation: It refers to creating instances for a class or objects for a class.
− Methods: functions defined in classes.
− Objects: data structure objects defined through classes. Objects include two data
members (class variable and instance variable), and methods.
You can choose various formats as required, but the simplest function to get the readable time
mode is asctime():
>>>import time
>>>localtime = time.asctime( time.localtime(time.time()) )
>>>print("Local time :", localtime)
Output:
Local time: Thu Apr 7 10:05:21 2016
3. Format date
>>>import time
# Format into 2016-03-20 11:45:39
>>>print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# Format into Sat Mar 28 22:24:24 2016
>>>print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
# Turn format string into timestamp
>>>a = "Sat Mar 28 22:24:24 2016"
>>>print(time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")))
Output:
2016-04-07 10:25:09
Thu Apr 07 10:25:09 2016
1459175064.0
4. Get calendar of a month
The calendar module can process yearly calendars and monthly calendars using multiple
methods, for example, printing a monthly calendar.
>>>import calendar
>>>cal = calendar.month(2016, 1)
>>>print("output calendar of January 2016:”)
>>>print(cal)
Output:
The following is the calendar of April 2014
April 2014
Mo Tu We Th Fr Sa Su
1
2345678
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
HCIA-AI Huawei Certification Course 26
8 Regular Expressions
re.match tries to match a mode from the string start position. If no mode is matched from the
string start, match() returns none.
Function syntax:
re.match(pattern, string, flags=0)
Instance:
>>>import re
>>>print(re.match('www', 'www.runoob.com').span()) # Match at start
>>>print(re.match('com', 'www.runoob.com')) # Match not at start
Output:
(0, 3)
None
2. re.search method
re.search scans the entire string and returns the first successful match.
Function syntax:
re.search(pattern, string, flags=0)
Instance:
>>>import re
>>>line = "Cats are smarter than dogs"
>>>searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)
>>>if searchObj:
>>>print("searchObj.group() : ", searchObj.group())
>>>print("searchObj.group(1) : ", searchObj.group(1))
>>>print("searchObj.group(2) : ", searchObj.group(2))
>>>else:
>>>print("Nothing found!!")
>>>import re
>>>phone = "2004-959-559 # This is an oversea telephone number"
# Delete Python comments in strings
>>>num = re.sub(r'#.*$', "", phone)
>>>print("The telephone number is", num)
# Delete non-number (-) strings
>>>num = re.sub(r'\D', "", phone)
>>>print("The telephone number is ", num)
Result:
The telephone number is: 2004-959-559
The telephone number is: 2004959559
5. re.compile function
The compile function compiles regular expressions and creates a regular expression (pattern)
object, which will be used by the match() and search() functions.
Function syntax:
re.compile(pattern[, flags])
>>>import re
>>> pattern = re.compile(r'\d+') # Match at least one number
>>> m = pattern.match('one12twothree34four') # Search head, no match
>>>print(m)
None
>>> m = pattern.match('one12twothree34four', 2, 10) # Match from ‘e’, no match
>>>print(m)
None
>>> m = pattern.match('one12twothree34four', 3, 10) # Match from ‘1’, matched
>>>print(m) # Return a match object
<_sre.SRE_Match object at 0x10a42aac0>
>>> m.group(0) # Ignorable 0
'12'
>>> m.start(0) # Ignorable 0
3
>>> m.end(0) # Ignorable 0
5
>>> m.span(0) # Ignorable 0
(3, 5)
6. findall
findall finds all strings that match regular expressions and returns a list. If there is no match, it
returns an empty list.
HCIA-AI Huawei Certification Course 29
Note: match and search match once, while findall matches all.
Function syntax:
findall(string[, pos[, endpos]])
>>>import re
>>>pattern = re.compile(r'\d+') # Search numbers
>>>result1 = pattern.findall('runoob 123 google 456')
>>>result2 = pattern.findall('run88oob123google456', 0, 10)
>>>print(result1)
>>>print(result2)
Output
['123', '456']
['88', '12']
7. re.finditer
Similar to findall, re.finditer finds all strings that match regular expressions, and returns them
as an iterator.
re.finditer(pattern, string, flags=0)
>>>import re
>>>it = re.finditer(r"\d+","12a32bc43jf3")
>>>for match in it:
>>>print(match.group())
Output:
12
32
43
3
8. re.split
The split method splits matched strings and returns a list. For example:
re.split(pattern, string[, maxsplit=0, flags=0])
Instance:
>>>import re
>>> re.split('\W+', 'runoob, runoob, runoob.')
['runoob', 'runoob', 'runoob', '']
>>> re.split('(\W+)', ' runoob, runoob, runoob.')
['', ' ', 'runoob', ', ', 'runoob', ', ', 'runoob', '.', '']
>>> re.split('\W+', ' runoob, runoob, runoob.', 1)
['', 'runoob, runoob, runoob.']
>>> re.split('a*', 'hello world') # Split will not split unmatched strings
['hello world']
HCIA-AI Huawei Certification Course 30
9 File Manipulation
This reminds your input of any strings and displays the same strings on the screen. When I
enter "Hello Python!", it outputs:
Please input: Hello Python!
Your input is: Hello Python!
HCIA-AI Huawei Certification Course 31
input( ) function:
The input([prompt]) and raw_input([prompt]) functions are similar, but the former can receive
a Python expression as the input and return the result.
>>>str = input("Please input:")
>>>print("Your input is: ", str)
Output:
Please input:[x*5 for x in range(2,10,2)]
Your input is: [10, 20, 30, 40]
2. Open and close files
Python provides essential functions and methods to manipulate files by default. You can use
the file object to do most file manipulations.
Open() function: You should open a file using the Python build-in open() function, and create
a file object, so that the related methods can call it to write and read.
# Open a file
>>>fo = open("foo.txt", "w")
>>>print("File name: “, fo.name)
>>>print("closed or not: ", fo.closed)
>>>print("access mode:”, fo.mode)
>>>print("space required at head and tail, or not: ", fo.softspace)
Output:
File name: foo.txt
Close or not: False
Access mode: w
Space required at head and tail, or not: 0
Close() function: For the file object, it refreshes any buffered data that has not been written
and closes the file. Then, the file cannot be written.
When a reference to a file object is re-assigned to another file, Python will close the previous
file. It is ideal to close a file using the close() function.
# Open a file
>>>fo = open("foo.txt", "w")
>>>print("File name: “, fo.name)
# Close the opened file
>>>fo.close()
Output:
File name: foo.txt
3. Write a file
write() function: It writes any string into an opened file. Note that Python strings can be
binary data, not just texts. This function will not add a line feed ('\n') at string ends.
# Open a file
>>>fo = open("foo.txt", "w")
>>>fo.write( "www.baidu.com!\nVery good site!\n")
# Close an opened file
>>>fo.close()
HCIA-AI Huawei Certification Course 32
The function above creates a foo.txt file, writes the received content into this file, and closes
the file. If you open this file, you will see:
www.baidu.com!
Very good site!
4. Read a file
Read() function: It reads strings from an opened file. Note that Python strings can be binary
data, not just texts.
# Open a file
>>>fo = open("foo.txt", "r+")
>>>str = fo.read(10)
>>>print("The read string is: ", str)
# Close an opened file
>>>fo.close()
Output:
The read string is: www.runoob.
5. Rename a file
The os module of Python provides a method to execute file processing operations, like
renaming and deleting files. To use this module, you have to import it first and then call
various functions.
rename(): It requires two arguments: current file name and new file name.
Function syntax:
os.rename(current_file_name, new_file_name)
>>>import os
# Rename file test1.txt to test2.txt
>>>os.rename( "test1.txt", "test2.txt" )
6. Delete a file
You can use the remove() method to delete a file, using the name of file to be deleted as an
argument.
Function syntax:
os.remove(file_name)
>>>import os
# Delete the existing file test2.txt
>>>os.remove("test2.txt")
HCIA-AI Huawei Certification Course
HCIA-AI
Math Basics
Experimental Guide
Version: 1.0
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.
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.
Contents
1.1 Introduction
1.1.1 Contents
Basic mathematics knowledge, including algorithm design and numerical processing
knowledge, is widely applied in the Artificial Intelligence (AI) domain, especially in the field
of traditional machine learning and deep learning. The main purpose of this section is to
implement some common mathematical knowledge based on the Python language and basic
mathematics modules to provide basic support for AI learning.
1.1.2 Framework
This document mainly uses the math library, numpy library, and scipy library. The math
library, a standard library of Python, provides some common mathematical functions; the
numpy library, a numerical calculation and expansion library of Python, is mainly used to
handle issues such as linear algebra, random number generation, and Fourier Transform; the
scipy library is used to deal with issues such as statistics, optimization, interpolation, and
bonus points.
1.2 Implementation
Import corresponding modules in advance.
>>> import math
>>> import numpy as np
>>> 5
# Code:
>>> math.ceil(4.99)
# Output:
>>> 5
>>> math.fsum([1,2,3,4])
# Output:
>>>10
1.2.13 pi Implementation
The pi parameter is a numeric constant, indicating the circular constant.
# Code:
>>> math.pi
# Output:
>>> 3.141592653589793
>>> math.pow(3,4)
# Output:
>>> 81.0
2.1 Introduction
2.1.1 Linear Algebra
Linear algebra is a mathematical branch widely used in various engineering technical
disciplines. Its concepts and conclusions can greatly simplify the derivation and expression of
AI formulas. Linear algebra can simplify complex problems so that we can perform efficient
mathematical operations.
In the context of deep learning, linear algebra is a mathematical tool that provides a technique
that helps us to operate arrays at the same time. Data structures like vectors and matrices can
store numbers and rules for operations such as addition, subtraction, multiplication, and
division.
[ 8, 9, 10, 11]],
[[ 4, 5, 6, 7],
[16, 17, 18, 19]],
[[ 8, 9, 10, 11],
[20, 21, 22, 23]]])
# Output:
>>> array([[ 3, 4, 5],
[ 9, 14, 19],
[15, 24, 33]])
2.2.10 Determinant
Obtain the determinant of a matrix.
# Code:
>>> E = array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> F = array([[-1, 0, 1],
[ 2, 3, 4],
[ 5, 6, 7]])
# Output:
>>> np.linalg.det(E)
>>> 6.6613381477509402e-16
# Output:
>>> np.linalg.det(F)
>>> 2.664535259100367e-15
1.52344501e-15, 2.17780259e-16])
>>> VT
# Output:
>>> matrix([[ -5.81200877e-01, -5.81200877e-01, -5.67421508e-01,
-3.49564973e-02, -3.49564973e-02],
[ 4.61260083e-03, 4.61260083e-03, -9.61674228e-02,
7.03814349e-01, 7.03814349e-01],
[ -4.02721076e-01, -4.02721076e-01, 8.17792552e-01,
5.85098794e-02, 5.85098794e-02],
[ -7.06575299e-01, 7.06575299e-01, -2.22044605e-16,
2.74107087e-02, -2.74107087e-02],
[ 2.74107087e-02, -2.74107087e-02, 2.18575158e-16,
7.06575299e-01, -7.06575299e-01]])
3.1 Introduction
3.1.1 Probability Theory
Probability theory is a mathematical branch of studying the quantitative regularity of random
phenomena. A random phenomenon is different from a decisive phenomenon in that a
decisive phenomenon inevitably occurs under certain conditions.
The probability theory is a mathematical tool used to describe uncertainty. A large number of
AI algorithms build models using the probabilistic or inference information of samples.
3.1.2 Experiment
This section describes the knowledge points of probability and statistics, and mainly uses the
numpy and scipy frameworks.
# Output:
>>> 1.920286436967152
# Code:
>>> np.std(ll)
# Output:
>>> 1.9790570145063195
import numpy as np
import matplotlib.pyplot as plt
x = np.random.poisson(lam=5, size=10000)
pillar = 15
The preceding figure can be displayed only in a visual system. Trainees can perform the test in the local
environment.
mu = 0
sigma = 1
x = np.arange(-5, 5, 0.1)
y = norm.pdf(x, mu, sigma)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('density')
plt.show()
# The distribution figure is as follows.
The preceding figure can be displayed only in a visual system. Trainees can perform the test in the local
environment.
a = 0.5
b = 0.5
x = np.arange(0.01, 1, 0.01)
y = beta.pdf(x, a, b)
plt.plot(x, y)
plt.title('Beta: a=%.1f,b=%.1f' % (a, b))
plt.xlabel('x')
plt.ylabel('density')
plt.show()
# The distribution figure is as follows.
The preceding figure can be displayed only in a visual system. Trainees can perform the test in the local
environment.
lam = 0.5
x = np.arange(0, 15, 0.1)
y = expon.pdf(x, lam)
plt.plot(x, y)
plt.title('Exponential: lam=%.2f' % lam)
plt.xlabel('x')
plt.ylabel('density')
plt.show()
# The distribution figure is as follows.
The preceding figure can be displayed only in a visual system. Trainees can perform the test in the local
environment.
4 Other Experiments
n = 9 # Degree of polynomial
# Target function
def real_func(x):
return np.sin(2 * np.pi * x)
# Polynomial function
def fit_func(p, x):
f = np.poly1d(p)
return f(x)
# Residual function
def residuals_func(p, y, x):
ret = fit_func(p, x) - y
return ret
The preceding figure can be displayed only in a visual system. Trainees can perform the test in the local
environment.
# Parameter initialization
theta0 = 0
theta1 = 0
theta2 = 0
while True:
cnt += 1
for lp in range(len(x)):
error1 += (y[lp]-(theta0 + theta1 * x[lp][1] + theta2 * x[lp][2]))**2/2
print(' theta0: %f, theta1: %f, theta2: %f, error1: %f' % (theta0, theta1, theta2, error1) )
print('Done: theta0 : %f, theta1 : %f, theta2 : %f' % (theta0, theta1, theta2) )
print ('Number of iterations: %d' % cnt )
# Output:
theta0 : 2.782632, theta1 : 3.207850, theta2 : 7.998823, error1 : 5997.941160
theta0 : 4.254302, theta1 : 3.809652, theta2 : 11.972218, error1 : 3688.116951
theta0 : 5.154766, theta1 : 3.351648, theta2 : 14.188535, error1 : 2889.123934
theta0 : 5.800348, theta1 : 2.489862, theta2 : 15.617995, error1 : 2490.307286
theta0 : 6.326710, theta1 : 1.500854, theta2 : 16.676947, error1 : 2228.380594
theta0 : 6.792409, theta1 : 0.499552, theta2 : 17.545335, error1 : 2028.776801
……
theta0 : 97.717864, theta1 : -13.224347, theta2 : 1.342491, error1 : 58.732358
theta0 : 97.718558, theta1 : -13.224339, theta2 : 1.342271, error1 : 58.732258
theta0 : 97.719251, theta1 : -13.224330, theta2 : 1.342051, error1 : 58.732157
Done: theta0 : 97.719942, theta1 : -13.224322, theta2 : 1.341832
Number of iterations: 2608
HCIA-AI
TensorFlow Programming Basics
Experiment Guide
Version: 1.0
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.
Email: [email protected]
Preface
Brief Introduction
This document is an HCIA-AI certification training course, intended to trainees who are
preparing for HCIA-AI tests or readers who want to know about AI basics and TensorFlow
basic programming.
Content Description
This experiment guide includes nine experiments, introducing basic equipment operation and
configuration, TensorFlow's helloworld, sessions, matrix multiplication, TensorFlow
virtualization, and housing price prediction.
Experiment 1: "Hello, TensorFlow".
Experiment 2: Understand functions of sessions through a session experiment using the
with session function.
Experiment 3: Understand matrix multiplication by multiplying two matrices with ranks
of tensors greater than 2.
Experiment 4: Understand the definition of variables. Define variables with Variable and
get_variable respectively and observe the difference between these two methods.
Experiment 5: Understand the visualization of TensorBoard. TensorBoard aggregates all
kinds of data into a log file. You can enable TensorBoard service to read the log file and
enable the 6060 port to provide web services so that users can view data via a browser.
Experiment 6: Understand data reading and processing by reading .csv files and
displaying them based on given conditions.
Experiment 7: Understand graphic operations. Create a graph in three ways and set it as
the default graph. Use the get_default_graph() function to access the default graph and
verify its settings.
Experiment 8: Understand save and use of models. After importing data, analyze data
characteristics and define variables based on the characteristics. Create a model and
define output nodes. Build the structure for forward propagation and then the structure
for backpropagation. Compile and train the model to get appropriate parameters. After
training data and testing the model, create a saver and a path to save parameters in the
session automatically. When the model is saved, you can access the model for use.
Experiment 9: A comprehensive experiment of forecasting housing price through the
instantiation of linear regression. Use the dataset of housing prices in Beijing and skills
in the prior eight experiments to forecast the housing price.
Experimental Environment
Networking
This experimental environment is compiled for AI engineers who are preparing for HCIA-AI
tests. Each set of experimental environment includes a remote elastic cloud server (ECS) with
two cores, 4-GB memory, and 64-bit CentOS 7.4.
Equipment
To meet the HCIA-AI experiment needs, adopt the following configuration for each set of
experimental environment:
The following table shows the device names and types and software versions.
Contents
Preface......................................................................................................................................... iii
1 Hello, TensorFlow! ................................................................................................................... 1
1.1 Introduction to the Experiment............................................................................................................................... 1
1.1.1 About the Experiment ......................................................................................................................................... 1
1.1.2 Objectives of the Experiment .............................................................................................................................. 1
1.1.3 Experiment Content ............................................................................................................................................ 1
1.1.4 Experimental Operations..................................................................................................................................... 1
1.2 Experimental Process ............................................................................................................................................ 2
1.2.1 Setting Encoding Declarations ............................................................................................................................ 2
1.2.2 Importing a Module ............................................................................................................................................ 2
1.2.3 Defining Variables .............................................................................................................................................. 2
1.2.4 Creating a Session .............................................................................................................................................. 2
1.2.5 Closing the Session ............................................................................................................................................. 2
1.2.6 Experimental Results .......................................................................................................................................... 3
1.3 Instance Description .............................................................................................................................................. 3
2 Session ....................................................................................................................................... 4
2.1 Introduction to the Experiment............................................................................................................................... 4
2.1.1 About the Experiment ......................................................................................................................................... 4
2.1.2 Objectives of the Experiment .............................................................................................................................. 4
2.1.3 Experiment Content ............................................................................................................................................ 4
2.1.4 Experimental Operations..................................................................................................................................... 4
2.2 Experimental Process ............................................................................................................................................ 5
2.2.1 Setting Encoding Declarations ............................................................................................................................ 5
2.2.2 Importing a Module ............................................................................................................................................ 5
2.2.3 Defining Constants ............................................................................................................................................. 5
2.2.4 Creating a Session .............................................................................................................................................. 5
2.2.5 Experimental Results .......................................................................................................................................... 6
2.3 Instance Description .............................................................................................................................................. 6
5 Visualization of TensorBoard............................................................................................... 13
5.1 Introduction to the Experiment..............................................................................................................................13
5.1.1 About the Experiment ........................................................................................................................................13
5.1.2 Objectives of the Experiment .............................................................................................................................13
5.1.3 Experiment Content ...........................................................................................................................................13
5.1.4 Experimental Operations....................................................................................................................................13
5.2 Experimental Process ...........................................................................................................................................14
5.2.1 Setting Encoding Declarations ...........................................................................................................................14
5.2.2 Importing a Module ...........................................................................................................................................14
5.2.3 Generating Analog Data .....................................................................................................................................14
5.2.4 Resetting the Computation Graph.......................................................................................................................14
5.2.5 Creating a Model ...............................................................................................................................................14
5.2.6 Creating a Forward Structure .............................................................................................................................15
5.2.7 Reverse Optimization ........................................................................................................................................15
5.2.8 Initializing Variables ..........................................................................................................................................15
1 Hello, TensorFlow!
Step 3 Select an ECS. Applicable operations of the ECS are displayed. Select Remote Login to log
in to the ECS.
Relational
database (0)
2 Session
Step 3 Select an ECS. Applicable operations of the ECS are displayed. Select Remote Login to log
in to the ECS.
3 Matrix Multiplication
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))
4 Definition of Variables
with tf.variable_scope("test2"):
var6 = tf.get_variable("varname",shape=[2],dtype=tf.float32)
5 Visualization of TensorBoard
tf.reset_default_graph()
#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")
#Set parameters.
training_epochs = 20
display_step = 2
summary_writer = tf.summary.FileWriter('log/mnist_with_summaries',sess.graph)
#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.
plotdata["avgloss"] = moving_average(plotdata["loss"])
plt.figure(1)
plt.subplot(211)
plt.show()
#Run the coord.request_stop () command to terminate all threads. Run the coord.join
(threads) to add threads to the main thread. Wait for the termination of threads.
7 Graphic Operation
g = tf.Graph()
with g.as_default():
c1 = tf.constant(0.0)
print(c1.graph)
print(g)
print(c.graph)
g2 = tf.get_default_graph()
print(g2)
tf.reset_default_graph()
g3 = tf.get_default_graph()
print(g3)
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)
exampleop
name: "exampleop"
op: "MatMul"
input: "Const"
input: "Const_1"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "transpose_a"
value {
b: false
}
}
attr {
key: "transpose_b"
value {
b: false
}
}
2018-05-11 15:43:16.483655: I
T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU
suppo
rts instructions that this TensorFlow binary was not compiled to use: AVX2
[[7.]]
Tensor("exampleop:0", shape=(1, 1), dtype=float32)
[<tf.Operation 'Const' type=Const>]
Tensor("Const:0", shape=(), dtype=float32)
________________________
import tensorflow as tf
import os
#Define parameters.
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
init = tf.global_variables_initializer()
#Define a session.
sess = tf.Session()
print("Training finished!")
import tensorflow as tf
#Define parameters.
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
9 Linear Regression:
Housing Price Forecast
print("Optimization Finished!")
training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')
HCIA-AI
Image Recognition Programming
Experiment Guide
Version: 1.0
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.
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.
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.
Contents
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.
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.
----End
Run mkdir HCIA-AI and ll to create a file folder. Check the result.
----End
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.
----End
----End
----End
----End
Calculate the sparse cross entropy between the prediction tag logits and original tag y_.
----End
Step 3 Define the model operation on the training set and verification set, and print the results.
Train a total of five epoches. Each epoch needs to run on the training set and verification set.
Print the corresponding loss value and accuracy of two data sets as well as the duration of
each epoch operation.
----End
----End
3.4 Summary
This chapter describes how to construct a CNN, define the model, set related parameters, and
test sample data based on the TensorFlow.
In the file directory, flower_photos is the data set and image.py is the image recognition
script.
Step 2 Open vim and create the Python script.
vim image_recog.py
cat image.py
----End
4.2 Summary
This chapter describes how to recognize images based on data sets and test the model effect.
As shown in the figure, the accuracy is high.
HCIA-AI
Speech Recognition Programming
Experiment Guide
Version: 1.0
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.
and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd.
All other trademarks or trade names mentioned in this document are the property of their respective holders.
Notice
The purchased products, services, and features are stipulated by the contract made between Huawei and the
customer. All or part of the products, services and features described in this document may not be within the
purchase scope or the usage scope. Unless otherwise specified in the contract, all statements, information, and
recommendations in this document are provided "AS IS" without warranties, guarantees or representations of any
kind, either express or implied.
The information in this document is subject to change without notice. Every effort has been made in the preparation
of this document to ensure accuracy of the contents, but all statements, information, and recommendations in this
document do not constitute a warranty of any kind, express or implied.
Website: http://e.huawei.com
Huawei Confidential
Copyright © Huawei Technologies Co., Ltd.
HCIA-AI Experiment Guide Page 2
Based on cutting-edge technologies and professional training systems, Huawei certification meets
the diverse AI technology demands of various clients. Huawei is committed to providing practical
and professional technical certification for our clients.
HCIA-AI V1.0 certification is intended to popularize AI and help understand deep learning and
Huawei Cloud EI, and learn the basic capabilities of programming based on the TensorFlow
framework, as a motive to promote talent training in the AI industry.
Content of HCIA-AI V1.0 includes but is not limited to: AI overview, Python programming and
experiments, mathematics basics and experiments, TensorFlow introduction and experiments, deep
learning pre-knowledge, deep learning overview, Huawei cloud EI overview, and application
experiments for image recognition, speech recognition and man-machine dialogue.
HCIA-AI certification will prove that you systematically understand and grasp Python
programming, essential mathematics knowledge in AI, basic programming methods of machine
learning and deep learning platform TensorFlow, pre-knowledge and overview of deep learning,
overview of Huawei cloud EI, basic programming for image recognition, speech recognition, and
man-machine dialogue. With this certification, you have required knowledge and techniques for AI
pre-sales basic support, AI after-sales technical support, AI products sales, AI project management,
and are qualified for positions such as natural language processing (NLP) engineers, image
processing engineers, voice processing engineers and machine learning algorithm engineers.
Enterprises with HCIA-AI-certified engineers have the basic understanding of AI technology,
framework, and programming, and are capable of leveraging AI, machine learning, and deep
learning technologies, as well as the open-source TensorFlow framework to design and develop AI
products and solutions like machine learning, image recognition, speech recognition, and
man-machine dialogue.
Huawei certification will help you open the industry window and the door to changes, standing in
the forefront of the AI world!
HCIA-AI Experiment Guide Page 3
Preface
Basic Introduction
This document is an HCIA-AI certification training course, and is intended to trainees who are
preparing for HCIA-AI tests or readers who want to know about AI basics. After understanding this
document, you can perform basic AI speech recognition programming.
Contents
This experiment guide includes one experiment, covering speech recognition, and is intended to help
trainees and readers complete the speech recognition experiment on their own.
Chapter 1 introduces the speech recognition experiment.
Chapter 2 describes preparations for the speech recognition model and relevant functions.
Chapter 3 describes code implementation of speech recognition and model training, and presents
training results.
Experimental Environment
Python Development Tool
This experimental environment is compiled based on Python 3.6.
HCIA-AI Experiment Guide Page 4
Contents
Preface ............................................................................................................................ 3
Basic Introduction .................................................................................................................................................3
Contents ...............................................................................................................................................................3
Knowledge Background of Readers ........................................................................................................................3
Experimental Environment .....................................................................................................................................3
1 Speech Recognition Experiment...................................................................................... 6
1.1 Introduction to the Experiment ..........................................................................................................................6
1.1.1 About the Experiment ....................................................................................................................................6
1.1.2 Objectives of the Experiment .........................................................................................................................6
1.1.3 Experiment Roadmap ....................................................................................................................................6
1.2 Experiment Content .........................................................................................................................................7
1.2.1 Dataset .........................................................................................................................................................7
1.2.2 Algorithm .....................................................................................................................................................7
2 Preparations for the Speech Recognition Model ............................................................... 1
2.1 Model Preparations ..........................................................................................................................................1
2.1.1 Introduction ..................................................................................................................................................1
2.1.2 Objective ......................................................................................................................................................1
2.1.3 Main Content ................................................................................................................................................1
2.2 Function Building ............................................................................................................................................2
2.2.1 Installing a Framework ..................................................................................................................................2
2.2.2 Creating a Script ...........................................................................................................................................2
2.2.3 Importing Modules ........................................................................................................................................5
2.2.4 Reading Data ................................................................................................................................................5
2.2.5 Converting Voice into MFCC .........................................................................................................................6
2.2.6 Converting MFCC into Matrix .......................................................................................................................7
2.2.7 Alignment Function .......................................................................................................................................8
2.2.8 Converting Text into Vector ...........................................................................................................................9
2.2.9 Converting Dense Matrix into Sparse Matrix ...................................................................................................9
2.2.10 Converting Vector into Text ........................................................................................................................10
2.2.11 BiRNN Model ...........................................................................................................................................10
2.2.12 Calling the CPU ........................................................................................................................................12
2.2.13 Saving and Viewing a Script .......................................................................................................................13
HCIA-AI Experiment Guide Page 5
execution. Therefore, more quality and abundant features can be extracted, and man-made
interference can be minimized, which yields better results.
Though data has been optimized during preprocessing, a great number of preparations need to be
made. The preparation work is difficult, and therefore, you are required to learn and understand the
development process and solution roadmap in this stage.
1.2.2 Algorithm
1. BiRNN is short for bidirectional recurrent neural network. On a typical recurrent neural network,
status transmission is backward and unidirectional. In some cases, current output is in relationship
with the previous status and the next status. Therefore, a BiRNN is needed to resolve this problem.
For example, predicting a lacking word in a statement requires the previous content as well as
subsequent content. In this case, BiRNN works.
2. BiRNN is composed of two RNNs overlaid. At the time point t, input is provided for these two
opposite RNNs, and output is determined based on the status of the two unidirectional RNNs, as
shown in the following figure.
HCIA-AI Experiment Guide Page 1
2.1.2 Objective
Develop the modular function to facilitate optimization and adjustment.
Reduce workload to improve focus.
Standardize codes.
Decrease reading difficulty.
mkdir –p HCIA-AI/voice
Specific operation is as follows:
# 139 slices
# Normally distribute the data, subtract an average value, and then divide the obtained result by a variance
The orig_inputs represents the converted MFCC codes, and the trans_inputs represents data after time
sequence expansion, with the for loop supplementing 0. The last two lines are used for data
standardization.
The returned results mainly include voice matrix audio, voice matrix length audio_len, text vector
transcript, and text vector length transcript_len.
The returned results include text labels or vectors corresponding to the data.
The ndarray_to_text_ch function converts text vectors in the dense matrix into text. The code is as
follows:
# Convert the data into two-dimensional data and transmit it to the first layer.
# Convert two-dimensional [n_steps*batch_size, n_character] into three-dimensional time-major [n_steps, batch_size, n_character].
The data shape change process of the entire model is complex, and you need to convert the input into
two-dimensional Tensor before it can be transmitted to the full-connection layers. When data is
transmitted into the BiRNN through the full-connection layers, the data needs to be converted into
three-dimensional Tensor. Since the BiRNN outputs two-dimensional data, the data needs to be
converted back to two-dimensional Tensor before entering the full-connection layers later. The final
output can be three-dimensional Tensor if needed.
So far, the BiRNN model is built.
2.3 Conclusion
This chapter mainly introduces how to build functions for speech recognition, facilitating subsequent
model parameter calling. Main function modules are used for overall training iteration and test of
speech recognition, and encapsulation function modules are used for assisting the main functions in
completing training tasks.
HCIA-AI Experiment Guide Page 15
The datasets contain two data files data_thchs30 and data_thchs30_10. The data_thchs30 file contains
complete THCHS-30 datasets, and the data_thchs30_10 file contains selected datasets to adapt to the
limited sources used in this experiment. The latter file will be used in model training later.
Run the cd data_thchs30_10 command.
Specific operation is as follows:
HCIA-AI Experiment Guide Page 18
The train folder contains 10 .wav voice files, and the train.word.txt file includes text labels
corresponding to the voice data.
Press Enter to enter the edit interface, as shown in the following figure:
HCIA-AI Experiment Guide Page 20
The returned results include the file name of the first voice data in the original training data, text
corresponding to the first voice data, and size of the training dataset.
Build a word table. The datasets used in this experiment contain 10 corpuses, only 212 Chinese
characters. Build a word table based on the characters so that the characters can be vectorized later.
The returned result is the size of the word table. The code is as follows:
# Word table
The following code shows how to convert text vectors in the sparse matrix into text. The returned
result is "He swayed up and down the lane with the strength of his waist only, like a dolphin led by the
advantage of one head." The code is as follows:
# The batch_size and max_stepsize cannot be changed once they are adjusted.
# Voice log filter bank or MFCC features
# Text
# Sequence length
# Generate a saver.
After training:
Nine corpuses, 212 Chinese characters are iterated for 50 epochs, consuming 153.74 minutes (about
2.5 hours). The value of the iteration times is 19 powers of 10. The error rate of the model is 26.5%.
The following figure shows the result of another version. Fifty corpuses consume 823.65 minutes
(about 13.7 hours), with the error rate being 14.5%. For the model result, see the text in red boxes.
Original indicates the original corpus, and Decoded indicates the corpus trained based on the model.
The two corpuses highly match.
HCIA-AI Experiment Guide Page 30
3.5 Conclusion
This experiment tests the speech recognition system under the TensorFlow framework, and is intended
to train the speech recognition model based on the THCHS-30 dataset and BiRNN model, and assess
the model accuracy.
HCIA-AI Huawei Certification Course
HCIA-AI
Man-Machine Dialogue Programming
Experiment Guide
Version: 1.0
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.
and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd.
All other trademarks or trade names mentioned in this document are the property of their respective holders.
Notice
The purchased products, services, and features are stipulated by the contract made between Huawei and the
customer. All or part of the products, services, and features described in this document may not be within the
purchase scope or the usage scope. Unless otherwise specified in the contract, all statements, information, and
recommendations in this document are provided "AS IS" without warranties, guarantees or representations of any
kind, either express or implied.
The information in this document is subject to change without notice. Every effort has been made in the preparation
of this document to ensure accuracy of the content, but all statements, information, and recommendations in this
document do not constitute a warranty of any kind, express or implied.
Website: http://e.huawei.com
华为专有和保密信息
版权所有 © 华为技术有限公司
Introduction to Huawei Certification System
Based on cutting-edge technologies and professional training systems, Huawei certification meets
the diverse AI technology demands of various clients. Huawei is committed to providing practical
and professional technical certification for our clients.
HCIA-AI V1.0 certification is intended to popularize AI and help understand deep learning and
Huawei Cloud EI, and learn the basic capabilities of programming based on the TensorFlow
framework, as a motive to promote talent training in the AI industry.
Content of HCIA-AI V1.0 includes but is not limited to: AI overview, Python programming and
experiments, mathematics basics and experiments, TensorFlow introduction and experiments, deep
learning pre-knowledge, deep learning overview, Huawei cloud EI overview, and application
experiments for image recognition, speech recognition and man-machine dialogue.
HCIA-AI certification will prove that you systematically understand and grasp Python
programming, essential mathematics knowledge in AI, basic programming methods of machine
learning and deep learning platform TensorFlow, pre-knowledge and overview of deep learning,
overview of Huawei cloud EI, basic programming for image recognition, speech recognition, and
man-machine dialogue. With this certification, you have required knowledge and techniques for AI
pre-sales basic support, AI after-sales technical support, AI products sales, AI project management,
and are qualified for positions such as natural language processing (NLP) engineers, image
processing engineers, speech processing engineers and machine learning algorithm engineers.
Enterprises with HCIA-AI-certified engineers have the basic understanding of AI technology,
framework, and programming, and are capable of leveraging AI, machine learning, and deep
learning technologies, as well as the open-source TensorFlow framework to design and develop AI
products and solutions like machine learning, image recognition, speech recognition, and
man-machine dialogue.
Huawei certification will help you open the industry window and the door to changes, standing in
the forefront of the AI world!
华为专有和保密信息
版权所有 © 华为技术有限公司
Preface
Basic Introduction
This document is an HCIA-AI certification training course, and is intended to trainees who are
preparing for HCIA-AI tests or readers who want to know about AI basics. After understanding this
document, you can perform basic AI man-machine dialogue programming.
Contents
This experiment guide includes one experiment, covering man-machine dialogue, and is intended to
help trainees complete the man-machine dialogue experiment on their own.
Chapter 1 introduces the man-machine dialogue experiment.
Chapter 2 describes how to preprocess dialogue corpus
Chapter 3 describes how to build the Seq2Seq model.
Chapter 4 describes how to train the model.
Chapter 5 describes how to test the model.
Experimental Environment
Python Development Tool
This experimental environment is compiled based on Python 3.6.
华为专有和保密信息
版权所有 © 华为技术有限公司
HCIA-AI Experiment Guide Page 1
Contents
Preface ............................................................................................................................ 3
Basic Introduction .................................................................................................................................................3
Contents ...............................................................................................................................................................3
Knowledge Background of Readers ........................................................................................................................3
Experimental Environment .....................................................................................................................................3
1 Man-machine Dialogue Experiment ................................................................................ 3
1.1 Introduction to the Experiment ..........................................................................................................................3
1.1.1 About the Experiment ....................................................................................................................................3
1.1.2 Objectives of the Experiment .........................................................................................................................3
1.1.3 Experiment Roadmap ....................................................................................................................................3
1.1.4 Dataset .........................................................................................................................................................4
1.1.5 Algorithm .....................................................................................................................................................5
2 Dialogue Corpus Preprocessing ...................................................................................... 6
2.1 Introduction .....................................................................................................................................................6
2.1.1 Preparation ...................................................................................................................................................6
2.1.2 Main Functions .............................................................................................................................................6
2.2 Corpus Preparation ..........................................................................................................................................6
2.2.1 Installing a Framework ..................................................................................................................................6
2.2.2 Downloading the Corpus ...............................................................................................................................7
2.2.3 Creating a Script to Preprocess the Corpus ......................................................................................................9
2.3 Preprocessing ................................................................................................................................................10
2.3.1 Importing Modules ......................................................................................................................................10
2.3.2 Defining the basic_tokenizer Function ..........................................................................................................11
2.3.3 Defining the fenci Function ..........................................................................................................................11
2.3.4 Defining the getRawFileList Function ...........................................................................................................11
2.3.5 Defining the get_ch_lable Function ..............................................................................................................12
2.3.6 Defining the get_ch_lable Function ..............................................................................................................12
2.3.7 Defining the build_dataset Function ..............................................................................................................13
2.3.8 Defining the create_vocabulary Function ......................................................................................................13
2.3.9 Defining the create_seq2seqfile Function ......................................................................................................14
2.3.10 Defining the splitFileOneline Function ........................................................................................................15
2.3.11 Defining the initialize_vocabulary Function .................................................................................................15
HCIA-AI Experiment Guide Page 2
1.1.4 Dataset
The dialogue corpus used in this experiment is compiled by engineers, containing two .txt files,
totaling 12 sentences and six dialogue groups. The following figure shows the two files.
Nam
Type Size
e
.txt file
.txt file
one.txt
Format
File (F) Edit (E) View (V) Help (H)
(O)
Hi
Hi
Have you had your dinner?
Yes
What did you eat?
Salmon
Any else? Could you give me some?
No. I have not had enough.
two.txt
Help
File (F) Edit (E) Format (O) View (V)
(H)
How old are you?
Guess
Younger than 20
Bingo! I am 18.
1.1.5 Algorithm
A task which is processed in a sequence and mapped to another sequence is a Seq2Seq task. A
sequence can be a string. Common Seq2Seq tasks include machine translation, part-of-speech tagging,
speech recognition, and man-machine dialogue that this experiment mainly talks about
In this experiment, the LSTM and Encoder-Decoder algorithms are used.
Long Short-Term Memory (LSTM) is a recurrent neural network (RNN) processing and predicting
major events with long interval and delay in the time sequence. https://baike.baidu.com/item/time
sequence LSTM has been applied in the scientific and technological field. LSTM-based systems can
be used to process tasks such as machine translation, robot control, speech recognition, and chatting
robot.
Encoder-Decoder is the basic framework of Seq2Seq, and includes the encoder, decoder, and
intermediate status vector connecting them. By learning the input, the encoder encodes the input as a
status vector S with fixed size, and then transfers the S to the decoder. By learning the status vector S,
the decoder gives output.
Semantic
coding C
HCIA-AI Experiment Guide Page 6
2.1 Introduction
2.1.1 Preparation
Create a folder for storing project content.
Download datasets from the OBS service of Huawei cloud object storage service.
Create sub-folders in the project folder to meet different requirements.
Open the decompressed folder. The chatbot folder contains a data folder for storing data and a
myjiebadict.txt file for dividing Chinese characters. Open the data folder. The folder contains four
sub-folders and one .txt file. The four sub-folders store checkpoints, dialogue corpus, question IDS
files, and answer IDS files. The dictch.txt file is used for building a vocabulary.
2.3 Preprocessing
2.3.1 Importing Modules
Import relevant modules for project development.
The sys module is responsible for interaction between the program and Python interpreter, and
provides a series of functions and variables for controlling the operation environment of Python;
in this experiment, it is used for refreshing data.
The OS module is responsible for interaction between the program and OS, and provides an
interface for accessing the OS bottom layer; in this experiment, it is used for operating system
files, for example, returning a file list and multipath combination.
The matplotlib is a 2D graphics library of Python, and generates publishing-level graphics using
hard copy formats in cross-platform interactive environment, and supports different types of 2D
graphics to be generated using a small number of codes.
The matplotlib.pyplot is a function collection with command style, and looks like MATLAB.
The NumPy is an open-source value calculation package of Python; in this experiment, it is used
for processing matrices, for example, creating a matrix, reshaping its structure, and calculating
convergence.
The tensorFlow.python.platform.gfile provides file object APIs like Python for operating files.
The random module mainly generates random data; in this experiment, it is used to disorder the
original data sequence.
The re module is a regular expression module of Python; in this experiment, it is used for
replacement and division.
The collections module provides many useful collection classes; in this experiment, the counter
class of the collections module is used to track the occurrence times of values.
HCIA-AI Experiment Guide Page 11
The jieba module is a Python-based Chinese character division tool; in this experiment, it is used
to import files and divide Chinese characters.
The code is as follows:
# View the two training data paths and names. In case of a large file
volume, shuffle the file sequence.
# Traverse each file in the folder.
# Call the get_ch_lable function to obtain specific content and content length of each file, and assign
the value of the file content to the training_data and the content length to the training_datasz.
# Convert the file content into the array training_ci.
# Convert the arrany training_ci into
# Add the processed training_ci to the labels list.
# The length of this group of content is equal to the length of the last group of
content plus the length of this group (initial content length is 0).
# Call the list.extend() function to add multiple values of another sequence to the end of the list.
# Output the iterative length of every line of content in each text_file.
# Output the content and content length of all files.
# Call the build_dataset function to create a dataset, input all elements, and set the maximum size of
the vocabulary. The sequence number, content, dictionary, and reverse dictionary of each element are
returned
Number of lines:
sentence_to_ids function to convert each line of sentence (decoded first) into an IDS, traverse and
serialize the IDS in each sentence, and then write the IDS with space into the ids_file.
The code is as follows:
Number of
lines:
Number
Number of
of Chinese
Chinese characters:
characters: 34
16
3.1 Introduction
3.1.1 Model Content
1. Due to TensorFlow upgrade, the Seq2Seq model of the original dialogue module has been changed
significantly. Therefore, the original module is not found from the new TensorFlow. In this experiment,
the original Seq2Seq codes in the earlier TensorFlow are copied here.
2. A class is defined here, including the Seq2Seq model which the attention mechanism and a plurality
of buckets are added into, the get_batch function which extracts data in batch, and the step function
which injects data.
3. The codes in this experiment are huge, so trainees are required to simply master the experiment
process and roadmap in this HCIA-AI stage.
3.2.2 Creating the Seq2SeqModel Class and Defining the Seq2Seq Model
Define three functions under the class: __init__, get_batch, and step.
The Seq2Seq model carries the attention mechanism and a plurality of buckets.
The class implements an encoder composed of a multilayer perceptron and a decoder having the
attention mechanism.
The Seq2Seq model carries the attention mechanism and a plurality of buckets.
The class implements an encoder composed of a multilayer perceptron and a decoder having the attention
mechanism.
# 0 by default
HCIA-AI Experiment Guide Page 20
# The sampled softmax is useful when the sample number is smaller than the vocabulary
size.
# Inject data.
# Select a batch of data at random and use the data as the input of the encoder and decoder.
# Pad the data if necessary, reverse the input sequence of the encoder, and add a "GO" to the decoder.
# Padding
# Reversal
# Add an extra "GO" to the input data of the decoder, and pad the data.
# In case of forward spreading (training), the gradient norm and loss that need update are output.
# In case of forward + reverse spreading (test), the gradient does not need to be output. Only the loss and decoder size sequence are output.
HCIA-AI Experiment Guide Page 23
4 Model Training
4.2 Implementation
4.2.1 Importing Modules
Import relevant modules for running scripts. The math module is the mathematical knowledge module
of Python and used for processing mathematical operation. The six.moves module is used for shielding
function position changes in Python 2 and Python 3. The preprocessing module and seq_2_seq_model
module are defined previously, and are used for preprocessing data and building a model, respectively.
The code is as follows:
HCIA-AI Experiment Guide Page 24
# Global initialization
Step 2 Generate a session and file path for training, and prepare data.
The code is as follows:
Input
Output
"Result"
The __name__ indicates the current module name. When the module is run directly, the module name
is __main__. That is, when the module is run directly, the following code block will be run. When the
module is imported, the code block is not run.
The execution result is as follows:
HCIA-AI Experiment Guide Page 28
Input
Output
Input
Output
Input
Output
Input
HCIA-AI Experiment Guide Page 29
5 Model Test
Hi
Hi
How old are you?
Guess
What did you eat?
I ate
Hm