mindspore网络构建调用numpy算子失败

我在进行Pytorch网络向mindspore迁移时,发现mindspore里缺少一个einsum算子。经过查阅资料发现,numpy中有类似的api。所以就想在网络构建时调用numpy中的

einsum算子替代。

我看mindspore文档中说是,在construct函数中不能调用第三方算子,于是我先在init函数中初始化了一个np.einsum对象,然后在construct函数中调用该算子。

class SpatialGraphConv(nn.Cell):
    def __init__(self, in_channels, out_channels, max_graph_distance):
        super(SpatialGraphConv, self).__init__()

        # spatial class number (distance = 0 for class 0, distance = 1 for class 1, ...)
        self.s_kernel_size = max_graph_distance + 1

        # weights of different spatial classes
        self.gcn = nn.Conv2d(in_channels=in_channels, out_channels=out_channels*self.s_kernel_size,
                             kernel_size=1, has_bias=True, pad_mode='valid')
        self.einsum = np.einsum

    def construct(self, x, A):
        # numbers in same class have same weight
        x = self.gcn(x)

        # divide nodes into different classes
        n, kc, t, v = x.shape()
        x = x.view(n, self.s_kernel_size, kc//self.s_kernel_size, t, v)

        x_numpy = self.einsum('nkctv,kvw->nctw', (x.asnumpy(), A[:self.s_kernel_size].asnumpy()))
        x = ms.Tensor(x_numpy, dtype=ms.float32)

        return x

但是我在尝试进行模型训练时报错。

Traceback (most recent call last):
  File "train.py", line 42, in 
    model.train(10, train_ds, callbacks=[ckpoint, LossMonitor(0.01, 1875)])
  File "D:\anaconda\envs\gaitRecognition\lib\site-packages\mindspore\train\model.py", line 1044, in train
    self._train(epoch,
  File "D:\anaconda\envs\gaitRecognition\lib\site-packages\mindspore\train\model.py", line 98, in wrapper
    func(self, *args, **kwargs)
  File "D:\anaconda\envs\gaitRecognition\lib\site-packages\mindspore\train\model.py", line 620, in _train
    self._train_process(epoch, train_dataset, list_callback, cb_params, initial_epoch, valid_infos)
  File "D:\anaconda\envs\gaitRecognition\lib\site-packages\mindspore\train\model.py", line 907, in _train_process
    outputs = self._train_network(*next_element)
  File "D:\anaconda\envs\gaitRecognition\lib\site-packages\mindspore\nn\cell.py", line 578, in __call__
    out = self.compile_and_run(*args)
  File "D:\anaconda\envs\gaitRecognition\lib\site-packages\mindspore\nn\cell.py", line 965, in compile_and_run
    self.compile(*inputs)
  File "D:\anaconda\envs\gaitRecognition\lib\site-packages\mindspore\nn\cell.py", line 937, in compile
    _cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode,
  File "D:\anaconda\envs\gaitRecognition\lib\site-packages\mindspore\common\api.py", line 1137, in compile
    result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())
NameError: The name 'valid_einsum_kwargs' is not defined, or not supported in graph mode.

----------------------------------------------------
- C++ Call Stack: (For framework developers)
----------------------------------------------------
mindspore\ccsrc\pipeline\jit\parse\function_block.cc:308 HandleBuiltinNamespaceInfo

****************************************************解答*****************************************************

在MindSpore的1.8版本中已经支持了Einsum算子了。可访问下面超链接查看。如果当前使用的版本不支持,建议更新到1.8版本试试

mindspore.ops.Einsum

你可能感兴趣的:(深度学习,numpy,python,深度学习)