faknow.model.social_context
faknow.model.social_context.base_gnn
- class faknow.model.social_context.base_gnn.GAT(feature_size: int, hidden_size=128)[source]
Bases:
_BaseGNN
Graph Attention Networks, ICLR 2018 paper: https://openreview.net/forum?id=rJXMpikCZ code: https://github.com/safe-graph/GNN-FakeNews
- __init__(feature_size: int, hidden_size=128)[source]
- Parameters:
feature_size (int) – dimension of input node feature
hidden_size (int) – Default=128
- training: bool
- class faknow.model.social_context.base_gnn.GCN(feature_size: int, hidden_size=128)[source]
Bases:
_BaseGNN
Semi-Supervised Classification with Graph Convolutional Networks, ICLR 2017 paper: https://openreview.net/forum?id=SJU4ayYgl code: https://github.com/safe-graph/GNN-FakeNews
- __init__(feature_size: int, hidden_size=128)[source]
- Parameters:
feature_size (int) – dimension of input node feature
hidden_size (int) – Default=128
- training: bool
- class faknow.model.social_context.base_gnn.SAGE(feature_size: int, hidden_size=128)[source]
Bases:
_BaseGNN
Inductive Representation Learning on Large Graphs, NeurIPS 2017 paper: https://dl.acm.org/doi/10.5555/3294771.3294869 code: https://github.com/safe-graph/GNN-FakeNews
- __init__(feature_size: int, hidden_size=128)[source]
- Parameters:
feature_size (int) – dimension of input node feature
hidden_size (int) – Default=128
- training: bool
faknow.model.social_context.bigcn
- class faknow.model.social_context.bigcn.BiGCN(feature_size: int, hidden_size=128, out_size=128)[source]
Bases:
AbstractModel
Rumor Detection on Social Media with Bi-Directional Graph Convolutional Networks, AAAI 2020 paper: https://ojs.aaai.org/index.php/AAAI/article/view/5393 code: https://github.com/safe-graph/GNN-FakeNews
- __init__(feature_size: int, hidden_size=128, out_size=128)[source]
- Parameters:
feature_size (int) – dimension of input node feature
hidden_size (int) – dimension of hidden layer. Default=128
out_size (int) – dimension of output layer. Default=128
- calculate_loss(data: Batch) → Tensor[source]
calculate loss via CrossEntropyLoss
- Parameters:
data (Batch) – batch data
- Returns:
loss
- Return type:
torch.Tensor
- forward(x: Tensor, edge_index: Tensor, bu_edge_index: Tensor, batch: Tensor, root_index: Tensor)[source]
- Parameters:
x (Tensor) – node feature, shape=(num_nodes, feature_size), shape=(num_nodes, feature_size)
edge_index (Tensor) – top-down edge index, shape=(2, num_edges)
bu_edge_index (Tensor) – bottom-up edge index, shape=(2, num_edges)
batch (Tensor) – index of graph each node belongs to, shape=(num_nodes,)
root_index (Tensor) – index of root node for each graph, shape=(num_graphs,)
- Returns:
prediction of being fake, shape=(batch_size, 2)
- Return type:
Tensor
- predict(data_without_label) → Tensor[source]
predict the probability of being fake news
- Parameters:
data_without_label (Batch) – batch data
- Returns:
softmax probability, shape=(num_graphs, 2)
- Return type:
Tensor
- training: bool
faknow.model.social_context.dudef
- class faknow.model.social_context.dudef.DUDEF(input_size: int, emotion_len: int, hidden_size: int, embedding_matrix: Tensor)[source]
Bases:
AbstractModel
DUDEF: Mining Dual Emotion for Fake News Detection, WWW 2021 paper: https://dl.acm.org/doi/10.1145/3442381.3450004 code: https://github.com/RMSnow/WWW2021
- __init__(input_size: int, emotion_len: int, hidden_size: int, embedding_matrix: Tensor)[source]
- Parameters:
input_size (int) – dimension of input node feature
emotion_len (int) – dimension of dual_emotion
hidden_size (int) – dimension of hidden layer
embedding_matrix (Tensor) – word embedding matrix
- calculate_loss(data: Dict[str, Tensor]) → Tensor[source]
calculate loss via CrossEntropyLoss
- Parameters:
data (Dict[str, Tensor]) – batch data, shape=(2,batch_size,feature_size)
- Returns:
loss
- Return type:
Tensor
- forward(semantics: Tensor, emotions: Tensor) → Tensor[source]
- Parameters:
semantics (Tensor) – node semantics features, shape=(batch_size, feature_size)
emotions (Tensor) – node emotions features, shape=(batch_size, feature_size)
- Returns:
prediction of being fake, shape=(batch_size, 2)
- Return type:
Tensor
- predict(data: Dict[str, Tensor]) → Tensor[source]
predict the probability of being fake news
- Parameters:
data (Batch) – batch data, shape=(2,batch_size,feature_size)
- Returns:
softmax probability, shape=(batch_size, 2)
- Return type:
Tensor
- training: bool
faknow.model.social_context.ebgcn
- class faknow.model.social_context.ebgcn.EBGCN(input_size=5000, hidden_size=64, output_size=64, edge_num=2, dropout=0.5, num_class=4, edge_loss_weight=0.2)[source]
Bases:
AbstractModel
Towards Propagation Uncertainty: Edge-enhanced Bayesian Graph Convolutional Networks for Rumor Detection, ACL 2021 paper: https://aclanthology.org/2021.acl-long.297/ code: https://github.com/weilingwei96/EBGCN
- __init__(input_size=5000, hidden_size=64, output_size=64, edge_num=2, dropout=0.5, num_class=4, edge_loss_weight=0.2)[source]
- Parameters:
input_size (int) – the feature size of input. default=5000.
hidden_size (int) – the feature size of hidden embedding. default=64.
output_size (int) – the feature size of output embedding. default=64.
edge_num (int) – the num of edge type. default=2.
dropout (float) – dropout rate. default=0.5.
num_class (int) – the num of output type. default=4
edge_loss_weight (float) – the weight of edge loss. default=0.2.
- calculate_loss(data: Batch) → Dict[str, float | Any][source]
calculate loss for EBGCN
- Parameters:
data (Batch) – pyg batch data
- Returns:
loss dict with key ‘total_loss’, ‘pred_loss’, ‘edge_loss’
- Return type:
dict
- forward(node_features: Tensor, td_edge_index: Tensor, bu_edge_index: Tensor, root_index: Tensor, batch: Tensor)[source]
- Parameters:
node_features (Tensor) – feature of node in claims.
td_edge_index (Tensor) – directed adjacent martix in COO format from top to bottom.
bu_edge_index (Tensor) – directed adjacent martix in COO format from bottom to top.
root_index (Tensor) – index of root news in claims.
batch (Tensor) – vector mapping each node to its respective graph.
- Returns:
predict output. td_edge_loss(Tensor): edge loss of graph with direction from top to bottom. bu_edge_loss(Tensor): edge loss of graph with direction from bottom to top.
- Return type:
output(Tensor)
- predict(data_without_label: Batch) → Tensor[source]
predict the probability of being fake news
- Parameters:
data_without_label (Batch) – pyg batch data
- Returns:
softmax probability, shape=(batch_size, num_classes)
- Return type:
Tensor
- training: bool
faknow.model.social_context.eddfn
- class faknow.model.social_context.eddfn.EDDFN(input_size: int, domain_size: int, lambda1=1.0, lambda2=10.0, lambda3=5.0, hidden_size=512)[source]
Bases:
AbstractModel
Embracing Domain Differences in Fake News Cross-domain Fake News Detection using Multi-modal Data, AAAI 2021 paper: https://ojs.aaai.org/index.php/AAAI/article/view/16134 code: https://github.com/amilasilva92/cross-domain-fake-news-detection-aaai2021
- __init__(input_size: int, domain_size: int, lambda1=1.0, lambda2=10.0, lambda3=5.0, hidden_size=512)[source]
- Parameters:
input_size (int) – dimension of input representation
domain_size (int) – dimension of domain vector
lambda1 (float) – L_{recon} loss weight. Default=1.0
lambda2 (float) – L_{specific} loss weight. Default=10.0
lambda3 (float) – L_{shared} loss weight. Default=5.0
hidden_size (int) – size of hidden layer. Default=512
- calculate_loss(data: Tuple[Tensor]) → Dict[str, Tensor][source]
calculate total loss, including classification loss(BCELoss), reconstruction loss(MSELoss), specific domain loss(MSELoss) and shared domain loss(MSELoss)
- Parameters:
data (Tuple[Tensor]) – batch data tuple, including input, domain and label
- Returns:
loss dict, key: total_loss, class_loss, decoder_loss, specific_domain_loss, shared_domain_loss
- Return type:
Dict[str, Tensor]
- forward(input_representation: Tensor) → Tuple[Tensor, Tensor, Tensor, Tensor][source]
- Parameters:
input_representation (Tensor) – input representation consisting of text and propagation network
- Returns:
class_out (Tensor): prediction of being fake news, shape=(batch_size, 1) decoder_out (Tensor): prediction of input, shape=(batch_size, input_size) specific_domain (Tensor): specific domain output, shape=(batch_size, domain_size) shared_domain (Tensor): shared domain output, shape=(batch_size, domain_size)
- Return type:
tuple
- predict(data_without_label: Tuple[Tensor])[source]
predict the probability of being fake news
- Parameters:
data_without_label (Tuple[Tensor]) – batch data tuple, including input, domain
- Returns:
one hot probability, shape=(batch_size, 2)
- Return type:
Tensor
- training: bool
faknow.model.social_context.fang
- class faknow.model.social_context.fang.FANG(fang_data: FangDataset, input_size=100, embedding_size=16, num_stance=4, num_stance_hidden=4, timestamp_size=2, num_classes=2, dropout=0.0, device='cpu')[source]
Bases:
AbstractModel
FANG: Leveraging Social Context for Fake News Detection Using Graph Representation, CIKM 2020 paper: https://dl.acm.org/doi/10.1145/3340531.3412046 code: https://github.com/nguyenvanhoang7398/FANG
- __init__(fang_data: FangDataset, input_size=100, embedding_size=16, num_stance=4, num_stance_hidden=4, timestamp_size=2, num_classes=2, dropout=0.0, device='cpu')[source]
- Parameters:
fang_data (FangDataset) – global graph information.
input_size (int) – raw features’ embedding size. default=100.
embedding_size (int) – embedding size of fang and the output size of graphsage. default=16.
num_stance (int) – the total num of stance. default=4.
num_stance_hidden (int) – the feature size of stance(usually num_stance_hidden * num_stance = embedding_size). default=4.
timestamp_size (int) – the feature size of timestamp. default=2.
num_classes (int) – label num. default=2.
dropout (float) – dropout rate. default=0.1.
- calculate_loss(data: Tensor) → dict[source]
calculate total loss
- Parameters:
data (torch.Tensor) – nodes’ idx list
- Returns:
news_loss + stance_loss + unsup_loss
- Return type:
Torch.Tensor
- extract_most_attended_users(news_attn: Tensor, engage_users: list)[source]
using attention to search the most attended user to compute unsupvised loss.
- Parameters:
news_attn (torch.Tensor) – users’ attention for news.
engage_users (list) – all engaged users.
- fetch_news_user_stance(news_nodes: list, stance_lists: dict)[source]
fetch data required for stance prediction.
- Parameters:
news_nodes (list) – list of news node.
stance_lists (dict) – stance information dict for every node.
- forward(data_batch: list, test=False)[source]
- Parameters:
data_batch (list) – node’s idx list.
test (bool) – test or train. default=False
- get_neigh_weights(node: int, node_only=False)[source]
get node’s neighbor.
- Parameters:
node (int) – node idx.
node_only (bool) – whether to get only node or both node and its edge information. default=False.
- get_proximity_samples(nodes: list, num_pos: int, num_neg: int, neg_walk_len: int)[source]
positive samping and negative sampling.
- Parameters:
nodes (list) – node idx list.
num_pos (int) – positive sampling num.
num_neg (int) – negative sampling num.
neg_walk_len (int) – walk step.
- predict(data_without_label: dict) → Tensor[source]
predict the probability of being fake news
- Parameters:
data_without_label (dict) – batch data
- Returns:
predict probability, shape=(batch_size, 2)
- Return type:
torch.Tensor
- preprocess_news_classification_data(nodes_batch: list, embed_size: int, test=False)[source]
- Parameters:
nodes_batch (list) – the idx list of node to be processed.
embed_size (int) – the size of embedding features.
test (bool) – if test or trai
- training: bool
faknow.model.social_context.gcnfn
- class faknow.model.social_context.gcnfn.GCNFN(feature_size: int, hidden_size=128, dropout_ratio=0.5)[source]
Bases:
_BaseGCNFN
Fake news detection on social media using geometric deep learning, arXiv 2019 paper: https://arxiv.org/abs/1902.06673 code: https://github.com/safe-graph/GNN-FakeNews
- __init__(feature_size: int, hidden_size=128, dropout_ratio=0.5)[source]
- Parameters:
feature_size (int) – dimension of input node feature
hidden_size (int) – dimension of hidden layer. Default=128
dropout_ratio (float) – dropout ratio. Default=0.5
- training: bool
faknow.model.social_context.gnncl
- class faknow.model.social_context.gnncl.GNNCL(feature_size: int, max_nodes: int)[source]
Bases:
AbstractModel
Graph neural networks with continual learning for fake news detection from social media, arXiv 2020 paper: https://arxiv.org/abs/2007.03316 code: https://github.com/safe-graph/GNN-FakeNews
- __init__(feature_size: int, max_nodes: int)[source]
- Parameters:
feature_size (int) – dimension of input node feature
max_nodes (int) – maximum number of nodes in a graph
- calculate_loss(data: Batch) → Tensor[source]
calculate loss via CrossEntropyLoss
- Parameters:
data (Batch) – batch data
- Returns:
loss
- Return type:
torch.Tensor
- forward(x: Tensor, adj: Tensor, mask: Tensor | None = None)[source]
- Parameters:
x (Tensor) – node features, shape=(batch_size, max_nodes, feature_size)
adj (Tensor) – adjacency matrix, shape=(batch_size, max_nodes, max_nodes)
mask (Tensor) – mask for adjacency matrix, shape=(batch_size, max_nodes). Default=None.
- Returns:
output (Tensor): prediction of being fake, shape (batch_size, 2) l (Tensor): loss of first pooling layer e (Tensor): entropy of first pooling layer
- Return type:
tuple
- predict(data_without_label) → Tensor[source]
predict the probability of being fake news
- Parameters:
data_without_label (Batch) – batch data
- Returns:
softmax probability, shape=(batch_size, 2)
- Return type:
Tensor
- training: bool
faknow.model.social_context.trustrd
- class faknow.model.social_context.trustrd.TrustRD(pre_trained_model: TrustRDPreTrainModel, in_feature=192, hid_feature=64, num_classes=4, sigma_m=0.1, eta=0.4, zeta=0.02)[source]
Bases:
AbstractModel
Towards Trustworthy Rumor Detection with Interpretable Graph Structural Learning, CIKM 2023 paper: https://dl.acm.org/doi/10.1145/3583780.3615228 code: https://github.com/Anonymous4ScienceAuthor/TrustRD
- __init__(pre_trained_model: TrustRDPreTrainModel, in_feature=192, hid_feature=64, num_classes=4, sigma_m=0.1, eta=0.4, zeta=0.02)[source]
- Parameters:
pre_trained_model (TrustRDPreTrainModel) – the model of trained TrustRDPreTrainModel.
in_feature (int) – the feature size of input. default=192.
hid_feature (int) – the feature size of hidden embedding. default=64.
num_classes (int) – the num of class. default=4.
sigma_m (float) – data perturbation Standard Deviation. default=0.1
eta (float) – data perturbation weight. default=0.4.
zeta (float) – parameter perturbation weight. default=0.02
- calculate_loss(data: Batch) → Dict[str, float][source]
loss = pred_loss + 0.2 * loss_para + 0.2 * loss_data, where pred_loss is the cross entropy loss, loss_para is the parameter perturbation loss, and loss_data is the data perturbation loss.
- Parameters:
data (Batch) – batch data
- Returns:
loss dict with key ‘total_loss’, ‘pred_loss’, ‘para_loss’, ‘data_loss’
- Return type:
dict
- forward(embed: Tensor, data: Data)[source]
- Parameters:
embed (Tensor) – embedding features processed by TrustRDPreTrainModel.
data (Data) – batch data.
- Returns:
[predict_result, last_prob, kl_div]
- Return type:
Tensor
- predict(data_without_label: Batch) → Tensor[source]
predict the probability of being fake news
- Parameters:
data_without_label (Batch) – batch data
- Returns:
predication probability, shape=(num_graphs, 2)
- Return type:
Tensor
- training: bool
- class faknow.model.social_context.trustrd.TrustRDPreTrainModel(hidden_dim=64, num_gcn_layers=3, num_classes=4)[source]
Bases:
AbstractModel
Self-Supervised Pretraining Model for TrustRD
- __init__(hidden_dim=64, num_gcn_layers=3, num_classes=4)[source]
- Parameters:
hidden_dim (int) – the feature size of hidden embedding. default=64.
num_gcn_layers (int) – the gcn pre_trained_model layer num. default=3.
num_classes (int) – the num of class. default=4.
- calculate_loss(data: Batch) → Tensor[source]
calculate ib_loss
- Parameters:
data (Batch) – batch data
- Returns:
ib_loss
- Return type:
torch.Tensor
- forward(data: Batch) → Tuple[Tensor, Tensor, Tensor][source]
- Parameters:
data (Batch) – pyg data batch
- Returns:
- positive augment data, negative augment data
and average representation
- Return type:
Tuple[Tensor, Tensor, Tensor]
- training: bool
- faknow.model.social_context.trustrd.calculate_ib_loss(g_enc_pos: Tensor, g_enc_neg: Tensor, g_enc: Tensor) → Tensor[source]
Compute contrastive loss :param g_enc_pos: positive augment data. :type g_enc_pos: Tensor :param g_enc_neg: negative augment data. :type g_enc_neg: Tensor :param g_enc: average representation. g_enc = (g_enc_pos + g_enc_neg) / 2. :type g_enc: Tensor
- Returns:
ib_loss
- Return type:
Tensor
faknow.model.social_context.upfd
- class faknow.model.social_context.upfd.UPFDGAT(feature_size: int, hidden_size=128)[source]
Bases:
_BaseGNN
User Preference-aware Fake News Detection, SIGIR 2021 paper: https://dl.acm.org/doi/abs/10.1145/3404835.3462990 code: https://github.com/safe-graph/GNN-FakeNews
UPFD model with GAT layer
- __init__(feature_size: int, hidden_size=128)[source]
- Parameters:
feature_size (int) – dimension of input node feature
hidden_size (int) – dimension of hidden layer. Default=128
- training: bool
- class faknow.model.social_context.upfd.UPFDGCN(feature_size: int, hidden_size=128)[source]
Bases:
_BaseGNN
User Preference-aware Fake News Detection, SIGIR 2021 paper: https://dl.acm.org/doi/abs/10.1145/3404835.3462990 code: https://github.com/safe-graph/GNN-FakeNews
UPFD model with GCN layer
- __init__(feature_size: int, hidden_size=128)[source]
- Parameters:
feature_size (int) – dimension of input node feature
hidden_size (int) – dimension of hidden layer. Default=128
- training: bool
- class faknow.model.social_context.upfd.UPFDGCNFN(feature_size: int, hidden_size=128, dropout_ratio=0.5)[source]
Bases:
_BaseGCNFN
User Preference-aware Fake News Detection, SIGIR 2021 paper: https://dl.acm.org/doi/abs/10.1145/3404835.3462990 code: https://github.com/safe-graph/GNN-FakeNews
UPFD model with GCNFN layer
- __init__(feature_size: int, hidden_size=128, dropout_ratio=0.5)[source]
- Parameters:
feature_size (int) – dimension of input node feature
hidden_size (int) – dimension of hidden layer. Default=128
dropout_ratio (float) – dropout ratio. Default=0.5
- training: bool
- class faknow.model.social_context.upfd.UPFDSAGE(feature_size: int, hidden_size=128)[source]
Bases:
_BaseGNN
User Preference-aware Fake News Detection, SIGIR 2021 paper: https://dl.acm.org/doi/abs/10.1145/3404835.3462990 code: https://github.com/safe-graph/GNN-FakeNews
UPFD model with SAGE layer
- __init__(feature_size: int, hidden_size=128)[source]
- Parameters:
feature_size (int) – dimension of input node feature
hidden_size (int) – dimension of hidden layer. Default=128
- training: bool