使用MindSpore Cell的construct报错AttributeError: For 'Cell', the method 'construct' is not defined.
收藏回复举报
使用MindSpore Cell的construct报错AttributeError: For 'Cell', the method 'construct' is not defined.
发表于2024-02-23 08:44:44
0 查看

系统环境

硬件环境(Ascend/GPU/CPU): Ascend/GPU/CPU

MindSpore版本: mindspore=2.0.0

执行模式(PyNative/ Graph):不限

Python版本: Python=3.7

操作系统平台: 不限

报错信息

2.1问题描述

在pytorch迁移MindSpore过程中,错误信息如下

File "main_attention.py", line 88, in <module>

    autofe.fit_attention(args)

  File "/root/FETCH-main/autofe.py", line 251, in fit_attention

    actions, log_probs, m1_output, m2_output, m3_output, action_softmax = self.ppo.choose_action_c(

  File "/root/FETCH-main/feature_engineer/attention_searching/ppo.py", line 48, in choose_action_c

    action_softmax, m1_output, m2_output, m3_output = self.actor_c(input_c, step)

  File "/root/miniconda3/lib/python3.8/site-packages/mindspore/nn/cell.py", line 659, in __call__

    raise AttributeError("For 'Cell', the method 'construct' is not defined.")

AttributeError: For 'Cell', the method 'construct' is not defined.

2.2脚本信息

两段相关的脚本如下:

actions, log_probs, m1_output, m2_output, m3_output, action_softmax = self.ppo.choose_action_c(workers_c[i].states[-1], step, epoch, c_ops, sample_rule)

actions, log_probs, m1_output, m2_output, m3_output, action_softmax = ppo.choose_action_c(state_c, step, epoch, c_ops, sample_rule)

脚本定义如下:

self.actor_c = Actor(args, data_nums, operations_c, d_model, d_k, d_v, d_ff, n_heads, dropout=dropout, enc_load_pth=args.enc_c_pth)

class Actor(nn.Cell):

    def __init__(self, args, data_nums, operations, d_model, d_k, d_v, d_ff, n_heads, dropout=None, enc_load_pth=None):

        super(Actor, self).__init__()

        self.args = args

        self.reduction_dimension = ReductionDimension(data_nums, d_model)

        self.encoder = EncoderLayer(d_model, d_k, d_v, d_ff, n_heads, dropout)

        logging.info(f"Randomly initial encoder")

        if os.path.exists(enc_load_pth):

            self.encoder.load_state_dict(mindspore.load(enc_load_pth))

            logging.info(f"Successfully load encoder, enc_load_pth:{enc_load_pth}")

        self.select_operation = SelectOperations(d_model, operations)

        self.c_nums = len(args.c_columns)

        self.layernorm = nn.LayerNorm(normalized_shape=(data_nums,))

    def forward(self, input, step):

        input_norm = self.layernorm(input)

        data_reduction_dimension = self.reduction_dimension(input_norm)

        data_reduction_dimension = mindspore.ops.where(mindspore.ops.isnan(data_reduction_dimension),

                                               mindspore.ops.full_like(data_reduction_dimension, 0), data_reduction_dimension)

        encoder_output = self.encoder(data_reduction_dimension)

        encoder_output = mindspore.ops.where(mindspore.ops.isnan(encoder_output), mindspore.ops.full_like(encoder_output, 0), encoder_output)

        output = self.select_operation(encoder_output)

        output = mindspore.ops.where(mindspore.ops.isnan(output), mindspore.ops.full_like(output, 0), output)

        operation_softmax = mindspore.ops.softmax(output, dim=-1)

        return operation_softmax, data_reduction_dimension.squeeze(), \

               encoder_output.squeeze(), output

根因分析

******此处由用户填写******

解决方案

******此处由用户填写******

包含文字方案和最终脚本代码

请将正确的脚本打包并上传附件

我要发帖子