class my_loss(nn.Module):
def __init__(self):
super(my_loss,self).__init__()
def forward(self,label_column,predict_column):
'''
input:
label_column: batch_size*5*M*N, every pixel has five labels for five classes.
predict_column: batch_size*5*M*N, its size is same with label_column's.
output:
loss: the weighty entrypycrossloss and rice loss
'''
batch_size,channel,M,N=predict_column.shape
background=predict_column[:,channel-1,:,:]
label_column=F.softmax(label_column,dim=1)
predict_column=F.softmax(predict_column,dim=1)
max_value,max_class=torch.max(label_column,dim=1)
m=(max_value==channel-1).type(torch.float)
weight=1.0/max_value
cross=torch.zeros([batch_size,M,N])
for i in range(channel):
cross=cross-torch.bmm(label_column[:,i,:,:],torch.log(predict_column[:,i,:,:]))
encross=torch.sum(torch.bmm(weight,cross))/N/M/batch_size
diceloss=torch.tensor(1-2*(torch.sum(torch.mul(torch.sigmoid(background),m))/torch.sum(torch.sigmoid(background)+m)))
return torch.tensor(encross+diceloss)
我自己设计的代码块如上,但是最后运行结果为:AttributeError: 'my_loss' object has no attribute 'backward',为什么呢?