def plot( self, path: Union[str, None], plot_integral: bool, normalize: bool = True, title: str = None, ) -> Union[Tuple[plt.Figure, plt.Axes], Tuple[None, None]]: """ :param path: the path to save an img version of the chart, None to display the plot :param plot_integral: True to plot the calculated loss integrals for each layer, False to plot the averages :param normalize: normalize the values to a unit distribution (0 mean, 1 std) :param title: the title to put on the chart :return: the created figure and axes if path is None, otherwise (None, None) """ names = [ "{} ({})".format(res.name, res.id_) if res.id_ is not None else res.name for res in self._results ] values = [ res.sparse_integral if plot_integral else res.sparse_average for res in self._results ] if normalize: mean = numpy.mean(values) std = numpy.std(values) values = [(val - mean) / std for val in values] height = round(len(names) / 4) + 3 fig = plt.figure(figsize=(12, height)) ax = fig.add_subplot(111) if title is not None: ax.set_title(title) ax.invert_yaxis() frame = pandas.DataFrame( list(zip(names, values)), columns=["Param", "Sensitivity"] ) frame.plot.barh(ax=ax, x="Param", y="Sensitivity") plt.gca().invert_yaxis() if path is None: plt.show() return fig, ax path = clean_path(path) create_parent_dirs(path) plt.savefig(path) plt.close(fig) return None, None
def load_json(path: str): """ :param path: the path to load a previous analysis from :return: the KSPerfSensitivityAnalysis instance from the json """ path = clean_path(path) with open(path, "r") as file: objs = json.load(file) return PruningPerfSensitivityAnalysis.from_dict(objs)
def save_json(self, path: str): """ :param path: the path to save the json file at representing the layer sensitivities """ if not path.endswith(".json"): path += ".json" path = clean_path(path) create_parent_dirs(path) with open(path, "w") as file: json.dump(self.dict(), file, indent=2)
def plot( self, path: Union[str, None], title: str = None, ) -> Union[Tuple[plt.Figure, plt.Axes], Tuple[None, None]]: """ Plot the recorded sensitivity values :param path: the path for where to save the plot, if not supplied will display it :param title: the title of the plot to apply, defaults to '{plot_loss_key} LR Sensitivity' :return: the figure and axes if the figure was displayed; else None, None """ fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(111) if title is None: title = "" elif title == "__default__": title = "LR Sensitivity" ax.set_title(title) ax.set_xlabel("Learning Rate") ax.set_ylabel("Avg Loss") frame = pandas.DataFrame.from_records( [(lr_res["lr"], lr_res["loss_avg"]) for lr_res in self._results], columns=["Learning Rate", "Avg Loss"], ) frame.plot(x="Learning Rate", y="Avg Loss", marker=".", logx=True, ax=ax) if path is None: plt.show() return fig, ax path = clean_path(path) create_parent_dirs(path) plt.savefig(path) plt.close(fig) return None, None
def plot( self, path: Union[str, None], title: str = None, ) -> Union[Tuple[plt.Figure, plt.Axes], Tuple[None, None]]: """ :param path: the path to save an img version of the chart, None to display the plot :param title: the title to put on the chart :return: the created figure and axes if path is None, otherwise (None, None) """ names = ["{} ({})".format(res.name, res.id_) for res in self._results] values = [res.sparse_comparison() for res in self._results] height = round(len(names) / 4) + 3 fig = plt.figure(figsize=(12, height)) ax = fig.add_subplot(111) if title is not None: ax.set_title(title) ax.invert_yaxis() frame = pandas.DataFrame( list(zip(names, values)), columns=["Param", "Sparse comparison (ms)"] ) frame.plot.barh(ax=ax, x="Param", y="Sparse comparison (ms)") plt.gca().invert_yaxis() if path is None: plt.show() return fig, ax path = clean_path(path) create_parent_dirs(path) plt.savefig(path) plt.close(fig) return None, None