Shortcuts

BCGlow

class torcheeg.models.BCGlow(in_channels: int = 4, grid_size: tuple = (32, 32), hidden_channels: int = 64, num_steps: int = 32, num_blocks: int = 3, actnorm_scale: float = 1.0, flow_permutation: str = 'invconv', flow_coupling: str = 'affine', LU_decomposed: bool = True, num_classes: int = 2, learn_top: bool = True)[source][source]

This class implements a conditional normalized flow model that allows generating samples of specified classes. A flow-based model is dedicated to train an encoder that encodes the input as a hidden variable and makes the hidden variable obey the standard normal distribution. By good design, the encoder should be reversible. On this basis, as soon as the encoder is trained, the corresponding decoder can be used to generate samples from a Gaussian distribution according to the inverse operation. In particular, the Glow model is a easy-to-use flow-based model that replaces the operation of permutating the channel axes by introducing a 1x1 reversible convolution.

Below is a recommended suite for use in EEG generation:

import torch

from torcheeg.models.flow import BGlow

eeg = torch.randn(1, 4, 32, 32)
y = torch.randint(0, 2, (2, ))
model = BGlow(num_classes=2)
nll_loss, y_logits = model(eeg, y)
fake_X = model.sample(y=y, temperature=1.0)
Parameters:
  • in_channels (int) – The feature dimension of each electrode. (default: 4)

  • grid_size (tuple) – Spatial dimensions of grid-like EEG representation. (default: (9, 9))

  • hid_channels (int) – The basic hidden channels in the network blocks. (default: 512)

  • num_layers (int) – The number of steps in the flow, each step contains an affine coupling layer, an invertible 1x1 conv and an actnorm layer. (default: 32)

  • num_blocks (int) – Number of blocks, each block includes split, step of flow and squeeze. (default: 3)

  • actnorm_scale (float) – The pre-defined scale factor in the actnorm layer. (default: 1.0)

  • flow_permutation (str) – The used flow permutation method, options include invconv, shuffle and reverse. (default: invconv)

  • flow_coupling (str) – The used flow coupling method, options include additive and affine. (default: affine)

  • LU_decomposed (bool) – Whether to use LU decomposed 1x1 convs. (default: True)

  • learnable_prior (bool) – Whether to train top layer (prior). (default: True)

  • num_classes (int) – The number of classes. During the generation process, additional category labels are provided to guide the generation of samples for the specified category. (default: 2)

sample(y, temperature=1.0)[source][source]
Parameters:
  • y (torch.Tensor) – Category labels (int) for a batch of samples The shape should be [n,]. Here, n corresponds to the batch size.

  • temperature (float) – The hyper-parameter, temperature, to sample from gaussian distributions. (default: 1.0)

Returns:

the generated results, which should have the same shape as the input noise, i.e., [n, 4, 32, 32]. Here, n corresponds to the batch size, 4 corresponds to in_channels, and (32, 32) corresponds to grid_size.

Return type:

torch.Tensor

forward(x: Tensor, y: Tensor)[source][source]
Parameters:
  • x (torch.Tensor) – EEG signal representation. The ideal input shape is [n, 4, 32, 32]. Here, n corresponds to the batch size, 4 corresponds to the in_channels, and (32, 32) corresponds to the grid_size.

  • y (torch.Tensor) – Category labels (int) for a batch of samples The shape should be [n,]. Here, n corresponds to the batch size.

Returns:

The latent representation. torch.Tensor: The bit per dimension (BPD) negative log-likelihood. torch.Tensor: The logits of the predicted category.

Return type:

torch.Tensor

Read the Docs v: latest
Versions
latest
stable
v1.1.1
v1.1.0
v1.0.11
v1.0.10
v1.0.9
v1.0.8.post1
v1.0.8
v1.0.7
v1.0.6
v1.0.4
v1.0.3
v1.0.2
v1.0.1
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources