# ConvKB¶

class ConvKB(triples_factory, hidden_dropout_rate=0.0, embedding_dim=200, automatic_memory_optimization=None, loss=None, preferred_device=None, num_filters=400, random_seed=None, regularizer=None)[source]

An implementation of ConvKB from [nguyen2018].

ConvKB uses a convolutional neural network (CNN) whose feature maps capture global interactions of the input. Each triple $$(h,r,t) \in \mathbb{K}$$ is represented as a input matrix $$\mathbf{A} = [\mathbf{h}; \mathbf{r}; \mathbf{t}] \in \mathbb{R}^{d \times 3}$$ in which the columns represent the embeddings for $$h$$, $$r$$, and $$t$$. In the convolution layer, a set of convolutional filters $$\omega_i \in \mathbb{R}^{1 \times 3}, i=1, \dots, \tau,$$ are applied on the input in order to compute for each dimension global interactions of the embedded triple. Each $$\omega_i$$mathbf{A} creating a feature map $$\mathbf{v}_i = [v_{i,1},...,v_{i,d}] \in \mathbb{R}^d$$:

$\mathbf{v}_i = g(\omega_j \mathbf{A} + \mathbf{b})$

where $$\mathbf{b} \in \mathbb{R}$$ denotes a bias term and $$g$$ an activation function which is employed element-wise. Based on the resulting feature maps $$\mathbf{v}_1, \dots, \mathbf{v}_{\tau}$$, the plausibility score of a triple is given by:

$f(h,r,t) = [\mathbf{v}_i; \ldots ;\mathbf{v}_\tau] \cdot \mathbf{w}$

where $$[\mathbf{v}_i; \ldots ;\mathbf{v}_\tau] \in \mathbb{R}^{\tau d \times 1}$$ and $$\mathbf{w} \in \mathbb{R}^{\tau d \times 1}$$ with a certain weight sharing pattern in the first layer.

Initialize the model.

To be consistent with the paper, pass entity and relation embeddings pre-trained from TransE.

Attributes Summary

 hpo_default The default strategy for optimizing the model’s hyper-parameters regularizer_default_kwargs The LP settings used by [nguyen2018] for ConvKB.

Methods Summary

 score_hrt(hrt_batch) Forward pass.

Attributes Documentation

hpo_default: ClassVar[Mapping[str, Any]] = {'embedding_dim': {'high': 300, 'low': 50, 'q': 50, 'type': <class 'int'>}, 'hidden_dropout_rate': {'high': 0.9, 'low': 0.1, 'type': <class 'float'>}, 'num_filters': {'high': 500, 'low': 300, 'q': 50, 'type': <class 'int'>}}

The default strategy for optimizing the model’s hyper-parameters

regularizer_default_kwargs: ClassVar[Optional[Mapping[str, Any]]] = {'apply_only_once': True, 'normalize': True, 'p': 2.0, 'weight': 0.0005}

The LP settings used by [nguyen2018] for ConvKB.

Methods Documentation

score_hrt(hrt_batch)[source]

Forward pass.

This method takes head, relation and tail of each triple and calculates the corresponding score.

Parameters

hrt_batch (LongTensor) – shape: (batch_size, 3), dtype: long The indices of (head, relation, tail) triples.

Raises

NotImplementedError – If the method was not implemented for this class.

Return type

FloatTensor`

Returns

shape: (batch_size, 1), dtype: float The score for each triple.