def before_fit(self, **kwargs): self.train_pca = ifnone( self.train_pca, self.do_pca(get_xy(self.learn.dls)[0], do_train=True)) self.train_trace = scatter(self.train_pca, name='Training data', mode='markers', marker_color='#539dcc', marker_size=1.5 if self.is_3d else 4) self.weight_pca = self.do_pca(self.learn.model.weights) self.weight_trace = scatter(self.weight_pca, name='SOM weights', mode='markers', marker_color='#e58368', marker_size=3 if self.is_3d else 6) expl_var = str( tuple( map(lambda pct: f'{pct:.0f}%', self.pca.explained_variance_ratio_ * 100)))[1:-1] layout = go.Layout( title=f"SOM Visualization ({expl_var} explained variance)") self.fig = go.FigureWidget([self.train_trace, self.weight_trace], layout=layout) self.fig.update_layout(margin=dict(l=20, r=20, t=20, b=20), paper_bgcolor="LightSteelBlue")
def batch_stats(self, funcs=None): "Grab a batch of data and call reduction function `func` per channel" funcs = ifnone(funcs, [torch.mean, torch.std]) ds_type = DatasetType.Valid if self.valid_dl else DatasetType.Train x = self.one_batch(ds_type=ds_type, denorm=False)[0].cpu() def multi_channel_view(x): return x.transpose(3, 0).contiguous().view(x.shape[3], -1) return [func(multi_channel_view(x), 1) for func in funcs]
def on_epoch_end(self, epoch: int, smooth_loss: Tensor, last_metrics: MetricsList, **kwargs: Any) -> bool: last_metrics = ifnone(last_metrics, []) stats = [ str(stat) if isinstance(stat, int) else '#na#' if stat is None else f'{stat:.6f}' for name, stat in zip( self.learn.recorder.names, [epoch, smooth_loss] + last_metrics) ] SendData.sendMessage(key=self.key, auth_token=self.auth_token, params=stats, ModelName=self.ModelName)
def before_fit(self): self.train_pca = ifnone( self.train_pca, self.do_pca(get_xy(self.learn.dls)[0], do_train=True)) self.weight_pca = self.do_pca(self.learn.model.weights) plt.ion() self.fig = plt.figure() self.train_axis = self.fig.add_subplot( 111, projection='3d' if self.is_3d else None) # Draw weights self.weight_scatter = self.train_axis.scatter(*tuple( [self.weight_pca[:, i] for i in range(self.weight_pca.shape[-1])]), c="#e58368", zorder=100) # Draw training data (once) self.train_axis.scatter(*tuple( [self.train_pca[:, i] for i in range(self.train_pca.shape[-1])]), c="#539dcc")
def on_epoch_end(self, epoch: int, smooth_loss: Tensor, last_metrics: MetricsList, **kwargs: Any) -> bool: if (time.time() - self.start_time > 3000): self.start_time = time.time() self.key, self.auth_token = SendData.signin(email=self.email, password=self.password) last_metrics = ifnone(last_metrics, []) self.stats = [ str(stat) if isinstance(stat, int) else '#na#' if stat is None else f'{stat:.6f}' for name, stat in zip( self.learn.recorder.names, [epoch, smooth_loss] + last_metrics) ] SendData.sendMessage(key=self.key, auth_token=self.auth_token, params=self.stats, ModelName=self.ModelName)