ConvKBInteraction

class ConvKBInteraction(hidden_dropout_rate: float = 0.0, embedding_dim: int = 200, num_filters: int = 400)[source]

Bases: Interaction[Tensor, Tensor, Tensor]

The stateful ConvKB interaction function.

ConvKB uses a convolutional neural network (CNN) whose feature maps capture global interactions of the input.

For given input representations for head entity, relation and tail entity, denoted by \(\mathbf{h}, \mathbf{r}, \mathbf{t} \in \mathbb{R}^d\), it first combines them to a matrix \(\mathbf{A} = [\mathbf{h}; \mathbf{r}; \mathbf{t}] \in \mathbb{R}^{d \times 3}\).

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\) is applied on every row of \(\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}\) is a shared weight vector.

ConvKB may be seen as a restriction of ERMLPInteraction with a certain weight sharing pattern in the first layer.

Initialize the interaction module.

Parameters:
  • hidden_dropout_rate (float) – the dropout rate applied on the hidden layer

  • embedding_dim (int) – the entity and relation embedding dimension

  • num_filters (int) – the number of filters (=output channels) of the convolution

Methods Summary

forward(h, r, t)

Evaluate the interaction function.

reset_parameters()

Reset parameters the interaction function may have.

Methods Documentation

forward(h: Tensor, r: Tensor, t: Tensor) Tensor[source]

Evaluate the interaction function.

See also

Interaction.forward for a detailed description about the generic batched form of the interaction function.

Parameters:
  • h (Tensor) – shape: (*batch_dims, d) The head representations.

  • r (Tensor) – shape: (*batch_dims, d) The relation representations.

  • t (Tensor) – shape: (*batch_dims, d) The tail representations.

Returns:

shape: batch_dims The scores.

Return type:

Tensor

reset_parameters()[source]

Reset parameters the interaction function may have.