Plot a connection weight matrix of a Conv2dConnection. :param weights: Weight matrix of Conv2dConnection object. :param wmin: Minimum allowed weight value. :param wmax: Maximum allowed weight value. :param im: Used for re-drawing the weights plot. :param figsize: Horizontal
(
weights: torch.Tensor,
wmin: float = 0.0,
wmax: float = 1.0,
im: Optional[AxesImage] = None,
figsize: Tuple[int, int] = (5, 5),
cmap: str = "hot_r",
title: Optional[str] = None,
)
| 262 | |
| 263 | |
| 264 | def plot_conv2d_weights( |
| 265 | weights: torch.Tensor, |
| 266 | wmin: float = 0.0, |
| 267 | wmax: float = 1.0, |
| 268 | im: Optional[AxesImage] = None, |
| 269 | figsize: Tuple[int, int] = (5, 5), |
| 270 | cmap: str = "hot_r", |
| 271 | title: Optional[str] = None, |
| 272 | ) -> AxesImage: |
| 273 | # language=rst |
| 274 | """ |
| 275 | Plot a connection weight matrix of a Conv2dConnection. |
| 276 | |
| 277 | :param weights: Weight matrix of Conv2dConnection object. |
| 278 | :param wmin: Minimum allowed weight value. |
| 279 | :param wmax: Maximum allowed weight value. |
| 280 | :param im: Used for re-drawing the weights plot. |
| 281 | :param figsize: Horizontal, vertical figure size in inches. |
| 282 | :param cmap: Matplotlib colormap. |
| 283 | :param title: Title of the plot. |
| 284 | :return: Used for re-drawing the weights plot. |
| 285 | """ |
| 286 | |
| 287 | sqrt1 = int(np.ceil(np.sqrt(weights.size(0)))) |
| 288 | sqrt2 = int(np.ceil(np.sqrt(weights.size(1)))) |
| 289 | height, width = weights.size(2), weights.size(3) |
| 290 | reshaped = reshape_conv2d_weights(weights) |
| 291 | |
| 292 | if not im: |
| 293 | fig, ax = plt.subplots(figsize=figsize) |
| 294 | im = ax.imshow(reshaped, cmap=cmap, vmin=wmin, vmax=wmax, aspect="auto") |
| 295 | div = make_axes_locatable(ax) |
| 296 | cax = div.append_axes("right", size="5%", pad=0.05) |
| 297 | |
| 298 | for i in range(height, sqrt1 * sqrt2 * height, height): |
| 299 | ax.axhline(i - 0.5, color="g", linestyle="--") |
| 300 | if i % sqrt1 == 0: |
| 301 | ax.axhline(i - 0.5, color="g", linestyle="-") |
| 302 | |
| 303 | for i in range(width, sqrt1 * sqrt2 * width, width): |
| 304 | ax.axvline(i - 0.5, color="g", linestyle="--") |
| 305 | if i % sqrt1 == 0: |
| 306 | ax.axvline(i - 0.5, color="g", linestyle="-") |
| 307 | |
| 308 | ax.set_xticks(()) |
| 309 | ax.set_yticks(()) |
| 310 | ax.set_aspect("auto") |
| 311 | if title != None: |
| 312 | ax.set_title(title + " Weights") |
| 313 | |
| 314 | plt.colorbar(im, cax=cax) |
| 315 | fig.tight_layout() |
| 316 | else: |
| 317 | im.set_data(reshaped) |
| 318 | |
| 319 | return im |
| 320 | |
| 321 |
no test coverage detected