match_matrix_tensor

paddle.fluid.contrib.layers.nn. match_matrix_tensor ( x, y, channel_num, act=None, param_attr=None, dtype='float32', name=None ) [source]

Calculate the semantic matching matrix of two word sequences with variable length. Given a query A of length n and a title B of length m, the input shape are respectively [n, h] and [m, h], which h is hidden_size. If channel_num is set to 3, it will generate a learnable parameter matrix W with shape [h, 3, h]. Then the semantic matching matrix of query A and title B is calculated by A * W * B.T = [n, h]*[h, 3, h]*[h, m] = [n, 3, m]. The learnable parameter matrix W is equivalent to a fully connected layer in the calculation process. If act is provided, the corresponding activation function will be applied to output matrix. The x and y should be LodTensor and only one level LoD is supported.

Given a 1-level LoDTensor x:
    x.lod =  [
        [2,                     3,                               ]]
    x.data = [[0.3, 0.1], [0.2, 0.3], [
        0.5, 0.6], [0.7, 0.1], [0.3, 0.4]]
    x.dims = [5, 2]
y is a Tensor:
    y.lod =  [[3,                                 1,       ]]
    y.data = [[0.1, 0.2], [0.3, 0.7], [0.9, 0.2], [0.4, 0.1]]
    y.dims = [4, 2]
set channel_num 2, then we get a 1-level LoDTensor:
    # where 12 = channel_num * x.lod[0][0] * y.lod[0][0]
    out.lod =  [[12, 6]]
    out.dims = [18, 1]     # where 18 = 12 + 6
Parameters
  • x (Variable) – Input variable x which should be 1-level LodTensor.

  • y (Variable) – Input variable y which should be 1-level LodTensor.

  • channel_num (int) – The channel number of learnable parameter W.

  • act (str, default None) – Activation to be applied to the output of this layer.

  • param_attr (ParamAttr|list of ParamAttr, default None) – The parameter attribute for learnable parameters/weights of this layer.

  • dtype ('float32') – The data type of w data.

  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically. Default: None

Returns

output with LoD specified by this layer.

Return type

Variable

Examples

import numpy as np
from paddle.fluid import layers
from paddle.fluid import contrib

x_lod_tensor = layers.data(name='x', shape=[10], lod_level=1)
y_lod_tensor = layers.data(name='y', shape=[10], lod_level=1)
out, out_tmp = contrib.match_matrix_tensor(
    x=x_lod_tensor, y=y_lod_tensor, channel_num=3)