Skip to content

INN.Sequential

Zhang Yanbo edited this page Oct 26, 2022 · 1 revision

CLASS INN.Sequential(*args)

A Sequential container, simular to torch.nn.Sequential. The inputs should be INN.INNAbstract.INNModules, and the input will pass through them one-by-one. The sequential module will have the same methods as single layers, such as forward, inverse, computing_p.

Methods

forward(input, log_p0=0, log_det_J_=0)

Compute the forward result y. If compute_p=True, it will return y, logp and log_detJ.

inverse(y, num_iter=100)

Compute the inverse of y. The parameter num_iter is only for ResFlow modules. For other modules, this parameter will not having any effects.

Example

import INN
import torch

model = INN.Sequential(INN.Nonlinear(3, method='RealNVP'),
                       INN.BatchNorm1d(3),
                       INN.Linear(3))
model.eval()

x = torch.Tensor([[1,2,3],
                  [4,5,6],
                  [7,8,9]])

y, logp, logdet = model(x)
print(y)

x_hat = model.inverse(y)
print(x_hat)

'''Results
# y = model(x)
tensor([[ -4.9253,   1.0349,  -0.1721],
        [-18.1465,   5.9512,  -2.1945],
        [-29.2788,  10.0235,  -2.2862]], grad_fn=<MmBackward>)
# x_hat = model.inverse(y)
tensor([[1.0000, 2.0000, 3.0000],
        [4.0000, 5.0000, 6.0000],
        [7.0000, 8.0000, 9.0000]], grad_fn=<AddBackward0>)
'''
Clone this wiki locally