Getting losses while predicting inputs

Hi, i am trying to get loss for every input prediction of model. So i have a model after training i am pass inputs to prediction.

output = model(inputs)

to get loss, i try

loss = output.loss

This does not return any value, but it return generator type. Is there any way to convert generator and get value out of this.

You might be able to get the values by passing the generator to e.g. a list, but it usually depends on your use case. I.e. how is the loss calculated, why is it a generator etc.

I didn’t get how to pass as list, or is it like this list(loss)

Yes, you could try to use list(loss), but I would also strongly recommend to check why it’s a generator as it seems you don’t expect it:

loss = (torch.randn(1) for _ in range(10))
print(type(loss))
# <class 'generator'>

print(list(loss))
# [tensor([-1.6251]), tensor([-2.1271]), tensor([0.6758]), tensor([0.5699]), tensor([0.1286]), tensor([-1.4132]), tensor([0.1561]), tensor([1.1689]), tensor([-0.4857]), tensor([-1.3968])]

I tried compute, list(loss) and its {list:0} [] . I am not sure how its emtpy. Any ideas to debug?

I am computing distillation process as in this link https://github.com/subhasisj/Model-Compression-Techniques/blob/main/01-Modelling-KD.ipynb

Cell 75 can give more idea about the process.