I was struggling build a lstm model during the model evaluation stage, bug shows different device, can anybody help me out

for the model building stage

    import torch
from torch import nn 
import torch.nn.functional as F
from torch import utils

class LSTM(nn.Module):
    def __init__(self, vocab_size, embed_dim):
        super().__init__()
        self.embedding = nn.EmbeddingBag(vocab_size, embed_dim) # embedding layer
        self.lstm = nn.LSTM(embed_dim,hidden_dim, vocab_size,n_class,bidirectional=True, batch_first=True, dropout=0.3)
        self.fc1 = nn.Linear(hidden_dim * n_class,hidden_dim)
        
    
    def forward(self, text):
       
        text_embeded = self.embedding(text)
        
        output, (h_n, c_n) = self.lstm(text_embeded)
        out = torch.cat([h_n[-1, :, :], h_n[-2, :, :]], dim=-1)

        out_fc1 = self.fc1(output)
        out_fc1_relu = F.relu(out_fc1)
        return out_fc1_relu
            
# model initalization
# hyperparameter 
embed_dim = 32
hidden_dim = 16
n_class = 5

model = LSTM(vocab_size, embed_dim)


loss_fn = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)



for the evaluation stage

def device():

  if torch.cuda.is_available():
    return torch.device('cuda')
  else:
    return torch.device('cpu')
from sklearn.metrics import accuracy_score
loss_list_train= []
loss_list_test = []
epochs =  30
for epoch in range(epochs):
  loss_now = 0.0
  correct = 0
  
  for sentence, targets in train_loader:
    sentence = sentence.to(device())
    targets = targets.to(device())

    temp_batch_size = sentence.shape[0]
    
    model.train()
    optimizer.zero_grad()
    pred = model(sentence)
    # loss = loss_function(pred.view(-1, pred.shape[-1]), targets.view(-1))
    loss = F.nll_loss(pred, targets)
    loss.backward()
    optimizer.step()
    
    loss_now += loss.item() * temp_batch_size
    predicted = torch.argmax(pred,-1)
    correct += accuracy_score(predicted.view(-1).cpu().numpy(),targets.view(-1).cpu()).numpy() * temp_batch_size
    
    model.eval()
    train_loss = loss_now/len(train_data)
    outputs = model(torch.from_numpy(sentence_padding_train).to(device())) # training set raw outputs
    predicted = torch.argmax(outputs, 1) # training set predictions
    f1 = f1_score(label_train, predicted.cpu().numpy().reshape(-1), average='weighted')
# training set f1-score

    outputs_test = model(torch.from_numpy(sentence_padding_val). to(device())) # testing set raw outputs
    loss_test = loss_function(outputs_test, torch.from_numpy(label_val).view(-1).to(device())) # testing set average loss
    predicted_test = torch.argmax(outputs_test, 1) # testing set predictions
    f1_test =f1_score(label_val, predicted_test.cpu().numpy(), average-'weighted' )
# testing set f1-score

    loss_list_train.append(train_loss)
    loss_list_test.append(loss_test.item())
    print('Epoch {}: {:.4f} (train loss), {:.4f} (val loss), {:.4f} (train f1-score), {:.4f} (test f1-score), {:.4f}'.format(epoch + 1, train_loss, loss_test.item,f1,f1_test))

the bug shows below, says &quot;Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument indices in method wrapper___embedding_bag&quot;, I've already define the device, how can I solve this problem?
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-99-2fb3a5047897&gt; in &lt;module&gt;()
     22     model.train()
     23     optimizer.zero_grad()
---&gt; 24     pred = model(sentence)
     25     # loss = loss_function(pred.view(-1, pred.shape[-1]), targets.view(-1))
     26     loss = F.nll_loss(pred, targets)

4 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

&lt;ipython-input-94-7b5f176858ed&gt; in forward(self, text)
     14     def forward(self, text):
     15 
---&gt; 16         text_embeded = self.embedding(text)
     17 
     18         output, (h_n, c_n) = self.lstm(text_embeded)

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/sparse.py in forward(self, input, offsets, per_sample_weights)
    385                                self.scale_grad_by_freq, self.mode, self.sparse,
    386                                per_sample_weights, self.include_last_offset,
--&gt; 387                                self.padding_idx)
    388 
    389     def extra_repr(self) -&gt; str:

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in embedding_bag(input, weight, offsets, max_norm, norm_type, scale_grad_by_freq, mode, sparse, per_sample_weights, include_last_offset, padding_idx)
   2363 
   2364     ret, _, _, _ = torch.embedding_bag(
-&gt; 2365         weight, input, offsets, scale_grad_by_freq, mode_enum, sparse, per_sample_weights, include_last_offset, padding_idx
   2366     )
   2367     return ret

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument indices in method wrapper___embedding_bag)


The error indicates that two tensors are on different devices (clear to understand).
Since you'd moved your input tensor to your GPU via sentence = sentence.to(device()) but not your model, the model's parameters are on the CPU which causes this error. Just add model.to(device()) before your training procedure starts.
