RumourDetectionTwitterModel

class RumourDetectionTwitterModel(config: sgnlp.models.rumour_detection_twitter.config.RumourDetectionTwitterConfig)[source]

Class to create the Hierarhical Transformer with structure and post level attention used to evaluate Twitter threads.

This method inherits from RumourDetectionTwitterPreTrainedModel for weights initalization and utility functions from transformer PreTrainedModel class.

Parameters

config (RumourDetectionTwitterConfig) – Model configuration class with the default parameters required for the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Use the from_pretrained method to load the model weights.

Example:

# 1. From config / default parameters (untrained)
config = RumourDetectionTwitterConfig()
model = RumourDetectionTwitterModel(config)

# 2. From pretrained
config = RumourDetectionTwitterConfig.from_pretrained("https://storage.googleapis.com/sgnlp/models/rumour_detection_twitter/config.json")
model = RumourDetectionTwitterModel.from_pretrained("https://storage.googleapis.com/sgnlp/models/rumour_detection_twitter/pytorch_model.bin", config=config)
forward(token_ids: torch.Tensor, time_delay_ids: torch.Tensor, structure_ids: torch.Tensor, token_attention_mask=None, post_attention_mask=None, labels: Optional[torch.Tensor] = None)[source]

Forward method to compute model output given a Twitter thread.

Parameters
  • token_ids (torch.Tensor) – Token indices of all threads. Shape of (batch_size, max_posts, max_token_length)

  • time_delay_ids (torch.Tensor) – Time delay indices for each tweet in each thread. Note that this is not the actual time delay (in seconds/minutes/etc). This is the index of the binned time delay. Shape of (batch_size, max_posts)

  • structure_ids (torch.Tensor) – Contains the structure category index for each post with respect to every other post in the same thread. Shape of (batch_size, max_posts, max_posts)

  • token_attention_mask (torch.Tensor) – Tensor with elements of only 0 or 1. Indicates which tokens in each tweet should be considered (ie, which tokens are not padding). Shape of (batch_size, max_posts, max_token_length)

  • post_attention_mask (torch.Tensor) – Tensor with elements of only 0 or 1. Indicates which post in each thread should be considered (ie, which tokens are not padding). Shape of (batch_size, max_posts)

  • labels (Optional[torch.Tensor]) – Tensor with labels. Shape of (batch_size, 1). Defaults to None

Returns

raw prediction logits of shape (batch_size, num_classes). num_classes = 4 by default.

Return type

torch.Tenosr

load_pretrained_embedding(pretrained_embedding_path)[source]

Load pretrained embedding matrix to the embedding layer

Parameters

pretrained_embedding_path (str) – path to a .npy file containing the pretrained embeddings. Note that the position of each word’s embedding in the matrix has to correspond to the index of that word in the tokenizer’s vocab.

prepare_word_pos(token_ids)[source]

Generates the position indices for the tokens in each thread.

Parameters

token_ids (torch.Tensor) – Token indices of all threads. Shape of (batch_size, max_posts, max_token_length)

Returns

position indices of each token in each thread of each post. Shape should be equivalent to token_ids - (batch_size, max_posts, max_token_length).

Return type

torch.Tenosr