1

I just started learning Pytorch and created my first LSTM. The dataset is time series data. Below is the training code. Using .double() does not fix the error. It is running in a Windows 11 environment.

import torch
import torch.nn as nn
from torch.optim import SGD
import math
import numpy as np

class Predictor(nn.Module):
    def __init__(self, inputDim, hiddenDim, outputDim):
        super(Predictor, self).__init__()

        self.rnn = nn.LSTM(input_size = inputDim,
                            hidden_size = hiddenDim,
                            batch_first = True)
        self.output_layer = nn.Linear(hiddenDim, outputDim)
    
    def forward(self, inputs, hidden0=None):
        output, (hidden, cell) = self.rnn(inputs, hidden0)
        output = self.output_layer(output[:, -1, :])

        return output

def mkDataSet(train_x, train_y=None):

    t_train_x = []
    t_train_y = []


    sequence_length = 50
    data_length = train_x.shape[0]

    for offset in range(data_length-sequence_length):
        t_train_x.append([train_x.iloc[offset + i] for i in range(sequence_length)])
        try:
            t_train_y.append([train_y.iloc[offset + i] for i in range(sequence_length)])
        except:
            pass

    return t_train_x, t_train_y

def mkRandomBatch(train_x, train_t, batch_size=10):
    batch_x = []
    batch_t = []

    for _ in range(batch_size):
        idx = np.random.randint(0, len(train_x) - 1)
        batch_x.append(train_x[idx])
        try:
            batch_t.append(train_t[idx])
        except:
            pass
    
    return torch.tensor(batch_x), torch.tensor(batch_t)

def main(train_x=train_x, train_y=train_y, test_x=test_x):
    training_size = 10000
    test_size = 1000
    epochs_num = 1000
    hidden_size = 5
    batch_size = 100

    train_x_origin, train_y_origin, test_x_origin = train_x.copy(), train_y.copy(), test_x.copy()

    train_x, train_t = mkDataSet(train_x, train_y)
    test_x = mkDataSet(test_x)

    model = Predictor(train_x_origin.shape[1], hidden_size, 1)
    criterion = nn.MSELoss()
    optimizer = SGD(model.parameters(), lr=0.01)

    for epoch in range(epochs_num):
        # training
        running_loss = 0.0
        training_accuracy = 0.0
        for i in range(int(training_size / batch_size)):
            optimizer.zero_grad()

            data, label = mkRandomBatch(train_x, train_t, batch_size)

            output = model(data)

            loss = criterion(output, label)
            loss.backward()
            optimizer.step()

            running_loss += loss.data[0]
            training_accuracy += np.sum(np.abs((output.data - label.data).numpy()) < 0.1)

        #test
        test_accuracy = 0.0
        for i in range(int(test_size / batch_size)):
            offset = i * batch_size
            data, label = torch.tensor(test_x[offset:offset+batch_size])
            output = model(data, None)
        
        training_accuracy /= training_size

        print('%d loss: %.3f, training_accuracy: %.5f, test_accuracy: %.5f' % (
            epoch + 1, running_loss, training_accuracy))


if __name__ == '__main__':
    main(train_x, train_y, test_x)

Then I have this error:

RuntimeError                              Traceback (most recent call last)
.ipynb Cell 26' in <cell line: 113>()
    109         print('%d loss: %.3f, training_accuracy: %.5f, test_accuracy: %.5f' % (
    110             epoch + 1, running_loss, training_accuracy))
    113 if __name__ == '__main__':
--> 114     main(train_x, train_y, test_x)

.ipynb Cell 26' in main(train_x, train_y, test_x)
     87 optimizer.zero_grad()
     89 data, label = mkRandomBatch(train_x, train_t, batch_size)
---> 91 output = model(data)
     93 loss = criterion(output, label)
     94 loss.backward()

File ~\Documents\lib\site-packages\torch\nn\modules\module.py:1110, in Module._call_impl(self, *input, **kwargs)
   1106 # If we don't have any hooks, we want to skip the rest of the logic in
   1107 # this function, and just call forward.
   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):
-> 1110     return forward_call(*input, **kwargs)
   1111 # Do not call functions when jit is used
   1112 full_backward_hooks, non_full_backward_hooks = [], []

.ipynb Cell 26' in Predictor.forward(self, inputs, hidden0)
     16 def forward(self, inputs, hidden0=None):
---> 17     output, (hidden, cell) = self.rnn(inputs, hidden0)
     18     output = self.output_layer(output[:, -1, :])
     20     return output

File ~\Documents\lib\site-packages\torch\nn\modules\module.py:1110, in Module._call_impl(self, *input, **kwargs)
   1106 # If we don't have any hooks, we want to skip the rest of the logic in
   1107 # this function, and just call forward.
   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):
-> 1110     return forward_call(*input, **kwargs)
   1111 # Do not call functions when jit is used
   1112 full_backward_hooks, non_full_backward_hooks = [], []

File ~\Documents\lib\site-packages\torch\nn\modules\rnn.py:761, in LSTM.forward(self, input, hx)
    759 self.check_forward_args(input, hx, batch_sizes)
    760 if batch_sizes is None:
--> 761     result = _VF.lstm(input, hx, self._flat_weights, self.bias, self.num_layers,
    762                       self.dropout, self.training, self.bidirectional, self.batch_first)
    763 else:
    764     result = _VF.lstm(input, batch_sizes, hx, self._flat_weights, self.bias,
    765                       self.num_layers, self.dropout, self.training, self.bidirectional)

RuntimeError: expected scalar type Double but found Float

I think there is something wrong with 'data' and I print its type by print(type(data)):

torch.Tensor

which is float instead of double. Do you know what`s wrong? Thanks for your help!

0

1 Answer 1

5

Your input data to the model is tensor of type Double, while the model expects a float tensor. Do this in the last line of mkRandomBatch() function:

 return torch.tensor(batch_x).float(), torch.tensor(batch_t)

You may or may not get a similar error for label tensor as well during loss calculation, in that case try converting label tensor to float as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! With your help, the error in the title was resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.