mindspore.nn.GraphCell

class mindspore.nn.GraphCell(graph, params_init=None)[source]

Base class for running the graph loaded from a MindIR.

This feature is still under development. Currently GraphCell do not support modifying the structure of the diagram, and can only use data that shape and type are the same as the input when exporting the MindIR.

Parameters
  • graph (FuncGraph) – A compiled graph loaded from MindIR.

  • params_init (dict) – Parameters need to be inited in the graph. The key is the parameter name whose type is str, and the value is a Tensor or Parameter. If the parameter exists in the graph according to the name, update it’s value. If the parameter does not exist, ignore it. Default: None.

Raises
  • TypeError – If the graph is not a FuncGraph.

  • TypeError – If the params_init is not a dict.

  • TypeError – If the key of the params_init is not a str.

  • TypeError – If the value of the params_init is neither a Tensor nor a Parameter.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> import mindspore.nn as nn
>>> from mindspore import Tensor, export, load
>>>
>>> net = nn.Conv2d(1, 1, kernel_size=3, weight_init="ones")
>>> input = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32))
>>> export(net, input, file_name="net", file_format="MINDIR")
>>> graph = load("net.mindir")
>>> net = nn.GraphCell(graph)
>>> output = net(input)
>>> print(output)
[[[[4. 6. 4.]
   [6. 9. 6.]
   [4. 6. 4.]]]]