pyhealth.models.SparcNet#

The SparcNet Model: Jin Jing, et al. Development of Expert-level Classification of Seizures and Rhythmic and Periodic Patterns During EEG Interpretation. Neurology 2023.

class pyhealth.models.DenseLayer(input_channels, growth_rate, bn_size, drop_rate=0.5, conv_bias=True, batch_norm=True)[source]#

Bases: Sequential

Densely connected layer :param input_channels: number of input channels :param growth_rate: rate of growth of channels in this layer :param bn_size: multiplicative factor for the bottleneck layer (does not affect the output size) :param drop_rate: dropout rate :param conv_bias: whether to use bias in convolutional layers :param batch_norm: whether to use batch normalization

Example

>>> x = torch.randn(128, 5, 1000)
>>> batch, channels, length = x.shape
>>> model = DenseLayer(channels, 5, 2)
>>> y = model(x)
>>> y.shape
torch.Size([128, 10, 1000])
forward(x)[source]#

Define 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 pyhealth.models.DenseBlock(num_layers, input_channels, growth_rate, bn_size, drop_rate=0.5, conv_bias=True, batch_norm=True)[source]#

Bases: Sequential

Densely connected block :param num_layers: number of layers in this block :param input_channls: number of input channels :param growth_rate: rate of growth of channels in this layer :param bn_size: multiplicative factor for the bottleneck layer (does not affect the output size) :param drop_rate: dropout rate :param conv_bias: whether to use bias in convolutional layers :param batch_norm: whether to use batch normalization

Example

>>> x = torch.randn(128, 5, 1000)
>>> batch, channels, length = x.shape
>>> model = DenseBlock(3, channels, 5, 2)
>>> y = model(x)
>>> y.shape
torch.Size([128, 20, 1000])
class pyhealth.models.TransitionLayer(input_channels, output_channels, conv_bias=True, batch_norm=True)[source]#

Bases: Sequential

pooling transition layer

Parameters:
  • input_channls – number of input channels

  • output_channels – number of output channels

  • conv_bias – whether to use bias in convolutional layers

  • batch_norm – whether to use batch normalization

Example

>>> x = torch.randn(128, 5, 1000)
>>> model = TransitionLayer(5, 18)
>>> y = model(x)
>>> y.shape
torch.Size([128, 18, 500])
class pyhealth.models.SparcNet(dataset, feature_keys, label_key, mode, embedding_dim=128, hidden_dim=128, block_layers=4, growth_rate=16, bn_size=16, drop_rate=0.5, conv_bias=True, batch_norm=True, **kwargs)[source]#

Bases: BaseModel

The SparcNet model for sleep staging.

Paper: Jin Jing, et al. Development of Expert-level Classification of Seizures and Rhythmic and Periodic Patterns During EEG Interpretation. Neurology 2023.

Note

We use one encoder to handle multiple channel together.

Parameters:
  • dataset (BaseSignalDataset) – the dataset to train the model. It is used to query certain information such as the set of all tokens.

  • feature_keys (List[str]) – list of keys in samples to use as features, e.g. [“conditions”, “procedures”].

  • label_key (str) – key in samples to use as label (e.g., “drugs”).

  • mode (str) – one of “binary”, “multiclass”, or “multilabel”.

  • embedding_dim (int) – (not used now) the embedding dimension. Default is 128.

  • hidden_dim (int) – (not used now) the hidden dimension. Default is 128.

  • block_layer – the number of layers in each dense block. Default is 4.

  • growth_rate – the growth rate of each dense layer. Default is 16.

  • bn_size – the bottleneck size of each dense layer. Default is 16.

  • conv_bias – whether to use bias in convolutional layers. Default is True.

  • batch_norm – whether to use batch normalization. Default is True.

  • **kwargs – other parameters for the Deepr layer.

Examples

>>> from pyhealth.datasets import SampleSignalDataset
>>> samples = [
...         {
...             "record_id": "SC4001-0",
...             "patient_id": "SC4001",
...             "epoch_path": "/home/chaoqiy2/.cache/pyhealth/datasets/2f06a9232e54254cbcb4b62624294d71/SC4001-0.pkl",
...             "label": "W",
...         },
...         {
...             "record_id": "SC4001-1",
...             "patient_id": "SC4001",
...             "epoch_path": "/home/chaoqiy2/.cache/pyhealth/datasets/2f06a9232e54254cbcb4b62624294d71/SC4001-1.pkl",
...             "label": "R",
...         }
...     ]
>>> dataset = SampleSignalDataset(samples=samples, dataset_name="test")
>>>
>>> from pyhealth.models import SparcNet
>>> model = SparcNet(
...         dataset=dataset,
...         feature_keys=["signal"], # dataloader will load the signal from "epoch_path" and put it in "signal"
...         label_key="label",
...         mode="multiclass",
...     )
>>>
>>> from pyhealth.datasets import get_dataloader
>>> train_loader = get_dataloader(dataset, batch_size=2, shuffle=True)
>>> data_batch = next(iter(train_loader))
>>>
>>> ret = model(**data_batch)
>>> print(ret)
{
    'loss': tensor(0.6530, device='cuda:0', grad_fn=<NllLossBackward0>),
    'y_prob': tensor([[0.4459, 0.5541],
                    [0.5111, 0.4889]], device='cuda:0', grad_fn=<SoftmaxBackward0>),
    'y_true': tensor([1, 1], device='cuda:0'),
    'logit': tensor([[-0.2750, -0.0577],
                    [-0.1319, -0.1763]], device='cuda:0', grad_fn=<AddmmBackward0>)
}
label_tokenizer#

input statistics

forward(**kwargs)[source]#

Forward propagation.

Return type:

Dict[str, Tensor]

training: bool#