Post-pre learning rule for ``LocalConnection1D`` subclass of ``AbstractConnection`` class.
(self, **kwargs)
| 235 | ) |
| 236 | |
| 237 | def _local_connection1d_update(self, **kwargs) -> None: |
| 238 | # language=rst |
| 239 | """ |
| 240 | Post-pre learning rule for ``LocalConnection1D`` subclass of |
| 241 | ``AbstractConnection`` class. |
| 242 | """ |
| 243 | # Get LC layer parameters. |
| 244 | stride = self.connection.stride |
| 245 | batch_size = self.source.batch_size |
| 246 | kernel_height = self.connection.kernel_size |
| 247 | in_channels = self.connection.source.shape[0] |
| 248 | out_channels = self.connection.n_filters |
| 249 | height_out = self.connection.conv_size |
| 250 | |
| 251 | target_x = self.target.x.reshape(batch_size, out_channels * height_out, 1) |
| 252 | target_x = target_x * torch.eye(out_channels * height_out).to( |
| 253 | self.connection.w.device |
| 254 | ) |
| 255 | source_s = ( |
| 256 | self.source.s.type(torch.float) |
| 257 | .unfold(-1, kernel_height, stride) |
| 258 | .reshape(batch_size, height_out, in_channels * kernel_height) |
| 259 | .repeat(1, out_channels, 1) |
| 260 | .to(self.connection.w.device) |
| 261 | ) |
| 262 | |
| 263 | target_s = self.target.s.type(torch.float).reshape( |
| 264 | batch_size, out_channels * height_out, 1 |
| 265 | ) |
| 266 | target_s = target_s * torch.eye(out_channels * height_out).to( |
| 267 | self.connection.w.device |
| 268 | ) |
| 269 | source_x = ( |
| 270 | self.source.x.unfold(-1, kernel_height, stride) |
| 271 | .reshape(batch_size, height_out, in_channels * kernel_height) |
| 272 | .repeat(1, out_channels, 1) |
| 273 | .to(self.connection.w.device) |
| 274 | ) |
| 275 | |
| 276 | # Pre-synaptic update. |
| 277 | if self.nu[0].any(): |
| 278 | pre = self.reduction(torch.bmm(target_x, source_s), dim=0) |
| 279 | self.connection.w -= self.nu[0] * pre.view(self.connection.w.size()) |
| 280 | # Post-synaptic update. |
| 281 | if self.nu[1].any(): |
| 282 | post = self.reduction(torch.bmm(target_s, source_x), dim=0) |
| 283 | self.connection.w += self.nu[1] * post.view(self.connection.w.size()) |
| 284 | |
| 285 | super().update() |
| 286 | |
| 287 | def _local_connection2d_update(self, **kwargs) -> None: |
| 288 | # language=rst |