[Solved] MindSpore Error: ValueError: `padding_idx` in `Embedding` out of range

1 Error description

1.1 System Environment

Hardware Environment(Ascend/GPU/CPU): Ascend
Software Environment:
– MindSpore version (source or binary): 1.8.0
– Python version (eg, Python 3.7.5): 3.7.6
– OS platform and distribution (eg, Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic
– GCC/Compiler version (if compiled from source):

1.2 Basic information

1.2.1 Script

The training script completes the embedding layer operation by constructing a single-operator network of Embedding. The script is as follows:

 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 Error reporting

The error message here is as follows:

Traceback (most recent call last):
  File "C:/Users/l30026544/PycharmProjects/q2_map/new/I3MRK3.py", line 26, in <module>
    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`.

Cause Analysis

Let’s look at the error message. In ValueError, it is written that padding_idx in  Embedding should be in range of [0, 2000], but got 10000 with type  int., which means that the value of padding_idx’ in the Embedding operator needs to be between 0 and 2000, but Got 10000. Combined with the official website’s description of the usage of the Ebedding operator, it is found that padding_idx has been clearly specified, and its value needs to be between 0 and vocab_size:
image.png

2 Solutions

For the reasons known above, it is easy to make the following modifications:

 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)

At this point, the execution is successful, and the output is as follows:

Output: (8, 128, 768)

3 Summary

Steps to locate the error report:

1. Find the user code line that reported the error: example = Net(vocab_size=vocab_size, embedding_size=embedding_size, use_one_hot=use_one_hot, padding_idx=10000) ;

2. According to the keywords in the log error message, narrow the scope of the analysis problem padding_idx in  Embedding should be in range of [0, 2000], but got 10000 with type  int.  ;

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *