Customize a Model#
Here we introduce how to customize a TPP model with the support of EasyTPP.
Create a new TPP Model Class#
New models inherit from
easy_tpp.model.BaseModel. TorchBaseModel remains an
alias for backward compatibility.
from easy_tpp.model.basemodel import BaseModel
class NewModel(BaseModel):
def __init__(self, model_config):
super().__init__(model_config)
# Initialize the model's layers here.
def forward(self, time_seqs, time_delta_seqs, type_seqs, **kwargs):
# Return model-specific history states.
raise NotImplementedError
def loglike_loss(self, batch=None, **kwargs):
time_seqs, time_delta_seqs, type_seqs, seq_non_pad_mask, attention_mask = (
self.resolve_batch_inputs(batch, kwargs)
)
# Return (summed_loss, num_events), as required by ModelWrapper.
raise NotImplementedError
def compute_intensities_at_sample_times(
self, time_seqs, time_delta_seqs, type_seqs, sample_dtimes, **kwargs
):
# Return [batch_size, seq_len, num_samples, num_event_types].
# Honor kwargs.get("compute_last_step_only", False) for generation.
raise NotImplementedError
Rewrite Relevant Methods#
There are three important functions needed to be implemented:
forward: the input is the batch data and the output is states at each step.
loglike_loss: accepts named batch inputs and returns the summed loss and number of target events. The wrapper divides the loss by this count.compute_intensities_at_sample_times: it computes the intensities at each sampling steps.
Import your model class before constructing the runner so that
BaseModel.generate_model_from_config can find the subclass. Set
base_config.model_id to the exact class name (NewModel here).
See easy_tpp/model/nhp.py for a complete implementation; the skeleton
above only documents the interfaces.