This is a PyTorch implementation/tutorial of Fuzzy Tiling Activations: A Simple Approach to Learning Sparse Representations Online.

Fuzzy tiling activations are a form of sparse activations based on binning.

Binning is classification of a scalar value into a bin based on intervals. One problem with binning is that it gives zero gradients for most values (except at the boundary of bins). The other is that binning loses precision if the bin intervals are large.

FTA overcomes these disadvantages. Instead of hard boundaries like in Tiling Activations, FTA uses soft boundaries between bins. This gives non-zero gradients for all or a wide range of values. And also doesn't lose precision since it's captured in partial values.

$c$ is the tiling vector,

$c=(l,l+δ,l+2δ,…,u−2δ,u−δ)$

where $[l,u]$ is the input range, $δ$ is the bin size, and $u−l$ is divisible by $δ$.

Tiling activation is,

$ϕ(z)=1−I_{+}(max(c−z,0)+max(z−δ−c))$

where $I_{+}(⋅)$ is the indicator function which gives $1$ if the input is positive and $0$ otherwise.

Note that tiling activation gives zero gradients because it has hard boundaries.

The fuzzy indicator function,

$I_{η,+}(x)=I_{+}(η−x)x+I_{+}(x−η)$

which increases linearly from $0$ to $1$ when $0≤x<η$ and is equal to $1$ for $η≤x$. $η$ is a hyper-parameter.

FTA uses this to create soft boundaries between bins.

$ϕ_{η}(z)=1−I_{η,+}(max(c−z,0)+max(z−δ−c,0))$

Here's a simple experiment that uses FTA in a transformer.

```
61import torch
62from torch import nn
```

`65class FTA(nn.Module):`

`lower_limit`

is the lower limit $l$`upper_limit`

is the upper limit $u$`delta`

is the bin size $δ$`eta`

is the parameter $η$ that detemines the softness of the boundaries.

`70 def __init__(self, lower_limit: float, upper_limit: float, delta: float, eta: float):`

`77 super().__init__()`

Initialize tiling vector $c=(l,l+δ,l+2δ,…,u−2δ,u−δ)$

`80 self.c = nn.Parameter(torch.arange(lower_limit, upper_limit, delta), requires_grad=False)`

The input vector expands by a factor equal to the number of bins $δu−l $

`82 self.expansion_factor = len(self.c)`

$δ$

`84 self.delta = delta`

$η$

`86 self.eta = eta`

`88 def fuzzy_i_plus(self, x: torch.Tensor):`

`94 return (x <= self.eta) * x + (x > self.eta)`

`96 def forward(self, z: torch.Tensor):`

Add another dimension of size $1$. We will expand this into bins.

`99 z = z.view(*z.shape, 1)`

$ϕ_{η}(z)=1−I_{η,+}(max(c−z,0)+max(z−δ−c,0))$

`102 z = 1. - self.fuzzy_i_plus(torch.clip(self.c - z, min=0.) + torch.clip(z - self.delta - self.c, min=0.))`

Reshape back to original number of dimensions. The last dimension size gets expanded by the number of bins, $δu−l $.

`106 return z.view(*z.shape[:-2], -1)`

`109def _test():`

`113 from labml.logger import inspect`

Initialize

`116 a = FTA(-10, 10, 2., 0.5)`

Print $c$

`118 inspect(a.c)`

Print number of bins $δu−l $

`120 inspect(a.expansion_factor)`

Input $z$

`123 z = torch.tensor([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9., 10., 11.])`

Print $z$

`125 inspect(z)`

Print $ϕ_{η}(z)$

```
127 inspect(a(z))
128
129
130if __name__ == '__main__':
131 _test()
```