pyhealth.models.MICRON#

The separate callable MICRONLayer and the complete MICRON model.

class pyhealth.models.MICRONLayer(input_size, hidden_size, num_drugs, lam=0.1)[source]#

Bases: Module

MICRON layer.

Paper: Chaoqi Yang et al. Change Matters: Medication Change Prediction with Recurrent Residual Networks. IJCAI 2021.

This layer is used in the MICRON model. But it can also be used as a standalone layer.

Parameters
  • input_size (int) – input feature size.

  • hidden_size (int) – hidden feature size.

  • num_drugs (int) – total number of drugs to recommend.

  • lam (float) – regularization parameter for the reconstruction loss. Default is 0.1.

Examples

>>> from pyhealth.models import MICRONLayer
>>> patient_emb = torch.randn(3, 5, 32) # [patient, visit, input_size]
>>> drugs = torch.randint(0, 2, (3, 50)).float()
>>> layer = MICRONLayer(32, 64, 50)
>>> loss, y_prob = layer(patient_emb, drugs)
>>> loss.shape
torch.Size([])
>>> y_prob.shape
torch.Size([3, 50])
static compute_reconstruction_loss(logits, logits_residual, mask)[source]#
Return type

tensor

forward(patient_emb, drugs, mask=None)[source]#

Forward propagation.

Parameters
  • patient_emb (tensor) – a tensor of shape [patient, visit, input_size].

  • drugs (tensor) – a multihot tensor of shape [patient, num_labels].

  • mask (Optional[tensor]) – an optional tensor of shape [patient, visit] where 1 indicates valid visits and 0 indicates invalid visits.

Returns

a scalar tensor representing the loss. y_prob: a tensor of shape [patient, num_labels] representing

the probability of each drug.

Return type

loss

training: bool#
class pyhealth.models.MICRON(dataset, embedding_dim=128, hidden_dim=128, **kwargs)[source]#

Bases: BaseModel

MICRON model.

Paper: Chaoqi Yang et al. Change Matters: Medication Change Prediction with Recurrent Residual Networks. IJCAI 2021.

Note

This model is only for medication prediction which takes conditions and procedures as feature_keys, and drugs as label_key. It only operates on the visit level.

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

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

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

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

forward(conditions, procedures, drugs, **kwargs)[source]#

Forward propagation.

Parameters
  • conditions (List[List[List[str]]]) – a nested list in three levels [patient, visit, condition].

  • procedures (List[List[List[str]]]) – a nested list in three levels [patient, visit, procedure].

  • drugs (List[List[str]]) – a nested list in two levels [patient, drug].

Returns

loss: a scalar tensor representing the loss. y_prob: a tensor of shape [patient, visit, num_labels] representing

the probability of each drug.

y_true: a tensor of shape [patient, visit, num_labels] representing

the ground truth of each drug.

Return type

A dictionary with the following keys

training: bool#