Getting the error: RuntimeError: Expected tensor for argument #1 'indices' to have one of the following scalar types: Long, Int; but got torch.FloatTensor instead (while checking arguments for embedding)

Fahad_Charolia: <p>Even though the input to embedding layer is type <code>torch.int64</code> I am still getting the error <code>expected argument of scalar type long or int, but got torch.FloatTensor</code>.</p>
<p>`for epoch in range(inital_epoch, config[‘number_epochs’]):<br>
model.train()<br>
batch_iterator = tqdm(train_dataloader, desc = f’Processing epoch {epoch:02d}')<br>
for batch in batch_iterator:<br>
encoder_input = batch[‘encoder_input’].to(device) # (batch_size, context_len)<br>
decoder_input = batch[‘decoder_input’].to(device) # (batch_size, context_len)<br>
encoder_mask = batch[‘encoder_mask’].to(device) # (batch_size, 1, context_len)<br>
decoder_mask = batch[‘decoder_mask’].to(device) # (batch_size, context_len, context_len)</p>
<pre><code>        print(f"encoder_input dtype: {encoder_input.dtype}")
        print(f"encoder_input shape: {encoder_input.shape}")

        encoder_output = model.encoder(encoder_input, encoder_mask)`
</code></pre>
<p>In above code, I have added print statement to inspect the <code>dtype</code> and <code>shape</code> of the <code>encoder_input</code>.</p>
<p>When I execute the file I’m getting below error:<br>
<code>Using device cpu Processing epoch 00:   0%|                                                                                                       | 0/12771 [00:00&lt;?, ?it/s]encoder_input dtype: torch.int64 encoder_input shape: torch.Size([8, 440]) Processing epoch 00:   0%|                                                                                                       | 0/12771 [00:00&lt;?, ?it/s] Traceback (most recent call last):   File "E:\Fahad\College\BE\Project\MT using Transformers\train.py", line 137, in &lt;module&gt;     train_model(config)   File "E:\Fahad\College\BE\Project\MT using Transformers\train.py", line 107, in train_model     encoder_output = model.encoder(encoder_input, encoder_mask) # (batch_size, context_len, embedding_size)                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "E:\Fahad\College\BE\Project\MT using Transformers\model.py", line 246, in encoder     return self.encoder(src, src_mask)            ^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "E:\Fahad\College\BE\Project\MT using Transformers\model.py", line 240, in encoder     src = self.input_embedding(src)           ^^^^^^^^^^^^^^^^^^^^^^^^^   File "C:\Users\fahad\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\module.py", line 1511, in _wrapped_call_impl     return self._call_impl(*args, **kwargs)            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "C:\Users\fahad\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\module.py", line 1520, in _call_impl     return forward_call(*args, **kwargs)            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "E:\Fahad\College\BE\Project\MT using Transformers\model.py", line 18, in forward     return self.embedding(x) * math.sqrt(self.embedding_dim)            ^^^^^^^^^^^^^^^^^   File "C:\Users\fahad\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\module.py", line 1511, in _wrapped_call_impl     return self._call_impl(*args, **kwargs)            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "C:\Users\fahad\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\module.py", line 1520, in _call_impl     return forward_call(*args, **kwargs)            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "C:\Users\fahad\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\sparse.py", line 163, in forward     return F.embedding(            ^^^^^^^^^^^^   File "C:\Users\fahad\AppData\Roaming\Python\Python311\site-packages\torch\nn\functional.py", line 2237, in embedding     return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: Expected tensor for argument #1 'indices' to have one of the following scalar types: Long, Int; but got torch.FloatTensor instead (while checking arguments for embedding)</code></p>
<p>Thankyou!</p>

ptrblck: <p>Could you post a minimal and executable code snippet reproducing the issue, please?</p>

Fahad_Charolia: <p>Thankyou for your response. I made a silly mistake while defining the model. Now its is working fine.</p>