GithubHelp home page GithubHelp logo

aehrc / laat Goto Github PK

View Code? Open in Web Editor NEW
60.0 9.0 22.0 109.86 MB

A Label Attention Model for ICD Coding from Clinical Text

License: Other

Shell 0.38% Python 99.62%
label-attention-classification icd-coding clinical-text laat

laat's Introduction

A Label Attention Model for ICD Coding from Clinical Text Twitter

GitHub top language GitHub issues GitHub repo size GitHub last commit GitHub forks GitHub stars

This project provides the code for our JICAI 2020 A Label Attention Model for ICD Coding from Clinical Text paper.

The general architecture and experimental results can be found in our paper:

  @inproceedings{ijcai2020-461-vu,
      title     = {A Label Attention Model for ICD Coding from Clinical Text},
      author    = {Vu, Thanh and Nguyen, Dat Quoc and Nguyen, Anthony},
      booktitle = {Proceedings of the Twenty-Ninth International Joint Conference on Artificial Intelligence, {IJCAI-20}},             
      pages     = {3335--3341},
      year      = {2020},
      month     = {7},
      note      = {Main track}
      doi       = {10.24963/ijcai.2020/461},
      url       = {https://doi.org/10.24963/ijcai.2020/461},
   }

Please CITE our paper when this code is used to produce published results or incorporated into other software.

Requirements

  • python>=3.6
  • torch==1.4.0
  • scikit-learn==0.23.1
  • numpy==1.16.3
  • scipy==1.2.1
  • pandas==0.24.2
  • tqdm==4.31.1
  • nltk>=3.4.5
  • psycopg2==2.7.7
  • gensim==3.6.0
  • transformers==2.11.0

Run pip install -r requirements.txt to install the required libraries

Run python3 and run import nltk and nltk.download('punkt') for tokenization

Data preparation

MIMIC-III-full and MIMIC-III-50 experiments

data/mimicdata/mimic3

  • The id files are from caml-mimic
  • Install the MIMIC-III database with PostgreSQL following this instruction
  • Generate the train/valid/test sets using src/util/mimiciii_data_processing.py. (Configure the connection to PostgreSQL at Line 139)

MIMIC-II-full experiment

data/mimicdata/mimic2

  • Place the MIMIC-II file (MIMIC_RAW_DSUMS) to data/mimicdata/mimic2
  • Generate the train/valid/test sets using src/util/mimicii_data_processing.py.

Note that: The code will generate 3 files (train.csv, valid.csv, and test.csv) for each experiment.

Pretrained word embeddings

data/embeddings

We used gensim to train the embeddings (word2vec model) using the entire MIMIC-III discharge summary data.

Our code also supports subword embeddings (fastText) which helps produce better performances (see src/args_parser.py).

How to run

The problem and associated configurations are defined in configuration/config.json. Note that there are 3 files in each data folder (train.csv, valid.csv and test.csv)

There are common hyperparameters for all the models and the model-specific hyperparameters. See src/args_parser.py for more detail

Here is an example of using the framework on MIMIC-III dataset (full codes) with hierarchical join learning

python -m src.run \
    --problem_name mimic-iii_2_full \
    --max_seq_length 4000 \
    --n_epoch 50 \
    --patience 5 \
    --batch_size 8 \
    --optimiser adamw \
    --lr 0.001 \
    --dropout 0.3 \
    --level_projection_size 128 \
    --main_metric micro_f1 \
    --embedding_mode word2vec \
    --embedding_file data/embeddings/word2vec_sg0_100.model \
    --attention_mode label \
    --d_a 512 \
    RNN  \
    --rnn_model LSTM \
    --n_layers 1 \
    --bidirectional 1 \
    --hidden_size 512 

laat's People

Contributors

tienthanhdhcn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laat's Issues

Pretraining Details of Word2Vec

Hi !

Can i have more details about the pretaining stage ?

Actually i followed the steps written in the paper, but i can't have the same word2vec model offered in the file "word2vec_sg0_100.model".

Thanks a lot.

attention mechanism question

I was wondering why in line 108 you use the original embedding (x) of the text:

weighted_output = att_weights @ x

and not the weight variable (line 102, 104) that you use to calculate the attention weight:

att_weights = self.second_linears[label_level](weights)

Visualize the output of attention?

Is it possible to explain the output of attention? Which token is contributing how much to the prediction? the depth of colour on each text represents the level of contribution such as bert visualizer etc?

reproduce result not satisfying

I have followed the tips and use the command proposed, but the result like the f1-score is very low. Below is the training log, does someone know what is the problem ?
INFO Training with
{ 'attention_mode': 'label',
'batch_size': 8,
'best_model_path': None,
'bidirectional': 1,
'd_a': 512,
'dropout': 0.3,
'embedding_file': 'data/embeddings/word2vec_sg0_100.model',
'embedding_mode': 'word2vec',
'embedding_size': 100,
'hidden_size': 512,
'joint_mode': 'hierarchical',
'level_projection_size': 128,
'lr': 0.001,
'lr_scheduler_factor': 0.9,
'lr_scheduler_patience': 5,
'main_metric': 'micro_f1',
'max_seq_length': 4000,
'metric_level': 1,
'min_seq_length': -1,
'min_word_frequency': -1,
'mode': 'static',
'model': <class 'src.models.rnn.RNN'>,
'multilabel': 1,
'n_epoch': 50,
'n_layers': 1,
'optimiser': 'adamw',
'patience': 5,
'penalisation_coeff': 0.01,
'problem_name': 'mimic-iii_2_full',
'r': -1,
'resume_training': False,
'rnn_model': 'LSTM',
'save_best_model': 1,
'save_results': 1,
'save_results_on_train': False,
'shuffle_data': 1,
'use_last_hidden_state': 0,
'use_lr_scheduler': 1,
'use_regularisation': False,
'weight_decay': 0}

16:27:46 INFO Preparing the vocab
16:28:19 INFO Saved vocab and data to files
16:28:19 INFO Using cuda:1
16:28:19 INFO # levels: 2
16:28:19 INFO # labels at level 0: 1167
16:28:19 INFO # labels at level 1: 8929
16:28:19 INFO 47719.1631.3372
16:28:34 INFO Saved dataset path: cached_data/mimic-iii_2_full/4895ab78f2519b9330f8b64596585d7a.data.pkl
16:29:45 INFO 47719 instances with 70370595 tokens, Level_0 with 1158 labels, Level_1 with 8692 labels in the train dataset
16:29:45 INFO 1631 instances with 2890443 tokens, Level_0 with 738 labels, Level_1 with 3012 labels in the valid dataset
16:29:45 INFO 3372 instances with 6008978 tokens, Level_0 with 853 labels, Level_1 with 4085 labels in the test dataset
16:29:45 INFO Training epoch #1
17:58:55 INFO Learning rate at epoch #1: 0.001
17:58:55 INFO Loss on Train at epoch #1: 0.01475, micro_f1 on Valid: 0.02883
17:58:55 INFO [NEW BEST] (level_1) micro_f1 on Valid set: 0.02883
17:58:55 INFO Results on Valid set at epoch #1 with Averaged Loss 0.01446
17:58:55 INFO ======== Results at level_0 ========
17:58:55 INFO Results on Valid set at epoch #1 with Loss 0.04842:
[MICRO] accuracy: 0.01967 auc: 0.92422 precision: 0.31572 recall: 0.02055 f1: 0.03859 P@1: 0 P@5: 0 P@8: 0 P@10: 0 P@15: 0
[MACRO] accuracy: 0.00027 auc: 0.47509 precision: 0.00027 recall: 0.00086 f1: 0.00041 P@1: 0.31637 P@5: 0.30546 P@8: 0.32442 P@10: 0.31343 P@15: 0.28567

17:58:55 INFO ======== Results at level_1 ========
17:58:55 INFO Results on Valid set at epoch #1 with Loss 0.00993:
[MICRO] accuracy: 0.01463 auc: 0.93636 precision: 0.24493 recall: 0.01532 f1: 0.02883 P@1: 0 P@5: 0 P@8: 0 P@10: 0 P@15: 0
[MACRO] accuracy: 4e-05 auc: 0.47604 precision: 7e-05 recall: 0.00012 f1: 9e-05 P@1: 0.24893 P@5: 0.23495 P@8: 0.22854 P@10: 0.20815 P@15: 0.17613

17:59:07 INFO Training epoch #2
19:29:29 INFO Learning rate at epoch #2: 0.001
19:29:29 INFO Loss on Train at epoch #2: 0.01283, micro_f1 on Valid: 0.0
19:29:29 INFO [CURRENT BEST] (level_1) micro_f1 on Valid set: 0.02883
19:29:29 INFO Early stopping: 1/6
19:29:45 INFO Training epoch #3
21:00:44 INFO Learning rate at epoch #3: 0.001
21:00:44 INFO Loss on Train at epoch #3: 0.01276, micro_f1 on Valid: 0.02193
21:00:44 INFO [CURRENT BEST] (level_1) micro_f1 on Valid set: 0.02883
21:00:44 INFO Early stopping: 2/6
21:00:57 INFO Training epoch #4
22:33:08 INFO Learning rate at epoch #4: 0.001
22:33:08 INFO Loss on Train at epoch #4: 0.01273, micro_f1 on Valid: 0.0
22:33:08 INFO [CURRENT BEST] (level_1) micro_f1 on Valid set: 0.02883
22:33:08 INFO Early stopping: 3/6
22:33:18 INFO Training epoch #5
23:59:54 INFO Learning rate at epoch #5: 0.001
23:59:54 INFO Loss on Train at epoch #5: 0.01271, micro_f1 on Valid: 0.0
23:59:54 INFO [CURRENT BEST] (level_1) micro_f1 on Valid set: 0.02883
23:59:54 INFO Early stopping: 4/6
00:00:14 INFO Training epoch #6
01:24:16 INFO Learning rate at epoch #6: 0.001
01:24:16 INFO Loss on Train at epoch #6: 0.01269, micro_f1 on Valid: 0.0
01:24:16 INFO [CURRENT BEST] (level_1) micro_f1 on Valid set: 0.02883
01:24:16 INFO Early stopping: 5/6
01:24:29 INFO Training epoch #7
02:48:20 INFO Learning rate at epoch #7: 0.0009000000000000001
02:48:20 INFO Loss on Train at epoch #7: 0.01268, micro_f1 on Valid: 0.02239
02:48:20 INFO [CURRENT BEST] (level_1) micro_f1 on Valid set: 0.02883
02:48:20 INFO Early stopping: 6/6
02:48:44 WARNING Early stopped on Valid set!
02:48:44 INFO =================== BEST ===================
02:48:44 INFO Results on Valid set at epoch #1 with Averaged Loss 0.01446
02:48:44 INFO ======== Results at level_0 ========
02:48:44 INFO Results on Valid set at epoch #1 with Loss 0.04842:
[MICRO] accuracy: 0.01967 auc: 0.92422 precision: 0.31572 recall: 0.02055 f1: 0.03859 P@1: 0 P@5: 0 P@8: 0 P@10: 0 P@15: 0
[MACRO] accuracy: 0.00027 auc: 0.47509 precision: 0.00027 recall: 0.00086 f1: 0.00041 P@1: 0.31637 P@5: 0.30546 P@8: 0.32442 P@10: 0.31343 P@15: 0.28567

02:48:44 INFO ======== Results at level_1 ========
02:48:44 INFO Results on Valid set at epoch #1 with Loss 0.00993:
[MICRO] accuracy: 0.01463 auc: 0.93636 precision: 0.24493 recall: 0.01532 f1: 0.02883 P@1: 0 P@5: 0 P@8: 0 P@10: 0 P@15: 0
[MACRO] accuracy: 4e-05 auc: 0.47604 precision: 7e-05 recall: 0.00012 f1: 9e-05 P@1: 0.24893 P@5: 0.23495 P@8: 0.22854 P@10: 0.20815 P@15: 0.17613

02:48:44 INFO Results on Test set at epoch #1 with Averaged Loss 0.01484
02:48:44 INFO ======== Results at level_0 ========
02:48:44 INFO Results on Test set at epoch #1 with Loss 0.0495:
[MICRO] accuracy: 0.02047 auc: 0.92331 precision: 0.3363 recall: 0.02133 f1: 0.04011 P@1: 0 P@5: 0 P@8: 0 P@10: 0 P@15: 0
[MACRO] accuracy: 0.00029 auc: 0.4488 precision: 0.00029 recall: 0.00086 f1: 0.00043 P@1: 0.3363 P@5: 0.30919 P@8: 0.33278 P@10: 0.3196 P@15: 0.29187

02:48:44 INFO ======== Results at level_1 ========
02:48:44 INFO Results on Test set at epoch #1 with Loss 0.01023:
[MICRO] accuracy: 0.01676 auc: 0.93503 precision: 0.26517 recall: 0.01757 f1: 0.03296 P@1: 0 P@5: 0 P@8: 0 P@10: 0 P@15: 0
[MACRO] accuracy: 4e-05 auc: 0.47837 precision: 7e-05 recall: 0.00013 f1: 9e-05 P@1: 0.26898 P@5: 0.24614 P@8: 0.23458 P@10: 0.21059 P@15: 0.1828

02:48:44 INFO => loading best model 'checkpoints/mimic-iii_2_full/RNN_LSTM_1_512.static.label.0.001.0.3_72df8e44d8921dd19f07bab290d6a868/best_model.pkl'

How to generate train/test/valid without postgre?

Hi, I have already access to the mimic-iii data and having an issue install postgre and mimic database. is there any other way to get train, test, and valid.csv without using postgre same as caml?

inconsistencies with mimic-iii 50 dataset

Hello,

Thank you for providing an open-source code for the project.

While I was trying to run the code, I found that there are inconsistencies in mimic-iii 50 labels dataset that is produced using your code and the dataset that is created by using the CAML code (https://github.com/jamesmullenbach/caml-mimic)

Firstly I want to mention that both datasets have included the correct ids for patients but I found that the CAML code in the top-50 codes has included the code '37.23' but you have included '93.90'. This has the effect of creating different labels in all three parts of the datasets (train/dev/test)

For example for the '93.90' code for an instance in the dataset the caml paper had the labels:
{'412', '401.9', '36.15', '414.01', '496', '39.61'}
but your code had:
{'412', '401.9', '36.15', '414.01', '496', '93.90', '39.61'}
Or for the '37.23' code the caml code had the labels:
{'427.31', '37.23', '428.0', '272.0', '88.56', '414.01'}
but your code had the labels:
{'427.31', '428.0', '272.0', '88.56', '414.01'}

Could you let me know why there are inconsistencies with the dataset and which version did you used for yours expirements?

Reproducing Joint-Laat result

Hi, thank you for the paper. I am trying to reproduce the Joint-Laat results, I am using these parameters :

python3 -m src.run \
    --problem_name mimic-iii_2_full \
    --max_seq_length 4000 \
    --n_epoch 50 \
    --patience 5 \
    --batch_size 8 \
    --optimiser adamw \
    --lr 0.001 \
    --dropout 0.3 \
    --level_projection_size 128 \
    --main_metric micro_f1 \
    --embedding_mode word2vec \
    --embedding_file data/embeddings/word2vec_sg0_100.model \
    --attention_mode label \
    --d_a 512 \
    RNN  \
    --rnn_model LSTM \
    --n_layers 1 \
    --bidirectional 1 \
    --hidden_size 512 

Using this, I am able to get the following output:

00:32:03 INFO ======== Results at level_0 ========
00:32:03 INFO Results on Test set at epoch #8 with Loss 0.02354:
[MICRO] accuracy: 0.53093       auc: 0.98799    precision: 0.7335       recall: 0.65783 f1: 0.69361     P@1: 0  P@5: 0    P@8: 0  P@10: 0 P@15: 0
[MACRO] accuracy: 0.19297       auc: 0.93447    precision: 0.28883      recall: 0.25282 f1: 0.26963     P@1: 0.95878      P@5: 0.89359    P@8: 0.82681    P@10: 0.77921   P@15: 0.66222

00:32:03 INFO ======== Results at level_1 ========
00:32:03 INFO Results on Test set at epoch #8 with Loss 0.00541:
[MICRO] accuracy: 0.40399       auc: 0.98824    precision: 0.63812      recall: 0.52405 f1: 0.57549     P@1: 0  P@5: 0    P@8: 0  P@10: 0 P@15: 0
[MACRO] accuracy: 0.06239       auc: 0.91987    precision: 0.09447      recall: 0.08848 f1: 0.09138     P@1: 0.91163      P@5: 0.81109    P@8: 0.73873    P@10: 0.69256   P@15: 0.59085

which looks like the LAAT model result as reported in the paper, How can I reproduce Joint-LAAT model results with AUC of 92 something and f1 score of 10.7 ?

Issue with attention mechanism

It seems you are calculating the attention weights (in some settings) based on a linear transform of the inputs:

weights = F.tanh(self.first_linears[label_level](x))

However you will always only apply the attentions to the original input (without applying the first_linears):

weighted_output = att_weights @ x

Is that a bug or am I missing something in the implementation? There is no mention of any of this in the original paper, just that d_a is a hyperparam.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.