indiv_outcome.sequence.RNN

class pytrial.tasks.indiv_outcome.sequence.rnn.RNN(vocab_size, orders, mode, output_dim=None, max_visit=20, emb_size=64, n_rnn_layer=2, rnn_type='lstm', bidirectional=False, learning_rate=0.0001, weight_decay=0.0001, batch_size=64, epochs=10, evaluation_steps=100, num_worker=0, device='cuda:0', experiment_id='test')[source]

Implement an RNN based model for longitudinal patient records predictive modeling.

Parameters
  • vocab_size (list[int]) – A list of vocabulary size for different types of events, e.g., for diagnosis, procedure, medication.

  • orders (list[str]) – A list of orders when treating inputs events. Should have the same shape of vocab_size.

  • output_dim (int) –

    Output dimension of the model.

    • If binary classification, output_dim=1;

    • If multiclass/multilabel classification, output_dim=n_class

    • If regression, output_dim=1.

  • mode (str) – Prediction traget in [‘binary’,’multiclass’,’multilabel’,’regression’].

  • max_visit (int) – The maximum number of visits for input event codes.

  • emb_size (int) – Embedding size for encoding input event codes.

  • n_rnn_layer (int) – Number of RNN layers for encoding historical events.

  • rnn_type (str) – Pick RNN types in [‘rnn’,’lstm’,’gru’]

  • bidirectional (bool) – If True, it encodes historical events in bi-directional manner.

  • learning_rate (float) – Learning rate for optimization based on SGD. Use torch.optim.Adam by default.

  • weigth_decay (float) – Regularization strength for l2 norm; must be a positive float. Smaller values specify weaker regularization.

  • batch_size (int) – Batch size when doing SGD optimization.

  • epochs (int) – Maximum number of iterations taken for the solvers to converge.

  • evaluation_steps (int) – Number of steps to evaluate the model on validation set or report the training loss.

  • num_worker (int) – Number of workers used to do dataloading during training.

  • device (str) – The model device.

  • experiment_id (str) – The prefix when saving the checkpoints during the training.

fit(train_data, valid_data=None)[source]

Train model with sequential patient records.

Parameters
  • train_data (SequencePatientBase) – A SequencePatientBase contains patient records where ‘v’ corresponds to visit sequence of different events; ‘y’ corresponds to labels.

  • valid_data (SequencePatientBase) – A SequencePatientBase contains patient records used to make early stopping of the model.

load_model(checkpoint)[source]

Load pretrained model from the disk.

Parameters

checkpoint (str) – The input directory that stores the trained pytorch model and configuration.

predict(test_data)[source]

Predict patient outcomes using longitudinal trial patient sequences.

Parameters

test_data (SequencePatientBase) – A SequencePatientBase contains patient records where ‘v’ corresponds to visit sequence of different events.

save_model(output_dir)[source]

Save the pretrained model to the disk.

Parameters

output_dir (str) – The output directory that stores the trained pytorch model and configuration.