Exemple #1
0
    def forward(
        self,
        query: paddle.Tensor,
        key: paddle.Tensor,
        value: paddle.Tensor,
        mask: Optional[paddle.Tensor] = None,
    ) -> Tuple[paddle.Tensor, paddle.Tensor]:
        batch_size = query.shape[0]

        query = self.query(query)
        key = self.key(key)
        value = self.value(value)

        # multi head
        query = query.reshape((batch_size, -1, self.num_attention_heads,
                               self.dims_per_head)).transpose((0, 2, 1, 3))
        key = key.reshape((batch_size, -1, self.num_attention_heads,
                           self.dims_per_head)).transpose((0, 2, 1, 3))
        value = value.reshape((batch_size, -1, self.num_attention_heads,
                               self.dims_per_head)).transpose((0, 2, 1, 3))

        # self attention
        context, attention = self.attention(query, key, value, attn_mask=mask)
        # concat heads
        context = context.transpose((0, 2, 1, 3)).reshape(
            (batch_size, -1, self.hidden_size))
        output = self.dense(context)

        return output, attention
Exemple #2
0
def gram_matrix(data: paddle.Tensor) -> paddle.Tensor:
    """Get gram matrix"""
    b, ch, h, w = data.shape
    features = data.reshape((b, ch, w * h))
    features_t = features.transpose((0, 2, 1))
    gram = features.bmm(features_t) / (ch * h * w)
    return gram
Exemple #3
0
def magphase(x: Tensor) -> Tuple[Tensor, Tensor]:
    """Compute compext norm of a given tensor.
    Typically,the input tensor is the result of a complex Fourier transform.
    Parameters:
        x(Tensor): The input tensor of shape (..., 2).
    Returns:
        The tuple of magnitude and phase.

    Shape:
        x: the shape of x is arbitrary, with the shape of last axis being 2
        outputs: the shapes of magnitude and phase are both input.shape[:-1]

     Examples:

        .. code-block:: python

        import paddle
        import paddleaudio.functional as F
        x = paddle.randn((10, 10, 2))
        angle, phase = F.magphase(x)

    """
    if x.shape[-1] != 2:
        raise ParameterError(
            f'complex tensor must be of shape (..., 2), but received {x.shape} instead'
        )
    mag = paddle.sqrt(paddle.square(x).sum(axis=-1))
    x0 = x.reshape((-1, 2))
    phase = paddle.atan2(x0[:, 0], x0[:, 1])
    phase = phase.reshape(x.shape[:-1])

    return mag, phase
Exemple #4
0
    def forward(self, X: paddle.Tensor):
        # input X is a 3D feature map
        self.P = paddle.bmm(self.weight.expand_as(self.G), self.G)

        x = paddle.bmm(
            self.P.transpose((0, 2, 1)).expand((X.shape[0], self.C, self.C)),
            X.reshape((X.shape[0], X.shape[1], -1))).reshape(X.shape)
        return x
Exemple #5
0
 def _shape(self, tensor: paddle.Tensor, seq_len: int, bsz: int):
     return tensor.reshape(
         (bsz, seq_len, self.num_heads, self.head_dim)).transpose(
             (0, 2, 1, 3))  #?.contiguous()