Expected object of scalar type Byte but got scalar type Float

Hi everyone, I would like to do image classification of my own dataset (containing nump images). I am using RNN for this purpose but I got runtime error of " Expected object of scalar type Byte but got scalar type Float for argument #2 ‘mat2’ in call to _th_mm". this happens when images going to pass to model.

here is my training loop code:

# Number of steps to unroll
seq_dim = 28  

iter = 0
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        model.train()
        # Load images as tensors with gradient accumulation abilities
        images = images.view(-1, seq_dim, input_dim)

        # Clear gradients w.r.t. parameters
        optimizer.zero_grad()

        # Forward pass to get output/logits
        # outputs.size() --> 100, 10
        outputs = model(images)

        # Calculate Loss: softmax --> cross entropy loss
        loss = criterion(outputs, labels)

        # Getting gradients w.r.t. parameters
        loss.backward()

        # Updating parameters
        optimizer.step()

        iter += 1

        if iter % 500 == 0:
            model.eval()
            # Calculate Accuracy         
            correct = 0
            total = 0
            # Iterate through test dataset
            for images, labels in test_loader:
                # Load images to a Torch tensors with gradient accumulation abilities
                images = images.view(-1, seq_dim, input_dim)

                # Forward pass only to get logits/output
                outputs = model(images)

                # Get predictions from the maximum value
                _, predicted = torch.max(outputs.data, 1)

                # Total number of labels
                total += labels.size(0)

                # Total correct predictions
                correct += (predicted == labels).sum()

            accuracy = 100 * correct / total

            # Print Loss
            print('Iteration: {}. Loss: {}. Accuracy: {}'.format(iter, loss.item(), accuracy))

plus my RNN network class is :

class RNNModel(nn.Module):
   def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
       super(RNNModel, self).__init__()
       # Hidden dimensions
       self.hidden_dim = hidden_dim

       # Number of hidden layers
       self.layer_dim = layer_dim

       # Building your RNN
       # batch_first=True causes input/output tensors to be of shape
       # (batch_dim, seq_dim, feature_dim)
       self.rnn = nn.RNN(input_dim, hidden_dim, layer_dim, batch_first=True, nonlinearity='relu')

       # Readout layer
       self.fc = nn.Linear(hidden_dim, output_dim)

   def forward(self, x):
       # Initialize hidden state with zeros
       h0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()

       # We need to detach the hidden state to prevent exploding/vanishing gradients
       # This is part of truncated backpropagation through time (BPTT)
       out, hn = self.rnn(x, h0.detach())

       # Index hidden state of last time step
       # out.size() --> 100, 28, 100
       # out[:, -1, :] --> 100, 100 --> just want last time step hidden states! 
       out = self.fc(out[:, -1, :]) 
       # out.size() --> 100, 10
       return out

input_dim = 28
hidden_dim = 100
layer_dim = 1
output_dim = 10
model = RNNModel(input_dim, hidden_dim, layer_dim, output_dim)
criterion = nn.CrossEntropyLoss()
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 

could you please help me.

Could you check the type() of your images tensor?
I assume, if might by a ByteTensor, while a FloatTensor is expected to match the tensor type of the model’s parameters.
In that case, you could call images = images.float() to get the expected type.

Also, if that’s the case, I assume you have not normalized your input data, which could help during training.

6 Likes