trajdl.algorithms package#
- class trajdl.algorithms.GMVSAE(tokenizer: str | Any, embedding_dim: int, hidden_size: int, mem_num: int, mode: str, num_layers: int = 1, num_neg_samples: int = 64, init_mu_c_pretrained_path: str | None = None, pretrain_ckpt_folder: str | None = None)[source]#
Bases:
PretrainTrainFramework- abnormal_detect(decoder_seq: LongTensor, decoder_lengths: List[int], decoder_labels: LongTensor, mask: BoolTensor) Tensor[source]#
使用解码器进行序列的异常检测
- Parameters:
decoder_seq (torch.LongTensor) – 解码器输入的序列
decoder_lengths (List[int]) – 解码器输入的各序列的长度
decoder_labels (torch.LongTensor) – 解码器需要的输出
mask (torch.BoolTensor) – 解码器输入序列长度对应的mask矩阵
- Returns:
shape is (B,),表示每条序列的异常分数
- Return type:
torch.Tensor
- compute_loss(batch: GMVSAESample)[source]#
计算损失
- Parameters:
batch (GMVSAESample) – 输入样本
- Returns:
loss (torch.Tensor) – shape is (1,)
batch_size (int) – batch size
- decode(init_state: Tensor, decoder_seq: LongTensor, decoder_lengths: List[int], decoder_labels: LongTensor, mask: BoolTensor) Tensor[source]#
init_state: shape is (B, H)
decoder_seq: shape is (B, T + 1), each of seq added BOS decoder_lengths: List[int], encoder_lengths + 1 decoder_labels: shape is (B, T + 1), each of seq added EOS mask: shape is (B, T + 1)
- forward(batch: GMVSAESample) Tensor[source]#
推理的逻辑,在预训练阶段和评估阶段的生成结果不同。 1. 预训练阶段是生成隐变量z 2. 评估阶段是生成序列的异常分数。
- Parameters:
batch (GMVSAESample) – 输入样本
batch_idx (int) – lightning框架使用的batch_idx
- Returns:
z (torch.Tensor) – 当模式为预训练的时候返回这一项,shape is (num_layers, B, H),隐变量z
inference_result (torch.Tensor) – 当模式为评估的时候返回这一项,shape is (B,),是各条序列的异常分数
- generate_z(encoder_seq: LongTensor, encoder_lengths: List[int], return_loss: bool = False) Tuple[Tensor, Tensor, Tensor] | Tensor[source]#
这个是编码阶段进行z的生成
- Parameters:
encoder_seq (torch.LongTensor) – 编码器的输入序列,shape是(B, T)
encoder_lengths (List[int]) – 编码器输入序列各个序列的长度
return_loss (bool, optional) – 是否返回损失,默认是False
- Returns:
(z, batch_gaussian_loss, batch_uniform_loss) (Tuple[torch.Tensor, torch.Tensor, torch.Tensor]) – 当return_loss为True的时候,再返回两个loss,三个tensor的shape都是(num_layers, B, H)
z (torch.Tensor) – 当return_loss为False的时候,只返回z,shape是(num_layers, B, H)
- init_decoder_state_for_inference(batch_size: int, c_idx: int) Tensor[source]#
init a decoder state for inference
- property mem_num: int#
- training_step(batch: GMVSAESample, batch_idx: int)[source]#
Here you compute and return the training loss and some additional metrics for e.g. the progress bar or logger.
- Parameters:
batch – The output of your data iterable, normally a
DataLoader.batch_idx – The index of this batch.
dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)
- Returns:
Tensor- The loss tensordict- A dictionary which can include any keys, but must include the key'loss'in the case of automatic optimization.None- In automatic optimization, this will skip to the next batch (but is not supported for multi-GPU, TPU, or DeepSpeed). For manual optimization, this has no special meaning, as returning the loss is not required.
In this step you’d normally do the forward pass and calculate the loss for a batch. You can also do fancier things like multiple forward passes or something model specific.
Example:
def training_step(self, batch, batch_idx): x, y, z = batch out = self.encoder(x) loss = self.loss(out, x) return loss
To use multiple optimizers, you can switch to ‘manual optimization’ and control their stepping:
def __init__(self): super().__init__() self.automatic_optimization = False # Multiple optimizers (e.g.: GANs) def training_step(self, batch, batch_idx): opt1, opt2 = self.optimizers() # do training_step with encoder ... opt1.step() # do training_step with decoder ... opt2.step()
Note
When
accumulate_grad_batches> 1, the loss returned here will be automatically normalized byaccumulate_grad_batchesinternally.
- validation_step(batch: GMVSAESample, batch_idx: int)[source]#
Operates on a single batch of data from the validation set. In this step you’d might generate examples or calculate anything of interest like accuracy.
- Parameters:
batch – The output of your data iterable, normally a
DataLoader.batch_idx – The index of this batch.
dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)
- Returns:
Tensor- The loss tensordict- A dictionary. Can include any keys, but must include the key'loss'.None- Skip to the next batch.
# if you have one val dataloader: def validation_step(self, batch, batch_idx): ... # if you have multiple val dataloaders: def validation_step(self, batch, batch_idx, dataloader_idx=0): ...
Examples:
# CASE 1: A single validation dataset def validation_step(self, batch, batch_idx): x, y = batch # implement your own out = self(x) loss = self.loss(out, y) # log 6 example images # or generated text... or whatever sample_imgs = x[:6] grid = torchvision.utils.make_grid(sample_imgs) self.logger.experiment.add_image('example_images', grid, 0) # calculate acc labels_hat = torch.argmax(out, dim=1) val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0) # log the outputs! self.log_dict({'val_loss': loss, 'val_acc': val_acc})
If you pass in multiple val dataloaders,
validation_step()will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.# CASE 2: multiple validation dataloaders def validation_step(self, batch, batch_idx, dataloader_idx=0): # dataloader_idx tells you which dataset this is. ...
Note
If you don’t need to validate you don’t need to implement this method.
Note
When the
validation_step()is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of validation, the model goes back to training mode and gradients are enabled.
- class trajdl.algorithms.HIER(tokenizer_path: str, hidden_size: int, num_layers: int, h_grid: HierarchyGridSystem, location_embedding_dims: List[int], week_embedding_dim: int = 4, hour_embedding_dim: int = 4, duration_embedding_dim: int = 4, dropout: float = 0.1, reduction: str | LossEnum = 'mean')[source]#
Bases:
LightningModule- forward(src: LongTensor, week: LongTensor, hour: LongTensor, duration: LongTensor, lengths: List[int])[source]#
Same as
torch.nn.Module.forward().- Parameters:
*args – Whatever you decide to pass into the forward method.
**kwargs – Keyword arguments are also possible.
- Returns:
Your model’s output
- training_step(batch, batch_idx: int) Tensor[source]#
Here you compute and return the training loss and some additional metrics for e.g. the progress bar or logger.
- Parameters:
batch – The output of your data iterable, normally a
DataLoader.batch_idx – The index of this batch.
dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)
- Returns:
Tensor- The loss tensordict- A dictionary which can include any keys, but must include the key'loss'in the case of automatic optimization.None- In automatic optimization, this will skip to the next batch (but is not supported for multi-GPU, TPU, or DeepSpeed). For manual optimization, this has no special meaning, as returning the loss is not required.
In this step you’d normally do the forward pass and calculate the loss for a batch. You can also do fancier things like multiple forward passes or something model specific.
Example:
def training_step(self, batch, batch_idx): x, y, z = batch out = self.encoder(x) loss = self.loss(out, x) return loss
To use multiple optimizers, you can switch to ‘manual optimization’ and control their stepping:
def __init__(self): super().__init__() self.automatic_optimization = False # Multiple optimizers (e.g.: GANs) def training_step(self, batch, batch_idx): opt1, opt2 = self.optimizers() # do training_step with encoder ... opt1.step() # do training_step with decoder ... opt2.step()
Note
When
accumulate_grad_batches> 1, the loss returned here will be automatically normalized byaccumulate_grad_batchesinternally.
- validation_step(batch, batch_idx: int) Tensor[source]#
Operates on a single batch of data from the validation set. In this step you’d might generate examples or calculate anything of interest like accuracy.
- Parameters:
batch – The output of your data iterable, normally a
DataLoader.batch_idx – The index of this batch.
dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)
- Returns:
Tensor- The loss tensordict- A dictionary. Can include any keys, but must include the key'loss'.None- Skip to the next batch.
# if you have one val dataloader: def validation_step(self, batch, batch_idx): ... # if you have multiple val dataloaders: def validation_step(self, batch, batch_idx, dataloader_idx=0): ...
Examples:
# CASE 1: A single validation dataset def validation_step(self, batch, batch_idx): x, y = batch # implement your own out = self(x) loss = self.loss(out, y) # log 6 example images # or generated text... or whatever sample_imgs = x[:6] grid = torchvision.utils.make_grid(sample_imgs) self.logger.experiment.add_image('example_images', grid, 0) # calculate acc labels_hat = torch.argmax(out, dim=1) val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0) # log the outputs! self.log_dict({'val_loss': loss, 'val_acc': val_acc})
If you pass in multiple val dataloaders,
validation_step()will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.# CASE 2: multiple validation dataloaders def validation_step(self, batch, batch_idx, dataloader_idx=0): # dataloader_idx tells you which dataset this is. ...
Note
If you don’t need to validate you don’t need to implement this method.
Note
When the
validation_step()is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of validation, the model goes back to training mode and gradients are enabled.
- class trajdl.algorithms.T2VEC(embedding_dim: int, hidden_size: int, tokenizer: str | AbstractTokenizer, knn_indices_path: str, knn_distances_path: str, num_layers: int = 1, bidirectional_encoder: bool = False, embedding_path: str | None = None, freeze_embedding: bool = False, dropout: float = 0.0)[source]#
Bases:
BaseLightningModel- compute_loss(batch: T2VECSample)[source]#
- Parameters:
batch (T2VECSample)
- Returns:
loss (torch.Tensor) – shape is (1,)
batch_size (int)
- encode(batch: T2VECSample) Tuple[Tensor, Tensor][source]#
- forward(batch: T2VECSample) Tensor[source]#
推理的逻辑
- Parameters:
batch (T2VECSample)
- Return type:
torch.Tensor, shape is (batch_size, hidden_size)
- training_step(batch: T2VECSample, batch_idx: int) Tensor[source]#
- Parameters:
batch (T2VECSample)
- validation_step(batch: T2VECSample, batch_idx: int) Tensor[source]#
Operates on a single batch of data from the validation set. In this step you’d might generate examples or calculate anything of interest like accuracy.
- Parameters:
batch – The output of your data iterable, normally a
DataLoader.batch_idx – The index of this batch.
dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)
- Returns:
Tensor- The loss tensordict- A dictionary. Can include any keys, but must include the key'loss'.None- Skip to the next batch.
# if you have one val dataloader: def validation_step(self, batch, batch_idx): ... # if you have multiple val dataloaders: def validation_step(self, batch, batch_idx, dataloader_idx=0): ...
Examples:
# CASE 1: A single validation dataset def validation_step(self, batch, batch_idx): x, y = batch # implement your own out = self(x) loss = self.loss(out, y) # log 6 example images # or generated text... or whatever sample_imgs = x[:6] grid = torchvision.utils.make_grid(sample_imgs) self.logger.experiment.add_image('example_images', grid, 0) # calculate acc labels_hat = torch.argmax(out, dim=1) val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0) # log the outputs! self.log_dict({'val_loss': loss, 'val_acc': val_acc})
If you pass in multiple val dataloaders,
validation_step()will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.# CASE 2: multiple validation dataloaders def validation_step(self, batch, batch_idx, dataloader_idx=0): # dataloader_idx tells you which dataset this is. ...
Note
If you don’t need to validate you don’t need to implement this method.
Note
When the
validation_step()is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of validation, the model goes back to training mode and gradients are enabled.
- class trajdl.algorithms.TULER(tokenizer: str | Path | AbstractTokenizer, num_users: int, embedding_dim: int, hidden_dim: int, rnn_type: str = 'lstm', num_layers: int = 1, dropout: float = 0.5, embedding_path: str | None = None, freeze_embedding: bool = False, bidirectional: bool = True, optimizer_type: str = 'adam', learning_rate: float = 0.001, topk: int = 5)[source]#
Bases:
BaseLightningModelTULER: Identifying Human Mobility via Trajectory Embeddings (IJCAI 2017)
- compute_loss(batch: TULERSample, return_prediction: bool = False)[source]#
- forward(sample: TULERSample) Tensor[source]#
推理的过程,给定多条序列和实际长度,返回这些序列在预测用户时的logits
- Parameters:
sample (TULERSample)
- Returns:
output – shape is (B, num_users), 每条序列的logits
- Return type:
torch.Tensor
- on_validation_epoch_start()[source]#
Called in the validation loop at the very beginning of the epoch.
- test_step(batch: TULERSample, batch_idx: int) None[source]#
Operates on a single batch of data from the test set. In this step you’d normally generate examples or calculate anything of interest such as accuracy.
- Parameters:
batch – The output of your data iterable, normally a
DataLoader.batch_idx – The index of this batch.
dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)
- Returns:
Tensor- The loss tensordict- A dictionary. Can include any keys, but must include the key'loss'.None- Skip to the next batch.
# if you have one test dataloader: def test_step(self, batch, batch_idx): ... # if you have multiple test dataloaders: def test_step(self, batch, batch_idx, dataloader_idx=0): ...
Examples:
# CASE 1: A single test dataset def test_step(self, batch, batch_idx): x, y = batch # implement your own out = self(x) loss = self.loss(out, y) # log 6 example images # or generated text... or whatever sample_imgs = x[:6] grid = torchvision.utils.make_grid(sample_imgs) self.logger.experiment.add_image('example_images', grid, 0) # calculate acc labels_hat = torch.argmax(out, dim=1) test_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0) # log the outputs! self.log_dict({'test_loss': loss, 'test_acc': test_acc})
If you pass in multiple test dataloaders,
test_step()will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.# CASE 2: multiple test dataloaders def test_step(self, batch, batch_idx, dataloader_idx=0): # dataloader_idx tells you which dataset this is. ...
Note
If you don’t need to test you don’t need to implement this method.
Note
When the
test_step()is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of the test epoch, the model goes back to training mode and gradients are enabled.
- training_step(batch: TULERSample, batch_idx: int) Tensor[source]#
- Parameters:
batch (TULERSample) – 样本
batch_idx (int) – lightning框架需要的batch_idx
- Returns:
loss – shape is (1,)
- Return type:
torch.Tensor
- validation_step(batch: TULERSample, batch_idx: int) Tensor[source]#
Operates on a single batch of data from the validation set. In this step you’d might generate examples or calculate anything of interest like accuracy.
- Parameters:
batch – The output of your data iterable, normally a
DataLoader.batch_idx – The index of this batch.
dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)
- Returns:
Tensor- The loss tensordict- A dictionary. Can include any keys, but must include the key'loss'.None- Skip to the next batch.
# if you have one val dataloader: def validation_step(self, batch, batch_idx): ... # if you have multiple val dataloaders: def validation_step(self, batch, batch_idx, dataloader_idx=0): ...
Examples:
# CASE 1: A single validation dataset def validation_step(self, batch, batch_idx): x, y = batch # implement your own out = self(x) loss = self.loss(out, y) # log 6 example images # or generated text... or whatever sample_imgs = x[:6] grid = torchvision.utils.make_grid(sample_imgs) self.logger.experiment.add_image('example_images', grid, 0) # calculate acc labels_hat = torch.argmax(out, dim=1) val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0) # log the outputs! self.log_dict({'val_loss': loss, 'val_acc': val_acc})
If you pass in multiple val dataloaders,
validation_step()will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.# CASE 2: multiple validation dataloaders def validation_step(self, batch, batch_idx, dataloader_idx=0): # dataloader_idx tells you which dataset this is. ...
Note
If you don’t need to validate you don’t need to implement this method.
Note
When the
validation_step()is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of validation, the model goes back to training mode and gradients are enabled.