当前位置: 首页 > news >正文

《昇思25天学习打卡营第06天|网络构建》

网络构建

神经网络模型由神经网络层和Tensor操作构成

#实验环境已经预装了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, opsclass 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="normal", 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 logitsmodel = Network() #打印模型结构
print(model)
#Output#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>#>#>
X = ops.ones((1, 28, 28), mindspore.float32)
logits = model(X)
# print logits
logits
#Output 
#Tensor(shape=[1, 10], dtype=Float32, value= [[-2.40761833e-03,  2.76332069e-03,  4.36006673e-03 ... -2.03372864e-03,  2.23693671e-04,  5.74092008e-03]])
# 过Softmax获得预测概率
pred_prob = nn.Softmax(axis=1)(logits)
y_pred = pred_prob.argmax(1) #给出预测结果
print(f"Predicted class: {y_pred}")

模型层

#构建3个28*28的图像
input_image = ops.ones((3,28,28), mindspore.float32)
print(input_image.shape)
#Output (3, 28, 28)

nn.Flatten

flatten = nn.Flatten()
flat_image = flatten(input_image)
print(flat_image.shape()
#Output(3, 728) 把图像碾平成一维 满足模型输入要求

nn.dense

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

nn.ReLu

#加入非线性激活函数,增加模型复杂度
hidden1 = nn.ReLU()(hidden1)

nn.Sequential

seq_modules = nn.SequentialCell(flatten,layer1,nn.ReLU(),nn.Dense(20, 10)
)logits = seq_modules(input_image)
print(logits.shape)

nn.Softmax

#将神经网络最后一个全连接层返回的logits值缩放至[0,1], 表示每个类别的概率预测。axis指定的维度数值和为1
softmax = nn.Softmax(axis=1)
pred_probab = softmax(logits)

模型参数

  • 通过model.parameters_and_names()获取
for name, param in model.parameters_and_names():print(f"Layer: {name}\nSize: {param.shape}\nValues : {param[:2]} \n")#Layer: dense_relu_sequential.0.weight
#Size: (512, 784)
#Values : [[ 0.01388695 -0.00604919 -0.00993734 ...  0.00366266  0.00065028 0.00334988] [ 0.01483851 -0.00953137  0.01422684 ...  0.01928892  0.00024049 -0.00365605]] #Layer: dense_relu_sequential.0.bias
#Size: (512,)
#Values : [0. 0.] #Layer: dense_relu_sequential.2.weight
#Size: (512, 512)
#Values : [[ 0.00495729 -0.01029267 -0.00672846 ...  0.02216997 -0.00423945 0.00603404] [-0.02003012  0.00643059  0.0076612  ... -0.0097923  -0.01475079 0.00485153]] #Layer: dense_relu_sequential.2.bias
#Size: (512,)
#Values : [0. 0.] #Layer: dense_relu_sequential.4.weight
#Size: (10, 512)
#Values : [[-0.00212924  0.0067424   0.00244794 ... -0.00193389 -0.01660973 -0.00875264] [-0.01889533  0.01057486 -0.0233639  ... -0.00306869 -0.007126 -0.00609088]] #Layer: dense_relu_sequential.4.bias
#Size: (10,)
#Values : [0. 0.]
http://www.lryc.cn/news/390747.html

相关文章:

  • 【链表】- 两两交换链表中的节点
  • java设计模式(四)——抽象工厂模式
  • 动物检测yolo格式数据集(水牛 、大象 、犀牛 、斑马四类)
  • 昇思25天学习打卡营第05天 | 数据变换 Transforms
  • Springboot+MySQL 公寓报修管理系统源码
  • jenkins 发布服务到linux服务器
  • Appium+python自动化(三十九)-Appium自动化测试框架综合实践 - 代码实现(超详解)
  • 防止跨站脚本攻击XSS之Antisamy
  • Python爬虫实战案例——王者荣耀皮肤抓取
  • PyTorch计算机视觉实战:目标检测、图像处理与深度学习
  • 4D 生物打印:将时间维度融入,打造个性化动态组织
  • 银行清算业务功能测试解析
  • CVE-2024-6387漏洞预警:尽快升级OpenSSH
  • 学习整理在php中使用PHPExcel读取excel表列数大于Z时读取不到的解决方案
  • python sklearn机械学习-数据预处理
  • 搜索引擎常用语法
  • 华为智能驾驶方案剖析
  • DDR3(一)
  • Eureka与Spring Cloud Bus的协同:打造智能服务发现新篇章
  • Kafka入门到精通(三)-Kafka
  • 高校教师教学质量评估系统-计算机毕业设计源码03344
  • 币界网讯,预计以太坊现货 ETF 将于 7 月中旬推出
  • 【FFmpeg】avio_open2函数
  • 技术成神之路:设计模式(二)建造者模式
  • 基于Springboot+Vue+mysql仓库管理系统仓库进销存管理系统
  • 爬虫scrapy库精简使用大全
  • Qt - 如何在新线程 (QThread)中使用一个进程 (QProcess)?
  • Qt绘制多线段
  • 去中心化革命:探索区块链技术的前沿
  • 2024年湖南省各市科小申报时间(科技型中小企业申报流程、条件、好处)新政