A trainable parameter with no elements, kept in the autograd graph by the loss.
| 76 | |
| 77 | |
| 78 | class EmptyTailModel(torch.nn.Module): |
| 79 | """A trainable parameter with no elements, kept in the autograd graph by the loss.""" |
| 80 | |
| 81 | def __init__(self, hidden=HIDDEN): |
| 82 | super().__init__() |
| 83 | self.dense = torch.nn.Linear(hidden, hidden, bias=False) |
| 84 | self.empty = torch.nn.Linear(hidden, 0, bias=False) |
| 85 | |
| 86 | def forward(self, x): |
| 87 | hidden = self.dense(x) |
| 88 | # `empty(hidden)` is (batch, 0); summing it keeps the parameter in the graph. |
| 89 | return hidden.sum() + self.empty(hidden).sum() |
| 90 | |
| 91 | |
| 92 | def _engine(case): |