domainlab.compos.nn_zoo package¶
Submodules¶
domainlab.compos.nn_zoo.net_adversarial module¶
- class domainlab.compos.nn_zoo.net_adversarial.AutoGradFunMultiply(*args, **kwargs)[source]¶
Bases:
Function
- static backward(ctx, grad_output)[source]¶
Defines a formula for differentiating the operation with backward mode automatic differentiation (alias to the vjp function).
This function is to be overridden by all subclasses.
It must accept a context
ctx
as the first argument, followed by as many outputs as theforward()
returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs toforward()
. Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.The context can be used to retrieve tensors saved during the forward pass. It also has an attribute
ctx.needs_input_grad
as a tuple of booleans representing whether each input needs gradient. E.g.,backward()
will havectx.needs_input_grad[0] = True
if the first input toforward()
needs gradient computated w.r.t. the output.
- static forward(ctx, x, alpha)[source]¶
Performs the operation.
This function is to be overridden by all subclasses.
It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).
The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with
ctx.save_for_backward()
if they are intended to be used inbackward
(equivalently,vjp
) orctx.save_for_forward()
if they are intended to be used for injvp
.
- class domainlab.compos.nn_zoo.net_adversarial.AutoGradFunReverseMultiply(*args, **kwargs)[source]¶
Bases:
Function
https://pytorch.org/docs/stable/autograd.html https://pytorch.org/docs/stable/notes/extending.html#extending-autograd
- static backward(ctx, grad_output)[source]¶
Defines a formula for differentiating the operation with backward mode automatic differentiation (alias to the vjp function).
This function is to be overridden by all subclasses.
It must accept a context
ctx
as the first argument, followed by as many outputs as theforward()
returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs toforward()
. Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.The context can be used to retrieve tensors saved during the forward pass. It also has an attribute
ctx.needs_input_grad
as a tuple of booleans representing whether each input needs gradient. E.g.,backward()
will havectx.needs_input_grad[0] = True
if the first input toforward()
needs gradient computated w.r.t. the output.
- static forward(ctx, x, alpha)[source]¶
Performs the operation.
This function is to be overridden by all subclasses.
It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).
The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with
ctx.save_for_backward()
if they are intended to be used inbackward
(equivalently,vjp
) orctx.save_for_forward()
if they are intended to be used for injvp
.
- class domainlab.compos.nn_zoo.net_adversarial.Flatten[source]¶
Bases:
Module
- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
domainlab.compos.nn_zoo.net_classif module¶
Classifier
domainlab.compos.nn_zoo.net_conv_conv_bn_pool_2 module¶
In PyTorch, images are represented as [channels, height, width]
- class domainlab.compos.nn_zoo.net_conv_conv_bn_pool_2.NetConvBnReluPool2L(isize, conv_stride, dim_out_h)[source]¶
Bases:
Module
- class domainlab.compos.nn_zoo.net_conv_conv_bn_pool_2.NetConvDense(isize, conv_stride, dim_out_h, args, dense_layer=None)[source]¶
Bases:
Module
For direct topic inference
For custom erm, which is extracting the path of VAE from encoder until classifier. note in encoder, there is extra layer of hidden to mean and scale, in this component, it is replaced with another hidden layer.
domainlab.compos.nn_zoo.net_gated module¶
- class domainlab.compos.nn_zoo.net_gated.Conv2d(input_channels, output_channels, kernel_size, stride, padding, dilation=1, activation=None, bias=True)[source]¶
Bases:
Module
- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class domainlab.compos.nn_zoo.net_gated.GatedConv2d(input_channels, output_channels, kernel_size, stride, padding, dilation=1, activation=None)[source]¶
Bases:
Module
- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class domainlab.compos.nn_zoo.net_gated.GatedDense(input_size, output_size, activation=None)[source]¶
Bases:
Module
- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
domainlab.compos.nn_zoo.nn module¶
domainlab.compos.nn_zoo.nn_alex module¶
- class domainlab.compos.nn_zoo.nn_alex.Alex4DeepAll(flag_pretrain, dim_y)[source]¶
Bases:
AlexNetBase
change the last layer output of AlexNet to the dimension of the
- class domainlab.compos.nn_zoo.nn_alex.AlexNetBase(flag_pretrain)[source]¶
Bases:
NetTorchVisionBase
AlexNet( (features): Sequential( (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2)) (1): ReLU(inplace=True) (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)) (4): ReLU(inplace=True) (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (7): ReLU(inplace=True) (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (9): ReLU(inplace=True) (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (11): ReLU(inplace=True) (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) ) (avgpool): AdaptiveAvgPool2d(output_size=(6, 6)) (classifier): Sequential( (0): Dropout(p=0.5, inplace=False) (1): Linear(in_features=9216, out_features=4096, bias=True) (2): ReLU(inplace=True) (3): Dropout(p=0.5, inplace=False) (4): Linear(in_features=4096, out_features=4096, bias=True) (5): ReLU(inplace=True) (6): Linear(in_features=4096, out_features=7, bias=True) ) )
- class domainlab.compos.nn_zoo.nn_alex.AlexNetNoLastLayer(flag_pretrain)[source]¶
Bases:
AlexNetBase
Change the last layer of AlexNet with identity layer, the classifier from VAE can then have the same layer depth as erm model so it is fair for comparison