Infotec Ai 1000 Program-hcia-Ai Lab Guide
Infotec Ai 1000 Program-hcia-Ai Lab Guide
Infotec Ai 1000 Program-hcia-Ai Lab Guide
AI Certification Training
HCIA-AI
ISSUE: 3.5
Machine Learning Lab Guide Page 2
Overview
This document applies to candidates who are preparing for the HCIA-AI exam and others
who want to learn basic machine learning knowledge.
Description
This lab guide comprises the following five experiments:
⚫ Experiment 1 - Linear regression. The Python tool package scikit-learn is used to
implement a simple linear regression algorithm.
⚫ Experiment 2 - Linear regression expansion. The Python basic tool package numpy is
used to implement algorithms such as linear regression and gradient descent from
scratch.
⚫ Experiment 3 - Logistic regression. The logistic regression algorithm in the tool
package is used to implement simple classification.
⚫ Experiment 4 - Decision tree. Trainees will construct a decision tree to predict the
weather, and visualize the decision tree.
⚫ Experiment 5 - K-means clustering algorithm.
Contents
1.1 Introduction
1.1.1 About This Lab
This lab introduces common machine learning algorithms to help you better understand
their functions and usages. Specifically, it will explain how to build a linear regression
algorithm from scratch and implement the decision tree and K-means clustering based
on scikit-learn.
1.1.2 Objectives
⚫ Build a linear regression algorithm from scratch.
⚫ Master the use of classification and regression algorithms.
⚫ Master the use of the V clustering algorithm.
⚫ Master the implementation process of machine learning algorithms.
plt.show()
Output:
Output:
Slope: [4.98467124]
Intercept: -274.8769665187576
Input:
plt.scatter(x,y)
plt.xlabel("area") # X axis indicates the area.
plt.ylabel("price") # Y axis indicates the price.
plt.plot([x[0],x[-1]],[x[0]*w+b,x[-1]*w+b])
Output:
Machine Learning Lab Guide Page 6
Output:
Array([373.13029447]) # The model predicts the house price of the sample.
Step 6 Define the function for visualizing the change curve of the loss function.
Input:
# Plot the loss function change curve.
def showJTheta(diff_value):
p_x = []
p_y = []
for (index, sum) in diff_value:
p_x.append(index)
p_y.append(sum)
plt.plot(p_x, p_y, color='b')
plt.xlabel('steps')
plt.ylabel('loss funtion')
plt.title('step - loss function curve')
plt.show()
Step 7 Define the function for visualizing data points and the fitted curve.
Machine Learning Lab Guide Page 8
Input:
# Plot the actual data points and the fitted curve.
def showlinercurve(theta, sample_training_set):
x, y = sample_training_set[:, 1], sample_training_set[:, 2]
z = theta[0] + theta[1] * x
plt.scatter(x, y, color='b', marker='x',label="sample data")
plt.plot(x, z, 'r', color="r",label="regression curve")
plt.xlabel('x')
plt.ylabel('y')
plt.title('liner regression curve')
plt.legend()
plt.show()
y=[1,1,0,0,1,1,1,1,0,1,1,0,1,1,0,1,0,0,0,1,1,1,0,1,0,1,0,1,1,0]
Output:
[[-0.60583897 -0.29313058]
[-0.37682768 -0.09050576]
[ 0.56003671 0.71999355]
[ 0.14365254 -0.09050576]
[-0.14781638 -0.09050576]
[-0.68911581 -0.49575541]
[-0.48092372 -0.41470548]
[ 3.47472592 2.34099218]
[-0.32269773 -0.49575541]
[-0.56420055 -0.29313058]
[-0.89730789 -0.49575541]
[-0.27273163 -0.57680534]
[-0.68911581 -0.33365555]
[-0.68911581 -0.49575541]
[-0.62665818 -0.57680534]
[-0.10617796 -0.09050576]
[ 0.56003671 -0.09050576]
[ 0.14365254 -0.49575541]
[-0.14781638 -0.29313058]
[-0.68911581 -0.41470548]
[-0.48092372 -0.33365555]
[ 2.64195758 3.15149149]
[-0.21027401 -0.49575541]
[-0.29355084 -0.29313058]
Machine Learning Lab Guide Page 11
[-0.89730789 -0.69838024]
[-0.27273163 -0.17155569]
[ 1.80918923 -0.41470548]
[-0.59751129 -0.33365555]
[ 0.97642089 3.15149149]
[-0.25191242 -0.49575541]]
Output:
LogisticRegression()
Output:
Value to be predicted: [[-0.68911581 -0.57680534]]
predicted label = [1]
probability = [[0.41886952 0.58113048]]
Step 3 Define the function for saving the generated tree diagram.
Input:
def showtree2pdf(trainedTree,finename):
dot_data = tree.export_graphviz(trainedTree, out_file=None) # Export the tree in Graphviz format.
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf(finename) # Save the tree diagram to the local machine in PDF format.
The file content is a visualized display of the decision tree. In the diagram, X[2] is the
third feature variable (humidity); X[0] is the first feature variable (weather); X[3] is the
fourth feature variable (wind); entropy is the entropy value of the node; and samples is
the number of samples in the node, for example, 14 in the first node (root node)
indicates the number of samples in the training set; and value indicates the numbers of
samples of different types, for example, in the root node, 5 indicates the number of "no"
samples, and 9 indicates the number of "yes" samples.
Predict a new sample. Input:
testVec = [0,0,1,1] # Weather is sunny, temperature is low, humidity is high, and wind is strong.
print(decisionTree.predict(np.array(testVec).reshape(1,-1))) # Predict.
Output:
['N']
Output:
Dimension of X is (500, 2)
Dimension of y is (500,).
The dataset contains 500 sample points, and each sample point contains two features.
Output:
Machine Learning Lab Guide Page 15
Next, draw a scatter graph where points in different colors correspond to the generated
labels.
Input:
color = ["red","pink","orange","green"]
fig, ax1 = plt.subplots(1)
for i in range(4):
ax1.scatter(X[y==i, 0], X[y==i, 1] # Draw the color based on the label.
,marker='o' # Set the shape of the point to circle.
,s=8 # Set the size of the point.
,c=color[i]
)
plt.show()
Output:
Output:
[0 0 2 1 2 1 2 2 2 2 0 0 2 1 2 0 2 0 1 2 2 2 2 1 2 2 1 1 2 2 0 1 2 0 2 0 2
2022212202211122202222211221202220220
2202221121122122102210020110121221122
0121212122002221002122220121120211122
0022101222222222100021022012222022100
2200211002120010212202202222022212120
2222212102021120102200002202211221222
1212212002222112120101001011222222201
0002220200200210221120112011221220012
0211222021121111002122012121222122010
0000020101121222012120220221102212200
2022020210122122102112222010210002120
2222022222202202121222111222021201010
2 1 1 0 2 2 0 2 2 2 0 2 1 2 2 0 0 0 2]
Output:
[[-7.09306648 -8.10994454]
[-1.54234022 4.43517599]
[-8.0862351 -3.5179868 ]]
for i in range(n_clusters):
ax1.scatter(X[y_pred1==i, 0], X[y_pred1==i, 1]
,marker='o' # Set the shape of the point to circle.
,s=8 # Set the size of the point.
,c=color[i]
)
ax1.scatter(centroid1[:,0],centroid1[:,1]
,marker="x"
,s=15
,c="black")
Machine Learning Lab Guide Page 17
plt.show()
Output:
The graph shows that the data samples are aggregated into three clusters, and the
centroid of each cluster is represented by x.
Output:
Centroid: [[ -6.08459039 -3.17305983]
[ -1.54234022 4.43517599]
[ -7.09306648 -8.10994454]
[-10.00969056 -3.84944007]]
for i in range(n_clusters):
ax1.scatter(X[y_pred2==i, 0], X[y_pred2==i, 1]
,marker='o' # Set the shape of the point to circle.
,s=8 # Set the size of the point.
,c=color[i]
Machine Learning Lab Guide Page 18
ax1.scatter(centroid2[:,0],centroid2[:,1]
,marker="x"
,s=15
,c="black")
plt.show()
Output:
The graph shows that the data samples are aggregated into four clusters, and the
centroid of each cluster is represented by x. Compare the scatter graphs generated based
on the original data and data aggregated into four types. It can be seen that many
sample points are aggregated into wrong clusters.
1.3 Question
How to implement K-means from scratch using Python?
1.4 Summary
This lab introduces the machine learning implementation process, including data import,
segmentation, standardization, as well as models and hyperparameters. Common
machine learning algorithms are implemented based on scikit-learn to help you better
understand how to build and use machine learning models.
AI Certification Training
HCIA-AI
Development Framework
Lab Guide
ISSUE: 3.5
1
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 1
Overview
This document applies to candidates who are preparing for the HCIA-AI exam and others
who want to learn basic AI knowledge and basic MindSpore programming.
Description
This guide introduces the following five exercises:
⚫ Exercise 1: MindSpore basics, which describes the basic syntax and common modules
of MindSpore.
⚫ Exercise 2: Handwritten character recognition, in which the MindSpore framework is
used to recognize handwritten characters.
⚫ Exercise 3: MobileNetV2 image classification, which mainly introduces the
classification of flower images using the lightweight network MobileNetV2.
⚫ Exercise 4: ResNet-50 image classification exercise, which mainly introduces the
classification of flower images using the ResNet-50 model.
⚫ Exercise 5: TextCNN sentiment analysis, which mainly introduces sentiment analysis
of statements using the TextCNN model.
1
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 2
Contents
2
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 3
3
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 4
1 MindSpore Basics
1.1 Introduction
1.1.1 About This Lab
This exercise introduces the tensor data structure of MindSpore. By performing a series of
operations on tensors, you can understand the basic syntax of MindSpore.
1.1.2 Objectives
⚫ Master the method of creating tensors.
⚫ Master the attributes and methods of tensors.
1.2 Procedure
1.2.1 Introduction to Tensors
Tensor is a basic data structure in MindSpore network computing. For details about data
types in tensors, see the dtype description on the MindSpore official website.
Tensors of different dimensions represent different data. For example, a 0-dimensional
tensor represents a scalar, a 1-dimensional tensor represents a vector, a 2-dimensional
tensor represents a matrix, and a 3-dimensional tensor may represent the three channels
of RGB images.
MindSpore tensors support different data types, including int8, int16, int32, int64, uint8,
uint16, uint32, uint64, float16, float32, float64 and bool_, which correspond to the data
types of NumPy.
In the computation process of MindSpore, the int data type in Python is converted into
the defined int64 type, and the float data type is converted into the defined float32 type.
4
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 5
The data type can be specified during tensor initialization. However, if the data type is
not specified, the initial values int, float, and bool generate 0-dimensional tensors with
mindspore.int32, mindspore.float32 and mindspore.bool_ data types, respectively. The
data types of the 1-dimensional tensors generated by the initial values tuple and list
correspond to the data types of tensors stored in the tuple and list. If multiple types of
data are contained, the MindSpore data type corresponding to the data type with the
highest priority is selected (Boolean < int < float). If the initial value is Tensor, the data
type is tensor. If the initial value is NumPy.array, the generated tensor data type
corresponds to NumPy.array.
Code:
# Import MindSpore.
import mindspore
# The cell outputs multiple lines at the same time.
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
import numpy as np
from mindspore import Tensor
from mindspore import dtype
# Use an array to create a tensor.
x = Tensor(np.array([[1, 2], [3, 4]]), dtype.int32)
x
Output:
Code:
Output:
Code:
5
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 6
m = Tensor(True, dtype.bool_)
m
Output:
Code:
Output
Code:
Output:
Code:
Output:
Code:
6
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 7
shape = (2, 2)
ones = ops.Ones()
output = ones(shape,dtype.float32)
print(output)
zeros = ops.Zeros()
output = zeros(shape, dtype.float32)
print(output)
Output:
[[1. 1.]
[1. 1.]]
[[0. 0.]
[0. 0.]]
x.shape # Shape
x.dtype # Data type
x.ndim # Dimension
x.size # Size
Output:
(2, 2)
mindspore.int32
2
4
y
y_array
Output:
7
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 8
Code:
Output:
Code:
Output:
[[[0. 1.]
[2. 3.]]
[[4. 5.]
[6. 7.]]]
Code:
zeros = ops.Zeros()
output = zeros((2,2), dtype.float32)
print("output: {}".format(type(output)))
8
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 9
n_output = output.asnumpy()
print("n_output: {}".format(type(n_output)))
Output:
import os
import mindspore.dataset as ds
import matplotlib.pyplot as plt
Output:
9
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 10
For datasets that cannot be directly loaded by MindSpore, you can build a custom
dataset class and use the GeneratorDataset API to customize data loading.
Code:
import numpy as np
np.random.seed(58)
class DatasetGenerator:
# When a dataset object is instantiated, the __init__ function is called. You can perform operations
such as data initialization.
def __init__(self):
self.data = np.random.sample((5, 2))
self.label = np.random.sample((5, 1))
# Define the __getitem__ function of the dataset class to support random access and obtain and
return data in the dataset based on the specified index value.
def __getitem__(self, index):
return self.data[index], self.label[index]
# Define the __len__ function of the dataset class and return the number of samples in the dataset.
def __len__(self):
return len(self.data)
# After the dataset class is defined, the GeneratorDataset API can be used to load and access dataset
samples in custom mode.
dataset_generator = DatasetGenerator()
dataset = ds.GeneratorDataset(dataset_generator, ["data", "label"], shuffle=False)
# Use the create_dict_iterator method to obtain data.
for data in dataset.create_dict_iterator():
print('{}'.format(data["data"]), '{}'.format(data["label"]))
Output:
The dataset APIs provided by MindSpore support data processing methods such as shuffle
and batch. You only need to call the corresponding function API to quickly process data.
In the following example, the datasets are shuffled, and two samples form a batch.
Code:
ds.config.set_seed(58)
# Shuffle the data sequence. buffer_size indicates the size of the shuffled buffer in the dataset.
dataset = dataset.shuffle(buffer_size=10)
# Divide the dataset into batches. batch_size indicates the number of data records contained in each
batch. Set this parameter to 2.
dataset = dataset.batch(batch_size=2)
10
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 11
print("label: {}".format(data["label"]))
Output:
Code:
DATA_DIR = './MNIST/train'
# Obtain six samples.
mnist_dataset = ds.MnistDataset(DATA_DIR, num_samples=6, shuffle=False)
# View the original image data.
mnist_it = mnist_dataset.create_dict_iterator()
data = next(mnist_it)
plt.imshow(data['image'].asnumpy().squeeze(), cmap=plt.cm.gray)
plt.title(data['label'].asnumpy(), fontsize=20)
plt.show()
Output:
11
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 12
plt.title(data['label'].asnumpy(), fontsize=20)
plt.show()
Output:
import mindspore as ms
import mindspore.nn as nn
from mindspore import Tensor
import numpy as np
Output:
[[1. 1. 1.]
[2. 2. 2.]]
[[3. 3. 3.]
[6. 6. 6.]]
Code:
12
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 13
print(conv2d(input_x).shape)
Output:
Code:
relu = nn.ReLU()
input_x = Tensor(np.array([-1, 2, -3, 2, -1]), ms.float16)
output = relu(input_x)
print(output)
Output:
[0. 2. 0. 2. 0.]
Code:
print(max_pool2d(input_x).shape)
Output:
Code:
flatten = nn.Flatten()
input_x = Tensor(np.ones([1, 16, 5, 5]), ms.float32)
output = flatten(input_x)
print(output.shape)
Output:
(1, 400)
13
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 14
The Cell class of MindSpore is the base class for building all networks and the basic unit
of a network. When a neural network is required, you need to inherit the Cell class and
overwrite the __init__ and construct methods.
Code:
class LeNet5(nn.Cell):
"""
Lenet network structure
"""
def __init__(self, num_class=10, num_channel=1):
super(LeNet5, self).__init__()
# Define the required operations.
self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
self.fc1 = nn.Dense(16 * 4 * 4, 120)
self.fc2 = nn.Dense(120, 84)
self.fc3 = nn.Dense(84, num_class)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
self.flatten = nn.Flatten()
Output:
14
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 15
A loss function is used to validate the difference between the predicted and actual values
of a model. Here, the absolute error loss function L1Loss is used. mindspore.nn.loss also
provides many other loss functions, such as SoftmaxCrossEntropyWithLogits, MSELoss,
and SmoothL1Loss.
The output value and target value are provided to compute the loss value. The method is
as follows:
Code:
import numpy as np
import mindspore.nn as nn
from mindspore import Tensor
import mindspore.dataset as ds
import mindspore as ms
loss = nn.L1Loss()
output_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(np.float32))
target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(np.float32))
print(loss(output_data, target_data))
Output:
1.5
Common deep learning optimization algorithms include SGD, Adam, Ftrl, lazyadam,
Momentum, RMSprop, Lars, Proximal_ada_grad, and lamb.
Momentum optimizer: mindspore.nn.Momentum
Code:
15
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 16
Code:
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.vision.c_transforms as CV
from mindspore.train.callback import LossMonitor
DATA_DIR = './MNIST/train'
mnist_dataset = ds.MnistDataset(DATA_DIR)
resize_op = CV.Resize((28,28))
rescale_op = CV.Rescale(1/255,0)
hwc2chw_op = CV.HWC2CHW()
Code:
# Test set
DATA_DIR = './forward_mnist/MNIST/test'
dataset = ds.MnistDataset(DATA_DIR)
resize_op = CV.Resize((28,28))
rescale_op = CV.Rescale(1/255,0)
hwc2chw_op = CV.HWC2CHW()
import mindspore as ms
16
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 17
# net indicates a defined network model, which is used before or after training.
ms.save_checkpoint(net, "./MyNet.ckpt") # net indicates the training network, and ./MyNet.ckpt
indicates the path for saving the network model.
Output:
2. The other one is to save the interface during network model training. MindSpore
automatically saves the number of epochs and number of steps set during training. That
is, the intermediate weight parameters generated during the model training process are
also saved to facilitate network fine-tuning and stop training.
Code:
To compute the input derivative, you need to define a network requiring a derivative. The
following uses a network f(x,y)=z∗x∗y formed by the MatMul operator as an example.
Code:
import numpy as np
import mindspore.nn as nn
import mindspore.ops as ops
from mindspore import Tensor
from mindspore import ParameterTuple, Parameter
from mindspore import dtype as mstype
17
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 18
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.matmul = ops.MatMul()
self.z = Parameter(Tensor(np.array([1.0], np.float32)), name='z')
Output:
class GradNetWrtX(nn.Cell):
def __init__(self, net):
super(GradNetWrtX, self).__init__()
self.net = net
self.params = ParameterTuple(net.trainable_params())
self.grad_op = ops.GradOperation(get_by_list=True)
Output:
18
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 19
1.3 Question
When the following code is used to create two tensors, t1 and t2, can t1 be created
properly? Check whether the two tensors have the same outputs. If not, what is the
difference?
19
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 20
2.1 Introduction
2.1.1 About This Exercise
This exercise implements the MNIST handwritten character recognition, which is a typical
case in the deep learning field. The whole process is as follows:
⚫ Process the required dataset. (The MNIST dataset is used in this example.)
⚫ Define a network. (A simple fully-connected network is built in this example.)
⚫ Define a loss function and an optimizer.
⚫ Load the dataset and perform training. After the training is complete, use the test set
for validation.
2.2 Preparations
Before you start, check whether MindSpore has been correctly installed. You are advised
to install MindSpore on your computer by referring to the MindSpore official website
https://www.mindspore.cn/install/en.
In addition, you should have basic mathematical knowledge, including knowledge of
Python coding basics, probability, and matrices.
Recommended environment:
Version: MindSpore 1.7
Programming language: Python 3.7
20
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 21
links are for downloading test data files, and the last two links are for downloading
training data files.
Download and decompress the files, and store them in the workspace directories ./MNIST
/train and ./MNIST /test.
The directory structure is as follows:
└─MNIST
├─ test
│ t10k-images.idx3-ubyte
│ t10k-labels.idx1-ubyte
│
└─ train
train-images.idx3-ubyte
train-labels.idx1-ubyte
2.3.2 Procedure
Step 1 Import the Python library and module and configure running information.
import mindspore as ms
import mindspore.context as context
import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.vision.c_transforms as CV
from mindspore.nn.metrics import Accuracy
context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
The graph mode is used in this exercise. You can configure hardware information as
required. For example, if the code runs on the Ascend AI processor, set device_target to
21
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 22
Ascend. This rule also applies to the code running on the CPU and GPU. For details about
parameters, see the context.set_context API description at
https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.context.html.
Use the data reading function of MindSpore to read the MNIST dataset and view the
data volume and sample information of the training set and test set.
Code:
Datasets are crucial for training. A good dataset can effectively improve training accuracy
and efficiency. Generally, before loading a dataset, you need to perform some operations
on the dataset.
Define a dataset and data operations.
We define the create_dataset function to create a dataset. In this function, we define the
data augmentation and processing operations to be performed:
⚫ Read the dataset.
⚫ Define parameters required for data augmentation and processing.
⚫ Generate corresponding data augmentation operations according to the parameters.
⚫ Use the map function to apply data operations to the dataset.
⚫ Process the generated dataset.
Code:
22
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 23
ds = ds.batch(batch_size, drop_remainder=True)
return ds
In the preceding information, batch_size indicates the number of data records in each
batch. Assume that each batch contains 32 data records. Modify the image size,
normalization, and image channel, and then modify the data type of the label. Perform
the shuffle operation, set batch_size, and set drop_remainder to True. In this case, data
that cannot form a batch in the dataset will be discarded.
MindSpore supports multiple data processing and augmentation operations, which are
usually used together. For details, see Data Processing and Augmentation on the
MindSpore official website.
Read the first 10 samples and visualize the samples to determine whether the samples
are real datasets.
Code:
# Display the first 10 images and the labels, and check whether the images are correctly labeled.
ds = create_dataset(training=False)
data = ds.create_dict_iterator().__next__()
images = data['image'].asnumpy()
labels = data['label'].asnumpy()
plt.figure(figsize=(15,5))
for i in range(1,11):
plt.subplot(2, 5, i)
plt.imshow(np.squeeze(images[i]))
plt.title('Number: %s' % labels[i])
plt.xticks([])
plt.show()
Output:
23
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 24
# Create a model. The model consists of three fully connected layers. The final output layer uses
softmax for classification (10 classes consisting of numbers 0 to 9.)
class ForwardNN(nn.Cell):
def __init__(self):
super(ForwardNN, self).__init__()
self.flatten = nn.Flatten()
self.fc1 = nn.Dense(784, 512, activation='relu')
self.fc2 = nn.Dense(512, 128, activation='relu')
self.fc3 = nn.Dense(128, 10, activation=None)
A loss function is also called an objective function and is used to measure the difference
between a predicted value and an actual value. Deep learning reduces the loss value by
continuous iteration. Defining a good loss function can effectively improve model
performance.
An optimizer is used to minimize the loss function, improving the model during training.
After the loss function is defined, the weight-related gradient of the loss function can be
obtained. The gradient is used to indicate the weight optimization direction for the
optimizer, improving model performance. Loss functions supported by MindSpore include
SoftmaxCrossEntropyWithLogits, L1Loss, and MSELoss. SoftmaxCrossEntropyWithLogits is
used in this example.
MindSpore provides the callback mechanism to execute custom logic during training. The
following uses ModelCheckpoint provided by the framework as an example.
ModelCheckpoint can save the network model and parameters for subsequent fine-
tuning.
Code:
# Create a network, loss function, validation metric, and optimizer, and set related hyperparameters.
lr = 0.001
num_epoch = 10
momentum = 0.9
24
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 25
net = ForwardNN()
loss = nn.loss.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
metrics={"Accuracy": Accuracy()}
opt = nn.Adam(net.trainable_params(), lr)
# Build a model.
model = Model(net, loss, opt, metrics)
config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10)
ckpoint_cb = ModelCheckpoint(prefix="checkpoint_net",directory = "./ckpt" ,config=config_ck)
# Generate a dataset.
ds_eval = create_dataset(False, batch_size=32)
ds_train = create_dataset(batch_size=32)
# Train the model.
loss_cb = LossMonitor(per_print_times=1875)
time_cb = TimeMonitor(data_size=ds_train.get_dataset_size())
print("============== Starting Training ==============")
model.train(num_epoch, ds_train,callbacks=[ckpoint_cb,loss_cb,time_cb ],dataset_sink_mode=False)
Although loss values may fluctuate, they gradually decrease and the accuracy gradually
increases in general. Loss values displayed each time may be different because of their
randomicity. The following is an example of loss values output during training:
25
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 26
In this step, the original test set is used to validate the model.
Code:
# Use the test set to validate the model and print the overall accuracy.
metrics=model.eval(ds_eval)
print(metrics)
Output:
{'Accuracy': 0.9740584935897436}
26
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 27
3.1 Introduction
In this exercise, the lightweight network MobileNetV2 is used to classify flower image
datasets.
3.2 Preparations
Before you start, check whether MindSpore has been correctly installed. You are advised
to install MindSpore on your computer by referring to the MindSpore official website
https://www.mindspore.cn/install/en.
In addition, you should have basic mathematical knowledge, including knowledge of
Python coding basics, probability, and matrix.
Recommended environment:
Version: MindSpore 1.7
Programming language: Python 3.7
flower_photos_train
├── daisy
├── dandelion
├── roses
├── sunflowers
├── tulips
27
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 28
├── LICENSE.txt
flower_photos_test
├── daisy
├── dandelion
├── roses
├── sunflowers
├── tulips
├── LICENSE.txt
Obtain the datasets from the following links:
https://ascend-professional-construction-dataset.obs.myhuaweicloud.com/deep-
learning/flower_photos_train.zip
https://ascend-professional-construction-dataset.obs.myhuaweicloud.com/deep-
learning/flower_photos_test.zip
3.3.2 Procedure
Step 1 Load the dataset.
Define the create_dataset function, use the ImageFolderDataset API to load the flower
image classification dataset, and perform image enhancement on the dataset. Code:
import mindspore.dataset as ds
import mindspore.dataset.vision.c_transforms as CV
from mindspore import dtype as mstype
train_data_path = 'flower_photos_train'
val_data_path = 'flower_photos_test'
28
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 29
CV.Decode(),
CV.Resize(256),
CV.CenterCrop(image_size),
CV.HWC2CHW()
]
return data_set
dataset_train = create_dataset(train_data_path)
dataset_val = create_dataset(val_data_path)
The return value of the training dataset loaded from the create_dataset API is a
dictionary. You can use the create_dict_iterator API to create a data iterator and use next
to iteratively access the dataset. Here, batch_size is set to 18. Therefore, you can use next
to obtain 18 images and label data at a time. Code:
data = next(dataset_train.create_dict_iterator())
images = data["image"]
labels = data["label"]
# class_name corresponds to label. Labels are marked in ascending order of the folder character
string.
class_name = {0:'daisy',1:'dandelion',2:'roses',3:'sunflowers',4:'tulips'}
plt.figure(figsize=(15, 7))
for i in range(len(labels)):
# Obtain an image and its label.
data_image = images[i].asnumpy()
data_label = labels[i]
# Process images for display.
data_image = np.transpose(data_image, (1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
data_image = std * data_image + mean
data_image = np.clip(data_image, 0, 1)
# Display the image.
plt.subplot(3, 6, i + 1)
plt.imshow(data_image)
plt.title(class_name[int(labels[i].asnumpy())])
29
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 30
plt.axis("off")
plt.show()
Output:
Datasets are crucial for training. A good dataset can effectively improve training accuracy
and efficiency. MobileNet is a lightweight CNN proposed by Google in 2017 to focus on
mobile, embedded, and IoT devices. Compared with traditional convolutional neural
networks, MobileNet uses depthwise separable convolution to greatly reduce the model
parameters and computation amount with a slight decrease in accuracy. In addition, the
width coefficient α and resolution coefficient β are introduced to meet the requirements
of different application scenarios.
Because a large amount of data is lost when the ReLU activation function in the
MobileNet processes low-dimensional feature information, the MobileNetV2 proposes to
use an inverted residual block and Linear Bottlenecks to design the network, improving
the accuracy of the model and making the optimized model smaller.
In the inverted residual block structure, the 1 x 1 convolution is used for dimension
increase, the 3 x 3 depthwise convolution is used, and the 1 x 1 convolution is used for
dimension reduction. This structure is opposite to the residual block structure. For the
residual block, the 1 x 1 convolution is first used for dimension reduction, then the 3 x 3
convolution is used, and finally the 1 x 1 convolution is used for dimension increase.
For details, see the MobileNetV2 paper at https://arxiv.org/pdf/1801.04381.pdf.
Code:
import numpy as np
import mindspore as ms
30
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 31
import mindspore.nn as nn
import mindspore.ops as ops
class GlobalAvgPooling(nn.Cell):
def __init__(self):
super(GlobalAvgPooling, self).__init__()
self.mean = ops.ReduceMean(keep_dims=False)
class ConvBNReLU(nn.Cell):
class InvertedResidual(nn.Cell):
31
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 32
layers = []
if expand_ratio != 1:
layers.append(ConvBNReLU(inp, hidden_dim, kernel_size=1))
layers.extend([
# dw
ConvBNReLU(hidden_dim, hidden_dim,
stride=stride, groups=hidden_dim),
# pw-linear
nn.Conv2d(hidden_dim, oup, kernel_size=1,
stride=1, has_bias=False),
nn.BatchNorm2d(oup),
])
self.conv = nn.SequentialCell(layers)
self.add = ops.Add()
self.cast = ops.Cast()
class MobileNetV2Backbone(nn.Cell):
32
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 33
input_channel = output_channel
# building last several layers
features.append(ConvBNReLU(input_channel, self.out_channels, kernel_size=1))
# make it nn.CellList
self.features = nn.SequentialCell(features)
self._initialize_weights()
def _initialize_weights(self):
self.init_parameters_data()
for _, m in self.cells_and_names():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.set_data(ms.Tensor(np.random.normal(0, np.sqrt(2. / n),
m.weight.data.shape).astype("float32")))
@property
def get_features(self):
return self.features
class MobileNetV2Head(nn.Cell):
33
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 34
x = self.dense(x)
if self.need_activation:
x = self.activation(x)
return x
def _initialize_weights(self):
self.init_parameters_data()
for _, m in self.cells_and_names():
if isinstance(m, nn.Dense):
m.weight.set_data(ms.Tensor(np.random.normal(
0, 0.01, m.weight.data.shape).astype("float32")))
if m.bias is not None:
m.bias.set_data(
ms.numpy.zeros(m.bias.data.shape, dtype="float32"))
class MobileNetV2Combine(nn.Cell):
def mobilenet_v2(num_classes):
backbone_net = MobileNetV2Backbone()
head_net = MobileNetV2Head(backbone_net.out_channels,num_classes)
return MobileNetV2Combine(backbone_net, head_net)
After a model, a loss function and an optimizer are created, the Model API is used to
initialize the model, the model.train API is used to train the model, and the model.eval
API is used to validate the model accuracy.
This section involves the following knowledge of transfer learning:
1. Download a pre-trained model weight.
Download the model file pre-trained on the ImageNet dataset, and save it to the
directory at the same level as the running code. Download link:
https://download.mindspore.cn/models/r1.7/mobilenetv2_ascend_v170_imagenet2012
_official_cv_top1acc71.88.ckpt.
2. Read the pre-trained model.
Read the pre-trained model file through the load_checkpoint() API. The output result
is in dictionary data format.
3. Modify pre-trained model parameters.
Modify the parameters related to the pre-trained model weight. (The model is pre-
trained on the ImageNet dataset to classify 1001 types. However, the current exercise is
34
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 35
to classify five types of flowers. The network model modifies the last fully-connected
layer.)
Code:
import mindspore
import mindspore.nn as nn
from mindspore.train import Model
from mindspore import Tensor, save_checkpoint
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor
from mindspore.train.serialization import load_checkpoint, load_param_into_net
# Create a model. The number of target classes is 5.
network = mobilenet_v2(5)
train_step_size = dataset_train.get_dataset_size()
epoch_size = 20
lr = nn.cosine_decay_lr(min_lr=0.0, max_lr=0.1,total_step=epoch_size *
train_step_size,step_per_epoch=train_step_size,decay_epoch=200)
# Define the optimizer.
network_opt = nn.Momentum(params=network.trainable_params(), learning_rate=0.01,
momentum=0.9)
# Set the number of steps for saving a model and the maximum number of models that can be
saved.
ckpt_config = CheckpointConfig(save_checkpoint_steps=100, keep_checkpoint_max=10)
# Save the model. Set the name, path, and parameters for saving the model.
ckpoint_cb = ModelCheckpoint(prefix="mobilenet_v2", directory='./ckpt', config=ckpt_config)
35
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 36
Output:
Define the visualize_model function, use the model with the highest validation accuracy
described above to predict the input image and visualize the prediction result.
Code:
36
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 37
picture_show = np.clip(picture_show, 0, 1)
plt.imshow(picture_show)
plt.axis('off')
plt.show()
visualize_model('ckpt/mobilenet_v2-5_201.ckpt', dataset_val)
Output:
3.4 Question
What API is used to read pre-trained models?
37
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 38
4.1 Introduction
This exercise implements the ResNet-50 image classification, a classic case in the deep
learning field. The entire process is as follows:
⚫ Process the required dataset. (The flower image dataset is used in this example.)
⚫ Define a network. You need to set up a ResNet-50 model structure.
⚫ Define a loss function and an optimizer.
⚫ Load the dataset and perform training. After the training is complete, use the test set
for validation.
4.2 Preparations
Before you start, check whether MindSpore has been correctly installed. You are advised
to install MindSpore on your computer by referring to the MindSpore official website
https://www.mindspore.cn/install/en.
In addition, you should have basic mathematical knowledge, including knowledge of
Python coding basics, probability, and matrices.
Recommended environment:
Version: MindSpore 1.7
Programming language: Python 3.7
flower_photos_train
38
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 39
├── daisy
├── dandelion
├── roses
├── sunflowers
├── tulips
├── LICENSE.txt
flower_photos_test
├── daisy
├── dandelion
├── roses
├── sunflowers
├── tulips
├── LICENSE.txt
Obtain the datasets from the following links:
https://ascend-professional-construction-dataset.obs.myhuaweicloud.com/deep-
learning/flower_photos_train.zip
https://ascend-professional-construction-dataset.obs.myhuaweicloud.com/deep-
learning/flower_photos_test.zip
4.3.2 Procedure
Step 1 Import the Python library and module and configure running information.
import mindspore
# MindSpore library
import mindspore.dataset as ds
# Dataset processing module
39
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 40
The edict stores the parameter configurations required for model training and testing.
Code:
cfg = edict({
'data_path': 'flowers/flower_photos_train', # Path of the training dataset
'test_path':'flowers/flower_photos_train', # Path of the test dataset
'data_size': 3616,
'HEIGHT': 224, # Image height
'WIDTH': 224, # Image width
'_R_MEAN': 123.68, # Average value of CIFAR-10
'_G_MEAN': 116.78,
'_B_MEAN': 103.94,
'_R_STD': 1, # Customized standard deviation
'_G_STD': 1,
'_B_STD':1,
'_RESIZE_SIDE_MIN': 256, # Minimum resize value for image enhancement
'_RESIZE_SIDE_MAX': 512,
40
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 41
Datasets are crucial for training. A good dataset can effectively improve training accuracy
and efficiency. Generally, before loading a dataset, you need to perform some operations
on the dataset.
Define a dataset and data operations.
We define the create_dataset function to create a dataset. In this function, we define the
data augmentation and processing operations to be performed:
⚫ Read the dataset.
⚫ Define parameters required for data augmentation and processing.
⚫ Generate corresponding data augmentation operations according to the parameters.
⚫ Use the map function to apply data operations to the dataset.
⚫ Process the generated dataset.
⚫ Display the processed data as an example.
Code:
# Data processing
def read_data(path,config,usage="train"):
# Read the source dataset of an image from a directory.
dataset = ds.ImageFolderDataset(path,
class_indexing={'daisy':0,'dandelion':1,'roses':2,'sunflowers':3,'tulips':4})
# define map operations
# Operator for image decoding
decode_op = vision.Decode()
# Operator for image normalization
normalize_op = vision.Normalize(mean=[cfg._R_MEAN, cfg._G_MEAN, cfg._B_MEAN],
std=[cfg._R_STD, cfg._G_STD, cfg._B_STD])
# Operator for image resizing
resize_op = vision.Resize(cfg._RESIZE_SIDE_MIN)
# Operator for image cropping
center_crop_op = vision.CenterCrop((cfg.HEIGHT, cfg.WIDTH))
# Operator for image random horizontal flipping
horizontal_flip_op = vision.RandomHorizontalFlip()
# Operator for image channel quantity conversion
channelswap_op = vision.HWC2CHW()
# Operator for random image cropping, decoding, encoding, and resizing
random_crop_decode_resize_op = vision.RandomCropDecodeResize((cfg.HEIGHT, cfg.WIDTH),
(0.5, 1.0), (1.0, 1.0), max_attempts=100)
41
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 42
# Data augmentation
dataset = dataset.repeat(1)
dataset.map_model = 4
return dataset
plt.figure()
plt.imshow(data_next['image'][0,0,...])
plt.colorbar()
plt.grid(False)
plt.show()
Output:
42
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 43
(https://arxiv.org/pdf/1512.03385.pdf)
If the dimensions are the same:
y = F(x, 𝑊𝑖 ) + 𝑥
F = 𝑊2 𝜎(𝑊, 𝑥)
If the dimensions are different:
y = F(x, 𝑊𝑖 ) + 𝑊𝑠 𝑥
Bottleneck module
The bottleneck module uses the 1x1 convolutional layer to reduce or expand the feature
map dimension so that the number of filters at the 3x3 convolutional layer is not
affected by the input of the upper layer, and the output of the 3x3 convolutional layer
does not affect the lower-layer module.
43
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 44
(https://arxiv.org/pdf/1512.03385.pdf)
ResNet-50 model
ResNet-50 has two basic blocks: convolutional block and identity block. The input and
output dimensions of the convolutional block are different and cannot be connected in
series. The convolutional block is used to change the network dimensions. The identity
block has the same input and output dimensions, which can be connected in series to
deepen the network.
(https://arxiv.org/pdf/1512.03385.pdf)
Code:
"""ResNet."""
44
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 45
Args:
in_channel (int): Input channel.
out_channel (int): Output channel.
stride (int): Stride size for the first convolutional layer. Default: 1.
Returns:
Tensor, output tensor.
Examples:
>>> ResidualBlock(3, 256, stride=2)
"""
expansion = 4 # In conv2_x--conv5_x, the number of convolution kernels at the first two layers is
one fourth of the number of convolution kernels at the third layer (an output channel).
45
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 46
# The number of convolution kernels at the first two layers is equal to a quarter of the
number of convolution kernels at the output channels.
channel = out_channel // self.expansion
# Layer 1 convolution
self.conv1 = _conv1x1(in_channel, channel, stride=1)
self.bn1 = _bn(channel)
# Layer 2 convolution
self.conv2 = _conv3x3(channel, channel, stride=stride)
self.bn2 = _bn(channel)
self.down_sample = False
# When the step is not 1 or the number of output channels is not equal to that of input
channels, downsampling is performed to adjust the number of channels.
if stride != 1 or in_channel != out_channel:
self.down_sample = True
self.down_sample_layer = None
# Adjust the number of channels using the 1x1 convolution.
if self.down_sample:
self.down_sample_layer = nn.SequentialCell([_conv1x1(in_channel, out_channel, stride),
# 1x1 convolution
_bn(out_channel)]) # Batch Norm
# Addition operator
self.add = ops.Add()
46
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 47
identity = self.down_sample_layer(identity)
return out
Args:
block (Cell): Block for network.
layer_nums (list): Numbers of block in different layers.
in_channels (list): Input channel in each layer.
out_channels (list): Output channel in each layer.
strides (list): Stride size in each layer.
num_classes (int): The number of classes that the training images belong to.
Returns:
Tensor, output tensor.
Examples:
>>> ResNet(ResidualBlock,
>>> [3, 4, 6, 3],
>>> [64, 256, 512, 1024],
>>> [256, 512, 1024, 2048],
>>> [1, 2, 2, 2],
>>> 10)
"""
# Input parameters: residual block, number of repeated residual blocks, input channel, output
channel, stride, and number of image classes
def __init__(self, block, layer_nums, in_channels, out_channels, strides, num_classes):
super(ResNet, self).__init__()
if not len(layer_nums) == len(in_channels) == len(out_channels) == 4:
raise ValueError("the length of layer_num, in_channels, out_channels list must be 4!")
# Layer 1 convolution; convolution kernels: 7x7, input channels: 3; output channels: 64; step:
2
self.conv1 = _conv7x7(3, 64, stride=2)
self.bn1 = _bn(64)
self.relu = ops.ReLU()
47
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 48
# Input parameters: residual block, number of repeated residual blocks, input channel, output
channel, and stride
def _make_layer(self, block, layer_num, in_channel, out_channel, stride):
"""
Make stage network of ResNet.
Args:
block (Cell): Resnet block.
layer_num (int): Layer number.
in_channel (int): Input channel.
out_channel (int): Output channel.
stride (int): Stride size for the first convolutional layer.
Returns:
SequentialCell, the output layer.
Examples:
>>> _make_layer(ResidualBlock, 3, 128, 256, 2)
"""
# Build the residual block of convn_x.
layers = []
48
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 49
return nn.SequentialCell(layers)
return out
Args:
class_num (int): Class number.
Returns:
Cell, cell instance of ResNet50 neural network.
Examples:
>>> net = resnet50(10)
"""
return ResNet(ResidualBlock, # Residual block
[3, 4, 6, 3], # Number of residual blocks
[64, 256, 512, 1024], # Input channel
[256, 512, 1024, 2048], # Output channel
[1, 2, 2, 2], # Step
class_num) # Number of output classes
⚫ Start training.
After preprocessing data and defining the network, loss function, and optimizer, start
model training. Model training involves two iterations: multi-epoch iteration of datasets
and single-step iteration based on the batch size. The single-step iteration refers to
extracting data from a dataset by batch, inputting the data to a network to calculate a
loss function, and then calculating and updating a gradient of training parameters by
using an optimizer.
1. Download a pre-trained model.
Create the model_resnet directory, download the model file pre-trained on the
ImageNet dataset, and save the file to the model_resnet directory. Download link:
https://download.mindspore.cn/models/r1.7/resnet50_ascend_v170_imagenet2012_off
icial_cv_top1acc76.97_top5acc93.44.ckpt.
49
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 50
# Construct a ResNet-50 network. The number of output classes is 5, corresponding to five flower
classes.
net=resnet50(class_num=cfg.num_class)
# Smooth the loss value to solve the problem of the gradient being too small during training.
loss_scale = FixedLossScaleManager(cfg.loss_scale_num, False)
# Build the model. Input the network structure, loss function, optimizer, loss value smoothing, and
model evaluation metrics.
model = Model(net, loss_fn=loss, optimizer=opt, loss_scale_manager=loss_scale, metrics={'acc'})
50
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 51
# Model saving parameters. Set the number of steps for saving a model and the maximum number
of models that can be saved.
ckpt_config = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_steps,
keep_checkpoint_max=1)
# Save the model. Set the name, path, and parameters for saving the model.
ckpoint_cb = ModelCheckpoint(prefix=cfg.prefix, directory=cfg.directory, config=ckpt_config)
# Use the test set to validate the model and output the accuracy of the test set.
metric = model.eval(de_test)
print(metric)
Output:
Use the trained weight file to call model.predict() to test the test data and output the
prediction result and actual result.
Code:
# Model prediction. Select 10 samples from the test set for testing and output the prediction result
and actual result.
class_names = {0:'daisy',1:'dandelion',2:'roses',3:'sunflowers',4:'tulips'}
for i in range(10):
test_ = de_test.create_dict_iterator().__next__()
test = Tensor(test_['image'], mindspore.float32)
# Use the model for prediction.
predictions = model.predict(test)
predictions = predictions.asnumpy()
true_label = test_['label'].asnumpy()
# Show the prediction result.
p_np = predictions[0, :]
pre_label = np.argmax(p_np)
print('Prediction result of the ' + str(i) + '-th sample: ', class_names[pre_label], ' Actual result: ',
class_names[true_label[0]])
Output:
51
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 52
4.4 Question
In this exercise, how many convolutional and fully-connected layers does the ResNet-50
have? How many epochs are defined for model training? How many classes are output
for the network?
52
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 53
5.1 Introduction
This exercise implements the TextCNN sentiment analysis, a classic case in the deep
learning field. The entire process is as follows:
⚫ Download the required dataset. (The rt-polarity dataset is used to define the data
preprocessing function.)
⚫ Generate data for training and validation.
⚫ Define the TextCNN model structure build, training, validation, offline model loading,
and online inference functions.
⚫ Define various parameters required for training, such as optimizer, loss function,
checkpoint, and time monitor.
⚫ Load the dataset and perform training. After the training is complete, use the test set
for validation.
5.2 Preparations
Before you start, check whether MindSpore has been correctly installed. You are advised
to install MindSpore on your computer by referring to the MindSpore official website
https://www.mindspore.cn/install/en.
In addition, you should have basic mathematical knowledge, including basic knowledge
of Python coding basics, probability, and matrices.
Recommended environment:
Version: MindSpore 1.7
Programming language: Python 3.7
53
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 54
5.3.2 Procedure
Step 1 Import the Python library and module and configure running information.
import math
import numpy as np
import pandas as pd
import os
import math
import random
import codecs
from pathlib import Path
import mindspore
import mindspore.dataset as ds
import mindspore.nn as nn
from mindspore import Tensor
from mindspore import context
from mindspore.train.model import Model
from mindspore.nn.metrics import Accuracy
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
from mindspore.ops import operations as ops
cfg = edict({
'name': 'movie review',
'pre_trained': False,
'num_classes': 2,
'batch_size': 64,
'epoch_size': 4,
'weight_decay': 3e-5,
'data_path': './data/',
'device_target': 'CPU',
'device_id': 0,
'keep_checkpoint_max': 1,
54
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 55
'checkpoint_path': './ckpt/train_textcnn-4_149.ckpt',
'word_len': 51,
'vec_length': 40
})
context.set_context(mode=context.GRAPH_MODE, device_target=cfg.device_target,
device_id=cfg.device_id)
The graph mode is used in this exercise. You can configure hardware information as
required. For example, if the code runs on the Ascend AI processor, set device_target to
Ascend. This rule also applies to the code running on the CPU and GPU. For details about
the parameters, see the context.set_context API description at
https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.context.html.
class MovieReview:
55
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 56
'''
Movie review dataset
'''
def __init__(self, root_dir, maxlen, split):
'''
input:
root_dir: movie review data directory
maxlen: maximum length of a sentence
split: the training/validation ratio in the dataset
'''
self.path = root_dir
self.feelMap = {
'neg':0,
'pos':1
}
self.files = []
self.doConvert = False
mypath = Path(self.path)
if not mypath.exists() or not mypath.is_dir():
print("please check the root_dir!")
raise ValueError
# Read data.
self.word_num = 0
self.maxlen = 0
self.minlen = float("inf")
self.maxlen = float("-inf")
self.Pos = []
self.Neg = []
for filename in self.files:
self.read_data(filename)
self.text2vec(maxlen=maxlen)
self.split_dataset(split=split)
56
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 57
.replace('\'','')\
.replace('.','')\
.replace(',','')\
.replace('[','')\
.replace(']','')\
.replace('(','')\
.replace(')','')\
.replace(':','')\
.replace('--','')\
.replace('-',' ')\
.replace('\\','')\
.replace('0','')\
.replace('1','')\
.replace('2','')\
.replace('3','')\
.replace('4','')\
.replace('5','')\
.replace('6','')\
.replace('7','')\
.replace('8','')\
.replace('9','')\
.replace('`','')\
.replace('=','')\
.replace('$','')\
.replace('/','')\
.replace('*','')\
.replace(';','')\
.replace('<b>','')\
.replace('%','')
# self.Vocab['None']
for SentenceLabel in self.Pos+self.Neg:
vector = [0]*maxlen
for index, word in enumerate(SentenceLabel[0]):
if index >= maxlen:
break
57
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 58
random.shuffle(self.train)
def get_dict_len(self):
'''
Obtain the length of a dictionary consisting of characters in a dataset.
'''
if self.doConvert:
return len(self.Vocab)
else:
print("Haven't finished Text2Vec")
return -1
58
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 59
Use the customized read_data function to load the original dataset and use the text2vec
function to vectorize the text of the data. Use split_dataset to split data into training data
and test data, and then create_test_dataset and create_train_dataset call
mindspore.dataset. GeneratorDataset generates data for training and validation.
Configuration code:
vocab_size=instance.get_dict_len()
print("vocab_size:{0}".format(vocab_size))
item =dataset.create_dict_iterator()
for i,data in enumerate(item):
if i<1:
print(data)
print(data['data'][1])
else:
break
learning_rate = []
warm_up = [1e-3 / math.floor(cfg.epoch_size / 5) * (i + 1) for _ in range(batch_num)
for i in range(math.floor(cfg.epoch_size / 5))]
shrink = [1e-3 / (16 * (i + 1)) for _ in range(batch_num)
for i in range(math.floor(cfg.epoch_size * 3 / 5))]
normal_run = [1e-3 for _ in range(batch_num) for i in
range(cfg.epoch_size - math.floor(cfg.epoch_size / 5)
- math.floor(cfg.epoch_size * 2 / 5))]
learning_rate = learning_rate + warm_up + normal_run + shrink
Model classes define model structure build, training, validation, offline model loading,
and online inference functions.
59
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 60
Code:
def make_conv_layer(kernel_size):
weight_shape = (96, 1, *kernel_size)
weight = _weight_variable(weight_shape)
return nn.Conv2d(in_channels=1, out_channels=96, kernel_size=kernel_size, padding=1,
pad_mode="pad", weight_init=weight, has_bias=True)
class TextCNN(nn.Cell):
def __init__(self, vocab_len, word_len, num_classes, vec_length):
super(TextCNN, self).__init__()
self.vec_length = vec_length
self.word_len = word_len
self.num_classes = num_classes
self.unsqueeze = ops.ExpandDims()
self.embedding = nn.Embedding(vocab_len, self.vec_length, embedding_table='normal')
self.slice = ops.Slice()
self.layer1 = self.make_layer(kernel_height=3)
self.layer2 = self.make_layer(kernel_height=4)
self.layer3 = self.make_layer(kernel_height=5)
self.concat = ops.Concat(1)
def construct(self,x):
x = self.unsqueeze(x, 1)
x = self.embedding(x)
x1 = self.layer1(x)
x2 = self.layer2(x)
x3 = self.layer3(x)
60
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 61
x = self.drop(x)
x = self.fc(x)
return x
net = TextCNN(vocab_len=instance.get_dict_len(), word_len=cfg.word_len,
num_classes=cfg.num_classes, vec_length=cfg.vec_length)
print(net)
A loss function is also called an objective function and is used to measure the difference
between a predicted value and an actual value. Deep learning reduces the loss value by
continuous iteration. Defining a good loss function can effectively improve model
performance.
An optimizer is used to minimize the loss function, improving the model during training.
After the loss function is defined, the weight-related gradient of the loss function can be
obtained. The gradient is used to indicate the weight optimization direction for the
optimizer, improving model performance. Loss functions supported by MindSpore include
SoftmaxCrossEntropyWithLogits, L1Loss, and MSELoss. SoftmaxCrossEntropyWithLogits is
used in this example.
MindSpore provides the callback mechanism to execute custom logic during training. The
following uses ModelCheckpoint provided by the framework as an example.
ModelCheckpoint can save the network model and parameters for subsequent fine-
tuning.
Code:
61
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 62
loss_cb = LossMonitor()
Output:
A single piece of review text data is obtained for testing, and a sentiment category and a
probability of the sentiment category are output.
Code:
def preprocess(sentence):
sentence = sentence.lower().strip()
sentence = sentence.replace('\n','')\
.replace('"','')\
.replace('\'','')\
.replace('.','')\
.replace(',','')\
.replace('[','')\
.replace(']','')\
.replace('(','')\
.replace(')','')\
.replace(':','')\
.replace('--','')\
.replace('-',' ')\
.replace('\\','')\
.replace('0','')\
.replace('1','')\
.replace('2','')\
.replace('3','')\
.replace('4','')\
.replace('5','')\
.replace('6','')\
.replace('7','')\
62
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 63
.replace('8','')\
.replace('9','')\
.replace('`','')\
.replace('=','')\
.replace('$','')\
.replace('/','')\
.replace('*','')\
.replace(';','')\
.replace('<b>','')\
.replace('%','')\
.replace(" "," ")
sentence = sentence.split(' ')
maxlen = cfg.word_len
vector = [0]*maxlen
for index, word in enumerate(sentence):
if index >= maxlen:
break
if word not in instance.Vocab.keys():
print(word,"The word does not appear in the dictionary.")
else:
vector[index] = instance.Vocab[word]
sentence = vector
return sentence
def inference(review_en):
review_en = preprocess(review_en)
input_en = Tensor(np.array([review_en]).astype(np.int32))
output = net(input_en)
if np.argmax(np.array(output[0])) == 1:
print("Positive comments")
else:
print("Negative comments")
Code:
Output:
5.4 Question
If you want to perform the preceding exercise on a GPU, which configuration item do
you need to change to GPU?
63