mindspore-ai / mindspore

MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.
https://gitee.com/mindspore/mindspore
Apache License 2.0
4.3k stars 711 forks source link

Something didn't work for nn.SequentialCell #109

Closed leonwanghui closed 3 years ago

leonwanghui commented 3 years ago

Environment

Hardware Environment(Ascend/GPU/CPU):

/device cpu

Software Environment:

Describe the current behavior

When I tried to use nn.SequentialCell class to construct the network using the below code:

import numpy as np
import mindspore.nn as nn
import mindspore.context as context
from mindspore import Tensor

if __name__ == "__main__":
    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")

    net = nn.SequentialCell([
        nn.Conv2d(1, 6, 5, pad_mode='valid', weight_init="ones"),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2, stride=2),
        nn.Conv2d(6, 16, 5, pad_mode='valid', weight_init="ones"),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2, stride=2)
    ])

    x = np.ones([1, 1, 28, 28]).astype(np.float32)
    print(net(Tensor(x)).asnumpy())

Some errors occurred:

Traceback (most recent call last):
  File "sequential.py", line 20, in <module>
    print(net(Tensor(x)).asnumpy())
  File "D:\Applications\Anaconda3\lib\site-packages\mindspore\nn\cell.py", line 322, in __call__
    out = self.compile_and_run(*inputs)
  File "D:\Applications\Anaconda3\lib\site-packages\mindspore\nn\cell.py", line 578, in compile_and_run
    self.compile(*inputs)
  File "D:\Applications\Anaconda3\lib\site-packages\mindspore\nn\cell.py", line 565, in compile
    _executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)
  File "D:\Applications\Anaconda3\lib\site-packages\mindspore\common\api.py", line 493, in compile
    obj.check_names()
  File "D:\Applications\Anaconda3\lib\site-packages\mindspore\nn\cell.py", line 825, in check_names
    format(value, param, param.name))
ValueError: The value of 3.weight is Parameter (name=weight), its name 'weight' already exists.

But if I removed some operators like this:

    net = nn.SequentialCell([
        nn.Conv2d(1, 6, 5, pad_mode='valid', weight_init="ones"),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2, stride=2)
    ])

Everything worked well.

Describe the expected behavior

Please take a look if the nn.SequentialCell doesn't support two operators with the same names.

Steps to reproduce the issue

Related log / screenshot

Special notes for this issue

Caozhou1995 commented 3 years ago

@leonwanghui Thank you for your question.

The reason for this problem is that under the MindSprore Parameter naming mechanism, the use of SequentialCell will cause the internal parameter name unable to be added index prefix. In graph mode, it will check whether the parameter has the same name. It is recommended that you switch to pynative mode to avoid this problem.

The problem has been recorded and will be fixed later. Please keep your attention.

leonwanghui commented 3 years ago

@Caozhou1995 Got it, so I will keep this issue open until the bug fixed, thanks!

Caozhou1995 commented 3 years ago

@leonwanghui Thank u for your attention! The bug has been fixed and u can retry it.

leonwanghui commented 3 years ago

Got it, thx!