speechbrain.nnet.quaternion_networks.q_CNN module

Library implementing quaternion-valued convolutional neural networks.

Authors
  • Titouan Parcollet 2020

Summary

Classes:

QConv1d

This function implements quaternion-valued 1d convolution.

QConv2d

This function implements quaternion-valued 1d convolution.

Reference

class speechbrain.nnet.quaternion_networks.q_CNN.QConv1d(out_channels, kernel_size, input_shape=None, stride=1, dilation=1, padding='same', groups=1, bias=True, padding_mode='reflect', init_criterion='glorot', weight_init='quaternion', spinor=False, vector_scale=False)[source]

Bases: Module

This function implements quaternion-valued 1d convolution.

Parameters:
  • input_shape (tuple) – The shape of the input.

  • out_channels (int) – Number of output channels. Please note that these are quaternion-valued neurons. If 256 channels are specified, the output dimension will be 1024.

  • kernel_size (int) – Kernel size of the convolutional filters.

  • stride (int, optional) – Stride factor of the convolutional filters (default 1).

  • dilation (int, optional) – Dilation factor of the convolutional filters (default 1).

  • padding (str, optional) – (same, valid, causal). If “valid”, no padding is performed. If “same” and stride is 1, output shape is same as input shape. “causal” results in causal (dilated) convolutions (default “same”).

  • padding_mode (str, optional) – This flag specifies the type of padding. See torch.nn documentation for more information (default “reflect”).

  • groups (int, optional) – Default: 1 This option specifies the convolutional groups. See torch.nn documentation for more information (default 1).

  • bias (bool, optional) – If True, the additive bias b is adopted (default True).

  • init_criterion (str , optional) – (glorot, he). This parameter controls the initialization criterion of the weights. It is combined with weights_init to build the initialization method of the quaternion-valued weights (default “glorot”).

  • weight_init (str, optional) – (quaternion, unitary). This parameter defines the initialization procedure of the quaternion-valued weights. “quaternion” will generate random quaternion weights following the init_criterion and the quaternion polar form. “unitary” will normalize the weights to lie on the unit circle (default “quaternion”). More details in: “Quaternion Recurrent Neural Networks”, Parcollet T. et al.

  • spinor (bool, optional) – When True, the layer will be turned into a spinor layer. More precisely W*x will be turned into W*x*W-1. The input x will be rotated by W such as in a spinor neural network. However, x MUST be a quaternion with the real part equal to zero. (0 + xi + yj + zk). Indeed, the rotation operation only acts on the vector part. Note that W will always be normalized before the rotation to ensure the quaternion algebra (default False). More details in: “Quaternion neural networks”, Parcollet T.

  • vector_scale (bool, optional) – The vector_scale is only used when spinor = True. In the context of a spinor neural network, multiple rotations of the input vector x are performed and summed. Hence, the norm of the output vector always increases with the number of layers, making the neural network instable with deep configurations. The vector_scale parameters are learnable parameters that acts like gates by multiplying the output vector with a small trainable parameter (default False).

Example

>>> inp_tensor = torch.rand([10, 16, 40])
>>> cnn_1d = QConv1d(
...     input_shape=inp_tensor.shape, out_channels=12, kernel_size=3
... )
>>> out_tensor = cnn_1d(inp_tensor)
>>> out_tensor.shape
torch.Size([10, 16, 48])
forward(x)[source]

Returns the output of the convolution.

Parameters:

x (torch.Tensor (batch, time, channel)) – Input to convolve. 3d or 4d tensors are expected.

training: bool
class speechbrain.nnet.quaternion_networks.q_CNN.QConv2d(out_channels, kernel_size, input_shape=None, stride=1, dilation=1, padding='same', groups=1, bias=True, padding_mode='reflect', init_criterion='glorot', weight_init='quaternion', spinor=False, vector_scale=False)[source]

Bases: Module

This function implements quaternion-valued 1d convolution.

Parameters:
  • input_shape (tuple) – The shape of the input.

  • out_channels (int) – Number of output channels. Please note that these are quaternion-valued neurons. If 256 channels are specified, the output dimension will be 1024.

  • kernel_size (int) – Kernel size of the convolutional filters.

  • stride (int, optional) – Stride factor of the convolutional filters (default 1).

  • dilation (int, optional) – Dilation factor of the convolutional filters (default 1).

  • padding (str, optional) – (same, causal). If “valid”, no padding is performed. If “same” and stride is 1, output shape is same as input shape (default “same”).

  • padding_mode (str, optional) – This flag specifies the type of padding. See torch.nn documentation for more information. (default “reflect”)

  • groups (int, optional) – This option specifies the convolutional groups. See torch.nn documentation for more information. (default 1).

  • bias (bool, optional) – If True, the additive bias b is adopted (default True).

  • init_criterion (str , optional) – (glorot, he). This parameter controls the initialization criterion of the weights. It is combined with weights_init to build the initialization method of the quaternion-valued weights (default “glorot”).

  • weight_init (str, optional) – (quaternion, unitary). This parameter defines the initialization procedure of the quaternion-valued weights. “quaternion” will generate random quaternion weights following the init_criterion and the quaternion polar form. “unitary” will normalize the weights to lie on the unit circle (default “quaternion”). More details in: “Quaternion Recurrent Neural Networks”, Parcollet T. et al.

  • spinor (bool, optional) – When True, the layer will be turned into a spinor layer. More precisely W*x will be turned into W*x*W-1. The input x will be rotated by W such as in a spinor neural network. However, x MUST be a quaternion with the real part equal to zero. (0 + xi + yj + zk). Indeed, the rotation operation only acts on the vector part. Note that W will always be normalized before the rotation to ensure the quaternion algebra (default False). More details in: “Quaternion neural networks”, Parcollet T.

  • vector_scale (bool, optional) – The vector_scale is only used when spinor = True. In the context of a spinor neural network, multiple rotations of the input vector x are performed and summed. Hence, the norm of the output vector always increases with the number of layers, making the neural network instable with deep configurations. The vector_scale parameters are learnable parameters that acts like gates by multiplying the output vector with a small trainable parameter (default False).

Example

>>> inp_tensor = torch.rand([10, 4, 16, 40])
>>> cnn_1d = QConv2d(
...     input_shape=inp_tensor.shape, out_channels=12, kernel_size=3
... )
>>> out_tensor = cnn_1d(inp_tensor)
>>> out_tensor.shape
torch.Size([10, 4, 16, 48])
training: bool
forward(x)[source]

Returns the output of the convolution.

Parameters:

x (torch.Tensor (batch, time, channel)) – Input to convolve. 3d or 4d tensors are expected.