Losses#
Pass a loss by name or hand Trainer a callable:
import pyqit
import pennylane.numpy as pnp
pyqit.Trainer(max_epochs=30, loss_fn="cross_entropy")
def weighted_mse(preds, targets):
return pnp.mean((preds - targets) ** 2 * (1 + targets))
pyqit.Trainer(max_epochs=30, loss_fn=weighted_mse)
Available losses#
The name you pass is the class’s name tag, given on its page along with the
targets it expects.
Mean squared error, the Trainer's default loss. |
|
Hinge loss for binary labels encoded as 0/1. |
|
Cross entropy over class probabilities. |
|
Resolve a loss name to a callable bound to |
Models emit probabilities, not logits#
This trips up anyone porting a torch loss. F.cross_entropy applies its own
log-softmax, so feeding it probabilities scores the wrong thing quietly rather
than raising. CrossEntropyLoss takes the log directly instead. Write
new torch losses the same way.
PennyLane ships no ML losses of its own, so the pnp implementations here are
hand written by design.
Adding a loss#
Subclass BaseLoss and tag it. Nothing hand-registers it, because
loss_registry() discovers classes by walking the package for an
object_type of "loss". The backends tag declares which backends the
class implements, and a backend listed there needs the matching _pennylane
or _torch method. target_dtype tells the Lightning adapter whether
targets are class indices.
One caveat costs people an afternoon. skbase’s class walk skips modules whose name starts with an underscore, so a loss defined in a private module never registers and never says why.
Add the class name to the list above. See the contributing guide.
Base class for backend-dispatching losses. |