Hey Nay!



 fauxneticien:

labels   = torch.tensor([
    # Class, t_start, t_end
    [0, 0, 0],
    [1, 0.25, 0.31],
    [0, 0, 0]
])

# Simulate output = model(distance_matrix)
output = torch.tensor([
    [0.1478, 0.1370, 0.2288],
    [0.7052, 0.3207, 0.5404],
    [0.4511, 0.2978, 0.5279]
])

# Binary cross-entropy error on class labels, mean-squared error on time points
# Use labels to zero-out irrelevant rows
loss = BCE(output[:,:1], labels[:,:1]) + MSE(labels[:,:1] * output[:,-2:], labels[:,-2:])

I was wondering whether using the labels to zero out the rows of the output I wasn’t interested in (where class = 0) was the right way to go about this.


I don’t really understand your overall use case – “test utterance,”
applying the model to a distance matrix – so I don’t have any
thoughts on your “real” problem
Three lower-level comments:
Your code for loss looks sensible and correct.  (I’ll leave it up to
you as to whether at the higher level it does anything useful or
what you want it to do.)
As written, your loss doesn’t care at all about the values of
output[0, 1], output[0, 2], output[2, 1], and output[2, 1].
So, in principle, these values – and the weights that produce
them – could drift off and become large.  My gut tells me that it
might be beneficial (although hardly essential) to regulate these
output values (that don’t logically matter).
Assuming that a “class-0” labels row – that is, a row i for which
labels[i, 0] is 0 – is all zeros – that is, for that i, labels[i, j]
is 0.0 for all j – then simply using:
MSE (output[:, -2:], labels[:, -2:])

(or MSE (output[:, 1:], labels[:, 1:]), see below), will
provide that regularization.  (Weight decay or a weight-regularization
term in your loss would presumably provide equivalent benefit.)
As a minor stylistic comment:  Your choice of slicing notation looks
rather perverse to me.  For readability (at least for me), I would prefer,
e.g., output[:, 0] to extract the first column, and output[:, 1:] to
extract the remaining columns.  output[:, :1] and output[:, 1:]
could also work, as you could argue that using :1 and 1: together
emphasizes that you are using all of the columns (but I still like 0
better for the first column).
Best.
K. Frank