forked from rusty1s/pytorch_sparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* disentangle config from HeteroPostLayer * update * update * update * add doc * fix imports * update test * fix API test * fix test * fix tests * add doc * MLPNodeHead * update pyg * reset * resolve comments * update doc * typo * typo * fix test * update
- Loading branch information
Showing
15 changed files
with
540 additions
and
449 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from .layer import HeteroPostLayer | ||
from .gnn import HeteroGNN | ||
from .encoder import HeteroFeatureEncoder | ||
from .general_hetero_gnn import GeneralHeteroGNN | ||
from .head import MLPNodeHead | ||
from .model_builder import create_model | ||
|
||
__all__ = classes = [ | ||
'HeteroPostLayer', | ||
'HeteroGNN', | ||
'create_model' | ||
'HeteroFeatureEncoder', | ||
'GeneralHeteroGNN', | ||
'MLPNodeHead', | ||
'create_model', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from typing import Dict, List | ||
|
||
import torch | ||
from torch import Tensor | ||
from torch.nn import Embedding | ||
from torch_geometric.nn import Linear | ||
from torch_geometric.typing import NodeType | ||
|
||
|
||
class HeteroFeatureEncoder(torch.nn.Module): | ||
r"""Encodes continuous (:obj:`feat`) and discrete features | ||
(:obj:`discrete_feat`) into a shared embedding space. | ||
Args: | ||
out_channels (int): Size of each output sample. | ||
emb_size (Dict[str, List[int]]): The number of embeddings for each | ||
discrete feature in each node type. | ||
bias (bool, optional): If set to :obj:`False`, the layer will not learn | ||
an additive bias. (default: :obj:`True`) | ||
""" | ||
def __init__( | ||
self, | ||
out_channels: int, | ||
emb_size: Dict[str, List[int]], | ||
bias: bool = True, | ||
): | ||
super().__init__() | ||
|
||
self.out_channels = out_channels | ||
|
||
self.lin_dict = torch.nn.ModuleDict() | ||
for node_type in emb_size.keys(): | ||
self.lin_dict[node_type] = Linear(-1, out_channels, bias=bias) | ||
|
||
self.emb_dict = torch.nn.ModuleDict() | ||
for node_type, sizes in emb_size.items(): | ||
self.emb_dict[node_type] = torch.nn.ModuleList( | ||
[Embedding(size, out_channels) for size in sizes]) | ||
|
||
self.reset_parameters() | ||
|
||
def reset_parameters(self): | ||
for lin in self.lin_dict.values(): | ||
lin.reset_parameters() | ||
for emb_list in self.emb_dict.values(): | ||
for emb in emb_list: | ||
torch.nn.init.xavier_uniform_(emb.weight) | ||
|
||
def forward( | ||
self, | ||
feat_dict: Dict[NodeType, Tensor], | ||
discrete_feat_dict: Dict[NodeType, Tensor], | ||
) -> Dict[NodeType, Tensor]: | ||
"""""" | ||
out_dict = {} | ||
for node_type in feat_dict: | ||
out = self.lin_dict[node_type](feat_dict[node_type]) | ||
for i, emb in enumerate(self.emb_dict[node_type]): | ||
index = discrete_feat_dict[node_type][:, i] | ||
out = out + emb(index) | ||
out_dict[node_type] = out | ||
return out_dict | ||
|
||
def __repr__(self) -> str: | ||
return f'{self.__class__.__name__}({self.out_channels})' |
Oops, something went wrong.