您好,欢迎来到易妖游戏网。
搜索
您的当前位置:首页昇思25天学习打卡营第9天|使用静态图加速

昇思25天学习打卡营第9天|使用静态图加速

来源:易妖游戏网

背景介绍

背景介绍
AI编译框架分为两种运行模式,分别是动态图模式以及静态图模式。MindSpore默认情况下是以动态图模式运行,但也支持手工切换为静态图模式。


一、动态图模式

动态图的特点是计算图的构建和计算同时发生(Define by run),其符合Python的解释执行方式,在计算图中定义一个Tensor时,其值就已经被计算且确定,因此在调试模型时较为方便,能够实时得到中间结果的值,但由于所有节点都需要被保存,导致难以对整个计算图进行优化。

在MindSpore中,动态图模式又被称为PyNative模式。由于动态图的解释执行特性,在脚本开发和网络流程调试过程中,推荐使用动态图模式进行调试。 如需要手动控制框架采用PyNative模式,可以通过以下代码进行网络构建:
代码如下:

import numpy as np
import mindspore as ms
from mindspore import nn, Tensor
ms.set_context(mode=ms.PYNATIVE_MODE)  # 使用set_context进行动态图模式的配置

class Network(nn.Cell): #创建网络
    def __init__(self):
        super().__init__()
        self.flatten = nn.Flatten()
        self.dense_relu_sequential = nn.SequentialCell(
            nn.Dense(28*28, 512),
            nn.ReLU(),
            nn.Dense(512, 512),
            nn.ReLU(),
            nn.Dense(512, 10)
        )

    def construct(self, x):
        x = self.flatten(x)
        logits = self.dense_relu_sequential(x)
        return logits

model = Network()
input = Tensor(np.ones([, 1, 28, 28]).astype(np.float32))
output = model(input)
print(output)

输出为:

[[-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]
 [-0.02603745  0.06676177 -0.03676818 -0.05512222  0.00681252  0.01841582
  -0.15437548  0.00288332  0.05265238  0.12307557]]

二.静态图模式

相较于动态图而言,静态图的特点是将计算图的构建和实际计算分开(Define and run)。有关静态图模式的运行原理,可以参考静态图语法支持。

在MindSpore中,静态图模式又被称为Graph模式,在Graph模式下,基于图优化、计算图整图下沉等技术,编译器可以针对图进行全局的优化,获得较好的性能,因此比较适合网络固定且需要高性能的场景。

如需要手动控制框架采用静态图模式,可以通过以下代码进行网络构建:
代码如下:

import numpy as np
import mindspore as ms
from mindspore import nn, Tensor
ms.set_context(mode=ms.GRAPH_MODE)  # 使用set_context进行运行静态图模式的配置

class Network(nn.Cell):
    def __init__(self):
        super().__init__()
        self.flatten = nn.Flatten()
        self.dense_relu_sequential = nn.SequentialCell(
            nn.Dense(28*28, 512),
            nn.ReLU(),
            nn.Dense(512, 512),
            nn.ReLU(),
            nn.Dense(512, 10)
        )

    def construct(self, x):
        x = self.flatten(x)
        logits = self.dense_relu_sequential(x)
        return logits

model = Network()
input = Tensor(np.ones([, 1, 28, 28]).astype(np.float32))
output = model(input)
print(output)

输出为:

[[ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]
 [ 0.04487463  0.05917881  0.09132607 -0.14080067 -0.0950603   0.08488107
  -0.15471692  0.05267194 -0.07524846  0.032702]]

三.静态图模式的使用场景

MindSpore编译器重点面向Tensor数据的计算以及其微分处理。因此使用MindSpore API以及基于Tensor对象的操作更适合使用静态图编译优化。其他操作虽然可以部分入图编译,但实际优化作用有限。另外,静态图模式先编译后执行的模式导致其存在编译耗时。因此,如果函数无需反复执行,那么使用静态图加速也可能没有价值。

静态图模式开启方式
通常情况下,由于动态图的灵活性,我们会选择使用PyNative模式来进行自由的神经网络构建,以实现模型的创新和优化。但是当需要进行性能加速时,我们需要对神经网络部分或整体进行加速。MindSpore提供了两种切换为图模式的方式,分别是基于装饰器的开启方式以及基于全局context的开启方式。

1.基于装饰器的开启方式

MindSpore提供了jit装饰器,可以通过修饰Python函数或者Python类的成员函数使其被编译成计算图,通过图优化等技术提高运行速度。此时我们可以简单的对想要进行性能优化的模块进行图编译加速,而模型其他部分,仍旧使用解释执行方式,不丢失动态图的灵活性。无论全局context是设置成静态图模式还是动态图模式,被jit修饰的部分始终会以静态图模式进行运行。

在需要对Tensor的某些运算进行编译加速时,可以在其定义的函数上使用jit修饰器,在调用该函数时,该模块自动被编译为静态图。需要注意的是,jit装饰器只能用来修饰函数,无法对类进行修饰。jit的使用示例如下:

import numpy as np
import mindspore as ms
from mindspore import nn, Tensor

class Network(nn.Cell):
    def __init__(self):
        super().__init__()
        self.flatten = nn.Flatten()
        self.dense_relu_sequential = nn.SequentialCell(
            nn.Dense(28*28, 512),
            nn.ReLU(),
            nn.Dense(512, 512),
            nn.ReLU(),
            nn.Dense(512, 10)
        )

    def construct(self, x):
        x = self.flatten(x)
        logits = self.dense_relu_sequential(x)
        return logits

input = Tensor(np.ones([, 1, 28, 28]).astype(np.float32))

@ms.jit  # 使用ms.jit装饰器,使被装饰的函数以静态图模式运行
def run(x):
    model = Network()
    return model(x)

output = run(input)
print(output)

输出为:

[[-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]
 [-0.0530697   0.06060634  0.00633216 -0.19853005  0.05003442 -0.02622198
   0.09134673  0.073167 -0.02446126  0.00113623]]

除使用修饰器外,也可使用函数变换方式调用jit方法,示例如下:

import numpy as np
import mindspore as ms
from mindspore import nn, Tensor

class Network(nn.Cell):
    def __init__(self):
        super().__init__()
        self.flatten = nn.Flatten()
        self.dense_relu_sequential = nn.SequentialCell(
            nn.Dense(28*28, 512),
            nn.ReLU(),
            nn.Dense(512, 512),
            nn.ReLU(),
            nn.Dense(512, 10)
        )

    def construct(self, x):
        x = self.flatten(x)
        logits = self.dense_relu_sequential(x)
        return logits

input = Tensor(np.ones([, 1, 28, 28]).astype(np.float32))

def run(x):
    model = Network()
    return model(x)

run_with_jit = ms.jit(run)  # 通过调用jit将函数转换为以静态图方式执行
output = run(input)
print(output)

输出为:

[[-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]
 [-0.09300444  0.1777347  -0.09692062 -0.21035613 -0.11580152 -0.018455
  -0.04511954  0.00248753  0.00139829 -0.1870872 ]]

当我们需要对神经网络的某部分进行加速时,可以直接在construct方法上使用jit修饰器,在调用实例化对象时,该模块自动被编译为静态图。示例如下:

import numpy as np
import mindspore as ms
from mindspore import nn, Tensor

class Network(nn.Cell):
    def __init__(self):
        super().__init__()
        self.flatten = nn.Flatten()
        self.dense_relu_sequential = nn.SequentialCell(
            nn.Dense(28*28, 512),
            nn.ReLU(),
            nn.Dense(512, 512),
            nn.ReLU(),
            nn.Dense(512, 10)
        )

    @ms.jit  # 使用ms.jit装饰器,使被装饰的函数以静态图模式运行
    def construct(self, x):
        x = self.flatten(x)
        logits = self.dense_relu_sequential(x)
        return logits

input = Tensor(np.ones([, 1, 28, 28]).astype(np.float32))
model = Network()
output = model(input)
print(output)

2.基于context的开启方式

context模式是一种全局的设置模式。代码示例如下:

import numpy as np
import mindspore as ms
from mindspore import nn, Tensor
ms.set_context(mode=ms.GRAPH_MODE)  # 使用set_context进行运行静态图模式的配置

class Network(nn.Cell):
    def __init__(self):
        super().__init__()
        self.flatten = nn.Flatten()
        self.dense_relu_sequential = nn.SequentialCell(
            nn.Dense(28*28, 512),
            nn.ReLU(),
            nn.Dense(512, 512),
            nn.ReLU(),
            nn.Dense(512, 10)
        )

    def construct(self, x):
        x = self.flatten(x)
        logits = self.dense_relu_sequential(x)
        return logits

model = Network()
input = Tensor(np.ones([, 1, 28, 28]).astype(np.float32))
output = model(input)
print(output)

总结与感想

今天主要学习了静态图和动态图的区别。动态图的特点是计算图的构建和计算同时发生,因此在调试模型时较为方便,能够实时得到中间结果的值,但由于所有节点都需要被保存,导致难以对整个计算图进行优化。而静态图相较于动态图而言,静态图的特点是将计算图的构建和实际计算分开。静态图模式又被称为Graph模式,在Graph模式下,基于图优化、计算图整图下沉等技术,编译器可以针对图进行全局的优化,获得较好的性能,因此比较适合网络固定且需要高性能的场景。同时,我还学习了两种切换静态图的方法,分别是基于装饰器的开启方式以及基于全局context的开启方式。期待进一步的学习。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- vipyiyao.com 版权所有 湘ICP备2023022495号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务