The question is how? Can you maybe help me learn how to do it?
I know that (I’ve read the documentation), but as I said I didn’t find a specification for this function.
You can have a look at my whole model:
class MyModel(nn.Module):
def __init__(self, na, nb, nc):
super(MyModel, self).__init__()
self.ma = torch.hub.load('pytorch/vision:v0.5.0', 'densenet121', pretrained=True)
self.ma.eval()
self.gr = nn.Sequential(nn.Linear(1000, na))
self.vd = nn.Sequential(nn.Linear(1000, nb))
self.cd = nn.Sequential(nn.Linear(1000, nc))
def forward(self,x):
x = self.ma(x)
a = (self.gr(x), self.vd(x), self.cd(x))
x = torch.cat(a, axis=1)
return x
def apply_to_three(a, b, s1, s2, s3, f, agg):
print(a.shape)
print(b.shape)
return agg([f(a[:,:s1], b[:,:s1]),f(a[:,s1:s2], b[:,s1:s2]), f(a[:,s2:s3], b[:,s2:s3])])
s1 = na
s2 = s1+nb
s3 = s2+nc
loss_function = partial(apply_to_three, f=nn.CrossEntropyLoss, s1=s1, s2=s2, s3=s3, agg=sum)
# Create learner
learner = Learner(data, model=MyModel(na, nb, nc), loss_func=loss_function, metrics=accuracy)
I know that you maybe can do the same with a cnn_learner, but I would like to both learn and enrich it.
Say you have three categories with size 167, 8, 5.
Then I get for one picture: 10;23;1 and for the next 123;1;2 and for another one just 5;8.
Maybe it’s a problem of show_batch itself(?)
PS: I am new to fastai, pytorch, but not at all new to the concepts of deep-learning and programming in Python.