we check how is the network created.
Hopefully this blog can be helpful, so I would try to depicit t more detailed.
This is what your need. Surely you can also use other architectures, the principles are the same,
%%capture captured_output
# 实验环境已经预装了mindspore==2.2.14,如需更换mindspore版本,可更改下面mindspore的版本号
!pip uninstall mindspore -y
!pip install -i https://pypi.mirrors.ustc.edu.cn/simple mindspore==2.2.14
import mindspore
from mindspore import nn, ops
in I show a cell first:
class Network(nn.Cell):
def __init__(self):
super().__init__()
self.flatten = nn.Flatten()
self.dense_relu_sequential = nn.SequentialCell(
nn.Dense(28*28, 512, weight_init="normal",bias_init="zeros"),
nn.Relu(),
nn.Dense(512, 512, weight_init="init",bias_init = "zeros"),
nn.Relu(),
nn.Dense(512, 10, weight_init="normal", bias_init="zeros")
)
def construct(self, x):
x = self.flatten(x)
logits = self.dense_relu_sequential(x)
return logits
in code above. it is just needed to remember 2 item: init and construct.
In fact, init is just tell what the blocks you want look like, and construct just connect them.
For example, 'Dense' means a kind of block(layer) that every neuron is connected to each other, and 'relu' is a acitvation function doing nothing but control some calculations and pass the result to next layer.'Construct', or 'forward' sometimes, means you building a Lego hall by stacking each layer, i.e. you choose 1st floor to be kitchen, 2nd to be auditory... they are connected in someway(just the order you choose.)
other network like resnet, googlenet follows this classical architecture with different blocks and constructions.
we run:
model = Network()
we print(model)
and see some strange thing:
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>
>
>
we do not need to dive into it to check our model, since we just think it like your brain, you have lots of neurons and they connected in all kinds of ways, but we just care the input and output, and how your neuron works leaves for those biologists.
X = ops.ones((1,28,28),mindspore.float32)
logits = model(X)
logits
logits is what we predict,all though we didnot even train the model,
random values. 10 is the output channels that we chosen in :
nn.Dense(512, 10, weight_init="normal",bias_init="zeros")
so what we train is just those weight and bias.
for example, now our bias in each dense layer is zeros, so we can check them:
for name, param in model.parameters_and_names():
print(f"layer:{name}\n Size:{param.shape}\nValues:{param[:2]}\n")
one layer may have weight and bias:
later we train them. If any errors, thanks for pointing out.