项目作者: khirotaka

项目描述 :
[Implementation example] Attend and Diagnose: Clinical Time Series Analysis Using Attention Models
高级语言: Python
项目地址: git://github.com/khirotaka/SAnD.git
创建时间: 2019-10-01T05:06:51Z
项目社区:https://github.com/khirotaka/SAnD

开源协议:MIT License

下载


SAnD

This has been archived.

AAAI 2018 Attend and Diagnose: Clinical Time Series Analysis Using Attention Models

Codacy Badge
contributions welcome
License: MIT

Warning This code is UNOFFICIAL.

Paper: Attend and Diagnose: Clinical Time Series Analysis Using Attention Models

If you want to run this code,
you need download some dataset and write experimenting code.

  1. from comet_ml import Experiment
  2. from SAnD.core.model import SAnD
  3. from SAnD.utils.trainer import NeuralNetworkClassifier
  4. model = SAnD( ... )
  5. clf = NeuralNetworkClassifier( ... )
  6. clf.fit( ... )

Installation

git clone https://github.com/khirotaka/SAnD.git

Requirements

  • Python 3.6
  • Comet.ml
  • PyTorch v1.1.0 or later

Simple Usage

Here’s a brief overview of how you can use this project to help you solve the classification task.

Download this project

First, create an empty directory.
In this example, I’ll call it “playground”.
Run the git init & git submodule add command to register SAnD project as a submodule.

```shell script
$ mkdir playground/
$ cd playground/
$ git init
$ git submodule add https://github.com/khirotaka/SAnD.git

  1. Now you're ready to use `SAnD` in your project.
  2. ### Preparing the Dataset
  3. Prepare the data set of your choice.
  4. Remember that the input dimension to the SAnD model is basically three dimensions of `[N, seq_len, features]`.
  5. This example shows how to use `torch.randn()` as a pseudo dataset.
  6. ```python
  7. from comet_ml import Experiment
  8. import torch
  9. import torch.nn as nn
  10. import torch.optim as optim
  11. from torch.utils.data import TensorDataset, DataLoader
  12. from SAnD.core.model import SAnD
  13. from SAnD.utils.trainer import NeuralNetworkClassifier
  14. x_train = torch.randn(1024, 256, 23) # [N, seq_len, features]
  15. x_val = torch.randn(128, 256, 23) # [N, seq_len, features]
  16. x_test = torch.randn(512, 256, 23) # [N, seq_len, features]
  17. y_train = torch.randint(0, 9, (1024, ))
  18. y_val = torch.randint(0, 9, (128, ))
  19. y_test = torch.randint(0, 9, (512, ))
  20. train_ds = TensorDataset(x_train, y_train)
  21. val_ds = TensorDataset(x_val, y_val)
  22. test_ds = TensorDataset(x_test, y_test)
  23. train_loader = DataLoader(train_ds, batch_size=128)
  24. val_loader = DataLoader(val_ds, batch_size=128)
  25. test_loader = DataLoader(test_ds, batch_size=128)

Note:
In my experience, I have a feeling that SAnD is better at problems with a large number of features.

Training SAnD model using Trainer

Finally, train the SAnD model using the included NeuralNetworkClassifier.
Of course, you can also have them use a well-known training tool such as PyTorch Lightning.
The included NeuralNetworkClassifier depends on the comet.ml‘s logging service.

  1. in_feature = 23
  2. seq_len = 256
  3. n_heads = 32
  4. factor = 32
  5. num_class = 10
  6. num_layers = 6
  7. clf = NeuralNetworkClassifier(
  8. SAnD(in_feature, seq_len, n_heads, factor, num_class, num_layers),
  9. nn.CrossEntropyLoss(),
  10. optim.Adam, optimizer_config={"lr": 1e-5, "betas": (0.9, 0.98), "eps": 4e-09, "weight_decay": 5e-4},
  11. experiment=Experiment()
  12. )
  13. # training network
  14. clf.fit(
  15. {"train": train_loader,
  16. "val": val_loader},
  17. epochs=200
  18. )
  19. # evaluating
  20. clf.evaluate(test_loader)
  21. # save
  22. clf.save_to_file("save_params/")

For the actual task, choose the appropriate hyperparameters for your model and optimizer.

Regression Task

There are two ways to use SAnD in a regression task.

  1. Specify the number of output dimensions in num_class.
  2. Inherit class SAnD and overwrite ClassificationModule with RegressionModule.

I would like to introduce a second point.

  1. from SAnD.core.model import SAnD
  2. from SAnD.core.modules import RegressionModule
  3. class RegSAnD(SAnD):
  4. def __init__(self, *args, **kwargs):
  5. super(RegSAnD, self).__init__(*args, **kwargs)
  6. d_model = kwargs.get("d_model")
  7. factor = kwargs.get("factor")
  8. output_size = kwargs.get("n_class") # output_size
  9. self.clf = RegressionModule(d_model, factor, output_size)
  10. model = RegSAnD(
  11. input_features=..., seq_len=..., n_heads=..., factor=...,
  12. n_class=..., n_layers=...
  13. )

The contents of both ClassificationModule and RegressionModule are almost the same, so the 1st is recommended.

Please let me know when my code has been used to bring products or research results to the world.
It’s very encouraging :)

Author

Hirotaka Kawashima (川島 寛隆)

License

Copyright (c) 2019 Hirotaka Kawashima
Released under the MIT license