title: RuntimeError: value cannot be converted to type uint8_t without overflow: -0.192746
body: <p>I am new to Pytorch and am aiming to do an image classification task using a CNN based on the EMNIST dataset.</p>
  <p>I read my data in as follows:</p>
  <pre><code>emnist = scipy.io.loadmat(DATA_DIR + '/emnist-letters.mat')
  data = emnist ['dataset']
  X_train = data ['train'][0, 0]['images'][0, 0]
  X_train = X_train.reshape((-1,28,28), order='F')
  
  y_train = data ['train'][0, 0]['labels'][0, 0]
  
  X_test = data ['test'][0, 0]['images'][0, 0]
  X_test = X_test.reshape((-1,28,28), order = 'F')
  
  y_test = data ['test'][0, 0]['labels'][0, 0]
  
  train_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_train), torch.from_numpy(y_train))
  test_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_test), torch.from_numpy(y_test))
  
  batch_size = 128
  n_iters = 3000
  num_epochs = n_iters / (len(train_dataset) / batch_size)
  num_epochs = int(num_epochs)
  
  train_loader = torch.utils.data.DataLoader(dataset=train_dataset, 
                                             batch_size=batch_size, 
                                             shuffle=True)
  
  test_loader = torch.utils.data.DataLoader(dataset=test_dataset, 
                                            batch_size=batch_size, 
                                            shuffle=False)
  </code></pre>
  <p>Then, I found the following configurations (that I still have to adjust to fit to my data):</p>
  <pre><code>class CNNModel(nn.Module):
      def __init__(self):
          super(CNNModel, self).__init__()
          
          # Convolution 1
          self.cnn1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=0)
          self.relu1 = nn.ReLU()
          
          # Max pool 1
          self.maxpool1 = nn.MaxPool2d(kernel_size=2)
       
          # Convolution 2
          self.cnn2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=0)
          self.relu2 = nn.ReLU()
          
          # Max pool 2
          self.maxpool2 = nn.MaxPool2d(kernel_size=2)
          
          # Fully connected 1 (readout)
          self.fc1 = nn.Linear(32 * 4 * 4, 10) 
      
      def forward(self, x):
          # Convolution 1
          out = self.cnn1(x)
          out = self.relu1(out)
          
          # Max pool 1
          out = self.maxpool1(out)
          
          # Convolution 2 
          out = self.cnn2(out)
          out = self.relu2(out)
          
          # Max pool 2 
          out = self.maxpool2(out)
          
          # Resize
          # Original size: (100, 32, 7, 7)
          # out.size(0): 100
          # New out size: (100, 32*7*7)
          out = out.view(out.size(0), -1)
  
          # Linear function (readout)
          out = self.fc1(out)
          
          return out
  
  
  model = CNNModel()
  
  criterion = nn.CrossEntropyLoss()
  </code></pre>
  <p>To train the model, I use the following code:</p>
  <pre><code>iter = 0
  for epoch in range(num_epochs):
      for i, (images, labels) in enumerate(train_loader):
          
          # Add a single channel dimension
          # From: [batch_size, height, width]
          # To: [batch_size, 1, height, width]
          images = images.unsqueeze(1)
  
          # Forward pass to get output/logits
          outputs = model(images)
          
          # Clear gradients w.r.t. parameters
          optimizer.zero_grad()
          
          # Forward pass to get output/logits
          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:
              # Calculate Accuracy         
              correct = 0
              total = 0
              # Iterate through test dataset
              for images, labels in test_loader:
                 
                  images = images.unsqueeze(1)
                  
                  # 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)
                  
                  correct += (predicted == labels).sum()
              
              accuracy = 100 * correct / total
              
              # Print Loss
              print('Iteration: {}. Loss: {}. Accuracy: {}'.format(iter, loss.data[0], accuracy))
  </code></pre>
  <p>However, when I run this, I get the following error:</p>
  <pre><code>---------------------------------------------------------------------------
  RuntimeError                              Traceback (most recent call last)
  &lt;ipython-input-27-1fbdd53d1194&gt; in &lt;module&gt;()
       12 
       13         # Forward pass to get output/logits
  ---&gt; 14         outputs = model(images)
       15 
       16         # Clear gradients w.r.t. parameters
  
  4 frames
  /usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
      348                             _pair(0), self.dilation, self.groups)
      349         return F.conv2d(input, weight, self.bias, self.stride,
  --&gt; 350                         self.padding, self.dilation, self.groups)
      351 
      352     def forward(self, input):
  
  RuntimeError: value cannot be converted to type uint8_t without overflow: -0.0510302
  </code></pre>
  <p>I found <a href="https://stackoverflow.com/questions/60253449/pytorch-conv2d-value-cannot-be-converted-to-type-uint8-t-without-overflow">this</a> question already and think that the solution might work for me as well. However, I don't understand where in my code I can implement this.</p>
  <p>What can I do to overcome this problem?</p>
  <p>Ps.</p>
  <p>I have used the following import statements:</p>
  <pre><code>    import scipy .io
      import torch
      import torch.nn as nn
      import torchvision.transforms as transforms
      import torchvision.datasets as dsets
      from torch.autograd import Variable
      import cv2
      import torch
      import torch.nn as nn
      import torch.optim as optim
      import torch.nn.functional as F
      import numpy as np
      import os
      from PIL import Image
      from PIL import ImageOps
      from torchvision import datasets, transforms
      from torch.autograd import Variable
      import matplotlib.pyplot as plt
      from torchvision.datasets import ImageFolder
      from torch.utils.data import DataLoader
      from torchvision.transforms import ToTensor
      from torch.nn import Sequential
      from torch.nn import Conv2d
      from torch.nn import BatchNorm2d
      from torch.nn import MaxPool2d
      from torch.nn import ReLU
      from torch.nn import Linear
  </code></pre>