Invertible Networks API reference
InvertibleNetworks.clear_grad!
— MethodP = clear_grad!(NL::Invertible)
Resets the gradient of all the parameters in NL
InvertibleNetworks.get_grads
— MethodP = 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
.
InvertibleNetworks.get_params
— MethodP = 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
.
InvertibleNetworks.reset!
— MethodP = reset!(NL::Invertible)
Resets the data of all the parameters in NL
InvertibleNetworks.clear_grad!
— Methodclear_grad!(NL::NeuralNetLayer)
or
clear_grad!(P::AbstractArray{Parameter, 1})
Set gradients of each Parameter
in the network layer to nothing
.
Activation functions
InvertibleNetworks.ExpClamp
— Methody = ExpClamp(x)
Soft-clamped exponential function. See also: ExpClampGrad
InvertibleNetworks.ExpClampInv
— Methodx = ExpClampInv(y)
Inverse of ExpClamp function. See also: ExpClamp
, ExpClampGrad
InvertibleNetworks.GaLU
— MethodInvertibleNetworks.GaLUgrad
— MethodΔx = GaLUgrad(Δy, x)
Backpropagate data residual through GaLU activation.
Input:
Δy
: residualx
: original input (since not invertible)
Output:
Δx
: backpropagated residual
See also: GaLU
InvertibleNetworks.LeakyReLU
— MethodInvertibleNetworks.LeakyReLUgrad
— MethodΔx = ReLUgrad(Δy, y; slope=0.01f0)
Backpropagate data residual through leaky ReLU function.
Input:
Δy
: residualy
: original outputslope
: slope of non-active part of ReLU
Output:
Δx
: backpropagated residual
See also: LeakyReLU
, LeakyReLUinv
InvertibleNetworks.LeakyReLUinv
— MethodInvertibleNetworks.ReLU
— MethodInvertibleNetworks.ReLUgrad
— MethodΔx = ReLUgrad(Δy, x)
Backpropagate data residual through ReLU function.
Input:
Δy
: data residualx
: original input (since not invertible)
Output:
Δx
: backpropagated residual
See also: ReLU
InvertibleNetworks.Sigmoid
— Methody = Sigmoid(x; low=0, high=1)
Sigmoid activation function. Shifted and scaled such that output is [low,high].
See also: SigmoidInv
, SigmoidGrad
InvertibleNetworks.SigmoidGrad
— MethodΔ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
: residualy
: original outputx
: 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
InvertibleNetworks._sigmoidinv
— Methodx = SigmoidInv(y; low=0, high=1f0)
Inverse of Sigmoid function. Shifted and scaled such that output is [low,high]
See also: Sigmoid
, SigmoidGrad
Dimensions manipulation
InvertibleNetworks.Haar_squeeze
— MethodY = 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 dimensionsnx
xny
(xnz
) xn_channel
xbatchsize
Output:
if 4D tensor:
Y
: Reshaped tensor of dimensionsnx/2
xny/2
xn_channel*4
xbatchsize
or if 5D tensor:
Y
: Reshaped tensor of dimensionsnx/2
xny/2
xnz/2
xn_channel*8
xbatchsize
See also: wavelet_unsqueeze
, Haar_unsqueeze
, HaarLift
, squeeze
, unsqueeze
InvertibleNetworks.invHaar_unsqueeze
— MethodX = 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 dimensionsnx
xny
(xnz
) xn_channel
xbatchsize
Output:
If 4D tensor:
X
: Reshaped tensor of dimensionsnx*2
xny*2
xn_channel/4
xbatchsize
If 5D tensor:
X
: Reshaped tensor of dimensionsnx*2
xny*2
xnz*2
xn_channel/8
xbatchsize
See also: wavelet_unsqueeze
, Haar_unsqueeze
, HaarLift
, squeeze
, unsqueeze
InvertibleNetworks.squeeze
— MethodY = 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 dimensionsnx
xny
(xnz
) xn_channel
xbatchsize
pattern
: Squeezing pattern1 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 dimensionsnx/2
xny/2
xn_channel*4
xbatchsize
or if 5D tensor:
Y
: Reshaped tensor of dimensionsnx/2
xny/2
xnz/2
xn_channel*8
xbatchsize
See also: unsqueeze
, wavelet_squeeze
, wavelet_unsqueeze
InvertibleNetworks.tensor_cat
— MethodX = 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 dimensionsnx
[xny
[xnz
]] xn_channel
xbatchsize
Output:
X
: ND output tensor of dimensionsnx
[xny
[xnz
]] xn_channel*2
xbatchsize
See also: tensor_split
InvertibleNetworks.tensor_split
— MethodY, 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 dimensionsnx
[xny
[xnz
]] xn_channel
xbatchsize
Output:
Y
,Z
: ND output tensors, each of dimensionsnx
[xny
[xnz
]] xn_channel/2
xbatchsize
See also: tensor_cat
InvertibleNetworks.unsqueeze
— MethodX = 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 dimensionsnx
xny
(xnz
) xn_channel
xbatchsize
pattern
: Squeezing pattern1 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 dimensionsnx*2
xny*2
xn_channel/4
xbatchsize
If 5D tensor:
X
: Reshaped tensor of dimensionsnx*2
xny*2
xnz*2
xn_channel/8
xbatchsize
See also: squeeze
, wavelet_squeeze
, wavelet_unsqueeze
InvertibleNetworks.wavelet_squeeze
— MethodY = 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 dimensionsnx
xny
(xnz
) xn_channel
xbatchsize
type
: Wavelet filter type. Possible values areWT.haar
for Haar wavelets,WT.coif2
,WT.coif4
, etc. for Coiflet wavelets, orWT.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 dimensionsnx/2
xny/2
xn_channel*4
xbatchsize
or if 5D tensor:
Y
: Reshaped tensor of dimensionsnx/2
xny/2
xnz/2
xn_channel*8
xbatchsize
See also: wavelet_unsqueeze
, squeeze
, unsqueeze
InvertibleNetworks.wavelet_unsqueeze
— MethodX = 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 dimensionsnx
xny
(xnz
) xn_channel
xbatchsize
type
: Wavelet filter type. Possible values arehaar
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 dimensionsnx*2
xny*2
xn_channel/4
xbatchsize
If 5D tensor:
X
: Reshaped tensor of dimensionsnx*2
xny*2
xnz*2
xn_channel/8
xbatchsize
See also: wavelet_squeeze
, squeeze
, unsqueeze
Layers
InvertibleNetworks.ActNorm
— TypeAN = 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 channelslogdet
: 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!
InvertibleNetworks.AffineLayer
— TypeAL = AffineLayer(nx, ny, nc; logdet=false)
Create a layer for an affine transformation.
Input:
nx
,ny,
nc`: input dimensions and number of channelslogdet
: 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!
InvertibleNetworks.ConditionalLayerGlow
— TypeCL = 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 layerRB::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 layerk1
,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 withlogdet=true
)Inverse mode:
X = CL.inverse(Y, C)
Backward mode:
ΔX, X = CL.backward(ΔY, Y, C)
Trainable parameters:
None in
CL
itselfTrainable parameters in residual block
CL.RB
and 1x1 convolution layerCL.C
See also: Conv1x1
, ResidualBlock
, get_params
, clear_grad!
InvertibleNetworks.ConditionalLayerHINT
— TypeCH = 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 bothX
andY
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 permuteX
andY
. Default istrue
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
itselfTrainable parameters in coupling layers
CH.CL_X
,CH.CL_Y
,CH.CL_YX
and in permutation layersCH.C_X
andCH.C_Y
.
See also: CouplingLayerBasic
, ResidualBlock
, get_params
, clear_grad!
InvertibleNetworks.ConditionalResidualBlock
— TypeRB = 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 imageny1
,ny2
,ny_in
: spatial dimensions and no. of channels of input datan_hidden
: number of hidden channelsk1
,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
andRB.W3
Bias terms
RB.b0
,RB.b1
andRB.b2
See also: get_params
, clear_grad!
InvertibleNetworks.Conv1x1
— TypeC = 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 channelsv1
,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!
InvertibleNetworks.CouplingLayerBasic
— TypeCL = 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 channelsk1
,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 withlogdet=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
itselfTrainable parameters in residual block
CL.RB
See also: ResidualBlock
, get_params
, clear_grad!
InvertibleNetworks.CouplingLayerGlow
— TypeCL = 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 layerRB::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 channelsk1
,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 withlogdet=true
)Inverse mode:
X = CL.inverse(Y)
Backward mode:
ΔX, X = CL.backward(ΔY, Y)
Trainable parameters:
None in
CL
itselfTrainable parameters in residual block
CL.RB
and 1x1 convolution layerCL.C
See also: Conv1x1
, ResidualBlock
, get_params
, clear_grad!
InvertibleNetworks.CouplingLayerHINT
— TypeH = 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 channelslogdet
: bool to indicate whether to return the log determinant. Default isfalse
.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
itselfTrainable parameters in coupling layers
H.CL
See also: CouplingLayerBasic
, ResidualBlock
, get_params
, clear_grad!
InvertibleNetworks.CouplingLayerIRIM
— TypeIL = 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 channelsk1
,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
itselfTrainable parameters in residual block
IL.RB
and 1x1 convolution layerIL.C
See also: Conv1x1
, ResidualBlock!
, get_params
, clear_grad!
InvertibleNetworks.FluxBlock
— TypeFB = FluxBlock(model::Chain)
Create a (non-invertible) neural network block from a Flux network.
Input:
model
: Flux neural network of typeChain
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!
InvertibleNetworks.HyperbolicLayer
— TypeHyperbolicLayer(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 operatoraction
: 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 ofn_in
.α
: Step size for second time derivative. Default is 1.n_hidden
: Increase the no. of channels byn_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 kernelHL.b
: Bias
See also: get_params
, clear_grad!
InvertibleNetworks.ResidualBlock
— TypeRB = 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 channelsn_hidden
: number of hidden channelsn_out
: number of ouput channelsactivation
: activation type between conv layers and final outputk1
,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. Forfan=false
, the last activation function is a gated linear unit (thereby bringing the output back to the original dimensions). Forfan=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 weightsb1
,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
andRB.W3
Bias terms
RB.b1
andRB.b2
See also: get_params
, clear_grad!
Networks
InvertibleNetworks.NetworkConditionalGlow
— TypeG = 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 blocksL
: 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 dimensionssqueeze_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
itselfTrainable parameters in activation normalizations
G.AN[i,j]
and coupling layersG.C[i,j]
, wherei
andj
range from1
toL
andK
respectively.
See also: ActNorm
, CouplingLayerGlow!
, get_params
, clear_grad!
InvertibleNetworks.NetworkConditionalHINT
— TypeCH = 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 blocksdepth
: number network layersk1
,k2
: kernel size for first and third residual layer (k1
) and second layer (k2
)p1
,p2
: respective padding sizes for residual block layerss1
,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
itselfTrainable parameters in activation normalizations
CH.AN_X[i]
andCH.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!
InvertibleNetworks.NetworkGlow
— TypeG = 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 blocksL
: 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 dimensionssqueeze_type
: squeeze type that happens at each multiscale levellogdet
: 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
itselfTrainable parameters in activation normalizations
G.AN[i,j]
and coupling layersG.C[i,j]
, wherei
andj
range from1
toL
andK
respectively.
See also: ActNorm
, CouplingLayerGlow!
, get_params
, clear_grad!
InvertibleNetworks.NetworkHyperbolic
— TypeH = 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 blocksarchitecture
: 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 to1
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
itselfTrainable parameters in the hyperbolic layers
H.HL[j]
.
See also: CouplingLayer!
, get_params
, clear_grad!
InvertibleNetworks.NetworkLoop
— TypeL = 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 blocksmaxiter
: number unrolled loop iterationsΨ
: link functionk1
,k2
: stencil sizes for convolutions in the residual blocks. The first convolution uses a stencil of size and stridek1
, thereby downsampling the input. The second convolutions uses a stencil of sizek2
. The last layer uses a stencil of size and stridek1
, 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 blocks1
,s2
: stride for the first and third convolution (s1
) and the second convolution (s2
) in residual blockndims
: 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
itselfTrainable parameters in the invertible coupling layers
L.L[i]
, and actnorm layersL.AN[i]
, wherei
ranges from1
to the number of loop iterations.
See also: CouplingLayerIRIM
, ResidualBlock
, get_params
, clear_grad!
InvertibleNetworks.NetworkMultiScaleConditionalHINT
— TypeCH = 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 blocksL
: 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 layersndims
: 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
itselfTrainable parameters in activation normalizations
CH.AN_X[i]
andCH.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!
InvertibleNetworks.NetworkMultiScaleHINT
— TypeH = 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 blocksL
: 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 layersndims
: 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
itselfTrainable 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!
InvertibleNetworks.SummarizedNet
— TypeG = 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
itselfTrainable parameters in conditional approximator
G.cond_net
and smmary networkG.sum_net
,
See also: ActNorm
, CouplingLayerGlow!
, get_params
, clear_grad!
AD Integration
InvertibleNetworks.backward_update!
— MethodUpdate state in the backward pass
InvertibleNetworks.check_coherence
— MethodError if mismatch between state and network
InvertibleNetworks.current
— MethodGet current state of the tape
InvertibleNetworks.forward_update!
— MethodUpdate state in the forward pass.
InvertibleNetworks.isa_newblock
— MethodDetermine if the input is related to a new block of invertible operations
InvertibleNetworks.reset!
— MethodReset the state of the tape