# 网络构建

[1]:

import mindspore
from mindspore import nn, ops


## 定义模型类

construct意为神经网络（计算图）构建，相关内容详见计算图

[2]:

class Network(nn.Cell):
def __init__(self):
super().__init__()
self.flatten = nn.Flatten()
self.dense_relu_sequential = nn.SequentialCell(
nn.Dense(28*28, 512),
nn.ReLU(),
nn.Dense(512, 512),
nn.ReLU(),
nn.Dense(512, 10)
)

def construct(self, x):
x = self.flatten(x)
logits = self.dense_relu_sequential(x)
return logits


[3]:

model = Network()
print(model)

[3]:

Network<
(flatten): Flatten<>
(dense_relu_sequential): SequentialCell<
(0): Dense<input_channels=784, output_channels=512, has_bias=True>
(1): ReLU<>
(2): Dense<input_channels=512, output_channels=512, has_bias=True>
(3): ReLU<>
(4): Dense<input_channels=512, output_channels=10, has_bias=True>
>
>


model.construct()方法不可直接调用。

[4]:

X = ops.ones((1, 28, 28), mindspore.float32)
logits = model(X)
print(logits)

[4]:

Tensor(shape=[1, 10], dtype=Float32, value=
[[-5.08734025e-04,  3.39190010e-04,  4.62840870e-03 ... -1.20305456e-03, -5.05689112e-03,  3.99264274e-03]])


[5]:

pred_probab = nn.Softmax(axis=1)(logits)
y_pred = pred_probab.argmax(1)
print(f"Predicted class: {y_pred}")

Predicted class: [4]


## 模型层

[6]:

input_image = ops.ones((3, 28, 28), mindspore.float32)
print(input_image.shape)

[6]:

(3, 28, 28)


### nn.Flatten

[7]:

flatten = nn.Flatten()
flat_image = flatten(input_image)
print(flat_image.shape)

[7]:

(3, 784)


### nn.Dense

nn.Dense为全连接层，其使用权重和偏差对输入进行线性变换。

[8]:

layer1 = nn.Dense(in_channels=28*28, out_channels=20)
hidden1 = layer1(flat_image)
print(hidden1.shape)

[8]:

(3, 20)


### nn.ReLU

nn.ReLU层给网络中加入非线性的激活函数，帮助神经网络学习各种复杂的特征。

[9]:

print(f"Before ReLU: {hidden1}\n\n")
hidden1 = nn.ReLU()(hidden1)
print(f"After ReLU: {hidden1}")

Before ReLU: [[-0.04736331  0.2939465  -0.02713677 -0.30988005 -0.11504349 -0.11661264
0.18007928  0.43213072  0.12091967 -0.17465964  0.53133243  0.12605792
0.01825903  0.01287796  0.17238477 -0.1621131  -0.0080034  -0.24523425
-0.10083733  0.05171938]
[-0.04736331  0.2939465  -0.02713677 -0.30988005 -0.11504349 -0.11661264
0.18007928  0.43213072  0.12091967 -0.17465964  0.53133243  0.12605792
0.01825903  0.01287796  0.17238477 -0.1621131  -0.0080034  -0.24523425
-0.10083733  0.05171938]
[-0.04736331  0.2939465  -0.02713677 -0.30988005 -0.11504349 -0.11661264
0.18007928  0.43213072  0.12091967 -0.17465964  0.53133243  0.12605792
0.01825903  0.01287796  0.17238477 -0.1621131  -0.0080034  -0.24523425
-0.10083733  0.05171938]]

After ReLU: [[0.         0.2939465  0.         0.         0.         0.
0.18007928 0.43213072 0.12091967 0.         0.53133243 0.12605792
0.01825903 0.01287796 0.17238477 0.         0.         0.
0.         0.05171938]
[0.         0.2939465  0.         0.         0.         0.
0.18007928 0.43213072 0.12091967 0.         0.53133243 0.12605792
0.01825903 0.01287796 0.17238477 0.         0.         0.
0.         0.05171938]
[0.         0.2939465  0.         0.         0.         0.
0.18007928 0.43213072 0.12091967 0.         0.53133243 0.12605792
0.01825903 0.01287796 0.17238477 0.         0.         0.
0.         0.05171938]]


### nn.SequentialCell

nn.SequentialCell是一个有序的Cell容器。输入Tensor将按照定义的顺序通过所有Cell。我们可以使用SequentialCell来快速组合构造一个神经网络模型。

[10]:

seq_modules = nn.SequentialCell(
flatten,
layer1,
nn.ReLU(),
nn.Dense(20, 10)
)

logits = seq_modules(input_image)
print(logits.shape)

[10]:

(3, 10)


### nn.Softmax

[11]:

softmax = nn.Softmax(axis=1)
pred_probab = softmax(logits)


## 模型参数

[12]:

print(f"Model structure: {model}\n\n")

for name, param in model.parameters_and_names():
print(f"Layer: {name}\nSize: {param.shape}\nValues : {param[:2]} \n")

Model structure: Network<
(flatten): Flatten<>
(dense_relu_sequential): SequentialCell<
(0): Dense<input_channels=784, output_channels=512, has_bias=True>
(1): ReLU<>
(2): Dense<input_channels=512, output_channels=512, has_bias=True>
(3): ReLU<>
(4): Dense<input_channels=512, output_channels=10, has_bias=True>
>
>

Layer: dense_relu_sequential.0.weight
Size: (512, 784)
Values : [[-0.01491369  0.00353318 -0.00694948 ...  0.01226766 -0.00014423
0.00544263]
[ 0.00212971  0.0019974  -0.00624789 ... -0.01214037  0.00118004
-0.01594325]]

Layer: dense_relu_sequential.0.bias
Size: (512,)
Values : [0. 0.]

Layer: dense_relu_sequential.2.weight
Size: (512, 512)
Values : [[ 0.00565423  0.00354313  0.00637383 ... -0.00352688  0.00262949
0.01157355]
[-0.01284141  0.00657666 -0.01217057 ...  0.00318963  0.00319115
-0.00186801]]

Layer: dense_relu_sequential.2.bias
Size: (512,)
Values : [0. 0.]

Layer: dense_relu_sequential.4.weight
Size: (10, 512)
Values : [[ 0.0087168  -0.00381866 -0.00865665 ... -0.00273731 -0.00391623
0.00612853]
[-0.00593031  0.0008721  -0.0060081  ... -0.00271535 -0.00850481
-0.00820513]]

Layer: dense_relu_sequential.4.bias
Size: (10,)
Values : [0. 0.]