TypeError: new() received an invalid combination of arguments - got (Tensor, int), but expected one of: * (*, torch.device device) didn't match because some of the arguments have invalid types: (Tensor, int)

Hi there, I am building my first neural net with pytorch to predict a single output from an image using a pretrained resnet18 model and keep getting this error. I don’t understand what I am doing wrong here.

#ERROR
TypeError: new() received an invalid combination of arguments - got (Tensor, int), but expected one of:
 * (*, torch.device device)
      didn't match because some of the arguments have invalid types: (Tensor, int)
 * (torch.Storage storage)
 * (Tensor other)
 * (tuple of ints size, *, torch.device device)
 * (object data, *, torch.device device)

Below is my code

# DATALOADER

class RosData(Dataset):
    def __init__(self, csv_file, root_dir, transform=None):
        self.annotations = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.img_dir = sorted_alphanumeric(os.listdir(img_folder))
        self.transform = transform

    def __len__(self):
        return len(self.annotations)

    def __getitem__(self, index):
        img_path = os.path.join(self.root_dir,self.img_dir[index]) #change to list of images
        image = Image.open(img_path)
        y_label = torch.tensor(int(self.annotations.iloc[index, -9]*1e-5))

        if self.transform:
            image = self.transform(image)

        sample = {'image': image, 'label': y_label}

        return sample

#MODEL

def ResNet18(num_classes):

    model = resnet18(pretrained=True)
    model.fc = nn.Sequential(
        nn.BatchNorm1d(512),
        nn.Dropout(0.5),
        nn.Linear(512, num_classes),
    )
    return model


def train(train_loader, model, criterion1, criterion2, optimizer, epoch, result_directory):

    #model.train()
    running_loss = 0.
    running_mean_loss = 0.
    running_variance_loss = 0.
    running_softmax_loss = 0.
    interval = 1
    for i, sample in enumerate(train_loader):
        images = sample['image'].to(device)
        labels = sample['label'].to(device)
        output = model(images)
        mean_loss, variance_loss = criterion1(output, labels)
        softmax_loss = criterion2(output, labels)
        loss = mean_loss + variance_loss + softmax_loss
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        running_loss += loss.data
        running_softmax_loss += softmax_loss.data
        running_mean_loss += mean_loss.data
        running_variance_loss += variance_loss.data
        if (i + 1) % interval == 0:
            print('[%d, %5d] mean_loss: %.3f, variance_loss: %.3f, softmax_loss: %.3f, loss: %.3f'
                  % (epoch, i, running_mean_loss / interval,
                     running_variance_loss / interval,
                     running_softmax_loss / interval,
                     running_loss / interval))
            with open(os.path.join(result_directory, 'log'), 'a') as f:
                f.write('[%d, %5d] mean_loss: %.3f, variance_loss: %.3f, softmax_loss: %.3f, loss: %.3f\n'
                        % (epoch, i, running_mean_loss / interval,
                           running_variance_loss / interval,
                           running_softmax_loss / interval,
                           running_loss / interval))
            running_loss = 0.
            running_mean_loss = 0.
            running_variance_loss = 0.
            running_softmax_loss = 0.



#TRAIN
criterion1 = nn.MSELoss
criterion2 = nn.CrossEntropyLoss
result_dir = os.getcwd()

train(train_dataset,ResNet18,criterion1,criterion2,optimizer,epoch=2,result_directory=result_dir )

@ptrblck please check this out

So the problem looks to be in the way you defined your model. In pytorch when using a model and the nn library you need to define the model before you can execute the forward pass in it. Currently you do not define your model before you are trying to use it so it is still expecting the number of classes and not an input to the model. You probably want to define your model before hand. You can do something like this:

model = ResNet18(num_classes)
train(train_dataset,model,criterion1,criterion2,optimizer,epoch=2,result_directory=result_dir )

then just input how many classes the model should output where I wrote num_classes. This will initialize your model correctly.

1 Like

Thanks @Dwight_Foster , this works