bartorch.nlop.Callback

bartorch.nlop.Callback#

class bartorch.nlop.Callback(oshape, ishape, forward, derivative, adjoint)#

A nonlinear operator implemented by Python functions on tensors.

forward evaluates the operator and fixes the point at which derivative and adjoint are taken until the next forward call, which is how BART’s solvers use them.

Parameters:
  • oshape (tuple of int, or a sequence of them) – Codomain and domain shapes, C order. A sequence of shapes makes an operator of that many outputs or inputs.

  • ishape (tuple of int, or a sequence of them) – Codomain and domain shapes, C order. A sequence of shapes makes an operator of that many outputs or inputs.

  • forward (callable) – forward(x) for one input, forward(*xs) for several; it returns one tensor per output. It fixes the linearisation point.

  • derivative (callable) – With one argument each way, derivative(dx) and adjoint(dy). With several, derivative(o, i, dx) and adjoint(o, i, dy): the derivative of output o by input i, and the adjoint of it.

  • adjoint (callable) – With one argument each way, derivative(dx) and adjoint(dy). With several, derivative(o, i, dx) and adjoint(o, i, dy): the derivative of output o by input i, and the adjoint of it.

Examples

>>> Callback((4,), (4,), lambda x: 2 * x, lambda d: 2 * d, lambda v: 2 * v)

Two inputs, so that the second can carry a weight a gradient reaches:

>>> Callback((4,), [(4,), ()], scale, d_scale, adj_scale)
__init__(oshape, ishape, forward, derivative, adjoint)#

Methods

__init__(oshape, ishape, forward, ...)

adjoint(dy)

DF(x)^H dy at the last evaluated point, for one input and one output.

chain(other, *[, output, input])

Output output of self into input input of other.

combine(other)

self and other side by side, sharing nothing.

del_out([output])

Drop an output, and everything computed only for it.

derivative(dx)

DF(x) dx at the last evaluated point, for one input and one output.

dup([a, b])

Make two inputs of the same shape one input, kept at a.

flatten([inputs_only])

Every input as one flat vector, and every output as another.

forward(*xs)

F(x), which also fixes where every derivative is taken.

jacobian([output, input])

DF/dx_input of one output, as a LinearOperator.

linearize(*xs)

The derivative at x, as a LinearOperator.

link([output, input])

Tie an output back into an input; both arguments go away.

permute_inputs(perm)

Reorder the inputs: the new input i is the old perm[i].

permute_outputs(perm)

Reorder the outputs: the new output o is the old perm[o].

pin(input, value)

Fix one input to value; the input goes away.

reshape_input(input, shape)

This operator with one input's shape written differently.

reshape_output(output, shape)

This operator with one output's shape written differently.

shift_input(new, old)

Move one input to another position, the rest closing up behind it.

shift_output(new, old)

Move one output to another position, the rest closing up behind it.

stack_inputs(a, b, axis)

Make two inputs one, concatenated along a C-order axis.

stack_outputs(a, b, axis)

Make two outputs one, concatenated along a C-order axis.

Attributes

device

ishape

The domain, for an operator with one input.

ishapes

The shape of each argument, outputs and inputs kept apart.

oshape

The codomain, for an operator with one output.

oshapes