MCPcopy Create free account
hub / github.com/BindsNET/bindsnet / proportion_weighting

Function proportion_weighting

bindsnet/evaluation/evaluation.py:153–202  ·  view source on GitHub ↗

Classify data with the label with highest average spiking activity over all neurons, weighted by class-wise proportion. :param spikes: Binary tensor of shape ``(n_samples, time, n_neurons)`` of a single layer's spiking activity. :param assignments: A vector of shape ``(n_ne

(
    spikes: torch.Tensor,
    assignments: torch.Tensor,
    proportions: torch.Tensor,
    n_labels: int,
)

Source from the content-addressed store, hash-verified

151
152
153def proportion_weighting(
154 spikes: torch.Tensor,
155 assignments: torch.Tensor,
156 proportions: torch.Tensor,
157 n_labels: int,
158) -> torch.Tensor:
159 # language=rst
160 """
161 Classify data with the label with highest average spiking activity over all neurons,
162 weighted by class-wise proportion.
163
164 :param spikes: Binary tensor of shape ``(n_samples, time, n_neurons)`` of a single
165 layer's spiking activity.
166 :param assignments: A vector of shape ``(n_neurons,)`` of neuron label assignments.
167 :param proportions: A matrix of shape ``(n_neurons, n_labels)`` giving the per-class
168 proportions of neuron spiking activity.
169 :param n_labels: The number of target labels in the data.
170 :return: Predictions tensor of shape ``(n_samples,)`` resulting from the "proportion
171 weighting" classification scheme. Samples that elicit no weighted activity from
172 any assigned neuron are predicted as ``-1`` (abstain) rather than defaulting to
173 class ``0``.
174 """
175 n_samples = spikes.size(0)
176
177 # Sum over time dimension (spike ordering doesn't matter).
178 spikes = spikes.sum(1)
179 if spikes.is_sparse:
180 spikes = spikes.to_dense()
181
182 rates = torch.zeros((n_samples, n_labels), device=spikes.device)
183 for i in range(n_labels):
184 # Count the number of neurons with this label assignment.
185 n_assigns = torch.sum(assignments == i).float()
186
187 if n_assigns > 0:
188 # Get indices of samples with this label.
189 indices = torch.nonzero(assignments == i).view(-1)
190
191 # Compute layer-wise firing rate for this label.
192 rates[:, i] += (
193 torch.sum((proportions[:, i] * spikes)[:, indices], 1) / n_assigns
194 )
195
196 # Predictions are arg-max of layer-wise firing rates.
197 predictions = torch.sort(rates, dim=1, descending=True)[1][:, 0]
198
199 # Abstain (-1) on samples with no activity, avoiding a biased vote for class 0.
200 predictions[rates.sum(1) == 0] = -1
201
202 return predictions
203
204
205def ngram(

Callers 5

SOM_LM-SNNs.pyFile · 0.90
batch_eth_mnist.pyFile · 0.90
eth_mnist.pyFile · 0.90
runFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected