mindspore.nn.GraphCell

class mindspore.nn.GraphCell(graph, params_init=None, obf_password=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.

  • obf_password (int) – The password used for dynamic obfuscation. “dynamic obfuscation” is used for model protection, which can refer to mindspore.obfuscate_model(). If the input ‘graph’ is a func_graph loaded from a mindir file obfuscated in password mode, then obf_password should be provided. obf_password should be larger than zero and less or equal than int_64 (9223372036854775807). 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 as ms
>>> import mindspore.nn as nn
>>> from mindspore import Tensor
>>>
>>> net = nn.Conv2d(1, 1, kernel_size=3, weight_init="ones")
>>> input = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32))
>>> ms.export(net, input, file_name="net", file_format="MINDIR")
>>> graph = ms.load("net.mindir")
>>> net = nn.GraphCell(graph)
>>> output = net(input)
>>> print(output)
[[[[4. 6. 4.]
   [6. 9. 6.]
   [4. 6. 4.]]]]