Invertible Networks API reference

InvertibleNetworks.get_gradsMethod
P = get_grads(NL::Invertible)

Returns a cell array of all parameters gradients in the network or layer. Each cell entry contains a reference to the original parameter's gradient; i.e. modifying the paramters in P, modifies the parameters in NL.

source
InvertibleNetworks.get_paramsMethod
P = get_params(NL::Invertible)

Returns a cell array of all parameters in the network or layer. Each cell entry contains a reference to the original parameter; i.e. modifying the paramters in P, modifies the parameters in NL.

source
InvertibleNetworks.clear_grad!Method
clear_grad!(NL::NeuralNetLayer)

or

clear_grad!(P::AbstractArray{Parameter, 1})

Set gradients of each Parameter in the network layer to nothing.

source

Activation functions

InvertibleNetworks.GaLUgradMethod
Δx = GaLUgrad(Δy, x)

Backpropagate data residual through GaLU activation.

Input:

  • Δy: residual

  • x: original input (since not invertible)

Output:

  • Δx: backpropagated residual

See also: GaLU

source
InvertibleNetworks.ReLUgradMethod
Δx = ReLUgrad(Δy, x)

Backpropagate data residual through ReLU function.

Input:

  • Δy: data residual

  • x: original input (since not invertible)

Output:

  • Δx: backpropagated residual

See also: ReLU

source
InvertibleNetworks.SigmoidGradMethod
Δx = SigmoidGrad(Δy, y; x=nothing, low=nothing, high=nothing)

Backpropagate data residual through Sigmoid function. Can be shifted and scaled such that output is (low,high]

Input:

  • Δy: residual

  • y: original output

  • x: original input, if y not available (in this case, set y=nothing)

  • low: if provided then scale and shift such that output is (low,high]

  • high: if provided then scale and shift such that output is (low,high]

Output:

  • Δx: backpropagated residual

See also: Sigmoid, SigmoidInv

source

Dimensions manipulation

InvertibleNetworks.Haar_squeezeMethod
Y = Haar_squeeze(X)

Perform a 1-level channelwise 2D/3D (lifting) Haar transform of X and squeeze output of each transform to increase channels by factor of 4 in 4D tensor or by factor of 8 in 5D channels.

Input:

  • X: 4D/5D input tensor of dimensions nx x ny (x nz) x n_channel x batchsize

Output:

if 4D tensor:

  • Y: Reshaped tensor of dimensions nx/2 x ny/2 x n_channel*4 x batchsize

or if 5D tensor:

  • Y: Reshaped tensor of dimensions nx/2 x ny/2 x nz/2 x n_channel*8 x batchsize

See also: wavelet_unsqueeze, Haar_unsqueeze, HaarLift, squeeze, unsqueeze

source
InvertibleNetworks.invHaar_unsqueezeMethod
X = invHaar_unsqueeze(Y)

Perform a 1-level inverse 2D/3D Haar transform of Y and unsqueeze output. This reduces the number of channels by factor of 4 in 4D tensors or by factor of 8 in 5D tensors and increases each spatial dimension by a factor of 2. Inverse operation of Haar_squeeze.

Input:

  • Y: 4D/5D input tensor of dimensions nx x ny (x nz) x n_channel x batchsize

Output:

If 4D tensor:

  • X: Reshaped tensor of dimensions nx*2 x ny*2 x n_channel/4 x batchsize

If 5D tensor:

  • X: Reshaped tensor of dimensions nx*2 x ny*2 x nz*2 x n_channel/8 x batchsize

See also: wavelet_unsqueeze, Haar_unsqueeze, HaarLift, squeeze, unsqueeze

source
InvertibleNetworks.squeezeMethod
Y = squeeze(X; pattern="column")

Squeeze operation that is only a reshape.

Reshape input image such that each spatial dimension is reduced by a factor of 2, while the number of channels is increased by a factor of 4 if 4D tensor and increased by a factor of 8 if 5D tensor.

Input:

  • X: 4D/5D input tensor of dimensions nx x ny (x nz) x n_channel x batchsize

  • pattern: Squeezing pattern

     1 2 3 4        1 1 3 3        1 3 1 3
     1 2 3 4        1 1 3 3        2 4 2 4
     1 2 3 4        2 2 4 4        1 3 1 3
     1 2 3 4        2 2 4 4        2 4 2 4
    
     column          patch       checkerboard

Output: if 4D tensor:

  • Y: Reshaped tensor of dimensions nx/2 x ny/2 x n_channel*4 x batchsize

or if 5D tensor:

  • Y: Reshaped tensor of dimensions nx/2 x ny/2 x nz/2 x n_channel*8 x batchsize

See also: unsqueeze, wavelet_squeeze, wavelet_unsqueeze

source
InvertibleNetworks.tensor_catMethod
X = tensor_cat(Y, Z)

Concatenate ND input tensors along the channel dimension. Inverse operation of tensor_split.

Input:

  • Y, Z: ND input tensors, each of dimensions nx [x ny [x nz]] x n_channel x batchsize

Output:

  • X: ND output tensor of dimensions nx [x ny [x nz]] x n_channel*2 x batchsize

See also: tensor_split

source
InvertibleNetworks.tensor_splitMethod
Y, Z = tensor_split(X)

Split ND input tensor in half along the channel dimension. Inverse operation of tensor_cat.

Input:

  • X: ND input tensor of dimensions nx [x ny [x nz]] x n_channel x batchsize

Output:

  • Y, Z: ND output tensors, each of dimensions nx [x ny [x nz]] x n_channel/2 x batchsize

See also: tensor_cat

source
InvertibleNetworks.unsqueezeMethod
X = unsqueeze(Y; pattern="column")

Undo squeezing operation by reshaping input image such that each spatial dimension is increased by a factor of 2, while the number of channels is decreased by a factor of 4 if 4D tensor of decreased by a factor of 8 if a 5D tensor.

Input:

  • Y: 4D/5D input tensor of dimensions nx x ny (x nz) x n_channel x batchsize

  • pattern: Squeezing pattern

         1 2 3 4        1 1 3 3        1 3 1 3
         1 2 3 4        1 1 3 3        2 4 2 4
         1 2 3 4        2 2 4 4        1 3 1 3
         1 2 3 4        2 2 4 4        2 4 2 4
    
         column          patch       checkerboard

Output: If 4D tensor:

  • X: Reshaped tensor of dimensions nx*2 x ny*2 x n_channel/4 x batchsize

If 5D tensor:

  • X: Reshaped tensor of dimensions nx*2 x ny*2 x nz*2 x n_channel/8 x batchsize

See also: squeeze, wavelet_squeeze, wavelet_unsqueeze

source
InvertibleNetworks.wavelet_squeezeMethod
Y = wavelet_squeeze(X; type=WT.db1)

Perform a 1-level channelwise 2D wavelet transform of X and squeeze output of each transform to increase number of channels by a factor of 4 if input is 4D tensor or by a factor of 8 if a 5D tensor.

Input:

  • X: 4D/5D input tensor of dimensions nx x ny (x nz) x n_channel x batchsize

  • type: Wavelet filter type. Possible values are WT.haar for Haar wavelets, WT.coif2, WT.coif4, etc. for Coiflet wavelets, or WT.db1, WT.db2, etc. for Daubechies wavetlets. See https://github.com/JuliaDSP/Wavelets.jl for a full list.

Output: if 4D tensor:

  • Y: Reshaped tensor of dimensions nx/2 x ny/2 x n_channel*4 x batchsize

or if 5D tensor:

  • Y: Reshaped tensor of dimensions nx/2 x ny/2 x nz/2 x n_channel*8 x batchsize

See also: wavelet_unsqueeze, squeeze, unsqueeze

source
InvertibleNetworks.wavelet_unsqueezeMethod
X = wavelet_unsqueeze(Y; type=WT.db1)

Perform a 1-level inverse 2D wavelet transform of Y and unsqueeze output. This reduces the number of channels by factor of 4 if 4D tensor or by a factor of 8 if 5D tensor and increases each spatial dimension by a factor of 2. Inverse operation of wavelet_squeeze.

Input:

  • Y: 4D/5D input tensor of dimensions nx x ny (x nz) x n_channel x batchsize

  • type: Wavelet filter type. Possible values are haar for Haar wavelets,

coif2, coif4, etc. for Coiflet wavelets, or db1, db2, etc. for Daubechies wavetlets. See https://github.com/JuliaDSP/Wavelets.jl for a full list.

Output: If 4D tensor:

  • X: Reshaped tensor of dimensions nx*2 x ny*2 x n_channel/4 x batchsize

If 5D tensor:

  • X: Reshaped tensor of dimensions nx*2 x ny*2 x nz*2 x n_channel/8 x batchsize

See also: wavelet_squeeze, squeeze, unsqueeze

source

Layers

InvertibleNetworks.ActNormType
AN = ActNorm(k; logdet=false)

Create activation normalization layer. The parameters are initialized during the first use, such that the output has zero mean and unit variance along channels for the current mini-batch size.

Input:

  • k: number of channels

  • logdet: bool to indicate whether to compute the logdet

Output:

  • AN: Network layer for activation normalization.

Usage:

  • Forward mode: Y, logdet = AN.forward(X)

  • Inverse mode: X = AN.inverse(Y)

  • Backward mode: ΔX, X = AN.backward(ΔY, Y)

Trainable parameters:

  • Scaling factor AN.s

  • Bias AN.b

See also: get_params, clear_grad!

source
InvertibleNetworks.AffineLayerType
AL = AffineLayer(nx, ny, nc; logdet=false)

Create a layer for an affine transformation.

Input:

  • nx, ny,nc`: input dimensions and number of channels

  • logdet: bool to indicate whether to compute the logdet

Output:

  • AL: Network layer for affine transformation.

Usage:

  • Forward mode: Y, logdet = AL.forward(X)

  • Inverse mode: X = AL.inverse(Y)

  • Backward mode: ΔX, X = AL.backward(ΔY, Y)

Trainable parameters:

  • Scaling factor AL.s

  • Bias AL.b

See also: get_params, clear_grad!

source
InvertibleNetworks.ConditionalLayerGlowType
CL = ConditionalLayerGlow(C::Conv1x1, RB::ResidualBlock; logdet=false)

or

CL = ConditionalLayerGlow(n_in, n_cond, n_hidden; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, logdet=false, ndims=2) (2D)

CL = ConditionalLayerGlow(n_in, n_cond, n_hidden; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, logdet=false, ndims=3) (3D)

CL = ConditionalLayerGlowGlow3D(n_in, n_cond, n_hidden; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, logdet=false) (3D)

Create a Real NVP-style invertible conditional coupling layer based on 1x1 convolutions and a residual block.

Input:

  • C::Conv1x1: 1x1 convolution layer

  • RB::ResidualBlock: residual block layer consisting of 3 convolutional layers with ReLU activations.

  • logdet: bool to indicate whether to compte the logdet of the layer

or

  • n_in,n_out, n_hidden: number of channels for: passive input, conditioned input and hidden layer

  • k1, k2: kernel size of convolutions in residual block. k1 is the kernel of the first and third operator, k2 is the kernel size of the second operator.

  • p1, p2: padding for the first and third convolution (p1) and the second convolution (p2)

  • s1, s2: stride for the first and third convolution (s1) and the second convolution (s2)

  • ndims : number of dimensions

Output:

  • CL: Invertible Real NVP conditional coupling layer.

Usage:

  • Forward mode: Y, logdet = CL.forward(X, C) (if constructed with logdet=true)

  • Inverse mode: X = CL.inverse(Y, C)

  • Backward mode: ΔX, X = CL.backward(ΔY, Y, C)

Trainable parameters:

  • None in CL itself

  • Trainable parameters in residual block CL.RB and 1x1 convolution layer CL.C

See also: Conv1x1, ResidualBlock, get_params, clear_grad!

source
InvertibleNetworks.ConditionalLayerHINTType
CH = ConditionalLayerHINT(n_in, n_hidden; k1=3, k2=3, p1=1, p2=1, s1=1, s2=1, permute=true, ndims=2) (2D)

CH = ConditionalLayerHINT3D(n_in, n_hidden; k1=3, k2=3, p1=1, p2=1, s1=1, s2=1, permute=true) (3D)

Create a conditional HINT layer based on coupling blocks and 1 level recursion.

Input:

  • n_in, n_hidden: number of input and hidden channels of both X and Y

  • k1, k2: kernel size of convolutions in residual block. k1 is the kernel of the first and third operator, k2 is the kernel size of the second operator.

  • p1, p2: padding for the first and third convolution (p1) and the second convolution (p2)

  • s1, s2: stride for the first and third convolution (s1) and the second convolution (s2)

  • permute: bool to indicate whether to permute X and Y. Default is true

  • ndims : number of dimensions

Output:

  • CH: Conditional HINT coupling layer.

Usage:

  • Forward mode: Zx, Zy, logdet = CH.forward_X(X, Y)

  • Inverse mode: X, Y = CH.inverse(Zx, Zy)

  • Backward mode: ΔX, ΔY, X, Y = CH.backward(ΔZx, ΔZy, Zx, Zy)

  • Forward mode Y: Zy = CH.forward_Y(Y)

  • Inverse mode Y: Y = CH.inverse(Zy)

Trainable parameters:

  • None in CH itself

  • Trainable parameters in coupling layers CH.CL_X, CH.CL_Y, CH.CL_YX and in permutation layers CH.C_X and CH.C_Y.

See also: CouplingLayerBasic, ResidualBlock, get_params, clear_grad!

source
InvertibleNetworks.ConditionalResidualBlockType
RB = ConditionalResidualBlock(nx1, nx2, nx_in, ny1, ny2, ny_in, n_hidden, batchsize; k1=3, k2=3, p1=1, p2=1, s1=1, s2=1)

Create a (non-invertible) conditional residual block, consisting of one dense and three convolutional layers with ReLU activation functions. The dense operator maps the data to the image space and both tensors are concatenated and fed to the subsequent convolutional layers.

Input:

  • nx1, nx2, nx_in: spatial dimensions and no. of channels of input image

  • ny1, ny2, ny_in: spatial dimensions and no. of channels of input data

  • n_hidden: number of hidden channels

  • k1, k2: kernel size of convolutions in residual block. k1 is the kernel of the first and third operator, k2 is the kernel size of the second operator.

  • p1, p2: padding for the first and third convolution (p1) and the second convolution (p2)

  • s1, s2: strides for the first and third convolution (s1) and the second convolution (s2)

or

Output:

  • RB: conditional residual block layer

Usage:

  • Forward mode: Zx, Zy = RB.forward(X, Y)

  • Backward mode: ΔX, ΔY = RB.backward(ΔZx, ΔZy, X, Y)

Trainable parameters:

  • Convolutional kernel weights RB.W0, RB.W1, RB.W2 and RB.W3

  • Bias terms RB.b0, RB.b1 and RB.b2

See also: get_params, clear_grad!

source
InvertibleNetworks.Conv1x1Type
C = Conv1x1(k; logdet=false)

or

C = Conv1x1(v1, v2, v3; logdet=false)

Create network layer for 1x1 convolutions using Householder reflections.

Input:

  • k: number of channels

  • v1, v2, v3: Vectors from which to construct matrix.

  • logdet: if true, returns logdet in forward pass (which is always zero)

Output:

  • C: Network layer for 1x1 convolutions with Householder reflections.

Usage:

  • Forward mode: Y, logdet = C.forward(X)

  • Backward mode: ΔX, X = C.backward((ΔY, Y))

Trainable parameters:

  • Householder vectors C.v1, C.v2, C.v3

See also: get_params, clear_grad!

source
InvertibleNetworks.CouplingLayerBasicType
CL = CouplingLayerBasic(RB::ResidualBlock; logdet=false)

or

CL = CouplingLayerBasic(n_in, n_hidden; k1=3, k2=3, p1=1, p2=1, s1=1, s2=1, logdet=false, ndims=2) (2D)

CL = CouplingLayerBasic(n_in, n_hidden; k1=3, k2=3, p1=1, p2=1, s1=1, s2=1, logdet=false, ndims=3) (3D)

CL = CouplingLayerBasic3D(n_in, n_hidden; k1=3, k2=3, p1=1, p2=1, s1=1, s2=1, logdet=false) (3D)

Create a Real NVP-style invertible coupling layer with a residual block.

Input:

  • RB::ResidualBlock: residual block layer consisting of 3 convolutional layers with ReLU activations.

  • logdet: bool to indicate whether to compte the logdet of the layer

or

  • n_in, n_hidden: number of input and hidden channels

  • k1, k2: kernel size of convolutions in residual block. k1 is the kernel of the first and third operator, k2 is the kernel size of the second operator.

  • p1, p2: padding for the first and third convolution (p1) and the second convolution (p2)

  • s1, s2: stride for the first and third convolution (s1) and the second convolution (s1)

  • ndims : Number of dimensions

Output:

  • CL: Invertible Real NVP coupling layer.

Usage:

  • Forward mode: Y1, Y2, logdet = CL.forward(X1, X2) (if constructed with logdet=true)

  • Inverse mode: X1, X2 = CL.inverse(Y1, Y2)

  • Backward mode: ΔX1, ΔX2, X1, X2 = CL.backward(ΔY1, ΔY2, Y1, Y2)

Trainable parameters:

  • None in CL itself

  • Trainable parameters in residual block CL.RB

See also: ResidualBlock, get_params, clear_grad!

source
InvertibleNetworks.CouplingLayerGlowType
CL = CouplingLayerGlow(C::Conv1x1, RB::ResidualBlock; logdet=false)

or

CL = CouplingLayerGlow(n_in, n_hidden; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, logdet=false, ndims=2) (2D)

CL = CouplingLayerGlow(n_in, n_hidden; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, logdet=false, ndims=3) (3D)

CL = CouplingLayerGlow3D(n_in, n_hidden; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, logdet=false) (3D)

Create a Real NVP-style invertible coupling layer based on 1x1 convolutions and a residual block.

Input:

  • C::Conv1x1: 1x1 convolution layer

  • RB::ResidualBlock: residual block layer consisting of 3 convolutional layers with ReLU activations.

  • logdet: bool to indicate whether to compte the logdet of the layer

or

  • n_in, n_hidden: number of input and hidden channels

  • k1, k2: kernel size of convolutions in residual block. k1 is the kernel of the first and third operator, k2 is the kernel size of the second operator.

  • p1, p2: padding for the first and third convolution (p1) and the second convolution (p2)

  • s1, s2: stride for the first and third convolution (s1) and the second convolution (s2)

  • ndims : number of dimensions

Output:

  • CL: Invertible Real NVP coupling layer.

Usage:

  • Forward mode: Y, logdet = CL.forward(X) (if constructed with logdet=true)

  • Inverse mode: X = CL.inverse(Y)

  • Backward mode: ΔX, X = CL.backward(ΔY, Y)

Trainable parameters:

  • None in CL itself

  • Trainable parameters in residual block CL.RB and 1x1 convolution layer CL.C

See also: Conv1x1, ResidualBlock, get_params, clear_grad!

source
InvertibleNetworks.CouplingLayerHINTType
H = CouplingLayerHINT(n_in, n_hidden; logdet=false, permute="none", k1=3, k2=3, p1=1, p2=1, s1=1, s2=1, ndims=2) (2D)

H = CouplingLayerHINT(n_in, n_hidden; logdet=false, permute="none", k1=3, k2=3, p1=1, p2=1, s1=1, s2=1, ndims=3) (3D)

H = CouplingLayerHINT3D(n_in, n_hidden; logdet=false, permute="none", k1=3, k2=3, p1=1, p2=1, s1=1, s2=1) (3D)

Create a recursive HINT-style invertible layer based on coupling blocks.

Input:

  • n_in, n_hidden: number of input and hidden channels

  • logdet: bool to indicate whether to return the log determinant. Default is false.

  • permute: string to specify permutation. Options are "none", "lower", "both" or "full".

  • k1, k2: kernel size of convolutions in residual block. k1 is the kernel of the first and third operator, k2 is the kernel size of the second operator.

  • p1, p2: padding for the first and third convolution (p1) and the second convolution (p2)

  • s1, s2: stride for the first and third convolution (s1) and the second convolution (s2)

  • ndims : number of dimensions

Output:

  • H: Recursive invertible HINT coupling layer.

Usage:

  • Forward mode: Y = H.forward(X)

  • Inverse mode: X = H.inverse(Y)

  • Backward mode: ΔX, X = H.backward(ΔY, Y)

Trainable parameters:

  • None in H itself

  • Trainable parameters in coupling layers H.CL

See also: CouplingLayerBasic, ResidualBlock, get_params, clear_grad!

source
InvertibleNetworks.CouplingLayerIRIMType
IL = CouplingLayerIRIM(C::Conv1x1, RB::ResidualBlock)

or

IL = CouplingLayerIRIM(n_in, n_hidden; k1=4, k2=3, p1=0, p2=1, s1=4, s2=1, logdet=false, ndims=2) (2D)

IL = CouplingLayerIRIM(n_in, n_hidden; k1=4, k2=3, p1=0, p2=1, s1=4, s2=1, logdet=false, ndims=3) (3D)

IL = CouplingLayerIRIM3D(n_in, n_hidden; k1=4, k2=3, p1=0, p2=1, s1=4, s2=1, logdet=false) (3D)

Create an i-RIM invertible coupling layer based on 1x1 convolutions and a residual block.

Input:

  • C::Conv1x1: 1x1 convolution layer
  • RB::ResidualBlock: residual block layer consisting of 3 convolutional layers with ReLU activations.

or

  • nx, ny, nz: spatial dimensions of input
  • n_in, n_hidden: number of input and hidden channels

  • k1, k2: kernel size of convolutions in residual block. k1 is the kernel of the first and third operator, k2 is the kernel size of the second operator.

  • p1, p2: padding for the first and third convolution (p1) and the second convolution (p2)

  • s1, s2: stride for the first and third convolution (s1) and the second convolution (s2)

Output:

  • IL: Invertible i-RIM coupling layer.

Usage:

  • Forward mode: Y = IL.forward(X)

  • Inverse mode: X = IL.inverse(Y)

  • Backward mode: ΔX, X = IL.backward(ΔY, Y)

Trainable parameters:

  • None in IL itself

  • Trainable parameters in residual block IL.RB and 1x1 convolution layer IL.C

See also: Conv1x1, ResidualBlock!, get_params, clear_grad!

source
InvertibleNetworks.FluxBlockType
FB = FluxBlock(model::Chain)

Create a (non-invertible) neural network block from a Flux network.

Input:

  • model: Flux neural network of type Chain

Output:

  • FB: residual block layer

Usage:

  • Forward mode: Y = FB.forward(X)

  • Backward mode: ΔX = FB.backward(ΔY, X)

Trainable parameters:

  • Network parameters given by Flux.parameters(model)

See also: Chain, get_params, clear_grad!

source
InvertibleNetworks.HyperbolicLayerType
HyperbolicLayer(n_in, kernel, stride, pad; action=0, α=1f0, n_hidden=1)
HyperbolicLayer(n_in, kernel, stride, pad; action=0, α=1f0, n_hidden=1, ndims=2)
HyperbolicLayer3D(n_in, kernel, stride, pad; action=0, α=1f0, n_hidden=1)

or

HyperbolicLayer(W, b, stride, pad; action=0, α=1f0)
HyperbolicLayer3D(W, b, stride, pad; action=0, α=1f0)

Create an invertible hyperbolic coupling layer.

Input:

  • kernel, stride, pad: Kernel size, stride and padding of the convolutional operator

  • action: String that defines whether layer keeps the number of channels fixed (0), increases it by a factor of 4 (or 8 in 3D) (1) or decreased it by a factor of 4 (or 8) (-1).

  • W, b: Convolutional weight and bias. W has dimensions of (kernel, kernel, n_in, n_in). b has dimensions of n_in.

  • α: Step size for second time derivative. Default is 1.

  • n_hidden: Increase the no. of channels by n_hidden in the forward convolution. After applying the transpose convolution, the dimensions are back to the input dimensions.

  • ndims: Number of dimension of the input (2 for 2D, 3 for 3D)

Output:

  • HL: Invertible hyperbolic coupling layer

Usage:

  • Forward mode: X_curr, X_new = HL.forward(X_prev, X_curr)

  • Inverse mode: X_prev, X_curr = HL.inverse(X_curr, X_new)

  • Backward mode: ΔX_prev, ΔX_curr, X_prev, X_curr = HL.backward(ΔX_curr, ΔX_new, X_curr, X_new)

Trainable parameters:

  • HL.W: Convolutional kernel

  • HL.b: Bias

See also: get_params, clear_grad!

source
InvertibleNetworks.ResidualBlockType
RB = ResidualBlock(n_in, n_hidden; k1=3, k2=3, p1=1, p2=1, s1=1, s2=1, fan=false)
RB = ResidualBlock3D(n_in, n_hidden; k1=3, k2=3, p1=1, p2=1, s1=1, s2=1, fan=false)

or

RB = ResidualBlock(W1, W2, W3, b1, b2; p1=1, p2=1, s1=1, s2=1, fan=false)
RB = ResidualBlock3D(W1, W2, W3, b1, b2; p1=1, p2=1, s1=1, s2=1, fan=false)

Create a (non-invertible) residual block, consisting of three convolutional layers and activation functions. The first convolution is a downsampling operation with a stride equal to the kernel dimension. The last convolution is the corresponding transpose operation and upsamples the data to either its original dimensions or to twice the number of input channels (for fan=true). The first and second layer contain a bias term.

Input:

  • n_in: number of input channels

  • n_hidden: number of hidden channels

  • n_out: number of ouput channels

  • activation: activation type between conv layers and final output

  • k1, k2: kernel size of convolutions in residual block. k1 is the kernel of the first and third operator, k2 is the kernel size of the second operator.

  • p1, p2: padding for the first and third convolution (p1) and the second convolution (p2)

  • s1, s2: stride for the first and third convolution (s1) and the second convolution (s2)

  • fan: bool to indicate whether the ouput has twice the number of input channels. For fan=false, the last activation function is a gated linear unit (thereby bringing the output back to the original dimensions). For fan=true, the last activation is a ReLU, in which case the output has twice the number of channels as the input.

or

  • W1, W2, W3: 4D tensors of convolutional weights

  • b1, b2: bias terms

Output:

  • RB: residual block layer

Usage:

  • Forward mode: Y = RB.forward(X)

  • Backward mode: ΔX = RB.backward(ΔY, X)

Trainable parameters:

  • Convolutional kernel weights RB.W1, RB.W2 and RB.W3

  • Bias terms RB.b1 and RB.b2

See also: get_params, clear_grad!

source

Networks

InvertibleNetworks.NetworkConditionalGlowType
G = NetworkGlow(n_in, n_cond, n_hidden, L, K; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1)

G = NetworkGlow3D(n_in, n_cond, n_hidden, L, K; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1)

Create a conditional invertible network based on the Glow architecture. Each flow step in the inner loop consists of an activation normalization layer, followed by an invertible coupling layer with 1x1 convolutions and a residual block. The outer loop performs a squeezing operation prior to the inner loop, and a splitting operation afterwards.

Input:

  • 'n_in': number of input channels of variable to sample

  • 'n_cond': number of input channels of condition

  • n_hidden: number of hidden units in residual blocks

  • L: number of scales (outer loop)

  • K: number of flow steps per scale (inner loop)

  • split_scales: if true, perform squeeze operation which halves spatial dimensions and duplicates channel dimensions then split output in half along channel dimension after each scale. Feed one half through the next layers, while saving the remaining channels for the output.

  • k1, k2: kernel size of convolutions in residual block. k1 is the kernel of the first and third

operator, k2 is the kernel size of the second operator.

  • p1, p2: padding for the first and third convolution (p1) and the second convolution (p2)

  • s1, s2: stride for the first and third convolution (s1) and the second convolution (s2)

  • ndims : number of dimensions

  • squeeze_type : squeeze type that happens at each multiscale level

Output:

  • G: invertible Glow network.

Usage:

  • Forward mode: ZX, ZC logdet = G.forward(X, C)

  • Backward mode: ΔX, X, ΔC = G.backward(ΔZX, ZX, ZC)

Trainable parameters:

  • None in G itself

  • Trainable parameters in activation normalizations G.AN[i,j] and coupling layers G.C[i,j], where i and j range from 1 to L and K respectively.

See also: ActNorm, CouplingLayerGlow!, get_params, clear_grad!

source
InvertibleNetworks.NetworkConditionalHINTType
CH = NetworkConditionalHINT(n_in, n_hidden, depth; k1=3, k2=3, p1=1, p2=1, s1=1, s2=1)

CH = NetworkConditionalHINT3D(n_in, n_hidden, depth; k1=3, k2=3, p1=1, p2=1, s1=1, s2=1)

Create a conditional HINT network for data-driven generative modeling based on the change of variables formula.

Input:

  • 'n_in': number of input channels

  • n_hidden: number of hidden units in residual blocks

  • depth: number network layers

  • k1, k2: kernel size for first and third residual layer (k1) and second layer (k2)

  • p1, p2: respective padding sizes for residual block layers

  • s1, s2: respective strides for residual block layers

Output:

  • CH: conditioinal HINT network

Usage:

  • Forward mode: Zx, Zy, logdet = CH.forward(X, Y)

  • Inverse mode: X, Y = CH.inverse(Zx, Zy)

  • Backward mode: ΔX, X = CH.backward(ΔZx, ΔZy, Zx, Zy)

Trainable parameters:

  • None in CH itself

  • Trainable parameters in activation normalizations CH.AN_X[i] and CH.AN_Y[i],

and in coupling layers CH.CL[i], where i ranges from 1 to depth.

See also: ActNorm, ConditionalLayerHINT!, get_params, clear_grad!

source
InvertibleNetworks.NetworkGlowType
G = NetworkGlow(n_in, n_hidden, L, K; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1)

G = NetworkGlow3D(n_in, n_hidden, L, K; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1)

Create an invertible network based on the Glow architecture. Each flow step in the inner loop consists of an activation normalization layer, followed by an invertible coupling layer with 1x1 convolutions and a residual block. The outer loop performs a squeezing operation prior to the inner loop, and a splitting operation afterwards.

Input:

  • 'n_in': number of input channels

  • n_hidden: number of hidden units in residual blocks

  • L: number of scales (outer loop)

  • K: number of flow steps per scale (inner loop)

  • split_scales: if true, perform squeeze operation which halves spatial dimensions and duplicates channel dimensions then split output in half along channel dimension after each scale. Feed one half through the next layers, while saving the remaining channels for the output.

  • k1, k2: kernel size of convolutions in residual block. k1 is the kernel of the first and third

operator, k2 is the kernel size of the second operator.

  • p1, p2: padding for the first and third convolution (p1) and the second convolution (p2)

  • s1, s2: stride for the first and third convolution (s1) and the second convolution (s2)

  • ndims : number of dimensions

  • squeeze_type : squeeze type that happens at each multiscale level

  • logdet : boolean to turn on/off logdet term tracking and gradient calculation

Output:

  • G: invertible Glow network.

Usage:

  • Forward mode: Y, logdet = G.forward(X)

  • Backward mode: ΔX, X = G.backward(ΔY, Y)

Trainable parameters:

  • None in G itself

  • Trainable parameters in activation normalizations G.AN[i,j] and coupling layers G.C[i,j], where i and j range from 1 to L and K respectively.

See also: ActNorm, CouplingLayerGlow!, get_params, clear_grad!

source
InvertibleNetworks.NetworkHyperbolicType
H = NetworkHyperbolic(n_in, architecture; k=3, s=1, p=1, logdet=true, α=1f0)
H = NetworkHyperbolic(n_in, architecture; k=3, s=1, p=1, logdet=true, α=1f0, ndims=2)
H = NetworkHyperbolic3D(n_in, architecture; k=3, s=1, p=1, logdet=true, α=1f0)

Create an invertible network based on hyperbolic layers. The network architecture is specified by a tuple of the form ((action1, nhidden1), (action2, nhidden2), ... ). Each inner tuple corresonds to an additional layer. The first inner tuple argument specifies whether the respective layer increases the number of channels (set to 1), decreases it (set to -1) or leaves it constant (set to 0). The second argument specifies the number of hidden units for that layer.

Input:

  • n_in: number of channels of input tensor.
  • n_hidden: number of hidden units in residual blocks

  • architecture: Tuple of tuples specifying the network architecture; ((action1, nhidden1), (action2, nhidden2))

  • k, s, p: Kernel size, stride and padding of convolutional kernels

  • logdet: Bool to indicate whether to return the logdet

  • α: Step size in hyperbolic network. Defaults to 1

  • ndims: Number of dimension

Output:

  • H: invertible hyperbolic network.

Usage:

  • Forward mode: Y_prev, Y_curr, logdet = H.forward(X_prev, X_curr)

  • Inverse mode: X_curr, X_new = H.inverse(Y_curr, Y_new)

  • Backward mode: ΔX_curr, ΔX_new, X_curr, X_new = H.backward(ΔY_curr, ΔY_new, Y_curr, Y_new)

Trainable parameters:

  • None in H itself

  • Trainable parameters in the hyperbolic layers H.HL[j].

See also: CouplingLayer!, get_params, clear_grad!

source
InvertibleNetworks.NetworkLoopType
L = NetworkLoop(n_in, n_hidden, maxiter, Ψ; k1=4, k2=3, p1=0, p2=1, s1=4, s2=1, ndims=2) (2D)

L = NetworkLoop3D(n_in, n_hidden, maxiter, Ψ; k1=4, k2=3, p1=0, p2=1, s1=4, s2=1) (3D)

Create an invertibel recurrent inference machine (i-RIM) consisting of an unrooled loop for a given number of iterations.

Input:

  • 'n_in': number of input channels

  • n_hidden: number of hidden units in residual blocks

  • maxiter: number unrolled loop iterations

  • Ψ: link function

  • k1, k2: stencil sizes for convolutions in the residual blocks. The first convolution uses a stencil of size and stride k1, thereby downsampling the input. The second convolutions uses a stencil of size k2. The last layer uses a stencil of size and stride k1, but performs the transpose operation of the first convolution, thus upsampling the output to the original input size.

  • p1, p2: padding for the first and third convolution (p1) and the second convolution (p2) in residual block

  • s1, s2: stride for the first and third convolution (s1) and the second convolution (s2) in residual block

  • ndims : number of dimensions

Output:

  • L: invertible i-RIM network.

Usage:

  • Forward mode: η_out, s_out = L.forward(η_in, s_in, d, A)

  • Inverse mode: η_in, s_in = L.inverse(η_out, s_out, d, A)

  • Backward mode: Δη_in, Δs_in, η_in, s_in = L.backward(Δη_out, Δs_out, η_out, s_out, d, A)

Trainable parameters:

  • None in L itself

  • Trainable parameters in the invertible coupling layers L.L[i], and actnorm layers L.AN[i], where i ranges from 1 to the number of loop iterations.

See also: CouplingLayerIRIM, ResidualBlock, get_params, clear_grad!

source
InvertibleNetworks.NetworkMultiScaleConditionalHINTType
CH = NetworkMultiScaleConditionalHINT(n_in, n_hidden, L, K; split_scales=false, k1=3, k2=3, p1=1, p2=1, s1=1, s2=1)

CH = NetworkMultiScaleConditionalHINT3D(n_in, n_hidden, L, K; split_scales=false, k1=3, k2=3, p1=1, p2=1, s1=1, s2=1)

Create a conditional HINT network for data-driven generative modeling based on the change of variables formula.

Input:

  • 'n_in': number of input channels
  • n_hidden: number of hidden units in residual blocks

  • L: number of scales (outer loop)

  • K: number of flow steps per scale (inner loop)

  • split_scales: if true, split output in half along channel dimension after each scale. Feed one half through the next layers, while saving the remaining channels for the output.

  • k1, k2: kernel size for first and third residual layer (k1) and second layer (k2)

  • p1, p2: respective padding sizes for residual block layers

  • s1, s2: respective strides for residual block layers

  • ndims : number of dimensions

Output:

  • CH: conditional HINT network

Usage:

  • Forward mode: Zx, Zy, logdet = CH.forward(X, Y)

  • Inverse mode: X, Y = CH.inverse(Zx, Zy)

  • Backward mode: ΔX, X = CH.backward(ΔZx, ΔZy, Zx, Zy)

Trainable parameters:

  • None in CH itself

  • Trainable parameters in activation normalizations CH.AN_X[i] and CH.AN_Y[i],

and in coupling layers CH.CL[i], where i ranges from 1 to depth.

See also: ActNorm, ConditionalLayerHINT!, get_params, clear_grad!

source
InvertibleNetworks.NetworkMultiScaleHINTType
H = NetworkMultiScaleHINT(n_in, n_hidden, L, K; split_scales=false, k1=3, k2=3, p1=1, p2=1, s1=1, s2=1, ndims=2)

H = NetworkMultiScaleHINT3D(n_in, n_hidden, L, K; split_scales=false, k1=3, k2=3, p1=1, p2=1, s1=1, s2=1)

Create a multiscale HINT network for data-driven generative modeling based on the change of variables formula.

Input:

  • 'n_in': number of input channels
  • n_hidden: number of hidden units in residual blocks

  • L: number of scales (outer loop)

  • K: number of flow steps per scale (inner loop)

  • split_scales: if true, split output in half along channel dimension after each scale. Feed one half through the next layers, while saving the remaining channels for the output.

  • k1, k2: kernel size for first and third residual layer (k1) and second layer (k2)

  • p1, p2: respective padding sizes for residual block layers

  • s1, s2: respective strides for residual block layers

  • ndims : number of dimensions

Output:

  • H: multiscale HINT network

Usage:

  • Forward mode: Z, logdet = H.forward(X)

  • Inverse mode: X = H.inverse(Z)

  • Backward mode: ΔX, X = H.backward(ΔZ, Z)

Trainable parameters:

  • None in H itself

  • Trainable parameters in activation normalizations H.AN[i],

and in coupling layers H.CL[i], where i ranges from 1 to depth.

See also: ActNorm, CouplingLayerHINT!, get_params, clear_grad!

source
InvertibleNetworks.SummarizedNetType
G = SummarizedNet(cond_net, sum_net)

Create a summarized neural conditional approximator from conditional approximator condnet and summary network sumnet.

Input:

  • 'cond_net': invertible conditional distribution approximator

  • 'sum_net': Should be flux layer. summary network. Should be invariant to a dimension of interest.

Output:

  • G: summarized network.

Usage:

  • Forward mode: ZX, ZY, logdet = G.forward(X, Y)

  • Backward mode: ΔX, X, ΔY = G.backward(ΔZX, ZX, ZY; Y_save=Y)

  • inverse mode: ZX, ZY logdet = G.inverse(ZX, ZY)

Trainable parameters:

  • None in G itself

  • Trainable parameters in conditional approximator G.cond_net and smmary network G.sum_net,

See also: ActNorm, CouplingLayerGlow!, get_params, clear_grad!

source

AD Integration