socube.net package

Module contents

class socube.net.InceptionBlock2D(in_channels: int, out_channels: int, nums: int = 3)

Bases: socube.net._base.ModuleBase

Inception block with multiple conv kernels

Parameters
  • in_channels (integer scalar value) – input data’s channel count

  • out_channels (integer scalar value) – output data’s channel count, it must be a multiple of num

  • nums (integer scalar value) – Number of convolution kernels

Examples

>>> iblock = InceptionBlock2D(48, 96, 3)
forward(x: torch.Tensor) → torch.Tensor

Data forward for a neural network waited to be implemented.

Parameters

x (torch.Tensor) – The input data.

Returns

Return type

The output data.

training = None
class socube.net.InceptionBlock2DLayer(in_channels: int, out_channels: int)

Bases: socube.net._base.ModuleBase

Inception block with multiple layers.

Parameters
  • in_channels (integer scalar value) – input data’s channel count

  • out_channels (integer scalar value) – output data’s channel count

forward(x: torch.Tensor) → torch.Tensor

Data forward for a neural network waited to be implemented.

Parameters

x (torch.Tensor) – The input data.

Returns

Return type

The output data.

training = None
class socube.net.ModuleMetaBase

Bases: abc.ABCMeta

Metaclass for all neural network model implemented in this package.

Parameters
  • cls (type) – The class being created, which is a instance of ModuleMetaBase and a subclass of torch.nn.Module.

  • in_channels (int) – The number of input channels. Any type created by this metaclass must contain this constructor parameter.

  • out_channels (int) – The number of output channels. Any type created by this metaclass must contain this constructor parameter.

  • *args (Any) – The arguments for the class constructor

  • **kwargs (Any) – The keyword arguments for the class constructor

Returns

Return type

A new instance of the class being created.

class socube.net.NetBase(in_channels: int, out_channels: int, shape: Tuple[int] = None, **kwargs)

Bases: socube.net._base.ModuleBase

Basic abstract class of all neural network models. Any subclass of this class must implement the following abstract methods:

Parameters
  • in_channels (int) – The number of input channels.

  • out_channels (int) – The number of output channels.

  • shape (Tuple[int]) – The shape of a sample.

  • **kwargs (Any) –

abstract criterion(y_predict: torch.Tensor, y_true: torch.Tensor) → torch.Tensor

Abstract methods to calculate the loss of the model.

Parameters
  • y_predict (torch.Tensor) – The predicted data.

  • y_true (torch.Tensor) – The true data.

Returns

Return type

The loss of the model.

property shape

Return the shape of a sample.

Returns

Return type

The shape of a sample.

training = None
class socube.net.ModuleBase(in_channels: int, out_channels: int, **kwargs)

Bases: torch.nn.modules.module.Module

Basical abstract class of all neural network modules. Any subclass of this class must implement the following abstract methods:

Parameters
  • in_channels (int) – The number of input channels.

  • out_channels (int) – The number of output channels.

  • **kwargs (Any) –

abstract forward(x: torch.Tensor) → torch.Tensor

Data forward for a neural network waited to be implemented.

Parameters

x (torch.Tensor) – The input data.

Returns

Return type

The output data.

freeze(layer: torch.nn.modules.module.Module) → None

Freeze parameters of a layer and its sublayers

Parameters

layer (Module) – The layer to be frozen.

parameters(recurse: bool = True, skip_no_grad: bool = False) → Iterator[torch.nn.parameter.Parameter]

Return model paramters with or without no grad parameters. If some layers are freezed, these parameters should not be updated by optimizer. Though no grad parameters won’t be updated even if they are sent to optimizer. But it is a waste of calculation resource.

Parameters
  • recurse (bool) – If True, return all parameters of all sublayers.

  • skip_no_grad (bool, default False) –

    If True, return all parameters with grad.

    it advised set as True when using optimizer

Returns

Return type

An iterator of all parameters.

training = None
unfreeze(layer: torch.nn.modules.module.Module) → None

Unfreeze parameters of a layer and its sublayers

Parameters

layer (Module) – The layer to be unfrozen.