Plot a connection weight matrix. :param weights: Weight matrix of ``Connection`` 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, vertical figure size i
(
weights: torch.Tensor,
wmin: Optional[float] = 0,
wmax: Optional[float] = 1,
im: Optional[AxesImage] = None,
figsize: Tuple[int, int] = (5, 5),
cmap: str = "hot_r",
save: Optional[str] = None,
title: Optional[str] = None,
)
| 179 | |
| 180 | |
| 181 | def plot_weights( |
| 182 | weights: torch.Tensor, |
| 183 | wmin: Optional[float] = 0, |
| 184 | wmax: Optional[float] = 1, |
| 185 | im: Optional[AxesImage] = None, |
| 186 | figsize: Tuple[int, int] = (5, 5), |
| 187 | cmap: str = "hot_r", |
| 188 | save: Optional[str] = None, |
| 189 | title: Optional[str] = None, |
| 190 | ) -> AxesImage: |
| 191 | # language=rst |
| 192 | """ |
| 193 | Plot a connection weight matrix. |
| 194 | |
| 195 | :param weights: Weight matrix of ``Connection`` object. |
| 196 | :param wmin: Minimum allowed weight value. |
| 197 | :param wmax: Maximum allowed weight value. |
| 198 | :param im: Used for re-drawing the weights plot. |
| 199 | :param figsize: Horizontal, vertical figure size in inches. |
| 200 | :param cmap: Matplotlib colormap. |
| 201 | :param save: file name to save fig, if None = not saving fig. |
| 202 | :param title: Title of the plot. |
| 203 | :return: ``AxesImage`` for re-drawing the weights plot. |
| 204 | """ |
| 205 | local_weights = weights.detach().clone().cpu().numpy() |
| 206 | if save is not None: |
| 207 | plt.ioff() |
| 208 | |
| 209 | fig, ax = plt.subplots(figsize=figsize) |
| 210 | |
| 211 | im = ax.imshow(local_weights, cmap=cmap, vmin=wmin, vmax=wmax, aspect="auto") |
| 212 | div = make_axes_locatable(ax) |
| 213 | cax = div.append_axes("right", size="5%", pad=0.05) |
| 214 | |
| 215 | ax.set_xticks(()) |
| 216 | ax.set_yticks(()) |
| 217 | ax.set_aspect("auto") |
| 218 | if title != None: |
| 219 | ax.set_title(title + " Weights") |
| 220 | |
| 221 | plt.colorbar(im, cax=cax) |
| 222 | fig.tight_layout() |
| 223 | |
| 224 | a = save.split(".") |
| 225 | if len(a) == 2: |
| 226 | save = a[0] + ".1." + a[1] |
| 227 | else: |
| 228 | a[1] = "." + str(1 + int(a[1])) + ".png" |
| 229 | save = a[0] + a[1] |
| 230 | |
| 231 | plt.savefig(save, bbox_inches="tight") |
| 232 | plt.savefig(a[0] + ".png", bbox_inches="tight") |
| 233 | |
| 234 | plt.close(fig) |
| 235 | plt.ion() |
| 236 | return im, save |
| 237 | else: |
| 238 | if not im: |
no test coverage detected