Plot a connection weight matrix of a :code:`Connection` with `locally connected structure _. :param lc: An object of the class LocalConnection2D :param input_channel: The input channel to plot its corresponding weights, defau
(
lc: object,
input_channel: int = 0,
output_channel: int = None,
im: Optional[AxesImage] = None,
lines: bool = True,
figsize: Tuple[int, int] = (5, 5),
cmap: str = "hot_r",
color: str = "r",
title: Optional[str] = None,
)
| 402 | |
| 403 | |
| 404 | def plot_local_connection_2d_weights( |
| 405 | lc: object, |
| 406 | input_channel: int = 0, |
| 407 | output_channel: int = None, |
| 408 | im: Optional[AxesImage] = None, |
| 409 | lines: bool = True, |
| 410 | figsize: Tuple[int, int] = (5, 5), |
| 411 | cmap: str = "hot_r", |
| 412 | color: str = "r", |
| 413 | title: Optional[str] = None, |
| 414 | ) -> AxesImage: |
| 415 | # language=rst |
| 416 | """ |
| 417 | Plot a connection weight matrix of a :code:`Connection` with `locally connected |
| 418 | structure <http://yann.lecun.com/exdb/publis/pdf/gregor-nips-11.pdf>_. |
| 419 | :param lc: An object of the class LocalConnection2D |
| 420 | :param input_channel: The input channel to plot its corresponding weights, default is the first channel |
| 421 | :param output_channel: If not None, will only plot the weights corresponding to this output channel (filter) |
| 422 | :param lines: Indicates whether or not draw horizontal and vertical lines separating input regions. |
| 423 | :param figsize: Horizontal and vertical figure size in inches. |
| 424 | :param cmap: Matplotlib colormap. |
| 425 | :return: ``ims, axes``: Used for re-drawing the plots. |
| 426 | """ |
| 427 | |
| 428 | n_sqrt = int(np.ceil(np.sqrt(lc.n_filters))) |
| 429 | sel_slice = lc.w.view( |
| 430 | lc.in_channels, |
| 431 | lc.n_filters, |
| 432 | lc.conv_size[0], |
| 433 | lc.conv_size[1], |
| 434 | lc.kernel_size[0], |
| 435 | lc.kernel_size[1], |
| 436 | ).cpu() |
| 437 | input_size = _pair(int(np.sqrt(lc.source.n))) |
| 438 | if output_channel is None: |
| 439 | sel_slice = sel_slice[input_channel, ...] |
| 440 | reshaped = reshape_local_connection_2d_weights( |
| 441 | sel_slice, lc.n_filters, lc.kernel_size, lc.conv_size, input_size |
| 442 | ) |
| 443 | else: |
| 444 | sel_slice = sel_slice[input_channel, output_channel, ...] |
| 445 | sel_slice = sel_slice.unsqueeze(0) |
| 446 | reshaped = reshape_local_connection_2d_weights( |
| 447 | sel_slice, 1, lc.kernel_size, lc.conv_size, input_size |
| 448 | ) |
| 449 | if im == None: |
| 450 | fig, ax = plt.subplots(figsize=figsize) |
| 451 | |
| 452 | im = ax.imshow( |
| 453 | reshaped.cpu(), cmap=cmap, vmin=lc.wmin, vmax=lc.wmax, aspect="auto" |
| 454 | ) |
| 455 | div = make_axes_locatable(ax) |
| 456 | cax = div.append_axes("right", size="5%", pad=0.05) |
| 457 | |
| 458 | if lines and output_channel is None: |
| 459 | for i in range( |
| 460 | n_sqrt * lc.kernel_size[0], |
| 461 | n_sqrt * lc.conv_size[0] * lc.kernel_size[0], |
no test coverage detected