220 Star 933 Fork 688

GVPMindSpore/mindscience

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

ENGLISH | 简体中文

示例网络

SciAI基础框架由若干基础模块构成,涵盖有神经网络搭建、训练、验证以及其他辅助函数等。下面的一个示例简要展示了SciAI框架的功能。

网络搭建

使用SciAI基础框架创建神经网络的原理与使用MindSpore构建网络一致,但过程将会十分简便。 如下示例代码创建了一个输入维度为2,输出维度为1,包含两层维度为5的中间层的神经网络。

from sciai.architecture import MLP

example_net = MLP(layers=[2, 5, 5, 1], weight_init=XavierTruncNormal(), bias_init='zeros', activation="tanh")

MLP将默认使用正态分布随机生成网络权重,偏差bias默认为0,激活函数默认为tanhMLP同时接受多样化的初始化方式和MindSpore提供的所有激活函数,您可在模型库中自行探索。

损失函数定义

损失函数定义为Cell的子类, 并将损失的计算方法写在方法construct中。

from mindspore import nn
from sciai.architecture import MSE

class ExampleLoss(nn.Cell):
    def __init__(self, network):
        super().__init__()
        self.network = network
        self.mse = MSE()

    def construct(self, x, y_true):
        y_predict = self.network(x)
        return self.mse(y_predict - y_true)

example_loss = ExampleLoss(example_net)

此时,通过直接调用example_loss,并将输入x与真实值y_true作为参数,便可计算得到当前example_net预测的损失。

网络训练

得到损失函数后,我们即可使用SciAI框架中已封装好的训练类,使用数据集进行训练,代码如下。

from mindspore import nn
from sciai.common import TrainCellWithCallBack
from sciai.context import init_project

# Get the correct platform automatically and set to GRAPH_MODE by default.
init_project()

example_optimizer = nn.Adam(example_loss.trainable_params())
example_trainer = TrainCellWithCallBack(example_loss, example_optimizer, loss_interval=100, time_interval=100)
for _ in range(num_iters):
    example_trainer(x_train, y_true)

在训练结束并且损失收敛时,通过调用y = example_net(x)即可得到x处的预测值y./example_net.py文件有该示例网络的完整训练流程。此示例网络仅仅展示了SciAI框架的基本能力和网络训练的基本流程, 通过此基础示例,您可以更好地理解在下面的高频模型库中,各个模型是如何使用SciAI和MindSpore实现训练与推理。

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mindspore/mindscience.git
git@gitee.com:mindspore/mindscience.git
mindspore
mindscience
mindscience
master

搜索帮助