Good afternoon!
I have a model that has 6 classes on which each class has several possible labels. I wanted to ask if it is possible to give a list of weights for each label of each class.
My model:
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.model = pretrainedmodels.__dict__["resnet50"](pretrained="imagenet")
self.fc1 = nn.Linear(2048, 3)
self.fc3 = nn.Linear(2048, 5)
self.fc4 = nn.Linear(2048, 4)
self.fc5 = nn.Linear(2048, 5)
self.fc7 = nn.Linear(2048, 2)
self.fc9 = nn.Linear(2048, 3)
def forward(self, x):
bs, _, _, _ = x.shape
x = self.model.features(x)
x = F.adaptive_avg_pool2d(x, 1).reshape(bs, -1)
label1 = torch.sigmoid(self.fc1(x))
label3 = torch.sigmoid(self.fc3(x))
label4 = torch.sigmoid(self.fc4(x))
label5 = torch.sigmoid(self.fc5(x))
label7 = torch.sigmoid(self.fc7(x))
label9 = self.fc9(x)
return {'label1': label1, 'label3': label3, 'label4': label4, 'label5': label5, 'label7': label7, 'label9': label9}
I wanted to do this:
weights = [[0.3, 0.3, 0.4], [0.2, 0.2, 0.2, 0.4], [0.3, 0.3, 0.3, 0.1], [0.3, 0.3, 0.3, 0.1], [0.7, 0.3], [0.1, 0.5, 0.4]]
class_weights = torch.FloatTensor(weights).cuda()
criterion = nn.CrossEntropyLoss()
I get this error: ValueError: expected sequence of length 3 at dim 1 (got 4)
Thank you in advance