import numpy as np # numpy
import matplotlib.pyplot as plt # Visualization.
import copy # Used to save network parameters.
import os, stat # Data path processing
from sklearn.model_selection import train_test_split
# easier to print
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
import mindspore
import mindspore.ops as ops
from mindspore import Tensor
from mindspore import Model
from mindspore import dtype, Tensor
from mindspore import Parameter
# mindspore.dataset
import mindspore.dataset as ds # Dataset loading.
import mindspore.dataset.transforms.c_transforms as C # Common conversion operators.
import mindspore.dataset.vision.c_transforms as CV # Image conversion operator.
# mindspore.common
from mindspore.common import dtype as mstype # Data form conversion.
from mindspore.common.initializer import Normal # Parameter initialization.
from mindspore.common.initializer import initializer
# mindspore.nn
import mindspore.nn as nn # Includes all network layers.
from mindspore.nn.metrics import Accuracy, Loss # Used for testing models.
from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
# mindspore.train.callback
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor, Callback # callback functions.
from mindspore import Model # Bears the network structure.
from mindspore import save_checkpoint, load_checkpoint # Saves and reads the optimal parameters.
from mindspore import context # Sets the MindSpore running environment.
Tensor: silimar to numpy array
b = a.asnumpy() #convert Tensor to numpy
Tensor(np.array([[1, 1, 1], [2, 2, 2]]), mindspore.float32)
Dataset Loading
# A) Read 3 images from the MNIST dataset.
dataset_dir = "./data/train"
mnist_dataset = ds.MnistDataset(dataset_dir=dataset_dir, num_samples=3)
plt.figure(figsize=(8,8)) # Set the image size.
i=1
# Print three subgraphs.
for dic in mnist_dataset.create_dict_iterator():
plt.subplot(3,3,i)
plt.imshow(dic['image'].asnumpy())
plt.axis('off')
i+=1
plt.show()
# B) Load the NumPy dataset.
data = ds.NumpySlicesDataset([1, 2, 3], column_names=["col_1"])
for x in data.create_dict_iterator(): print(x)
# C) use NumPy to read data in CSV format from Iris species dataset.
iris_data = numpy.genfromtxt('iris.csv', delimiter=',')
iris_data[:10] # check okay
iris_data = iris_data[1:] # drop column name
X = iris_data[:,:4].astype(np.float32) # Features
y = iris_data[:,-1].astype(np.int32) # Label
X /= np.max(np.abs(X),axis=0) # Data normalization to [0,1]
X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=0.2, random_state=42)
train_data = ds.NumpySlicesDataset((X_train, y_train) ) # Training set
test_data = ds.NumpySlicesDataset((X_test, y_test) ) # Test set
train_data = train_data.batch(32), test_data = test_data.batch(32) # Batch processing
Fully Connected Network Setup
# Use to construct a fully-connected network.
nn.Dense(in_channels=3, out_channels=3, weight_init=1)
class my_net(nn.Cell):
def __init__(self):
super(my_net, self).__init__()
self.fc1 = nn.Dense(4, 10) # 4 attributes
self.relu = nn.ReLU()
self.fc2 = nn.Dense(10, 3) # 3 categories
def construct(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
Model Training and Validation
# 1.Define the neural network.
net = my_net()
# 2.Define the loss function.
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True) # with logit transformation, Set sparse to True if the output is not one-hot encoding.
# 3.Define the optimizer.
net_optim = nn.Momentum(params=net.trainable_params(), learning_rate=0.01, momentum=0.9)
# 4.Build a model.
model = Model(network = net, loss_fn=net_loss, optimizer=net_optim, metrics={"accuracy": Accuracy()}) # Model evaluation metrics
# Train the model. epoch=number of training times
model.train(epoch=10, train_dataset=train_data)
# 一个epoch准确的说是在设置了验证集的情况下,是训练集和验证集全部经过一次网络训练+验证的过程。如果没有设置验证集,就是训练集的过程。
# Validate the model.
model.eval(valid_dataset=test_data) # test_dataset is an input parameter.
Parameter Initialization
weight initialization is critical to model performance because parameters need to be continuously updated. A good initialization parameter can accelerate model convergence, find the optimal solution (as much as practicable), and alleviate the problem of gradient disappearance and gradient explosion.
# init=alias name of class inheriting from Initializer, like "ones", "zeros"
tensor3 = initializer(init=mindspore.common.initializer.Normal(sigma=0.02), shape=[1, 2, 3], dtype=mindspore.float32)
Environment Setup
Currently, there are two mainstream execution modes of deep learning frameworks: static graph and dynamic graph. The static graph mode has a relatively high training performance, but it is difficult to debug. Conversely, the dynamic graph mode is easy to debug, but it is difficult to execute efficiently.
# 0->static graph, 1->dynamic graph
mode = mindspore.context.get_context('mode')
mindspore.context.set_context(mode=context.GRAPH_MODE or PYNATIVE_MODE) # static or dynamic
device_target = mindspore.context.get_context('device_target')
mindspore.context.set_context(device_target="CPU")
Dataset Preprocessing