Bernoulli
Bernoulli(event_dims=0)Sigmoid likelihood: each entry of \(f\) is the logit of an independent 0/1 target.
Per entry, with \(s(f) = 1 / (1 + e^{-f})\) the sigmoid,
\[\log p(y \mid f) = y \log s(f) + (1 - y) \log s(-f), \qquad H = \operatorname{diag}\big(s(f) \, s(-f)\big),\]
where \(H\) is the output Hessian.
Targets are shaped like \(f\): 0/1 labels (bool or int are cast to \(f\)’s dtype) or soft labels in \([0, 1]\). The fit refuses other values (check_targets), and so does the predictive’s log_prob, which takes only 0/1. log_prob is per position: \(f\)’s shape without the last event_dims, over which it sums. The default link is the sigmoid Probit on an exact output covariance and MC over the draws of a sampled one.
BCEWithLogitsLoss’s pos_weight has no counterpart: it reweights the positive targets, so the output Hessian would depend on the targets. Its weight is left out on purpose; Batch.weights gives per-example weights.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| event_dims | int | How many trailing dims of \(f\) form one event. | 0 |
Raises
| Name | Type | Description |
|---|---|---|
| StructureError | event_dims is negative. |
Methods
| Name | Description |
|---|---|
| average | The mean of \(s(f)\) over the \(S\) draws, as logits computed in log space. |
| check_targets | Raise ValueError unless every target lies in \([0, 1]\). |
| default_link | Probit for an exact output covariance (n_samples=None), else MC over all draws. |
| distribution | A torch Bernoulli with logits \(f\), the last event_dims dims as the event. |
| log_prob | Log-likelihood per position, summed over the last event_dims dims. |
| output_hessian | \(\operatorname{diag}\big(s(f) \, s(-f)\big)\), shaped like \(f\). |
average
Bernoulli.average(f, /)The mean of \(s(f)\) over the \(S\) draws, as logits computed in log space.
\[\log \frac{1}{S} \sum_{i=1}^S s(f_i) - \log \frac{1}{S} \sum_{i=1}^S s(-f_i),\]
which stays finite when saturated.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| f | Tensor | Output draws shaped [S, N, ...]: S draws for N examples. |
required |
Returns
| Name | Type | Description |
|---|---|---|
| Distribution | A distribution over targets, one per example. |
check_targets
Bernoulli.check_targets(y, /)Raise ValueError unless every target lies in \([0, 1]\).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| y | Targets | The batch’s targets. | required |
default_link
Bernoulli.default_link(n_samples, /)Probit for an exact output covariance (n_samples=None), else MC over all draws.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| n_samples | int | Literal['exact'] | The resolved Linearized.n_samples: "exact" for an exact output covariance, an int for a SampleCov with that many draws. An MC link must not ask for more. |
required |
Returns
| Name | Type | Description |
|---|---|---|
| LinkApprox | A link that supports this likelihood. |
distribution
Bernoulli.distribution(f, /)A torch Bernoulli with logits \(f\), the last event_dims dims as the event.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| f | Outputs | Model outputs for a batch. | required |
Returns
| Name | Type | Description |
|---|---|---|
| Distribution | A torch distribution over the batch’s targets. |
log_prob
Bernoulli.log_prob(f, y, /)Log-likelihood per position, summed over the last event_dims dims.
Soft labels are taken as they are; check_targets is what refuses values outside \([0, 1]\).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| f | Outputs | Model outputs for a batch. | required |
| y | Targets | Targets for the same batch. | required |
Returns
| Name | Type | Description |
|---|---|---|
| Tensor | A tensor shaped [N], or [N, *positions] (per token, say). |
Raises
| Name | Type | Description |
|---|---|---|
| StructureError | y is not shaped like f. |
output_hessian
Bernoulli.output_hessian(f, /)\(\operatorname{diag}\big(s(f) \, s(-f)\big)\), shaped like \(f\).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| f | Outputs | Model outputs for a batch. | required |
Returns
| Name | Type | Description |
|---|---|---|
| OutputHessian | The per-example output Hessian. |