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

Function reshape_local_connection_2d_weights

bindsnet/utils.py:219–278  ·  view source on GitHub ↗

Reshape a slice of weights of a LocalConnection2D slice for plotting. :param w: Slice of weights from a LocalConnection2D object. :param n_filters: Number of filters (output channels). :param kernel_size: Side length(s) of convolutional kernel. :param conv_size: Side length(s) o

(
    w: torch.Tensor,
    n_filters: int,
    kernel_size: Union[int, Tuple[int, int]],
    conv_size: Union[int, Tuple[int, int]],
    input_sqrt: Union[int, Tuple[int, int]],
)

Source from the content-addressed store, hash-verified

217
218
219def reshape_local_connection_2d_weights(
220 w: torch.Tensor,
221 n_filters: int,
222 kernel_size: Union[int, Tuple[int, int]],
223 conv_size: Union[int, Tuple[int, int]],
224 input_sqrt: Union[int, Tuple[int, int]],
225) -> torch.Tensor:
226 # language=rst
227 """
228 Reshape a slice of weights of a LocalConnection2D slice for plotting.
229 :param w: Slice of weights from a LocalConnection2D object.
230 :param n_filters: Number of filters (output channels).
231 :param kernel_size: Side length(s) of convolutional kernel.
232 :param conv_size: Side length(s) of convolution population.
233 :param input_sqrt: Sides length(s) of input neurons.
234 :return: A slice of LocalConnection2D weights reshaped as a collection of spatially ordered square grids.
235 """
236
237 k1, k2 = kernel_size
238 c1, c2 = conv_size
239 i1, i2 = input_sqrt
240
241 fs = int(np.ceil(np.sqrt(n_filters)))
242
243 w_ = torch.zeros((n_filters * k1, k2 * c1 * c2))
244
245 for n1 in range(c1):
246 for n2 in range(c2):
247 for feature in range(n_filters):
248 n = n1 * c2 + n2
249 filter_ = w[feature, n1, n2, :, :].view(k1, k2)
250 w_[feature * k1 : (feature + 1) * k1, n * k2 : (n + 1) * k2] = filter_
251
252 if c1 == 1 and c2 == 1:
253 square = torch.zeros((i1 * fs, i2 * fs))
254
255 for n in range(n_filters):
256 square[
257 (n // fs) * i1 : ((n // fs) + 1) * i2,
258 (n % fs) * i2 : ((n % fs) + 1) * i2,
259 ] = w_[n * i1 : (n + 1) * i2]
260
261 return square
262 else:
263 square = torch.zeros((k1 * fs * c1, k2 * fs * c2))
264
265 for n1 in range(c1):
266 for n2 in range(c2):
267 for f1 in range(fs):
268 for f2 in range(fs):
269 if f1 * fs + f2 < n_filters:
270 square[
271 k1 * (n1 * fs + f1) : k1 * (n1 * fs + f1 + 1),
272 k2 * (n2 * fs + f2) : k2 * (n2 * fs + f2 + 1),
273 ] = w_[
274 (f1 * fs + f2) * k1 : (f1 * fs + f2 + 1) * k1,
275 (n1 * c2 + n2) * k2 : (n1 * c2 + n2 + 1) * k2,
276 ]

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected