| 999 | |
| 1000 | |
| 1001 | class VisdomPlotLogger(BaseVisdomLogger): |
| 1002 | |
| 1003 | def __init__(self, plot_type, fields=None, win=None, env=None, opts={}, port=8097, server="localhost", name=None): |
| 1004 | ''' |
| 1005 | Multiple lines can be added to the same plot with the "name" attribute (see example) |
| 1006 | Args: |
| 1007 | fields: Currently unused |
| 1008 | plot_type: {scatter, line} |
| 1009 | |
| 1010 | Examples: |
| 1011 | >>> scatter_logger = VisdomPlotLogger('line') |
| 1012 | >>> scatter_logger.log(stats['epoch'], loss_meter.value()[0], name="train") |
| 1013 | >>> scatter_logger.log(stats['epoch'], loss_meter.value()[0], name="test") |
| 1014 | ''' |
| 1015 | super(VisdomPlotLogger, self).__init__(fields, win, env, opts, port, server) |
| 1016 | valid_plot_types = { |
| 1017 | "scatter": self.viz.scatter, |
| 1018 | "line": self.viz.line} |
| 1019 | self.plot_type = plot_type |
| 1020 | # Set chart type |
| 1021 | if plot_type not in valid_plot_types.keys(): |
| 1022 | raise ValueError("plot_type \'{}\' not found. Must be one of {}".format( |
| 1023 | plot_type, valid_plot_types.keys())) |
| 1024 | self.chart = valid_plot_types[plot_type] |
| 1025 | |
| 1026 | def log(self, *args, **kwargs): |
| 1027 | if self.win is not None and self.viz.win_exists(win=self.win, env=self.env): |
| 1028 | if len(args) != 2: |
| 1029 | raise ValueError("When logging to {}, must pass in x and y values (and optionally z).".format( |
| 1030 | type(self))) |
| 1031 | x, y = args |
| 1032 | self.chart( |
| 1033 | X=np.array([x]), |
| 1034 | Y=np.array([y]), |
| 1035 | update='append', |
| 1036 | win=self.win, |
| 1037 | env=self.env, |
| 1038 | opts=self.opts, |
| 1039 | **kwargs) |
| 1040 | else: |
| 1041 | if self.plot_type == 'scatter': |
| 1042 | chart_args = {'X': np.array([args])} |
| 1043 | else: |
| 1044 | chart_args = {'X': np.array([args[0]]), |
| 1045 | 'Y': np.array([args[1]])} |
| 1046 | self.win = self.chart( |
| 1047 | win=self.win, |
| 1048 | env=self.env, |
| 1049 | opts=self.opts, |
| 1050 | **chart_args) |
| 1051 | # For some reason, the first point is a different trace. So for now |
| 1052 | # we can just add the point again, this time on the correct curve. |
| 1053 | self.log(*args, **kwargs) |
| 1054 | |
| 1055 | |
| 1056 | class VisdomTextLogger(BaseVisdomLogger): |
nothing calls this directly
no outgoing calls
no test coverage detected