Classify data with the label with highest average spiking activity over all neurons. :param spikes: Binary tensor of shape ``(n_samples, time, n_neurons)`` of a layer's spiking activity. :param assignments: A vector of shape ``(n_neurons,)`` of neuron label assignments. :pa
(
spikes: torch.Tensor, assignments: torch.Tensor, n_labels: int
)
| 107 | |
| 108 | |
| 109 | def all_activity( |
| 110 | spikes: torch.Tensor, assignments: torch.Tensor, n_labels: int |
| 111 | ) -> torch.Tensor: |
| 112 | # language=rst |
| 113 | """ |
| 114 | Classify data with the label with highest average spiking activity over all neurons. |
| 115 | |
| 116 | :param spikes: Binary tensor of shape ``(n_samples, time, n_neurons)`` of a layer's |
| 117 | spiking activity. |
| 118 | :param assignments: A vector of shape ``(n_neurons,)`` of neuron label assignments. |
| 119 | :param n_labels: The number of target labels in the data. |
| 120 | :return: Predictions tensor of shape ``(n_samples,)`` resulting from the "all |
| 121 | activity" classification scheme. Samples that elicit no activity from any |
| 122 | assigned neuron are predicted as ``-1`` (abstain) rather than defaulting to |
| 123 | class ``0``. |
| 124 | """ |
| 125 | n_samples = spikes.size(0) |
| 126 | |
| 127 | # Sum over time dimension (spike ordering doesn't matter). |
| 128 | spikes = spikes.sum(1) |
| 129 | if spikes.is_sparse: |
| 130 | spikes = spikes.to_dense() |
| 131 | |
| 132 | rates = torch.zeros((n_samples, n_labels), device=spikes.device) |
| 133 | for i in range(n_labels): |
| 134 | # Count the number of neurons with this label assignment. |
| 135 | n_assigns = torch.sum(assignments == i).float() |
| 136 | |
| 137 | if n_assigns > 0: |
| 138 | # Get indices of samples with this label. |
| 139 | indices = torch.nonzero(assignments == i).view(-1) |
| 140 | |
| 141 | # Compute layer-wise firing rate for this label. |
| 142 | rates[:, i] = torch.sum(spikes[:, indices], 1) / n_assigns |
| 143 | |
| 144 | # Predictions are arg-max of layer-wise firing rates. |
| 145 | predictions = torch.sort(rates, dim=1, descending=True)[1][:, 0] |
| 146 | |
| 147 | # Abstain (-1) on samples with no activity, avoiding a biased vote for class 0. |
| 148 | predictions[rates.sum(1) == 0] = -1 |
| 149 | |
| 150 | return predictions |
| 151 | |
| 152 | |
| 153 | def proportion_weighting( |
no outgoing calls
no test coverage detected