unaiverse.modules.cnu.cnus¶
A Continual Neural Unit (CNU) is a neuron whose weights depend on the input. It owns m keys and m memories. On every sample it projects the input on the key space, scores the keys by cosine similarity, keeps the delta best ones and returns a blend of their memories with the weights of a softmax over the scores. A layer built on CNUs therefore applies different weights to different regions of the input space and learns a new region mostly by changing the memories of its keys. In the default configuration the keys themselves move towards the inputs that select them without any gradient.
CNUs is the base class of a bank of q such units. The class holds the keys, the memories and the counters and computes the blended weights. It leaves forward undefined: the layers of layers (LinearCNU, Conv2d) subclass it and turn the blended weights into a linear map or a convolution. A developer builds those layers and passes the parameters of this page through their **kwargs. The research context is in Models.
The shapes below use b for the batch size and the names of the constructor: q units, m keys and memories per unit, keys of size d and memories of size u.
CNUs
¶
CNUs(q=1, d=2, m=3, u=4, delta=3, gamma_alpha=0.1, tau_alpha=0.5, tau_mu=100, tau_eta=100, upd_m='WTA', upd_k='ad_hoc_WTA', beta_k=0.001, psi_fn='identity', scramble=False)
Bases: Module
Source code in unaiverse/modules/cnu/cnus.py
A torch.nn.Module holding q CNUs.
| Argument | Type | Default | Meaning |
|---|---|---|---|
q |
int |
1 |
The number of units. |
d |
int |
2 |
The size of each key. |
m |
int |
3 |
The number of keys of each unit. Each unit has as many memories as keys. |
u |
int |
4 |
The size of each memory. It is the number of weights that one unit returns. |
delta |
int |
3 |
The number of best keys whose memories are blended. Values above m are capped at m. |
gamma_alpha |
float |
0.1 |
The scale of the scores in the softmax. The scores are multiplied by gamma_alpha / sqrt(d) before the softmax. |
tau_alpha |
float |
0.5 |
Scrambling threshold on the score of the winning key. |
tau_mu |
float |
100 |
A key used fewer than tau_mu times counts as weak for scrambling. |
tau_eta |
float |
100 |
A key that has not won for at least tau_eta samples counts as old for scrambling. |
upd_m |
str or None |
"WTA" |
How the memories learn. None lets the gradient reach all the delta blended memories. "WTA" (winner takes all) lets it reach only the memory of the winning key. |
upd_k |
str or None |
"ad_hoc_WTA" |
How the keys learn. "ad_hoc_WTA" moves the winning key towards the input in training mode without any gradient. "grad_WTA" and None make the keys a torch.nn.Parameter updated by the optimizer. "grad_WTA" with upd_m="WTA" also stops the gradient through the scores of the keys that did not win. |
beta_k |
float |
0.001 |
The step of the ad hoc key update. |
psi_fn |
str |
"identity" |
The mode of psi that projects the input on the key space. |
scramble |
bool |
False |
True enables scrambling (below). It has an effect only with upd_k="ad_hoc_WTA". |
The constructor raises AssertionError for an unknown upd_m or upd_k and for upd_m="WTA" together with upd_k=None.
| Attribute | Kind | Shape | Content |
|---|---|---|---|
M |
torch.nn.Parameter |
[q, m, u] |
The memories. |
K |
buffer with "ad_hoc_WTA", torch.nn.Parameter otherwise |
[q, m, d] |
The keys, each of unit L2 norm. |
mu |
buffer with "ad_hoc_WTA", None otherwise |
[q, m] |
How many samples each key has won. |
eta |
buffer with "ad_hoc_WTA", None otherwise |
[q, m] |
How many samples have passed since each key last won. It starts at tau_eta. |
scrambling_count |
buffer, torch.long |
[q] |
How many scrambling operations each unit performed. |
reset_memories |
bool |
True lets reset_parameters initialize M. LinearCNU turns it off to apply its own initialization. |
Scrambling replaces a key that nobody uses with a new input. On a training step with scramble=True, a unit scrambles when the weakest top-1 score of the batch is below tau_alpha and the unit has at least one weak and old key (mu < tau_mu and eta >= tau_eta). The oldest such key becomes the projected sample with the weakest score, its memory becomes a copy of the memory of the key that the sample had selected before the scrambling, its usage mu restarts from zero and the sample counts as won by the new key. At most one key per unit changes per call.
reset_parameters
¶
Draws the keys uniformly in [-1/sqrt(d), 1/sqrt(d)] and normalizes each one to unit L2 norm. When reset_memories is True, it also draws the memories uniformly in [-1/sqrt(u), 1/sqrt(u)]. It then zeroes mu and fills eta with tau_eta. The constructor calls it and subclasses override it to initialize the memories their own way.
compute_weights
¶
Source code in unaiverse/modules/cnu/cnus.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
Returns the blended memories for a batch x as a tensor of shape [b, q, u]. The subclasses call it from forward. The steps are these:
- The method normalizes the keys in place when
upd_kis not"ad_hoc_WTA". With"ad_hoc_WTA"it detachesxfor the key matching and no gradient reaches the layers below through the scores. psi(x, psi_fn, key_size=d, normalize=True)maps the batch to[b, d].- The scores are the dot products with all the keys. The method keeps the
deltalargest scores of each unit in decreasing order and turns them into the weightsalpha = softmax(gamma_alpha / sqrt(d) · scores). - In training mode with
"ad_hoc_WTA"it runs scrambling (when enabled), addsbeta_k · psi(x)to the winning key of every sample, normalizes the keys again and recomputes the scores andalpha. It then resetsetaof the winning keys to zero, adds one to theirmuand addsbtoetaof every key. - It blends the memories of the selected keys with
alpha. Withupd_m="WTA"the memories of the keys that did not win enter the blend detached from the graph.
The key update of step 4 happens only when the module is in training mode (module.train()). In evaluation mode the keys and the counters do not change.
forward
¶
Raises NotImplementedError. CNUs is a base class and every subclass defines its own forward.
reset_counter
¶
Zeroes the debugging counter of the winning keys when the attribute debug is True and does nothing otherwise. The counter exists only when debug is True at construction. Because the constructor always starts with debug = False, the method has no effect in the current version.