utils/model.py
def fusion_layer(self, inp, mask, dim):
        """Fuse model predictions across the sequence length dimension"""
        # max pooling and sum can be handled easily
        if self.fusion in ["max_pooling", "sum"]:
            func = torch.max if self.fusion == "max_pooling" else torch.sum
            epsilon = torch.tensor(1e-16).to(inp)
            inp = torch.where(mask, inp, epsilon)
            inp = func(inp, dim=dim)
            if not isinstance(inp, torch.Tensor):
                inp = inp[0]
        # average pooling
        elif self.fusion == "average_pooling":
            assert inp.shape == mask.shape
            new_inp = []
            for inp_i, mask_i in zip(inp, mask):
                new_inp.append(inp_i[mask_i].mean())
            inp = torch.tensor(new_inp).to(inp)
        else:
            raise ValueError(f"Invalid fusion value. Expected one of ['max_pooling', 'average_pooling', 'sum'], got "
                             f"'{self.fusion}' instead.")

        return inp
Fuse model predictions across the sequence length dimension
Fuse model predictions across the sequence length dimension
Fuse model predictions across the sequence length dimension
def fusion_layer(self, inp, mask, dim):
        if self.fusion in ["max_pooling", "sum"]:
            func = torch.max if self.fusion == "max_pooling" else torch.sum
            epsilon = torch.tensor(1e-16).to(inp)
            inp = torch.where(mask, inp, epsilon)
            inp = func(inp, dim=dim)
            if not isinstance(inp, torch.Tensor):
                inp = inp[0]
        elif self.fusion == "average_pooling":
            assert inp.shape == mask.shape
            new_inp = []
            for inp_i, mask_i in zip(inp, mask):
                new_inp.append(inp_i[mask_i].mean())
            inp = torch.tensor(new_inp).to(inp)
        else:
            raise ValueError(f"Invalid fusion value. Expected one of ['max_pooling', 'average_pooling', 'sum'], got "
                             f"'{self.fusion}' instead.")
        return inp
def fusion_layer ( self , inp , mask , dim ) : if self . fusion in [ "max_pooling" , "sum" ] : func = torch . max if self . fusion == "max_pooling" else torch . sum epsilon = torch . tensor ( 1e-16 ) . to ( inp ) inp = torch . where ( mask , inp , epsilon ) inp = func ( inp , dim = dim ) if not isinstance ( inp , torch . Tensor ) : inp = inp [ 0 ] elif self . fusion == "average_pooling" : assert inp . shape == mask . shape new_inp = [ ] for inp_i , mask_i in zip ( inp , mask ) : new_inp . append ( inp_i [ mask_i ] . mean ( ) ) inp = torch . tensor ( new_inp ) . to ( inp ) else : raise ValueError ( f"Invalid fusion value. Expected one of ['max_pooling', 'average_pooling', 'sum'], got " f"'{self.fusion}' instead." ) return inp
Fuse model predictions across the sequence length dimension
Fuse model predictions across the sequence length dimension
"""Fuse model predictions across the sequence length dimension""" # max pooling and sum can be handled easily # average pooling
self
inp
mask
dim