Try to avoid the loop:
def myLoss(output, y):
    loss = (output - y).abs()
    mask = loss.mean() > threshold 
    loss[mask]  = loss[mask].add(1).log().mul(50)
    return loss.mean()

Maybe you would have to add a .clone() to the indexing line.