Give correct datatype to pytorch, but it does not accept
class Model(nn.Module):
    def __init__(self,
                input_size=12175,
                hidden_size=6,
                num_layers=1,
                batch_size=1,
                sequence_length=1,
                num_classes=6):
        &quot;&quot;&quot;RNN and FC. hidden_size and num_classes MUST equal.&quot;&quot;&quot;
        super().__init__()
        self.rnn = nn.RNN(input_size=input_size,
                         hidden_size=hidden_size,
                         batch_first=True)
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.batch_size = batch_size
        self.sequence_length = sequence_length
        self.num_classes = num_classes
        
        # Fully-Connected layer
        self.fc = nn.Linear(num_classes, num_classes)

    def forward(self, x, hidden):
        import ipdb; ipdb.set_trace()
        # Reshape input in (batch_size, sequence_length, input_size)
        x = x.view(self.batch_size, self.sequence_length, self.input_size)
        x = x.double()
        hidden = hidden.double()
        out, hidden = self.rnn(x, hidden)
        out = self.fc(out) # Add here
        return hidden, out
    
    def init_hidden(self):
        return torch.zeros(self.num_layers, self.batch_size, self.hidden_size)

And training
import itertools
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix

CLASS_LENGTH = 6

def train(model, device, train_loader, optimizer, epoch, criterion):
    &quot;&quot;&quot;
    This function has one line different from the ordinary `train()` function
    It has `make_variables()` to convert tuple of names to be a tensor
    &quot;&quot;&quot;
    model.train()
    hidden = model.init_hidden()
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad()
#         hidden = hidden.view(model.batch_size, model.sequence_length, CLASS_LENGTH)
        output, hidden = model(data, hidden)

        tmp = output.view(model.batch_size, CLASS_LENGTH)
        loss = criterion(tmp, target)
        loss.backward(retain_graph=True)
        optimizer.step()
        if batch_idx % 1000 == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()))

def test(model, device, test_loader, criterion):
    model.eval()
    test_loss = 0
    correct = 0
    y_test = []
    y_pred = []
    with torch.no_grad():
        for data, target in tqdm(test_loader):
            data, target = data.to(device), target.to(device)
            output, hidden = model(data, hidden)
            tmp = output.view(-1, COUNTRY_LENGTH)
            test_loss += criterion(tmp, target).item() # sum up batch loss
            pred = tmp.max(1, keepdim=True)[1] # get the index of the max log-probability

            pred_tmp = pred.view(-1)
            pred_list = pred_tmp.tolist()
            target_list = target.tolist()
            
            y_test += target_list
            y_pred += pred_list
            correct += pred.eq(target.view_as(pred)).sum().item()

    test_loss /= len(test_loader.dataset)

    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))
    
    # Confusion matrix
    confusion_mtx = confusion_matrix(y_test, y_pred)
    plot_confusion_matrix(confusion_mtx, classes=test_loader.dataset.countries, normalize=True,
                          title='Confusion matrix')

Problem:
I do not understand why it raise error since I am sure that I put correct datatype into it
ipdb&gt; n
&gt; /tmp/ipykernel_16/2018411812.py(30)forward()
     29         hidden = hidden.double()
---&gt; 30         out, hidden = self.rnn(x, hidden)
     31         out = self.fc(out) # Add here

ipdb&gt; n
RuntimeError: expected scalar type Double but found Float
&gt; /tmp/ipykernel_16/2018411812.py(30)forward()
     29         hidden = hidden.double()
---&gt; 30         out, hidden = self.rnn(x, hidden)
     31         out = self.fc(out) # Add here

ipdb&gt; x
tensor([[[-5.6964e-01, -5.1070e-01, -5.9109e-01,  ...,  1.5597e-15,
           1.5597e-15,  1.5597e-15]]], dtype=torch.float64)
ipdb&gt; hidden
tensor([[[0., 0., 0., 0., 0., 0.]]], dtype=torch.float64)

pytorch version: 1.8.1+cu102
OSX 10.15.7
i7
Question:
How to solve this problem?

You are ensure type for RNNs input and hidden state, but it also contain some tensor parameters, weights, biases etc. To make them double, try torch.set_default_tensor_type(t) before RNN object construction, but I personaly would use Float.
...
super().__init__()
torch.set_default_tensor_type(torch.DoubleTensor)
self.rnn = nn.RNN(input_size=input_size,
                     hidden_size=hidden_size,
                     batch_first=True)
# may be turn defaults back to floats:
# torch.set_default_tensor_type(torch.FloatTensor)

