Module pysimt.layers.attention.uniform

Expand source code
# -*- coding: utf-8 -*-
import torch


class UniformAttention(torch.nn.Module):
    """A dummy non-parametric attention layer that applies uniform weights."""
    def __init__(self):
        super().__init__()

    def forward(self, hid, ctx, ctx_mask=None):
        alpha = torch.ones(*ctx.shape[:2], device=ctx.device).div(ctx.shape[0])
        wctx = (alpha.unsqueeze(-1) * ctx).sum(0)
        return alpha, wctx

Classes

class UniformAttention

A dummy non-parametric attention layer that applies uniform weights.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

Expand source code
class UniformAttention(torch.nn.Module):
    """A dummy non-parametric attention layer that applies uniform weights."""
    def __init__(self):
        super().__init__()

    def forward(self, hid, ctx, ctx_mask=None):
        alpha = torch.ones(*ctx.shape[:2], device=ctx.device).div(ctx.shape[0])
        wctx = (alpha.unsqueeze(-1) * ctx).sum(0)
        return alpha, wctx

Ancestors

  • torch.nn.modules.module.Module

Class variables

var dump_patches : bool
var training : bool

Methods

def forward(self, hid, ctx, ctx_mask=None) ‑> Callable[..., Any]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the :class:Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Expand source code
def forward(self, hid, ctx, ctx_mask=None):
    alpha = torch.ones(*ctx.shape[:2], device=ctx.device).div(ctx.shape[0])
    wctx = (alpha.unsqueeze(-1) * ctx).sum(0)
    return alpha, wctx