# 报错ValueError：\`padding\_idx\` in \`Embedding超出范围

**1. 报错描述**

1.1 系统环境

```
Hardware Environment(Ascend/GPU/CPU): Ascend Software 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(&quot;Output:&quot;, output.shape)
```

1.2.2 报错

这里报错信息如下：

```
Traceback (most recent call last):
  File &quot;C:/Users/user1/PycharmProjects/q2_map/new/I3MRK3.py&quot;, line 26, in &lt;module&gt;
    example = Net(vocab_size=vocab_size, embedding_size=embedding_size, use_one_hot=use_one_hot, padding_idx=10000)
  File &quot;C:/Users/user1/PycharmProjects/q2_map/new/I3MRK3.py&quot;, 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 &quot;C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\layer\embedding.py&quot;, line 111, in __init__
    self.padding_idx = validator.check_int_range(padding_idx, 0, vocab_size, Rel.INC_BOTH,
  File &quot;C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\_checkparam.py&quot;, line 413, in check_int_range
    return check_number_range(arg_value, lower_limit, upper_limit, rel, int, arg_name, prim_name)
  File &quot;C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\_checkparam.py&quot;, line 209, in check_number_range
    raise ValueError(&quot;{} {} should be in range of {}, but got {} with type `{}`.&quot;.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之间：

![](figures/6-75.png)

**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(&quot;Output:&quot;, 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、 根据日志报错信息中的关键字，缩小分析问题的范围padding\_idx in Embedding should be in range of \[0, 2000\], but got 10000 with type int.

**4. 参考文档**

4.1  [Embedding算子API接口](https://www.mindspore.cn/docs/en/r1.7/api_python/nn/mindspore.nn.Embedding.html?highlight=embedding)

