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

打卡

import time
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'zozo')

一、介绍

神经网络模型是由神经网络层和Tensor操作构成的,mindspore.nn提供了常见神经网络层的实现,在MindSpore中,Cell类是构建所有网络的基类,也是网络的基本单元。一个神经网络模型表示为一个Cell,它由不同的子Cell构成。使用这样的嵌套结构,可以简单地使用面向对象编程的思维,对神经网络结构进行构建和管理。

import mindspore
from mindspore import nn, ops

二、定义模型类

当我们定义神经网络时,可以继承nn.Cell类,在__init__方法中进行子Cell的实例化和状态管理,在construct方法中实现Tensor操作。

construct意为神经网络(计算图)构建,相关内容详见使用静态图加速

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="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 logits

构建完成后,实例化Network对象,并查看其结构。

model = Network()
print(model)

我们构造一个输入数据,直接调用模型,可以获得一个十维的Tensor输出,其包含每个类别的原始预测值。

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

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

在此基础上,我们通过一个nn.Softmax层实例来获得预测概率。

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

三、模型层

本节分解上节构造的神经网络模型中的每一层。首先构造一个shape为(3, 28, 28)的随机数据(3个28x28的图像),依次通过每一个神经网络层来观察其效果。

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

1.nn.Flatten

实例化nn.Flatten层,将28x28的2D张量转换为784大小的连续数组。

flatten = nn.Flatten()
flat_image = flatten(input_image)
print(flat_image.shape)
(3, 784)

2.nn.Dense

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

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

3.nn.ReLU

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

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

4.nn.SequentialCell

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

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

logits = seq_modules(input_image)
print(logits.shape)
(3, 10)

5.nn.Softmax

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

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

四、模型参数

网络内部神经网络层具有权重参数和偏置参数(如nn.Dense),这些参数会在训练过程中不断进行优化,可通过 model.parameters_and_names() 来获取参数名及对应的参数详情。

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")

更多内置神经网络层详见mindspore.nn API

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的微信小程序制作学习计划打卡记录页面的代码示例,其中包含了前几学习时间: ``` <view class="container"> <view class="header"> <text>学习计划打卡记录</text> </view> <view class="content"> <view class="card"> <view class="card-header"> <text>今</text> <text>2021-07-01</text> </view> <view class="card-body"> <view class="card-row"> <text>学习时间:</text> <input type="number" placeholder="请输入学习时间(分钟)"/> </view> </view> </view> <view class="card"> <view class="card-header"> <text>昨</text> <text>2021-06-30</text> </view> <view class="card-body"> <view class="card-row"> <text>学习时间:</text> <input type="number" placeholder="请输入学习时间(分钟)"/> </view> <view class="card-row"> <text>已学习:</text> <text>120分钟</text> </view> </view> </view> <view class="card"> <view class="card-header"> <text>前</text> <text>2021-06-29</text> </view> <view class="card-body"> <view class="card-row"> <text>学习时间:</text> <input type="number" placeholder="请输入学习时间(分钟)"/> </view> <view class="card-row"> <text>已学习:</text> <text>90分钟</text> </view> </view> </view> </view> </view> ``` 在上面的代码中,我们使用了三个 `card` 元素来展示前几打卡记录。每个 `card` 元素都包含一个 `card-header` 元素和一个 `card-body` 元素。`card-header` 元素中包含了日期信息,而 `card-body` 元素中包含了输入框和已学习时间的显示。 在实际开发中,你需要将上面的代码替换成你自己的样式和数据。同时,你还需要编写处理用户输入和计算已学习时间的逻辑代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值