def save_loss(losses, ylabel='Loss'): min_freq = min(losses.values(), key=lambda x: x[1])[1] if min_freq == 0: return plt('loss').title('Losses curve') plt('loss').xlabel('x' + str(min_freq) + ' batches') plt('loss').ylabel(ylabel) for k in losses: offset = losses[k][1] // min_freq - 1 plt('loss').plot( # in order to align the multiple losses [ i for i in range( offset, len(losses[k][0]) * (losses[k][1] // min_freq) + offset, losses[k][1] // min_freq) ], losses[k][0], label=k) _json = json.dumps(losses[k][0]) path = output_path('loss_{}.logs'.format(k)) print_debug('Exporting loss at ' + path) f = open(path, "w") f.write(_json) f.close() plt('loss').legend() save_fig_direct_call(figure_name='loss')
def update(self, i): parameters = self.parameters[i] bias = self.bias[i] for j, p in enumerate(parameters): arrow = self.arrows[j] arrow.remove() p /= self.coef_norm norm = np.sqrt(p[0]**2 + p[1]**2) new_norm = norm * self.wk[i][j] if self.use_wk else norm b = -bias[j] if self.use_bias else 0.0 b /= norm dx, dy = p[0] / norm * new_norm, p[1] / norm * new_norm x, y = (0, 0) if not self.use_bias else (p[0] * b / norm, p[1] * b / norm) self.arrows[j] = plt('circle').arrow(x, y, dx, dy, shape='full', head_width=0.04, head_length=0.08, fc='gray', ec='gray')
def last_call(self): step = 0.005 x = np.arange(-1, 1. + step, step) y = np.sqrt(np.maximum(1. - x**2, np.zeros(x.shape))) plt('circle').plot(x, y) y = -np.sqrt(np.maximum(1. - x**2, np.zeros(x.shape))) plt('circle').plot(x, y) labels = self.dataset.labels dataset = self.dataset.dataset plt('circle').scatter(dataset[labels == 0][:, 0], dataset[labels == 0][:, 1]) plt('circle').scatter(dataset[labels == 1][:, 0], dataset[labels == 1][:, 1]) for i, p in enumerate(self.parameters[0]): norm = np.sqrt(p[0]**2 + p[1]**2) if norm > self.coef_norm: self.coef_norm = norm for i, p in enumerate(self.parameters[0]): p /= self.coef_norm norm = np.sqrt(p[0]**2 + p[1]**2) new_norm = norm * self.wk[0][i] if self.use_wk else norm b = -self.bias[0][i] if self.use_bias else 0. b /= norm dx, dy = p[0] * new_norm / norm, p[1] * new_norm / norm x, y = (0, 0) if not self.use_bias else (p[0] * b / norm, p[1] * b / norm) self.arrows.append( plt('circle').arrow(x, y, dx, dy, shape='full', head_width=0.04, head_length=0.08)) fig = get_figure('circle') self.axis = fig.gca() anim = FuncAnimation(fig, self.update, frames=np.arange(0, len(self.parameters)), interval=200) path = output_path('circle.gif') print_info('Saving GIF at ' + path) anim.save(path, dpi=80, writer='imagemagick') delete_figure('circle')
labels = torch.from_numpy( np.array([1 if i < 1000 else 0 for i in range(2000)])).long() train_bis = torch.utils.data.TensorDataset(data, labels) train_loader = torch.utils.data.DataLoader(train_bis, shuffle=False, batch_size=1, num_workers=16) loss_values = [] model.eval() model.hidden_layer = False for i, (data, label) in enumerate(train_loader): result = model(data) loss_values.append( label.detach().numpy()[0] - result[0][1].detach().numpy()) # *norm.pdf(r[i], np.pi/2, 0.5)) plt('gaussian').plot(np.linspace(-np.pi, np.pi, 2000), loss_values) ####################### # plots inside modules ####################### ax = plot_dataset(train.dataset, train.labels) plot_activation_rate(train.dataset, train.labels, model, ax=ax) plot_decision_boundary(model, ax=ax) plot_gradient_field(train.dataset, train.labels, model, ax=ax) save_fig()
def plot(self, item, cancel_one_hot=True, return_fig=False, style=special_parameters.plt_style, nb_cols=5, alpha=1.): """ Plot an environmental tensor (size > 1)... :param item: the GPS location (latitude, longitude) :param cancel_one_hot: if False, the variables that have to be display with a one hot encoding approach will be displayed as such. If True, all variables will have only one dimension. :param return_fig: if True, the matplotlib fig will be returned, if False, it will be displayed :param style: style of the chart """ if self.size > 1: with plt().style.context(style): metadata = [(r.name, [ item[1] - self.size // 2 * r.x_resolution, item[1] + self.size // 2 * r.x_resolution, item[0] - self.size // 2 * r.y_resolution, item[0] + self.size // 2 * r.y_resolution ]) for r in self.rasters for _ in range(1 if cancel_one_hot else len(r))] # metadata are the name of the variable and the bounding box in latitude-longitude coordinates # retrieve the patch... Eventually disabling the one hot encoding variables patch = self.__getitem__(item, cancel_one_hot) # computing number of rows and columns... nb_rows = (patch.shape[0] + (nb_cols - 1)) // nb_cols plt('patch', figsize=(nb_cols * 6.4 * self.resolution, nb_rows * 4.8 * self.resolution)) fig = get_figure('patch') for k, i in zip(metadata, range(patch.shape[0])): plt('patch').subplot(nb_rows, nb_cols, i + 1) plt('patch').title(k[0], fontsize=20) plt('patch').imshow(patch[i], extent=k[1], aspect='auto') plt('patch').colorbar() fig.tight_layout() fig.patch.set_alpha(alpha) if return_fig: return fig else: fig.show() plt('patch').close(fig) else: raise ValueError( 'Plot works only for tensors: size must be > 1...')