网站 关键字 标签,免费自助建网站软件,注册公司场地有什么要求,宁波是哪个省一、简介#xff1a;
神经网络模型是由神经网络层和Tensor操作构成的#xff0c;mindspore.nn提供了常见神经网络层的实现#xff0c;在MindSpore中#xff0c;Cell类是构建所有网络的基类#xff08;这个类和pytorch中的modul类是一样的作用#xff09;#xff0c;也是… 一、简介
神经网络模型是由神经网络层和Tensor操作构成的mindspore.nn提供了常见神经网络层的实现在MindSpore中Cell类是构建所有网络的基类这个类和pytorch中的modul类是一样的作用也是网络的基本单元。一个神经网络模型表示为一个Cell它由不同的子Cell构成。使用这样的嵌套结构可以简单地使用面向对象编程的思维对神经网络结构进行构建和管理。
二、环境准备
import mindspore
import time
from mindspore import nn, ops
没有下载mindspore的宝子还是回看我的昇思25天学习打卡营第1天|快速入门-CSDN博客先下载好再进行下面的操作。
三、神经网络搭建
1、定义模型类
我们首先要继承nn.Cell类并再__init__方法中进行子Cell的实例化和管理并再construct方法和pytorch中的forward方法一致中实现前向计算
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_initnormal, bias_initzeros),nn.ReLU(),nn.Dense(512, 512, weight_initnormal, bias_initzeros),nn.ReLU(),nn.Dense(512, 10, weight_initnormal, bias_initzeros))def construct(self, x):x self.flatten(x)logits self.dense_relu_sequential(x)return logits# 实例化并打印
model Network()
print(model)
print(time.strftime(%Y-%m-%d %H:%M:%S, time.localtime(time.time())), VertexGeek) ①self.flatten nn.Flatten()创建一个Flatten层并将其作为类的属性。Flatten层的作用是将输入的数据“压平”即不管输入数据的原始形状如何输出都将是沿着特定维度的连续数组。
② self.dense_relu_sequential nn.SequentialCell(...)创建一个SequentialCell它是一种特殊的Cell可以顺序地执行其中包含的多个层。这个SequentialCell包含了三个全连接层Dense每个全连接层后面跟着一个ReLU激活函数层除了最后一个全连接层 第一个nn.Dense(28*28, 512, weight_initnormal, bias_initzeros)这是一个全连接层它接受28*28784个输入并产生512个输出。权重weight_init和偏置bias_init分别使用正态分布和零值进行初始化。 nn.ReLU()ReLU激活函数其数学表达式为f(x) max(0, x)即负值输出为零正值保持不变。 接下来的两个nn.Dense与对应的nn.ReLU层与第一个类似它们分别接收512个输入并再次输出512个值以及最终输出10个值这可能对应于10个类别。 我们构造一个数据并使用softmax预测其概率
X ops.ones((1, 28, 28), mindspore.float32)
logits model(X)
# print logits
print(logits)pred_probab nn.Softmax(axis1)(logits)
y_pred pred_probab.argmax(1)
print(fPredicted class: {y_pred})
print(time.strftime(%Y-%m-%d %H:%M:%S, time.localtime(time.time())), VertexGeek) 2、模型层详解
1nn.Flatten: nn.Flantten方法用于将输入数据“压平”以便后续处理
input_image ops.ones((3, 28, 28), mindspore.float32)
print(input_image.shape)flatten nn.Flatten()
flat_image flatten(input_image)
print(flat_image.shape)print(time.strftime(%Y-%m-%d %H:%M:%S, time.localtime(time.time())), VertexGeek) 2nn.Dense nn.Dense层作为全连接层用于对输入的数据进行线性变换和处理
layer1 nn.Dense(in_channels28*28, out_channels20)
hidden1 layer1(flat_image)
print(hidden1.shape)print(time.strftime(%Y-%m-%d %H:%M:%S, time.localtime(time.time())), VertexGeek) 3nn.Relu
nn.Relu是本次实验中使用的激活函数用于对神经网络的权重进行处理以缓解欠拟合和过拟合的发生常见的激活函数处了Relu还有Sigmoid, Tanh等
print(fBefore ReLU: {hidden1}\n\n)
hidden1 nn.ReLU()(hidden1)
print(fAfter ReLU: {hidden1})print(time.strftime(%Y-%m-%d %H:%M:%S, time.localtime(time.time())), VertexGeek) 4nn.SequentialCell:
nn.SequentialCell和pytorch中的nn.Sequential的作用一样用于存放dense全连接层和激活函数层的组合以方便在前向计算中使用
seq_modules nn.SequentialCell(flatten,layer1,nn.ReLU(),nn.Dense(20, 10)
)logits seq_modules(input_image)
print(logits.shape)print(time.strftime(%Y-%m-%d %H:%M:%S, time.localtime(time.time())), VertexGeek) 5nn.Softmax: nn.softmax方法将神经网络最后一个全连接层返回的logits的值缩放为[0, 1]表示每个类别的预测概率。axis指定的维度数值和为1。
softmax nn.Softmax(axis1)
pred_probab softmax(logits)
print(pred_probab)
# argmax函数返回指定维度上最大值的索引
y_pred pred_probab.argmax(1)
print(fPredicted class: {y_pred})
print(time.strftime(%Y-%m-%d %H:%M:%S, time.localtime(time.time())), VertexGeek) 3、模型参数
网络内部神经网络层具有权重参数和偏置参数如nn.Dense这些参数会在训练过程中不断进行优化可通过 model.parameters_and_names() 来获取参数名及对应的参数详情。
print(fModel structure: {model}\n\n)for name, param in model.parameters_and_names():print(fLayer: {name}\nSize: {param.shape}\nValues : {param[:2]} \n)print(time.strftime(%Y-%m-%d %H:%M:%S, time.localtime(time.time())), VertexGeek)