metallic.models

Here is a collection of some commonly used models in meta-learning tasks.

CNN

class metallic.models.OmniglotCNN(n_classes: Optional[int] = None)[source]

Bases: torch.nn.modules.module.Module

The convolutional network used for experiments on Omniglot, firstly introduced by [1].

It has 4 modules with a 3 × 3 convolutions and 64 filters, followed by batch normalization, a ReLU nonlinearity, and 2 × 2 max-pooling.

This network assumes the images are downsampled to 28 × 28 and have 1 channel. Namely, the shapes of inputs are (1, 28, 28).

Parameters

n_classes (int) – Size of the network’s output. This corresponds to N in N-way classification. None if the linear classifier is not needed.

References

  1. Matching Networks for One Shot Learning.” Oriol Vinyals, et al. NIPS 2016.

forward(x: torch.Tensor)torch.Tensor[source]
Parameters

x (torch.Tensor) – Input data (batch_size, in_channels=1, img_size=28, img_size=28)

Returns

output – If n_classes is not None, return class scores (batch_size, n_classes), or return embedded features (batch_size, 64)

Return type

torch.Tensor

init_weights()None[source]
training: bool
class metallic.models.MiniImagenetCNN(n_classes: Optional[int] = None)[source]

Bases: torch.nn.modules.module.Module

The convolutional network used for experiments on MiniImagenet, firstly introduced by [1].

It has 4 modules with a 3 × 3 convolutions and 32 filters, followed by batch normalization, a ReLU nonlinearity, and 2 × 2 max-pooling.

This network assumes the images are downsampled to 84 × 84 and have 3 channel. Namely, the shapes of inputs are (3, 84, 84).

Parameters

n_classes (int, optional) – Size of the network’s output. This corresponds to N in N-way classification. None if the linear classifier is not needed.

References

  1. Optimization as a Model for Few-Shot Learning.” Sachin Ravi, et al. ICLR 2017.

forward(x: torch.Tensor)torch.Tensor[source]
Parameters

x (torch.Tensor) – Input data (batch_size, in_channels=3, img_size=84, img_size=84)

Returns

output – If n_classes is not None, return class scores (batch_size, n_classes), or return embedded features (batch_size, 800).

Return type

torch.Tensor

init_weights()None[source]
training: bool

MLP

class metallic.models.OmniglotMLP(input_size: int, n_classes: int)[source]

Bases: torch.nn.modules.module.Module

The fully-connected network used for experiments on Omniglot, firstly introduced by [1].

It has 4 hidden layers with sizes 256, 128, 64, 64, each including batch normalization and ReLU nonlinearities, followed by a linear layer and softmax.

Parameters
  • input_size (int) – Size of the network’s input

  • n_classes (int) – Size of the network’s output. This corresponds to N in N-way classification.

References

  1. Meta-Learning with Memory-Augmented Neural Networks.” Adam Santoro, et al. ICML 2016.

forward(x: torch.Tensor)torch.Tensor[source]
init_weights()None[source]
training: bool

Base

class metallic.models.ConvBlock(in_channels: int, out_channels: int, kernel_size: Union[int, Tuple[int]] = 3, stride: Union[int, Tuple[int]] = 1, pool_kernel_size: Union[int, Tuple[int]] = 2, dropout: Optional[float] = None)[source]

Bases: torch.nn.modules.module.Module

A base convolutional block.

Parameters
  • in_channels (int) – Number of channels in the input image

  • out_channels (int) – Number of channels produced by the convolution

  • kernel_size (int or tuple, optional, default=3) – Size of the convolving kernel

  • stride (int or tuple, optional, default=1) – Stride of the convolution

  • pool_kernel_size (int or tuple, optional, default=2)) – kernel_size of the max pooling layer

  • dropout (float, optional) – Dropout, None if no dropout layer

forward(x: torch.Tensor)[source]
init_weights()[source]
training: bool
class metallic.models.ConvGroup(in_channels: int = 1, hidden_size: int = 64, kernel_size: Union[int, Tuple[int]] = 3, stride: Union[int, Tuple[int]] = 1, pool_kernel_size: Union[int, Tuple[int]] = 2, dropout: Optional[float] = None, layers: int = 4)[source]

Bases: torch.nn.modules.module.Module

A base convolutional group.

Parameters
  • in_channels (int, optional, default=1) – Number of channels in the input image

  • hidden_size (int, optional, default=64) – Dimensionality of the hidden representation

  • kernel_size (int or tuple, optional, default=3) – Size of the convolving kernel

  • stride (int or tuple, optional, default=1) – Stride of the convolution

  • pool_kernel_size (int or tuple, optional, default=2)) – kernel_size of the max pooling layer

  • dropout (float, optional) – dropout, None if no dropout layer

  • layers (int, optional, default=4) – Number of convolutional layers

Notes

  • Omniglot: hidden_size=64, in_channels=1, pool=False

  • MiniImagenet: hidden_size=32, in_channels=3, pool=True

forward(x: torch.Tensor)[source]
training: bool
class metallic.models.LinearBlock(in_features: int, out_features: int)[source]

Bases: torch.nn.modules.module.Module

A base linear block.

Parameters
  • input_size (int) – Size of each input sample

  • output_size (int) – Size of each output sample

forward(x: torch.Tensor)[source]
init_weights()[source]
training: bool
class metallic.models.Flatten[source]

Bases: torch.nn.modules.module.Module

Flatten input tensor to (batch_size, -1) shape.

forward(x: torch.Tensor)[source]
training: bool