MindSpore 建立神经网络

 代码原地址:

https://www.mindspore.cn/tutorial/zh-CN/r1.2/model.html

 

 

 

 

建立神经网络:

 

复制代码
import mindspore.nn as nn

class LeNet5(nn.Cell):
    """
    Lenet网络结构
    """
    def __init__(self, num_class=10, num_channel=1):
        super(LeNet5, self).__init__()
        # 定义所需要的运算
        self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')
        self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
        self.fc1 = nn.Dense(16 * 5 * 5, 120)
        self.fc2 = nn.Dense(120, 84)
        self.fc3 = nn.Dense(84, num_class)
        self.relu = nn.ReLU()
        self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
        self.flatten = nn.Flatten()

    def construct(self, x):
        # 使用定义好的运算构建前向网络
        x = self.conv1(x)
        x = self.relu(x)
        x = self.max_pool2d(x)
        x = self.conv2(x)
        x = self.relu(x)
        x = self.max_pool2d(x)
        x = self.flatten(x)
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        x = self.relu(x)
        x = self.fc3(x)
        return x

model = LeNet5()

for m in model.parameters_and_names():
    print(m)
复制代码

 

 

 

 

 

 

 

 

 

复制代码
import mindspore
from mindspore import Tensor
import mindspore.nn as nn

import numpy as np

conv2d = nn.Conv2d(1, 6, 5, has_bias=False, weight_init='normal', pad_mode='valid')
input_x = Tensor(np.ones([1, 1, 32, 32]), mindspore.float32)

print(conv2d(input_x).shape)
复制代码

 

 

 

 

 

 

 

 

 

复制代码
import mindspore
from mindspore import Tensor
import mindspore.nn as nn

import numpy as np

relu = nn.ReLU()
input_x = Tensor(np.array([-1, 2, -3, 2, -1]), mindspore.float16)
output = relu(input_x)

print(output)
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

复制代码
import mindspore
from mindspore import Tensor
import mindspore.nn as nn

import numpy as np

max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
input_x = Tensor(np.ones([1, 6, 28, 28]), mindspore.float32)

print(max_pool2d(input_x).shape)
复制代码

 

 

 

 

 

 

 

复制代码
import mindspore
from mindspore import Tensor
import mindspore.nn as nn

import numpy as np

flatten = nn.Flatten()
input_x = Tensor(np.ones([1, 16, 5, 5]), mindspore.float32)
output = flatten(input_x)

print(output.shape)
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

复制代码
import mindspore
from mindspore import Tensor
import mindspore.nn as nn

import numpy as np

dense = nn.Dense(400, 120, weight_init='normal')
input_x = Tensor(np.ones([1, 400]), mindspore.float32)
output = dense(input_x)

print(output.shape)
复制代码

 

 

posted on   Angry_Panda  阅读(280)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
历史上的今天:
2020-07-05 《Python数据可视化之matplotlib实践》 源码 第三篇 演练 第八章
2020-07-05 《Python数据可视化之matplotlib实践》 源码 第二篇 精进 第七章

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示