Expected object of scalar type Byte but got scalar type Float

Majid_Shirazi: <p>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 <span class="hashtag">#2</span> ‘mat2’ in call to _th_mm". this happens when images going to pass to model.</p>
<p>here is my training loop code:</p>
<pre><code class="lang-auto"># 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() --&gt; 100, 10
        outputs = model(images)

        # Calculate Loss: softmax --&gt; 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))
</code></pre>
<p>plus my RNN network class is :</p>
<pre><code class="lang-auto">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() --&gt; 100, 28, 100
       # out[:, -1, :] --&gt; 100, 100 --&gt; just want last time step hidden states! 
       out = self.fc(out[:, -1, :]) 
       # out.size() --&gt; 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) 
</code></pre>
<p>could you please help me.</p>

ptrblck: <p>Could you check the <code>type()</code> of your <code>images</code> tensor?<br>
I assume, if might by a <code>ByteTensor</code>, while a <code>FloatTensor</code> is expected to match the tensor type of the model’s parameters.<br>
In that case, you could call <code>images = images.float()</code> to get the expected type.</p>
<p>Also, if that’s the case, I assume you have not normalized your input data, which <em>could</em> help during training.</p>