faknow.model.layers
faknow.model.layers.dct
- class faknow.model.layers.dct.DctInceptionBlock(in_channel=128, branch1_channels=None, branch2_channels=None, branch3_channels=None, branch4_channels=None)[source]
Bases:
Module
- __init__(in_channel=128, branch1_channels=None, branch2_channels=None, branch3_channels=None, branch4_channels=None)[source]
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(x)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class faknow.model.layers.dct.DctStem(kernel_sizes, num_channels)[source]
Bases:
Module
- __init__(kernel_sizes, num_channels)[source]
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(dct_img)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
faknow.model.layers.layer
- class faknow.model.layers.layer.BertEncoder(bert: str, fine_tune=False)[source]
Bases:
Module
Text encoder based on BERT to encode text into vectors.
- __init__(bert: str, fine_tune=False)[source]
- Parameters:
bert (str) – the name of pretrained BERT model
fine_tune (bool) – whether to fine tune BERT or not, default=False
- forward(token_id: Tensor, mask: Tensor) Tensor [source]
- Parameters:
token_id (torch.Tensor) – shape=(batch_size, max_len)
mask (torch.Tensor) – shape=(batch_size, max_len)
- Returns:
last hidden state from bert, shape=(batch_size, max_len, dim)
- Return type:
torch.Tensor
- training: bool
- class faknow.model.layers.layer.GradientReverseLayer(*args, **kwargs)[source]
Bases:
Function
gradient reverse layer, which is used to reverse the gradient in backward propagation, see https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function
- static backward(ctx, grad_output)[source]
reverse the gradient in backward propagation :param ctx: the context :type ctx: torch.autograd.function.Function :param grad_output: the gradient of output :type grad_output: torch.Tensor
- Returns:
torch.Tensor: the reversed gradient None: None
- Return type:
tuple
- class faknow.model.layers.layer.ResNetEncoder(out_size: int)[source]
Bases:
Module
Image encoder based on ResNet50 with pretrained weights on ImageNet1k to encode images pixels into vectors.
- __init__(out_size: int) None [source]
- Parameters:
out_size (int) – the size of output features of the fc layer in ResNet
- forward(image: Tensor) Tensor [source]
- Parameters:
image (torch.Tensor) – image pixels, shape=(batch_size, 3, 224, 224)
- Returns:
output from pretrained resnet model, shape=(batch_size, out_size)
- Return type:
torch.Tensor
- training: bool
- class faknow.model.layers.layer.SignedAttention(in_features: int, out_features: int, dropout: float, alpha: float, concat=True)[source]
Bases:
Module
signed attention layer for signed graph
- __init__(in_features: int, out_features: int, dropout: float, alpha: float, concat=True)[source]
- Parameters:
in_features (int) – the size of input features
out_features (int) – the size of output features
dropout (float) – the dropout rate
alpha (float) – the alpha value of LeakyReLU
concat (bool) – whether to concatenate the output features or not
- forward(x: Tensor, adj: Tensor)[source]
- Parameters:
x (torch.Tensor) – the input features
adj (torch.Tensor) – the adjacency matrix
- Returns:
the output features
- Return type:
torch.Tensor
- training: bool
- class faknow.model.layers.layer.SignedGAT(node_vectors: Tensor, cos_sim_matrix: Tensor, num_features: int, node_num: int, adj_matrix: Tensor, head_num=4, out_features=300, dropout=0.0, alpha=0.3)[source]
Bases:
Module
signed graph attention network
- __init__(node_vectors: Tensor, cos_sim_matrix: Tensor, num_features: int, node_num: int, adj_matrix: Tensor, head_num=4, out_features=300, dropout=0.0, alpha=0.3)[source]
- Parameters:
node_vectors (torch.Tensor) – the node vectors
cos_sim_matrix (torch.Tensor) – the cosine similarity matrix
num_features (int) – the size of input features
node_num (int) – the number of nodes
adj_matrix (torch.Tensor) – the adjacency matrix
head_num (int) – the number of attention heads
out_features (int) – the size of output features
dropout (float) – the dropout rate
alpha (float) – the alpha value of LeakyReLU in signed attention layer
- forward(post_id: Tensor)[source]
- Parameters:
post_id (torch.Tensor) – the post id
- Returns:
the output features
- Return type:
torch.Tensor
- training: bool
- class faknow.model.layers.layer.TextCNNLayer(embedding_dim: int, filter_num: int, filter_sizes: List[int], activate_fn: Callable | None = None)[source]
Bases:
Module
It’s not a whole TextCNN model. Only convolution and max pooling layers are included here but without an embedding layer or fully connected layer. Thus, it should be a part of your own TextCNN model.
- __init__(embedding_dim: int, filter_num: int, filter_sizes: List[int], activate_fn: Callable | None = None)[source]
- Parameters:
embedding_dim (int) – the dimension of word embedding
filter_num (int) – the number of filters, which is also the output channel
filter_sizes (List[int]) – the size of filters
activate_fn (Callable) – the activation function of convolution layer. Default=None
- forward(embedded_text: Tensor)[source]
- Parameters:
embedded_text (torch.Tensor) – the embedded text, shape=(batch_size, max_len, embedding_dim)
- Returns:
- the output of convolution and max pooling layer,
shape (batch_size, filter_num * len(filter_sizes))
- Return type:
torch.Tensor
- training: bool
faknow.model.layers.layers_m3fend
- class faknow.model.layers.layers_m3fend.Attention[source]
Bases:
Module
Compute ‘Scaled Dot Product Attention
- forward(query, key, value, mask=None, dropout=None)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class faknow.model.layers.layers_m3fend.MaskAttention(input_shape)[source]
Bases:
Module
Compute attention layer
- __init__(input_shape)[source]
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(inputs, mask=None)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class faknow.model.layers.layers_m3fend.MultiHeadedAttention(h, d_model, dropout=0.1)[source]
Bases:
Module
Take in model size and number of heads.
- __init__(h, d_model, dropout=0.1)[source]
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(query, key, value, mask=None)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class faknow.model.layers.layers_m3fend.SelfAttentionFeatureExtract(multi_head_num, input_size, output_size)[source]
Bases:
Module
- __init__(multi_head_num, input_size, output_size)[source]
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(inputs, query, mask=None)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class faknow.model.layers.layers_m3fend.cnn_extractor(feature_kernel, input_size)[source]
Bases:
Module
- __init__(feature_kernel, input_size)[source]
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(input_data)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
faknow.model.layers.transformer
- class faknow.model.layers.transformer.AddNorm(normalized_shape: int | List[int] | Size, dropout=0.0)[source]
Bases:
Module
residual add and layernorm
- __init__(normalized_shape: int | List[int] | Size, dropout=0.0)[source]
- Parameters:
normalized_shape (Union[int, List[int], torch.Size]) – input shape from an expected input of size
dropout (float) – dropout rate, default=0.
- forward(x: Tensor, y: Tensor)[source]
- Parameters:
x (Tensor) – residual
y (Tensor) – output of sublayer
- Returns:
layernorm(x + dropout(y))
- Return type:
Tensor
- training: bool
- class faknow.model.layers.transformer.EncoderLayer(input_size: int, ffn_hidden_size: int, head_num: int, k_out_size: int, v_out_size: int, dropout=0.0, bias=False, activation: ~typing.Callable | None = <function relu>)[source]
Bases:
Module
Encoder Layer in Transformer
- __init__(input_size: int, ffn_hidden_size: int, head_num: int, k_out_size: int, v_out_size: int, dropout=0.0, bias=False, activation: ~typing.Callable | None = <function relu>)[source]
- Parameters:
input_size (int) – input dimension
ffn_hidden_size (int) – hidden layer dimension of FFN
head_num (int) – number of attention heads
k_out_size (int) – output dimension of key
v_out_size (int) – output dimension of value
dropout (float) – dropout rate, default=0.
bias (bool) – whether to use bias in Linear layers, default=False
activation (Callable) – activation function for FFN, default=F.relu
- forward(x: Tensor, valid_lens: Tensor | None = None)[source]
- Parameters:
x (Tensor) – shape=(batch_size, num_steps, input_size)
valid_lens (Tensor) – shape=(batch_size,), default=None
- Returns:
shape=(batch_size,) or (batch_size, q_num)
- Return type:
Tensor
- training: bool
- class faknow.model.layers.transformer.FFN(input_size: int, hidden_size: int, output_size: int | None = None, dropout=0.0, activation: ~typing.Callable | None = <function relu>)[source]
Bases:
Module
Feed-Forward Networks
- __init__(input_size: int, hidden_size: int, output_size: int | None = None, dropout=0.0, activation: ~typing.Callable | None = <function relu>)[source]
- Parameters:
input_size (int) – input dimension
hidden_size (int) – hidden layer dimension
output_size (int) – output dimension, if None, output_size=input_size, default=None
dropout (float) – dropout rate, default=0.
activation (Callable) – activation function, default=F.relu
- forward(x: Tensor)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class faknow.model.layers.transformer.MultiHeadAttention(input_size: int, k_out_size: int, v_out_size: int, head_num: int, out_size: int | None = None, q_in_size: int | None = None, k_in_size: int | None = None, v_in_size: int | None = None, dropout=0.0, bias=False)[source]
Bases:
Module
Multi-head Attention with ScaledDotProductAttention
- __init__(input_size: int, k_out_size: int, v_out_size: int, head_num: int, out_size: int | None = None, q_in_size: int | None = None, k_in_size: int | None = None, v_in_size: int | None = None, dropout=0.0, bias=False)[source]
- Parameters:
input_size (int) – input dimension
k_out_size (int) – output dimension of key
v_out_size (int) – output dimension of value
head_num (int) – number of attention heads
out_size (int) – output dimension, if None, out_size=input_size, default=None
q_in_size (int) – input dimension of query, if None, q_in_size=input_size, default=None
k_in_size (int) – input dimension of key, if None, k_in_size=input_size, default=None
v_in_size (int) – input dimension of value, if None, v_in_size=input_size, default=None
dropout (float) – dropout rate, default=0.
bias (bool) – whether to use bias in Linear layers, default=False
- forward(queries: Tensor, keys: Tensor, values: Tensor, valid_lens: Tensor | None = None)[source]
- Parameters:
queries (Tensor) – shape=(batch_size, q_num, d)
keys (Tensor) – shape=(batch_size, k-v_num, d)
values (Tensor) – shape=(batch_size, k-v_num, v-dim)
valid_lens (Tensor) – shape=(batch_size,) or (batch_size, q_num)
- Returns:
multi-head output, shape=(batch_size, q_num, out_size * head_num)
- Return type:
Tensor
- training: bool
- class faknow.model.layers.transformer.PositionalEncoding(dim: int, dropout=0.0, max_len=1000)[source]
Bases:
Module
Positional encoding for inputs of transformer
- __init__(dim: int, dropout=0.0, max_len=1000)[source]
- Parameters:
dim (int) – the embedding dimension of input.
dropout (float) – dropout rate, Default=0.
max_len (int) – the max length of sequence length, Default=1000.
- forward(inputs: Tensor, step=None)[source]
- Parameters:
inputs (Tensor) – input tensor shape=(batch_size, length, embedding_dim)
step (int) – the cutting step of position encoding, Default=None
- Returns:
shape=(batch_size, length, embedding_dim)
- Return type:
Tensor
- training: bool
- class faknow.model.layers.transformer.ScaledDotProductAttention(dropout=0.0, epsilon=0.0)[source]
Bases:
Module
Scaled Dot Product Attention
- __init__(dropout=0.0, epsilon=0.0)[source]
- Parameters:
dropout (float) – dropout rate, default=0.
epsilon (float) – small constant for numerical stability, default=0.
- forward(queries: Tensor, keys: Tensor, values: Tensor, valid_lens: Tensor | None = None)[source]
- Parameters:
queries (Tensor) – shape=(batch_size, q_num, d)
keys (Tensor) – shape=(batch_size, k-v_num, d)
values (Tensor) – shape=(batch_size, k-v_num, v_dim)
valid_lens (Tensor) – shape=(batch_size,) or (batch_size, q_num), default=None
- Returns:
attention_values, shape=(batch_size, q_num, v_dim)
- Return type:
Tensor
- training: bool
- faknow.model.layers.transformer.masked_softmax(x: Tensor, valid_lens: Tensor | None = None)[source]
Perform softmax operation by masking elements on the last axis.
- Parameters:
x (Tensor) – shape=(batch_size, num_steps, num_hiddens)
valid_lens (Tensor) – shape=(batch_size,), default=None
- Returns:
shape=(batch_size, num_steps, num_hiddens)
- Return type:
Tensor
- faknow.model.layers.transformer.sequence_mask(x: Tensor, valid_len: Tensor | None = None, value=0.0)[source]
Mask irrelevant entries in sequences.
- Parameters:
x (Tensor) – shape=(batch_size, num_steps, num_hiddens)
valid_len (Tensor) – shape=(batch_size,), default=None
value (float) – value to be substituted in masked entries, default=0.
- Returns:
masked input x, shape=(batch_size, num_steps)
- Return type:
Tensor
- faknow.model.layers.transformer.transpose_output(x: Tensor, num_heads: int)[source]
Reverse the operation of transpose_qkv.
- Parameters:
x (Tensor) – shape=(batch_size * head_num, num, num_hiddens/head_num)
num_heads (int) – number of attention heads
- Returns:
- shape=(batch_size, num, num_hiddens),
where num_hiddens = head_num * out_size
- Return type:
Tensor
- faknow.model.layers.transformer.transpose_qkv(x: Tensor, num_heads: int)[source]
Transposition for parallel computation of multiple attention heads.
- Parameters:
x (Tensor) – shape=(batch_size, num, num_hiddens), where num_hiddens = head_num * out_size
num_heads (int) – number of attention heads
- Returns:
shape=(batch_size * head_num, num, num_hiddens/head_num)
- Return type:
Tensor