Huawei AI Certification Training
HCIA-AI
Mainstream Development
Framework
Lab Guide
Issue: 3.0
Huawei Technologies Co., Ltd.
1
Copyright © Huawei Technologies Co., Ltd. 2021. All rights reserved.
No part of this document may be reproduced or transferred in any form or by any means
without prior written consent of Huawei Technologies Co., Ltd.
Trademarks and Permissions
and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd.
All other trademarks and trade names mentioned in this document are the property of
their respective holders.
Notice
The purchased products, services, and features are stipulated by the commercial
contract made between Huawei and the customer. All or partial products, services, and
features described in this document may not be within the purchased scope or the
usage scope. Unless otherwise agreed by the contract, all statements, information, and
recommendations in this document are provided "AS IS" without warranties, guarantees
or representations of any kind, either express or implied.
The information in this document is subject to change without notice. Every effort has
been made in the preparation of this document to ensure accuracy of the contents, but
all statements, information, and recommendations in this document do not constitute a
warranty of any kind, express or implied.
Huawei Technologies Co., Ltd.
Address:
Huawei Industrial Base Bantian, Longgang Shenzhen 518129 People's
Republic of China
Website:
http://e.huawei.com
Huawei Proprietary and Confidential
Copyright © Huawei Technologies Co,Ltd
HCIA-AI Mainstream Development Framework Lab Guide
Huawei Certification System
3
Page
Huawei Certification is an integral part of the company's "Platform + Ecosystem"
strategy, it supports the ICT infrastructure featuring "Cloud-Pipe-Device". It evolves to
reflect the latest trends of ICT development.
Huawei Certification consists of two categories: ICT Infrastructure, and Cloud Service
& Platform. Huawei offers three levels of certification: Huawei Certified ICT Associate
(HCIA), Huawei Certified ICT Professional (HCIP), and Huawei Certified ICT Expert
(HCIE).
With its leading talent development system and certification standards, Huawei is
committed to developing ICT professionals in the digital era, building a healthy ICT talent
ecosystem.
HCIA-AI V3.0 certification is intended for cultivating and conducting qualification of
engineers who are capable of creatively designing and developing AI products and
solutions using machine learning and deep learning algorithms.
HCIA-AI V3.0 certified engineers understand the development history of AI, Huawei
Ascend AI system, and Huawei full-stack AI strategy in all scenarios, master traditional
machine learning and deep learning algorithms, and are able to use the TensorFlow and
MindSpore frameworks to build, train, and deploy neural networks. With this certificate,
you are qualified for positions including sales, marketing, product manager, project
management, and technical support in the AI field.
HCIA-AI Mainstream Development Framework Lab Guide
Huawei Certification Portfolio
4
Page
HCIA-AI Mainstream Development Framework Lab Guide
5
Page
About This Document
Introduction
This document is intended for trainees who are preparing for the HCIA-AI certification
examination or readers who want to learn AI basics and TensorFlow programming basics.
Description
This lab guide includes the following three exercises:
Exercise 1 mainly introduces the basic syntax of TensorFlow 2.
Exercise 2 introduces common modules of TensorFlow 2, especially the Keras API.
Exercise 3 is a handwritten font image recognition exercise. It uses basic code to help
learners understand how to recognize handwritten fonts using TensorFlow 2.
Background Knowledge Required
This course is a basic course for Huawei certification. Before beginning this course, you
should:
Have basic Python knowledge
Be familiar with basic concepts of TensorFlow
Understand basic Python programming knowledge
HCIA-AI Mainstream Development Framework Lab Guide
6
Page
Contents
About This Document
Introduction
Description
Background Knowledge Required
1 TensorFlow 2 Basics
1.1 Introduction
1.1.1 About This Exercise
1.1.2 Objectives
1.2 Tasks
1.2.1 Introduction to Tensors
1.2.2 Eager Execution Mode of TensorFlow 2
1.2.3 AutoGraph of TensorFlow 2
2 Common Modules of TensorFlow 2
2.1 Introduction
2.2 Objectives
2.3 Tasks
2.3.1 Model Building
2.3.2 Training and Evaluation
2.3.3 Model Saving and Restoration
3 Handwritten Digit Recognition with TensorFlow
3.1 Introduction
3.2 Objectives
3.3 Tasks
3.3.1 Project Description and Dataset Acquisition
3.3.2 Dataset Preprocessing and Visualization
3.3.3 DNN Construction
3.3.4 CNN Construction
3.3.5 Prediction Result Visualization
4 Image Classification
4.1 Introduction
4.1.1 About This Exercise
4.1.2 Objectives
4.2 Tasks
4.2.1 Importing Dependencies
4.2.2 Preprocessing Data
4.2.3 Building a Model
4.2.4 Training the Model
3
3
3
3
6
6
6
6
6
6
22
23
25
25
25
25
25
30
34
36
36
36
36
36
38
39
41
43
45
45
45
45
45
45
45
47
48
HCIA-AI Mainstream Development Framework Lab Guide
4.2.5 Evaluating the Model
4.3 Summary
7
Page
49
50
HCIA-AI Mainstream Development Framework Lab Guide
8
Page
1 TensorFlow 2 Basics
1.1 Introduction
1.1.1 About This Exercise
This exercise introduces tensor operations of TensorFlow 2, including tensor creation,
slicing, indexing, tensor dimension modification, tensor arithmetic operations, and tensor
sorting, to help you understand the basic syntax of TensorFlow 2.
1.1.2 Objectives
Learn how to create tensors.
Learn how to slice and index tensors.
Master the syntax of tensor dimension changes.
Master arithmetic operations of tensors.
Know how to sort tensors.
Understand eager execution and AutoGraph based on code.
1.2 Tasks
1.2.1 Introduction to Tensors
In TensorFlow, tensors are classified into constant and variable tensors.
Publicité
A defined constant tensor has an immutable value and dimension while a defined
variable tensor has a variable value and an immutable dimension.
In a neural network, a variable tensor is generally used as a matrix for storing weights
and other information, and is a trainable data type. A constant tensor can be used as a
variable for storing hyperparameters or other structural information.
1.2.1.1 Tensor Creation
1.2.1.1.1 Creating a Constant Tensor
Common methods for creating a constant tensor include:
tf.constant(): creates a constant tensor.
tf.zeros(), tf.zeros_like(), tf.ones(),tf.ones_like(): creates an all-zero or all-one
constant tensor.
tf.fill(): creates a tensor with a user-defined value.
tf.random: creates a tensor with a known distribution.
tf.convert_to_tensor: creates a list object by using NumPy and then converts it into a
tensor.
HCIA-AI Mainstream Development Framework Lab Guide
Step 1
tf.constant()
tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False):
9
Page
value: value
dtype: data type
shape: tensor shape
name: name for the constant tensor
verify_shape: Boolean that enables verification of a shape of values. The default value
is False. If verify_shape is set to True, the system checks whether the shape of value
is consistent with shape. If they are inconsistent, an error is reported.
Code:
import tensorflow as tf
print(tf.__version__)
const_a = tf.constant([[1, 2, 3, 4]],shape=[2,2], dtype=tf.float32) # Create a 2x2 matrix with values 1, 2, 3,
and 4.
const_a
Output:
2.0.0-beta1
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 2.],
[3., 4.]], dtype=float32)>
Code:
View common attributes.
print("value of const_a: ", const_a.numpy())
print("data type of const_a: ", const_a.dtype)
print("shape of const_a: ", const_a.shape)
print("device that const_a will be generated: ", const_a.device)
Output:
The value of const_a is [[1. 2.]
[3. 4.]]
The data type of const_a is <dtype: 'float32'>.
The shape of const_a is (2, 2).
const_a will be generated on /job:localhost/replica:0/task:0/device:CPU:0.
Step 2
tf.zeros(), tf.zeros_like(), tf.ones(),tf.ones_like()
The usage of tf.ones() and tf.ones_like() is similar to that of tf.zeros() and tf.zeros_like().
Therefore, the following describes how to use tf.ones() and tf.ones_like().
Create a tensor with all elements set to zero.
tf.zeros(shape, dtype=tf.float32, name=None):
shape: tensor shape
dtype: type
name: name for the operation
Code:
zeros_b = tf.zeros(shape=[2, 3], dtype=tf.int32) # Create a 2x3 matrix with all element values being 0.
HCIA-AI Mainstream Development Framework Lab Guide
10
Page
Create a tensor with all elements set to zero based on the input tensor, with its shape being
the same as that of the input tensor.
tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True):
input_tensor: tensor
dtype: type
name: name for the operation
optimize: optimize or not
Code:
zeros_like_c = tf.zeros_like(const_a)
View generated data.
zeros_like_c.numpy()
Output:
array([[0., 0.],
[0., 0.]], dtype=float32)
Step 3
tf.fill()
Create a tensor and fill it with a specific value.
tf.fill(dims, value, name=None):
dims: tensor shape, which is the same as the preceding shape
value: tensor value
name: name of the output
Code:
fill_d = tf.fill([3,3], 8) # 3x3 matrix with all element values being 8
View data.
fill_d.numpy()
Output:
array([[8, 8, 8],
[8, 8, 8],
[8, 8, 8]], dtype=int32)
Step 4
tf.random
This module is used to generate a tensor with a specific distribution. The common methods
in this module include tf.random.uniform(), tf.random.normal(), and tf.random.shuffle().
The following demonstrates how to use tf.random.normal().
Create a tensor that conforms to the normal distribution.
tf.random.normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32,seed=None, name=None):
shape: data shape
mean: mean value of Gaussian distribution
stddev: standard deviation of Gaussian distribution
dtype: data type
seed: random seed
11
Page
HCIA-AI Mainstream Development Framework Lab Guide
name: name for the operation
Code:
random_e = tf.random.normal([5,5],mean=0,stddev=1.0, seed = 1)
View the created data.
random_e.numpy()
Output:
array([[-0.8521641 , 2.0672443 , -0.94127315, 1.7840577 , 2.9919195 ],
[-0.8644102 , 0.41812655, -0.85865736, 1.0617154 , 1.0575105 ],
[ 0.22457163, -0.02204755, 0.5084496 , -0.09113179, -1.3036906 ],
[-1.1108295 , -0.24195422, 2.8516252 , -0.7503834 , 0.1267275 ],
[ 0.9460202 , 0.12648873, -2.6540542 , 0.0853276 , 0.01731399]],
dtype=float32)
Step 5 Create a list object by using NumPy and then convert it into a tensor by using
tf.convert_to_tensor.
This method can convert the given value to a tensor. It converts Python objects of various
types to Tensor objects.
tf.convert_to_tensor(value,dtype=None,dtype_hint=None,name=None):
value: value to be converted
dtype: tensor data type
dtype_hint: optional element type for the returned tensor, used when dtype is None. In
some cases, a caller may not have a dtype in mind when converting to a tensor, so
dtype_hint can be used as a soft preference.
Code:
Create a list.
list_f = [1,2,3,4,5,6]
View the data type.
type(list_f)
Output:
list
Code:
tensor_f = tf.convert_to_tensor(list_f, dtype=tf.float32)
tensor_f
Output:
<tf.Tensor: shape=(6,), dtype=float32, numpy=array([1., 2., 3., 4., 5., 6.], dtype=float32)>
1.2.1.1.2 Creating a Variable Tensor
In TensorFlow, variables are created and tracked via the tf.Variable class. A tf.Variable
represents a tensor whose value can be changed by running ops on it. Specific ops allow
you to read and modify the values of this tensor.
Code:
12
Page
HCIA-AI Mainstream Development Framework Lab Guide
To create a variable, provide an initial value.
var_1 = tf.Variable(tf.ones([2,3]))
var_1
Output:
<tf.Variable 'Variable:0' shape=(2, 3) dtype=float32, numpy=
array([[1., 1., 1.],
[1., 1., 1.]], dtype=float32)>
Code:
Read the variable value.
Print("value of var_1: ",var_1.read_value())
Publicité
Assign a new value to the variable.
var_value_1=[[1,2,3],[4,5,6]]
var_1.assign(var_value_1)
Print("new value for var_1: ",var_1.read_value())
Output:
Value of var_1: tf.Tensor(
[[1. 1. 1.]
[1. 1. 1.]], shape=(2, 3), dtype=float32)
New value for var_1: tf.Tensor(
[[1. 2. 3.]
[4. 5. 6.]], shape=(2, 3), dtype=float32)
Code:
Add a value to this variable.
var_1.assign_add(tf.ones([2,3]))
var_1
Output:
<tf.Variable 'Variable:0' shape=(2, 3) dtype=float32, numpy=
array([[2., 3., 4.],
[5., 6., 7.]], dtype=float32)>
1.2.1.2 Tensor Slicing and Indexing
1.2.1.2.1 Slicing
Major slicing methods include:
[start: end]: extracts a data slice from the start position to the end position of a tensor.
[start :end :step] or [::step]: extracts a data slice at an interval of step from the start
position to the end position of a tensor.
[::-1]: slices data from the last element.
'...': indicates a data slice of any length.
Code:
Create a 4-dimensional tensor. The tensor contains four images. The size of each image is 100 x 100 x
3.
tensor_h = tf.random.normal([4,100,100,3])
13
Page
HCIA-AI Mainstream Development Framework Lab Guide
tensor_h
Output:
<tf.Tensor: shape=(4, 100, 100, 3), dtype=float32, numpy=
array([[[[ 1.68444023e-01, -7.46562362e-01, -4.34964240e-01],
[-4.69263226e-01, 6.26460612e-01, 1.21065331e+00],
[ 7.21675277e-01, 4.61057723e-01, -9.20868576e-01],
...,
Code:
Extract the first image.
tensor_h[0,:,:,:]
Output:
<tf.Tensor: shape=(100, 100, 3), dtype=float32, numpy=
array([[[ 1.68444023e-01, -7.46562362e-01, -4.34964240e-01],
[-4.69263226e-01, 6.26460612e-01, 1.21065331e+00],
[ 7.21675277e-01, 4.61057723e-01, -9.20868576e-01],
...,
Code:
Extract one slice every two images.
tensor_h[::2,...]
Output:
<tf.Tensor: shape=(2, 100, 100, 3), dtype=float32, numpy=
array([[[[ 1.68444023e-01, -7.46562362e-01, -4.34964240e-01],
[-4.69263226e-01, 6.26460612e-01, 1.21065331e+00],
[ 7.21675277e-01, 4.61057723e-01, -9.20868576e-01],
...,
Code:
Slice data from the last element.
tensor_h[::-1]
Output:
<tf.Tensor: shape=(4, 100, 100, 3), dtype=float32, numpy=
array([[[[-1.70684665e-01, 1.52386248e+00, -1.91677585e-01],
[-1.78917408e+00, -7.48436213e-01, 6.10363662e-01],
[ 7.64770031e-01, 6.06725179e-02, 1.32704067e+00],
...,
1.2.1.2.2 Indexing
The basic format of an index is a[d1][d2][d3].
Code:
Obtain the pixel in the [20,40] position in the second channel of the first image.
tensor_h[0][19][39][1]
HCIA-AI Mainstream Development Framework Lab Guide
Output:
<tf.Tensor: shape=(), dtype=float32, numpy=0.38231283>
14
Page
If the indexes to be extracted are nonconsecutive, tf.gather and tf.gather_nd are commonly
used for data extraction in TensorFlow.
To extract data from a particular dimension:
tf.gather(params, indices,axis=None):
params: input tensor
indices: index of the data to be extracted
axis: dimension of the data to be extracted
Code:
Extract the first, second, and fourth images from tensor_h ([4,100,100,3]).
indices = [0,1,3]
tf.gather(tensor_h,axis=0,indices=indices)
Output:
<tf.Tensor: shape=(3, 100, 100, 3), dtype=float32, numpy=
array([[[[ 1.68444023e-01, -7.46562362e-01, -4.34964240e-01],
[-4.69263226e-01, 6.26460612e-01, 1.21065331e+00],
[ 7.21675277e-01, 4.61057723e-01, -9.20868576e-01],
...,
tf.gather_nd allows data extraction from multiple dimensions:
tf.gather_nd(params,indices):
params: input tensor
indices: index of the data to be extracted. Generally, this is a multidimensional list.
Code:
Extract the pixel in [1,1] in the first dimension of the first image and pixel in [2,2] in the first dimension of
the second image in tensot_h ([4,100,100,3]).
indices = [[0,1,1,0],[1,2,2,0]]
tf.gather_nd(tensor_h,indices=indices)
Output:
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.5705869, 0.9735735], dtype=float32)>
1.2.1.3 Tensor Dimension Modification
1.2.1.3.1 Dimension Display
Code:
const_d_1 = tf.constant([[1, 2, 3, 4]],shape=[2,2], dtype=tf.float32)
Three common methods for displaying a dimension:
print(const_d_1.shape)
print(const_d_1.get_shape())
print(tf.shape(const_d_1))# The output is a tensor. The value of the tensor indicates the size of the tensor
dimension to be displayed.
Output:
HCIA-AI Mainstream Development Framework Lab Guide
15
Page
(2, 2)
(2, 2)
tf.Tensor([2 2], shape=(2,), dtype=int32)
As described above, .shape and .get_shape() return TensorShape objects, while
tf.shape(x) returns Tensor objects.
1.2.1.3.2 Dimension Reshaping
tf.reshape(tensor,shape,name=None):
tensor: input tensor
shape: shape of the reshaped tensor
Code:
reshape_1 = tf.constant([[1,2,3],[4,5,6]])
print(reshape_1)
tf.reshape(reshape_1, (3,2))
Output:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 2],
[3, 4],
[5, 6]], dtype=int32)>
1.2.1.3.3 Dimension Expansion
tf.expand_dims(input,axis,name=None):
input: input tensor
axis: adds a dimension after the axis dimension. Given an input of D dimensions, axis
must be in range [-(D+1), D] (inclusive). A negative value indicates the reverse order.
Code:
Generate a 100 x 100 x 3 tensor to represent a 100 x 100 three-channel color image.
expand_sample_1 = tf.random.normal([100,100,3], seed=1)
print("original data size: ",expand_sample_1.shape)
Print("add a dimension (axis=0) before the first dimension: ",tf.expand_dims(expand_sample_1,
axis=0).shape)
Print("add a dimension (axis=1) before the second dimension: ",tf.expand_dims(expand_sample_1,
axis=1).shape)
Print("add a dimension (axis=-1) after the last dimension: ",tf.expand_dims(expand_sample_1, axis=-
1).shape)
Output:
Original data size: (100, 100, 3)
Add a dimension (axis=0) before the first dimension: (1, 100, 100, 3)
Add a dimension (axis=1) before the second dimension: (100, 1, 100, 3)
Add a dimension (axis=-1) after the last dimension: (100, 100, 3, 1)
1.2.1.3.4 Dimension Squeezing
tf.squeeze(input,axis=None,name=None):
This method is used to remove dimensions of size 1 from the shape of a tensor.
input: input tensor
HCIA-AI Mainstream Development Framework Lab Guide
16
Page
axis: If you don not want to remove all size 1 dimensions, remove specific size 1
dimensions by specifying axis.
Code:
Generate a 100 x 100 x 3 tensor.
orig_sample_1 = tf.random.normal([1,100,100,3])
print("original data size: ",orig_sample_1.shape)
squeezed_sample_1 = tf.squeeze(orig_sample_1)
print("squeezed data size: ",squeezed_sample_1.shape)
The dimension of 'squeeze_sample_2' is [1, 2, 1, 3, 1, 1].
squeeze_sample_2 = tf.random.normal([1, 2, 1, 3, 1, 1])
t_1 = tf.squeeze(squeeze_sample_2) # Dimensions of size 1 are removed.
print('t_1.shape:', t_1.shape)
Publicité
Remove a specific dimension:
't' is a tensor of shape [1, 2, 1, 3, 1, 1]
t_1_new = tf.squeeze(squeeze_sample_2, [2, 4])
print('t_1_new.shape:', t_1_new.shape)
Output:
Original data size: (1, 100, 100, 3)
Squeezed data size: (100, 100, 3)
t_1.shape: (2, 3)
t_1_new.shape: (1, 2, 3, 1)
1.2.1.3.5 Transpose
tf.transpose(a,perm=None,conjugate=False,name='transpose'):
a: input tensor
perm: permutation of the dimensions of a, generally used to transpose high-dimensional
arrays
conjugate: conjugate transpose
name: name for the operation
Code:
Low-dimensional transposition is simple. Input the tensor to be transposed by calling tf.transpose.
trans_sample_1 = tf.constant([1,2,3,4,5,6],shape=[2,3])
print("original data size: ",trans_sample_1.shape)
transposed_sample_1 = tf.transpose(trans_sample_1)
print("transposed data size: ",transposed_sample_1.shape)
Output:
Original data size: (2, 3)
Transposed data size: (3, 2)
Code:
The perm parameter is required for transposing high-dimensional data. perm indicates the permutation
of the dimensions of the input tensor.
For a three-dimensional tensor, its original dimension permutation is [0, 1, 2] (perm), indicating the
length, width, and height of the high-dimensional data, respectively.
HCIA-AI Mainstream Development Framework Lab Guide
17
Page
By changing the value sequence in perm, you can transpose the corresponding dimension of the data.
Generate a 4 x 100 x 200 x 3 tensor to represent four 100 x 200 three-channel color images.
trans_sample_2 = tf.random.normal([4,100,200,3])
print("original data size: ",trans_sample_2.shape)
Exchange the length and width of the four images. The value range of perm is changed from [0,1,2,3]
to [0,2,1,3].
transposed_sample_2 = tf.transpose(trans_sample_2,[0,2,1,3])
print("transposed data size: ",transposed_sample_2.shape)
Output:
Original data size: (4, 100, 200, 3)
Transposed data size: (4, 200, 100, 3)
1.2.1.3.6 Broadcast (broadcast_to)
broadcast_to is used to broadcast data from a low dimension to a high dimension.
tf.broadcast_to(input,shape,name=None):
input: input tensor
shape: size of the output tensor
Code:
broadcast_sample_1 = tf.constant([1,2,3,4,5,6])
print("original data: ",broadcast_sample_1.numpy())
broadcasted_sample_1 = tf.broadcast_to(broadcast_sample_1,shape=[4,6])
print("broadcast data: ",broadcasted_sample_1.numpy())
Output:
Original data: [1 2 3 4 5 6]
Broadcast data: [[1 2 3 4 5 6]
[1 2 3 4 5 6]
[1 2 3 4 5 6]
[1 2 3 4 5 6]]
Code:
During the operation, if two arrays have different shapes, TensorFlow automatically triggers the
broadcast mechanism as NumPy does.
a = tf.constant([[ 0, 0, 0],
[10,10,10],
[20,20,20],
[30,30,30]])
b = tf.constant([1,2,3])
print(a + b)
Output:
tf.Tensor(
[[ 1 2 3]
[11 12 13]
[21 22 23]
[31 32 33]], shape=(4, 3), dtype=int32)
1.2.1.4 Arithmetic Operations on Tensors
1.2.1.4.1 Arithmetic Operators
HCIA-AI Mainstream Development Framework Lab Guide
18
Page
Arithmetic operations include addition (tf.add), subtraction (tf.subtract), multiplication
(tf.multiply), division (tf.divide), logarithm (tf.math.log), and powers (tf.pow). The following
is an example of addition.
Code:
a = tf.constant([[3, 5], [4, 8]])
b = tf.constant([[1, 6], [2, 9]])
print(tf.add(a, b))
Output:
tf.Tensor(
[[ 4 11]
[ 6 17]], shape=(2, 2), dtype=int32)
1.2.1.4.2 Matrix Multiplication
Matrix multiplication is implemented by calling tf.matmul.
Code:
tf.matmul(a,b)
Output:
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[13, 63],
[20, 96]], dtype=int32)>
1.2.1.4.3 Tensor Statistics Collection
Methods for collecting tensor statistics include:
tf.reduce_min/max/mean(): calculates the minimum, maximum, and mean values.
tf.argmax()/tf.argmin(): calculates the positions of the maximum and minimum values.
tf.equal(): checks whether two tensors are equal by element.
tf.unique(): removes duplicate elements from a tensor.
tf.nn.in_top_k(prediction, target, K): calculates whether the predicted value is equal
to the actual value and returns a tensor of the Boolean type.
The following demonstrates how to use tf.argmax().
Return the subscript of the maximum value.
tf.argmax(input,axis):
input: input tensor
axis: The maximum value is output based on the axis dimension.
Code:
argmax_sample_1 = tf.constant([[1,3,2],[2,5,8],[7,5,9]])
print("input tensor: ",argmax_sample_1.numpy())
max_sample_1 = tf.argmax(argmax_sample_1, axis=0)
max_sample_2 = tf.argmax(argmax_sample_1, axis=1)
print("locate the maximum value by column: ",max_sample_1.numpy())
print("locate the maximum value by row: ",max_sample_2.numpy())
Output:
HCIA-AI Mainstream Development Framework Lab Guide
Input tensor: [1 3 2]
[2 5 8]
[7 5 9]]
Locate the maximum value by column: [2 1 2].
Locate the maximum value by row: [1 2 2].
1.2.1.5 Dimension-based Arithmetic Operations
19
Page
In TensorFlow, operations such as tf.reduce_* reduce tensor dimensions. These operations
can be performed on the dimension elements of a tensor, for example, calculating the mean
value by row and calculating a product of all elements in the tensor.
Common operations include tf.reduce_sum (addition), tf.reduce_prod (multiplication),
tf.reduce_min (minimum), tf.reduce_max (maximum), tf.reduce_mean (mean),
tf.reduce_all (logical AND), tf.reduce_any (logical OR), and tf.reduce_logsumexp
(log(sum(exp))).
The methods of using these operations are similar. The following uses the tf.reduce_sum
operation as an example.
Compute the sum of elements across dimensions of a tensor.
tf.reduce_sum(input_tensor, axis=None, keepdims=False,name=None):
input_tensor: tensor to reduce
axis: axis to be calculated. If this parameter is not specified, the mean value of all
elements is calculated.
keepdims: whether to reduce the dimension. If this parameter is set to True, the output
result retains the shape of the input tensor. If this parameter is set to False, the
dimension of the output result is reduced.
name: name for the operation
Code:
reduce_sample_1 = tf.constant([1,2,3,4,5,6],shape=[2,3])
print("original data",reduce_sample_1.numpy())
print("compute the sum of all elements in a tensor (axis=None):
",tf.reduce_sum(reduce_sample_1,axis=None).numpy())
print("compute the sum of each column by column (axis=0):
",tf.reduce_sum(reduce_sample_1,axis=0).numpy())
print("compute the sum of each column by row (axis=1):
",tf.reduce_sum(reduce_sample_1,axis=1).numpy())
Output:
Original data [1 2 3]
[4 5 6]]
Compute the sum of all elements in the tensor (axis=None): 21
Compute the sum of each column (axis=0): [5 7 9]
Compute the sum of each column (axis=1): [6 15]
1.2.1.6 Tensor Concatenation and Splitting
1.2.1.6.1 Tensor Concatenation
In TensorFlow, tensor concatenation operations include:
tf.contact(): concatenates tensors along one dimension. Other dimensions remain
unchanged.
HCIA-AI Mainstream Development Framework Lab Guide
20
Page
tf.stack(): stacks the tensor list of rank R into a tensor of rank (R+1). Dimensions are
changed after stacking.
Publicité
tf.concat(values, axis, name='concat'):
values: input tensor
axis: dimension along which to concatenate
name: name for the operation
Code:
concat_sample_1 = tf.random.normal([4,100,100,3])
concat_sample_2 = tf.random.normal([40,100,100,3])
Print("original data size: ",concat_sample_1.shape,concat_sample_2.shape)
concated_sample_1 = tf.concat([concat_sample_1,concat_sample_2],axis=0)
print("concatenated data size: ",concated_sample_1.shape)
Output:
Original data size: (4, 100, 100, 3) (40, 100, 100, 3)
Concatenated data size: (44, 100, 100, 3)
A dimension is added to an original matrix in the same way. axis determines the position
where the dimension is added.
tf.stack(values, axis=0, name='stack'):
values: a list of tensor objects with the same shape and type
axis: axis to stack along
name: name for the operation
Code:
stack_sample_1 = tf.random.normal([100,100,3])
stack_sample_2 = tf.random.normal([100,100,3])
Print("original data size: ",stack_sample_1.shape, stack_sample_2.shape)
Dimension addition after concatenating. If axis is set to 0, a dimension is added before the first
dimension.
stacked_sample_1 = tf.stack([stack_sample_1, stack_sample_2],axis=0)
print("concatenated data size: ",stacked_sample_1.shape)
Output:
Original data size: (100, 100, 3) (100, 100, 3)
Concatenated data size: (2, 100, 100, 3)
1.2.1.6.2 Tensor Splitting
In TensorFlow, tensor splitting operations include:
tf.unstack(): unpacks tensors along the specific dimension.
tf.split(): splits a tensor into a list of sub tensors based on specific dimensions.
Compared with tf.unstack(), tf.split() is more flexible.
tf.unstack(value,num=None,axis=0,name='unstack'):
value: input tensor
num: outputs a list containing num elements. num must be equal to the number of
elements in the specified dimension. Generally, this parameter is ignored.
axis: axis to unstack along
21
Page
HCIA-AI Mainstream Development Framework Lab Guide
name: name for the operation
Code:
Unpack data along the first dimension and output the unpacked data in a list.
tf.unstack(stacked_sample_1,axis=0)
Output:
[<tf.Tensor: shape=(100, 100, 3), dtype=float32, numpy=
array([[[ 0.0665694 , 0.7110351 , 1.907618 ],
[ 0.84416866, 1.5470593 , -0.5084871 ],
[-1.9480026 , -0.9899087 , -0.09975405],
...,
tf.split(value, num_or_size_splits, axis=0):
value: input tensor
num_or_size_splits: number of splits
axis: dimension along which to split
tf.split() can be split in either of the following ways:
1.
2.
If num_or_size_splits is an integer, the tensor is evenly split into several small tensors
along the axis=D dimension.
If num_or_size_splits is a vector, the tensor is split into several smaller tensors based
on the element values of the vector along the axis=D dimension.
Code:
import numpy as np
split_sample_1 = tf.random.normal([10,100,100,3])
print("original data size: ",split_sample_1.shape)
splited_sample_1 = tf.split(split_sample_1, num_or_size_splits=5,axis=0)
print("If m_or_size_splits is 5, the size of the split data is: ",np.shape(splited_sample_1))
splited_sample_2 = tf.split(split_sample_1, num_or_size_splits=[3,5,2],axis=0)
print("If num_or_size_splits is [3,5,2], the sizes of the split data are:",
np.shape(splited_sample_2[0]),
np.shape(splited_sample_2[1]),
np.shape(splited_sample_2[2]))
Output:
Original data size: (10, 100, 100, 3)
If m_or_size_splits is 5, the size of the split data is (5, 2, 100, 100, 3).
If num_or_size_splits is [3,5,2], the sizes of the split data are (3, 100, 100, 3) (5, 100, 100, 3) (2, 100,
100, 3).
1.2.1.7 Tensor Sorting
In TensorFlow, tensor sorting operations include:
tf.sort(): sorts tensors in ascending or descending order and returns the sorted tensors.
tf.argsort(): sorts tensors in ascending or descending order and returns the indices.
tf.nn.top_k(): returns the k largest values.
tf.sort/argsort(input, direction, axis):
input: input tensor
HCIA-AI Mainstream Development Framework Lab Guide
22
Page
direction: direction in which to sort the values. The value can be DESCENDING or
ASCENDING. The default value is ASCENDING.
axis: axis along which to sort The default value is -1, which sorts the last axis.
Code:
sort_sample_1 = tf.random.shuffle(tf.range(10))
print("input tensor: ",sort_sample_1.numpy())
sorted_sample_1 = tf.sort(sort_sample_1, direction="ASCENDING")
print("tensor sorted in ascending order: ",sorted_sample_1.numpy())
sorted_sample_2 = tf.argsort(sort_sample_1,direction="ASCENDING")
print("index of elements in ascending order: ",sorted_sample_2.numpy())
Output:
Input tensor: [1 8 7 9 6 5 4 2 3 0]
Tensor sorted in ascending order: [0 1 2 3 4 5 6 7 8 9]
Index of elements in ascending order: [9 0 7 8 6 5 4 2 1 3]
tf.nn.top_k(input,K,sorted=TRUE):
input: input tensor
K: k largest values to be output and their indices
sorted: sorted=TRUE indicates in ascending order. sorted=FALSE indicates in
descending order.
Two tensors are returned:
values: k largest values in each row
indices: indices of values within the last dimension of input
Code:
values, index = tf.nn.top_k(sort_sample_1,5)
print("input tensor: ",sort_sample_1.numpy())
print("k largest values in ascending order: ", values.numpy())
print("indices of the k largest values in ascending order: ", index.numpy())
Output:
Input tensor: [1 8 7 9 6 5 4 2 3 0]
The k largest values in ascending order: [9 8 7 6 5]
Indices of the k largest values in ascending order: [3 1 2 4 5]
1.2.2 Eager Execution Mode of TensorFlow 2
Eager execution mode:
The eager execution mode of TensorFlow is a type of imperative programming, which is the
same as the native Python. When you perform a particular operation, the system
immediately returns a result.
Graph mode:
TensorFlow 1 adopts the graph mode to first build a computational graph, enable a session,
and then feed actual data to obtain a result.
In eager execution mode, code debugging is easier, but the code execution efficiency is
lower.
The following implements simple multiplication by using TensorFlow to compare the
differences between the eager execution mode and the graph mode.
HCIA-AI Mainstream Development Framework Lab Guide
23
Page
Code:
x = tf.ones((2, 2), dtype=tf.dtypes.float32)
y = tf.constant([[1, 2],
[3, 4]], dtype=tf.dtypes.float32)
z = tf.matmul(x, y)
print(z)
Output:
tf.Tensor(
[[4. 6.]
[4. 6.]], shape=(2, 2), dtype=float32)
Code:
Use the syntax of TensorFlow 1.x in TensorFlow 2.x. You can install the v1 compatibility package in
TensorFlow 2 to inherit the TensorFlow 1.x code and disable the eager execution mode.
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
Create a graph and define it as a computational graph.
a = tf.ones((2, 2), dtype=tf.dtypes.float32)
b = tf.constant([[1, 2],
[3, 4]], dtype=tf.dtypes.float32)
c = tf.matmul(a, b)
Start a session and perform the multiplication operation to obtain data.
with tf.Session() as sess:
print(sess.run(c))
Output:
[[4. 6.]
[4. 6.]]
Restart the kernel to restore TensorFlow to version 2 and enable the eager...