MindSpore报错ValueError:`padding_idx` in `Embedding超出范围的报错

1 报错描述1.1 系统环境Hardware Environment(Ascend/GPU/CPU): AscendSoftware Environment:– MindSpore version (source or binary): 1.8.0– Python version (e.g., Python 3.7.5): 3.7.6– OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic– GCC/Compiler version (if compiled from source):1.2 基本信息1.2.1 脚本训练脚本是通过构建Embedding的单算子网络,完成嵌入层操作。脚本如下: 01 class Net(nn.Cell):
02 def __init__(self, vocab_size, embedding_size, use_one_hot, padding_idx=None):
03 super(Net, self).__init__()
04 self.op = nn.Embedding(vocab_size=vocab_size, embedding_size=embedding_size, use_one_hot=use_one_hot, padding_idx=padding_idx)
05
06 def construct(self, x):
07 output = self.op(x)
08 return output
09
10 input = Tensor(np.ones([8, 128]), mindspore.int32)
11 vocab_size = 2000
12 embedding_size = 768
13 use_one_hot = True
14 example = Net(vocab_size=vocab_size, embedding_size=embedding_size, use_one_hot=use_one_hot, padding_idx=10000)
15 output = example(input)
16 print("Output:", output.shape)
1.2.2 报错这里报错信息如下:Traceback (most recent call last):
File "C:/Users/l30026544/PycharmProjects/q2_map/new/I3MRK3.py", line 26, in

example = Net(vocab_size=vocab_size, embedding_size=embedding_size, use_one_hot=use_one_hot, padding_idx=10000)

File "C:/Users/l30026544/PycharmProjects/q2_map/new/I3MRK3.py", line 12, in init

self.op = nn.Embedding(vocab_size=vocab_size, embedding_size=embedding_size, use_one_hot=use_one_hot, padding_idx=padding_idx)

File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\layer\embedding.py", line 111, in init

self.padding_idx = validator.check_int_range(padding_idx, 0, vocab_size, Rel.INC_BOTH,

File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\_checkparam.py", line 413, in check_int_range

return check_number_range(arg_value, lower_limit, upper_limit, rel, int, arg_name, prim_name)

File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\_checkparam.py", line 209, in check_number_range

raise ValueError("{} {} should be in range of {}, but got {} with type `{}`.".format(

ValueError: padding_idx in Embedding should be in range of [0, 2000], but got 10000 with type int.

原因分析我们看报错信息,在ValueError中,写到padding_idx in Embedding should be in range of [0, 2000], but got 10000 with type int.,意思是Embedding 算子中的padding_idx’的值需要在0到2000之间, 但是得到的了10000。结合官网对Ebedding算子的用法说明, 发现对padding_idx已有明确规定, 其值需要满足在0到vocab_size之间:
MindSpore报错ValueError:`padding_idx` in `Embedding超出范围的报错_第1张图片
2 解决方法基于上面已知的原因,很容易做出如下修改: 01 class Net(nn.Cell):
02 def __init__(self, vocab_size, embedding_size, use_one_hot, padding_idx=None):
03 super(Net, self).__init__()
04 self.op = nn.Embedding(vocab_size=vocab_size, embedding_size=embedding_size, use_one_hot=use_one_hot, padding_idx=padding_idx)
05
06 def construct(self, x):
07 output = self.op(x)
08 return output
09
10 input = Tensor(np.ones([8, 128]), mindspore.int32)
11 vocab_size = 2000
12 embedding_size = 768
13 use_one_hot = True
14 example = Net(vocab_size=vocab_size, embedding_size=embedding_size, use_one_hot=use_one_hot, padding_idx=1000)
15 output = example(input)
16 print("Output:", output.shape)
此时执行成功,输出如下:Output: (8, 128, 768)3 总结定位报错问题的步骤:1、找到报错的用户代码行:example = Net(vocab_size=vocab_size, embedding_size=embedding_size, use_one_hot=use_one_hot, padding_idx=10000);2、 根据日志报错信息中的关键字,缩小分析问题的范围Vpadding_idx in Embedding should be in range of [0, 2000], but got 10000 with type int. ;4 参考文档4.1 Embedding算子API接口

你可能感兴趣的:(神经网络人工智能深度学习)