Hello there,
I want to train a simple non-linear equation with 4 parameters with a probabilistic loss function, the CRPS. The model predicts a distribution of outputs, that I create simply by repeatedly integating it with different initial values.
If I write a very simple custom loss function, such as the mean absolute error, the pipeline is working and the weights (the 4 parameter values) are changing.
But with the CRPS, I have to create the cdf of the predictions and the observation in a very long loss function with lots of operations (see below). Using this functions, my parameter values don’t change in the training. What am I doing wrong?
I appreciate any help, thank you very much!
def my_custom_loss(outputs, targets):
"""
outputs: tensor. vector of length n, containing ensemble predictions
targets: tensor. scalar, according observation.
"""
fc = torch.sort(outputs).values
ob = targets.clone()
m = len(fc)
cdf_fc = []
cdf_ob = []
delta_fc = []
# do for all ensemble members
for f in range(len(fc) - 1):
# check is ensemble member and its following ensemble member is smaller than observation.
if (fc[f] < ob) and (fc[f + 1] < ob):
cdf_fc.append((f + 1) * 1 / m)
cdf_ob.append(0)
delta_fc.append(fc[f + 1] - fc[f])
elif (fc[f] < ob) and (fc[f + 1] > ob):
# check is ensemble member is smaller than observation and its following ensemble member is larger than observation.
cdf_fc.append((f + 1) * 1 / m)
cdf_fc.append((f + 1) * 1 / m)
cdf_ob.append(0)
cdf_ob.append(1)
delta_fc.append(ob - fc[f])
delta_fc.append(fc[f + 1] - ob)
else:
cdf_fc.append((f + 1) * 1 / m)
cdf_ob.append(1)
delta_fc.append(fc[f + 1] - fc[f])
cdf_fc = torch.tensor(cdf_fc, dtype=torch.float, requires_grad=True)
cdf_ob = torch.tensor(cdf_ob, dtype=torch.float, requires_grad=True)
delta_fc = torch.tensor(delta_fc, dtype=torch.float, requires_grad=True)
loss = torch.sum(((cdf_fc - cdf_ob) ** 2) * delta_fc)
return loss