Skip to content

gcn

GCNFrameEncoder

Bases: RelationFrameEncoder

Use untrained GCN for aggregating neighboring embeddings with self.

Parameters:

Name Type Description Default
depth int

How many hops of neighbors should be incorporated

2
edge_weight float

Weighting of non-self-loops

1.0
self_loop_weight float

Weighting of self-loops

2.0
layer_dims int

Dimensionality of layers if used

300
bias bool

Whether to use bias in layers

False
use_weight_layers bool

Whether to use randomly initialized layers in aggregation

True
aggr str

Which aggregation to use. Can be :obj:"sum", :obj:"mean", :obj:"min" or :obj:"max"

'sum'
attribute_encoder HintOrType[TokenizedFrameEncoder]

HintOrType[TokenizedFrameEncoder]: Base encoder class

None
attribute_encoder_kwargs OptionalKwargs

OptionalKwargs: Keyword arguments for initializing encoder

None
Source code in klinker/encoders/gcn.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
class GCNFrameEncoder(RelationFrameEncoder):
    """Use untrained GCN for aggregating neighboring embeddings with self.

    Args:
        depth: How many hops of neighbors should be incorporated
        edge_weight: Weighting of non-self-loops
        self_loop_weight: Weighting of self-loops
        layer_dims: Dimensionality of layers if used
        bias: Whether to use bias in layers
        use_weight_layers: Whether to use randomly initialized layers in aggregation
        aggr: Which aggregation to use. Can be :obj:`"sum"`, :obj:`"mean"`, :obj:`"min"` or :obj:`"max"`
        attribute_encoder: HintOrType[TokenizedFrameEncoder]: Base encoder class
        attribute_encoder_kwargs: OptionalKwargs: Keyword arguments for initializing encoder
    """

    def __init__(
        self,
        depth: int = 2,
        edge_weight: float = 1.0,
        self_loop_weight: float = 2.0,
        layer_dims: int = 300,
        bias: bool = False,
        use_weight_layers: bool = True,
        aggr: str = "sum",
        attribute_encoder: HintOrType[TokenizedFrameEncoder] = None,
        attribute_encoder_kwargs: OptionalKwargs = None,
    ):
        if not TORCH_SCATTER:
            logger.error("Could not find torch_scatter and/or torch_sparse package!")
        self.depth = depth
        self.edge_weight = edge_weight
        self.self_loop_weight = self_loop_weight
        self.device = resolve_device()
        self.attribute_encoder = tokenized_frame_encoder_resolver.make(
            attribute_encoder, attribute_encoder_kwargs
        )
        layers: List[BasicMessagePassing]
        if use_weight_layers:
            layers = [
                FrozenGCNConv(
                    in_channels=layer_dims,
                    out_channels=layer_dims,
                    edge_weight=edge_weight,
                    self_loop_weight=self_loop_weight,
                    aggr=aggr,
                )
                for _ in range(self.depth)
            ]
        else:
            layers = [
                BasicMessagePassing(
                    edge_weight=edge_weight,
                    self_loop_weight=self_loop_weight,
                    aggr=aggr,
                )
                for _ in range(self.depth)
            ]
        self.layers = layers

    def _encode_rel(
        self,
        rel_triples_left: np.ndarray,
        rel_triples_right: np.ndarray,
        ent_features: NamedVector,
    ) -> GeneralVector:
        full_graph = np.concatenate([rel_triples_left, rel_triples_right])
        edge_index = torch.from_numpy(full_graph[:, [0, 2]]).t()
        x = ent_features.vectors
        for layer in self.layers:
            x = layer.forward(x, edge_index)
        return x