trace = dict(x=[1, 2, 3], y=[4, 5, 6], mode="markers+lines", type='custom', marker={ 'color': 'red', 'symbol': 104, 'size': "10" }, text=["one", "two", "three"], name='1st Trace') layout = dict(title="First Plot", xaxis={'title': 'x1'}, yaxis={'title': 'x2'}) viz._send({'data': [trace], 'layout': layout, 'win': 'mywin'}) # PyTorch tensor try: import torch viz.line(Y=torch.Tensor([[0., 0.], [1., 1.]])) except ImportError: print('Skipped PyTorch example') # audio demo: tensor = np.random.uniform(-1, 1, 441000) viz.audio(tensor=tensor, opts={'sample_frequency': 441000}) # audio demo: # download from http://www.externalharddrive.com/waves/animal/dolphin.wav try:
loss = criterion(outputs, targets) test_loss += loss.item() _, predicted = torch.max(outputs.data, 1) test_acc += predicted.eq(targets.data).cpu().sum().numpy() Y_loss[1].append(test_loss / num_images_test) Y_acc[1].append(test_acc / num_images_test) data_loss[1] = {'x': X, 'y': Y_loss[1], 'name': 'Test', 'type': 'line'} data_acc[1] = {'x': X, 'y': Y_acc[1], 'name': 'Test', 'type': 'line'} viz._send({ 'data': data_loss, 'win': win_loss, 'eid': env, 'layout': layout_loss, 'opts': opts_visdom }) viz._send({ 'data': data_acc, 'win': win_acc, 'eid': env, 'layout': layout_acc, 'opts': opts_visdom }) else: viz._send({ 'data': [data_loss[0]], 'win': win_loss, 'eid': env,
type='custom', mode="lines", name='mean loss') trace_entropy_mean = dict(x=NSTEPITER[::10], y=ENTROPY_MEAN, line={ 'color': 'red', 'width': 3 }, type='custom', mode="lines", name='mean entropy') vis._send({ 'data': [trace_value, trace_value_mean], 'layout': value_layout, 'win': 'valuewin' }) vis._send({ 'data': [trace_policy, trace_policy_mean], 'layout': policy_layout, 'win': 'policywin' }) vis._send({ 'data': [trace_entropy, trace_entropy_mean], 'layout': entropy_layout, 'win': 'entropywin' }) else: EPISODES.append(len(EPISODES) + 1)
class VisdomVisualizer(object): def __init__(self, envid, run_name, xaxis_name='Timestep', yaxis_name='Return', server='http://168.62.48.224', port=5000): self.viz = Visdom(server=server, port=port) assert self.viz.check_connection() self.run_name = run_name self.traces = {} self.win = None self.envid = envid self.layout = dict(title=run_name, xaxis={'title': xaxis_name}, yaxis={'title': yaxis_name}) self.data = {} def initialize(self, name, color): self.data[name] = dict(x=[], y=[], y_upper=[], y_lower=[]) self.traces[name] = dict(x=[], y=[], name=name, line=dict(color=color, width=3), mode="lines", type='custom') def send(self): try: self.win = self.viz._send({ 'data': list(self.traces.values()), 'layout': self.layout, 'win': self.win, 'eid': self.envid, }) except: print('Error: Send graph error! This error will be ignored.') self.win = None def paint(self, name, data: dict): for key, val in data.items(): self.data[name][key].append(float(val)) def draw_line(self, name, color): x, y = self.data[name]['x'], self.data[name]['y'] self.traces[name] = dict(x=x, y=y, name=name, line=dict(color=color, width=3), mode="lines", type='custom') self.send() def fill_line(self, name, color): x, y_upper, y_lower = self.data[name]['x'], self.data[name][ 'y_upper'], self.data[name]['y_lower'] name = name + '_fill' self.traces[name] = dict(x=x + x[::-1], y=y_upper + y_lower[::-1], name=name, line=dict(color='rgba(255,255,255,0)'), fill='tozerox', fillcolor=color, showlegend=True, type='custom') self.send()
class visdomRequest(Graphic): def __init__(self): super().__init__() self.vis = Visdom() self.win = 'HLF' self.env = 'main' self.globalLayout = { 'xaxis': { 'showgrid': False, 'showline': False, 'showticklabels': False }, 'yaxis': { 'showgrid': False, 'showline': False, 'showticklabels': False }, } self.PointsStyle = { 'size': 10, 'symbol': 'dot', 'color': '#e07b39', } self.selectedPointsStyle = { 'size': 17, 'symbol': 'dot', 'color': '#ff0000', } self.connectionsStyle = { 'width': 3, 'color': '#0e699e', } def request(self): dataPoints = { 'x': self.PointsX, 'y': self.PointsY, 'marker': self.PointsStyle, 'type': 'scatter', 'mode': 'markers', 'name': 'qubit', 'showlegend': False } dataSelectedPoints = { 'x': self.selectedPoints2X, 'y': self.selectedPoints2Y, 'marker': self.selectedPointsStyle, 'type': 'scatter', 'mode': 'markers', 'name': 'S_qubit', 'showlegend': False } dataConnections = list() for conn in self.connections: dataSingleConn = { 'x': [conn[0][0], conn[1][0]], 'y': [conn[0][1], conn[1][1]], 'line': self.connectionsStyle, 'type': 'line', 'mode': 'lines', 'name': 'Conn', 'showlegend': False } dataConnections.append(dataSingleConn) data = [dataPoints, dataSelectedPoints] + dataConnections opts = {'showlegend': False} self.vis._send({ 'data': data, 'win': self.win, 'eid': self.env, 'layout': self.globalLayout, 'opts': opts })
class Visualizer(object): def __init__(self): self.viz = Visdom() self.global_lower_std, self.global_upper_std, self.global_min, self.global_max = 0, 0, 0, 0 #return a line with the given points, color, and fill characteristics def get_line(self, x, y, name, color='transparent', isFilled=False, fillcolor='transparent'): if isFilled: fill = 'tonexty' else: fill = 'none' return dict( x=x, y=y, mode='lines', type='custom', line=dict(color=color), fill=fill, fillcolor=fillcolor, name=name, showlegend=False ) #return a set of dots at the given points #they have a cool color scheme so that's fun def get_dots(self, x, y, name): return dict( x=x, y=y, mode='markers', type='custom', marker=dict( size = 6, color = x, colorscale = 'Viridis', ), name=name, showlegend=False ) #create a plot of the loss def update_viz_loss(self, x, loss, color, env_name, win_name): loss = self.get_line(x, loss, 'loss', color=color) data = [loss] layout = dict( title=env_name + " Loss" ) self.viz._send({'data': data, 'layout': layout, 'win': win_name + "-loss"}) #create a plot with a line representing the mean and a shaded area around the mean representing the standard deviation of the rewards from all the actors def update_viz_mean(self, x, mean, std, colors, env_name, win_name): n = len(mean) upper_std = [0] * n lower_std = [0] * n for i in range(n): upper_std[i] = mean[i] + std[i] lower_std[i] = mean[i] - std[i] self.global_lower_std = min(lower_std) self.global_upper_std = max(upper_std) upper_line = self.get_line(x, upper_std, 'upper std', isFilled=True, fillcolor=colors[0]) mean = self.get_line(x, mean, 'mean', color=colors[1]) lower_line = self.get_line(x, lower_std, 'lower std') data = [lower_line, upper_line, mean] layout = dict( title=env_name + " Mean", yaxis=dict(range=[min(self.global_lower_std, self.global_min_r), max(self.global_upper_std, self.global_max_r)]) ) self.viz._send({'data': data, 'layout': layout, 'win': win_name + "-mean"}) #create a plot with a line representing the median and two shaded regions representing the quartiles and most extreme values of the rewards from all the actors def update_viz_median(self, x, median, first_quartile, third_quartile, min_r, max_r, colors, env_name, win_name): self.global_min_r = min(min_r) self.global_max_r = max(max_r) max_line = self.get_line(x, max_r, 'max', isFilled=True, fillcolor=colors[0]) upper_std = self.get_line(x, third_quartile, 'third quartile', isFilled=True, fillcolor=colors[1]) median = self.get_line(x, median, 'median', color=colors[2]) lower_std = self.get_line(x, first_quartile, 'first quartile') min_line = self.get_line(x, min_r, 'min') #'min_line' comes before 'max' so that 'max' can access 'min_line''s y values and stop the shading at them #same logic applies to 'lower_std' and 'upper_std', and they're placed after 'min' and 'max' so 'max' doesnt stop at one of them #'median' comes last so it doesnt stop the shading data = [min_line, max_line, lower_std, upper_std, median] layout = dict( title=env_name + " Median", yaxis=dict(range=[min(self.global_lower_std, self.global_min_r), max(self.global_upper_std, self.global_max_r)]) ) self.viz._send({'data': data, 'layout': layout, 'win': win_name + "-median"}) #create a plot with dots at the given points #even though the line graphs are much easier to follow, this looks a lot cooler cause color scales and all that def update_viz_dots(x, y, data_type, env_name, win_name): dots = self.get_dots(x, y, data_type) data = [dots] layout = dict( title=env_name + " " + data_type ) self.viz._send({'data': data, 'layout': layout, 'win': win_name + "-" + data_type})
class PlotLearning(object): def __init__(self, save_path, num_classes, plot_name='', env_name='experiments'): self.DEFAULT_PORT = 9898 self.DEFAULT_HOSTNAME = 'http://130.83.143.241' self.accuracy = [] self.val_accuracy = [] self.losses = [] self.val_losses = [] self.save_path_loss = os.path.join(save_path, plot_name + 'loss_plot.png') self.save_path_accu = os.path.join(save_path, plot_name + 'accu_plot.png') self.init_loss = -np.log(1.0 / num_classes) self.viz = Visdom(port=self.DEFAULT_PORT, server=self.DEFAULT_HOSTNAME, env=env_name) self.plot_name = plot_name self.env_name = env_name def plot(self, logs): self.accuracy.append(logs.get('acc')) self.val_accuracy.append(logs.get('val_acc')) self.losses.append(logs.get('loss')) self.val_losses.append(logs.get('val_loss')) best_val_acc = max(self.val_accuracy) best_train_acc = max(self.accuracy) best_val_epoch = self.val_accuracy.index(best_val_acc) best_train_epoch = self.accuracy.index(best_train_acc) plt.figure(1) plt.gca().cla() plt.ylim(0, 1) plt.plot(self.accuracy, label='train') plt.plot(self.val_accuracy, label='valid') plt.title("best_val@{0:}-{1:.2f}, best_train@{2:}-{3:.2f}".format( best_val_epoch, best_val_acc, best_train_epoch, best_train_acc)) plt.legend() plt.savefig(self.save_path_accu) best_val_loss = min(self.val_losses) best_train_loss = min(self.losses) best_val_epoch = self.val_losses.index(best_val_loss) best_train_epoch = self.losses.index(best_train_loss) plt.figure(2) plt.gca().cla() plt.ylim(0, self.init_loss) plt.plot(self.losses, label='train') plt.plot(self.val_losses, label='valid') plt.title("best_val@{0:}-{1:.2f}, best_train@{2:}-{3:.2f}".format( best_val_epoch, best_val_loss, best_train_epoch, best_train_loss)) plt.legend() plt.savefig(self.save_path_loss) def plot_logs(self, logs, trace_names, colors): accuracy_traces = [] loss_traces = [] trace_names_index = 0 for i in range(len(logs)): trace1 = dict(x=logs[i]['epoch'], y=logs[i]['train_accuracy'], mode="lines", type='custom', line={ 'color': colors[i], 'dash': 'dash' }, name=trace_names[trace_names_index]) accuracy_traces.append(trace1) trace_names_index += 1 trace2 = dict(x=logs[i]['epoch'], y=logs[i]['test_accuracy'], mode="lines", type='custom', line={ 'color': colors[i], 'shape': 'spline', 'smoothing': 1.3 }, name=trace_names[trace_names_index]) accuracy_traces.append(trace2) trace_names_index += 1 with open( os.path.join( './logs', 'accuracy_' + self.env_name + '_' + self.plot_name + '.json'), 'w') as accuracy_file: json.dump(accuracy_traces, accuracy_file) layout = dict(title="Accuracy Vs Epoch - " + self.plot_name, xaxis={'title': 'Epochs'}, yaxis={'title': 'Accuracy'}) self.viz._send({ 'data': accuracy_traces, 'layout': layout, 'win': 'Accuracy_' + self.plot_name }) trace_names_index = 0 for i in range(len(logs)): trace1 = dict(x=logs[i]['epoch'], y=logs[i]['train_loss'], mode="lines", type='custom', line={ 'color': colors[i], 'dash': 'dash' }, name=trace_names[trace_names_index]) loss_traces.append(trace1) trace_names_index += 1 trace2 = dict(x=logs[i]['epoch'], y=logs[i]['test_loss'], mode="lines", type='custom', line={ 'color': colors[i], 'shape': 'spline', 'smoothing': 1.3 }, name=trace_names[trace_names_index]) loss_traces.append(trace2) trace_names_index += 1 with open( os.path.join( './logs', 'loss_' + self.env_name + '_' + self.plot_name + '.json'), 'w') as loss_file: json.dump(loss_traces, loss_file) layout = dict(title="Loss Vs Epoch - " + self.plot_name, xaxis={'title': 'Epochs'}, yaxis={'title': 'Loss'}) self.viz._send({ 'data': loss_traces, 'layout': layout, 'win': 'Loss_' + self.plot_name }) def plot_live_logs(self, accuracy_traces, loss_traces): try: layout = dict(title="Accuracy Vs Epoch - " + self.plot_name, xaxis={'title': 'Epochs'}, yaxis={'title': 'Accuracy'}) self.viz._send({ 'data': accuracy_traces, 'layout': layout, 'win': 'Accuracy' + self.plot_name }) layout = dict(title="Loss Vs Epoch - " + self.plot_name, xaxis={'title': 'Epochs'}, yaxis={'title': 'Loss'}) self.viz._send({ 'data': loss_traces, 'layout': layout, 'win': 'Loss_' + self.plot_name }) except ConnectionError: print('Connection error...') time.sleep(5) def plot_saved_data(self, plot_data): layout = dict(title="Accuracy Vs Epoch - " + self.plot_name, xaxis={'title': 'Epochs'}, yaxis={'title': 'Accuracy'}) self.viz._send({ 'data': plot_data, 'layout': layout, 'win': 'Accuracy_check' })
values, log_probs, entropies, rewards = [], [], [], [] nstepIter += 1 Train(values, log_probs, entropies, rewards, obs, done) if episode % 10 == 0: REWARDS_DATA.append(np.mean(REWARDS[len(REWARDS) - 10:])) EPISODES_DATA.append(episode) trace = dict(x=EPISODES_DATA, y=REWARDS, type='custom', mode="lines", name='reward') trace2 = dict(x=EPISODES_DATA[::10], y=REWARDS_DATA, line={ 'color': 'red', 'width': 4 }, type='custom', mode="lines", name='mean reward') vis._send({ 'data': [trace, trace2], 'layout': reward_layout, 'win': 'rewardwin' }) env.close()
<ellipse cx="80" cy="80" rx="50" ry="30" style="fill:red;stroke:purple;stroke-width:2" /> Sorry, your browser does not support inline SVG. </svg> """ viz.svg( svgstr=svgstr, opts=dict(title='Example of SVG Rendering') ) # close text window: viz.close(win=textwindow) # assert that the closed window doesn't exist assert not viz.win_exists(textwindow) # Arbitrary visdom content trace = dict(x=[1, 2, 3], y=[4, 5, 6], mode="markers+lines", type='custom', marker={'color': 'red', 'symbol': 104, 'size': "10"}, text=["one", "two", "three"], name='1st Trace') layout = dict(title="First Plot", xaxis={'title': 'x1'}, yaxis={'title': 'x2'}) viz._send({'data': [trace], 'layout': layout, 'win': 'mywin'}) # PyTorch tensor try: import torch viz.line(Y=torch.Tensor([[0., 0.], [1., 1.]])) except ImportError: print('Skipped PyTorch example')
class VisdomWrap: def __init__(self): self.vis = Visdom() self.reward_layout = dict(title="Episode rewards", xaxis={'title': 'episode'}, yaxis={'title': 'reward'}) self.policy_layout = dict(title="Policy loss", xaxis={'title': 'iter'}, yaxis={'title': 'loss'}) self.value_layout = dict(title="Value loss", xaxis={'title': 'iter'}, yaxis={'title': 'loss'}) self.entropy_layout = dict(title="Entropies", xaxis={'title': 'iter'}, yaxis={'title': 'entropy'}) self.spatial_entropy_layout = dict(title="Spatial entropies", xaxis={'title': 'iter'}, yaxis={'title': 'spatial entropy'}) self.ITER = [] self.VALUELOSS = [] self.VALUELOSS_MEAN = [] self.valueloss_sample = [] self.POLICYLOSS = [] self.POLICYLOSS_MEAN = [] self.policyloss_sample = [] self.ENTROPY = [] self.ENTROPY_MEAN = [] self.entropy_sample = [] self.SPATIALENTROPY = [] self.SPATIALENTROPY_MEAN = [] self.spatial_entropy_sample = [] self.EPISODES = [] self.REWARDS = [] self.REWARDS_MEAN = [] self.reward_sample = [] def get_data(self): return self.VALUELOSS, self.VALUELOSS_MEAN, self.POLICYLOSS, self.POLICYLOSS_MEAN,\ self.ENTROPY, self.ENTROPY_MEAN, self.SPATIALENTROPY, self.SPATIALENTROPY_MEAN,\ self.REWARDS, self.REWARDS_MEAN, self.EPISODES, self.ITER def set_data(self, VALUELOSS, VALUELOSS_MEAN, POLICYLOSS, POLICYLOSS_MEAN, ENTROPY, ENTROPY_MEAN, SPATIALENTROPY, SPATIALENTROPY_MEAN, REWARDS, REWARDS_MEAN, EPISODES, ITER): self.VALUELOSS = VALUELOSS self.VALUELOSS_MEAN = VALUELOSS_MEAN self.POLICYLOSS = POLICYLOSS self.POLICYLOSS_MEAN = POLICYLOSS_MEAN self.ENTROPY = ENTROPY self.ENTROPY_MEAN = ENTROPY_MEAN self.SPATIALENTROPY = SPATIALENTROPY self.SPATIALENTROPY_MEAN = SPATIALENTROPY_MEAN self.REWARDS = REWARDS self.REWARDS_MEAN = REWARDS_MEAN self.EPISODES = EPISODES self.ITER = ITER def send_data(self, value_loss, policy_loss, entropy, spatial_entropy, done, reward): self.valueloss_sample.append(value_loss) self.policyloss_sample.append(policy_loss) self.entropy_sample.append(float(entropy)) self.spatial_entropy_sample.append(float(spatial_entropy)) if len(self.valueloss_sample) == 10: self.ITER.append(len(self.ITER) * 10) self.VALUELOSS.append(mean(self.valueloss_sample)) self.POLICYLOSS.append(mean(self.policyloss_sample)) self.ENTROPY.append(mean(self.entropy_sample)) self.SPATIALENTROPY.append(mean(self.spatial_entropy_sample)) self.valueloss_sample = [] self.policyloss_sample = [] self.entropy_sample = [] self.spatial_entropy_sample = [] if len(self.ITER) % 10 == 0: self.VALUELOSS_MEAN.append( mean(self.VALUELOSS[len(self.VALUELOSS) - 10:])) self.POLICYLOSS_MEAN.append( mean(self.POLICYLOSS[len(self.POLICYLOSS) - 10:])) self.ENTROPY_MEAN.append( mean(self.ENTROPY[len(self.ENTROPY) - 10:])) self.SPATIALENTROPY_MEAN.append( mean(self.SPATIALENTROPY[len(self.SPATIALENTROPY) - 10:])) trace_value = dict(x=self.ITER, y=self.VALUELOSS, type='custom', mode="lines", name='loss') trace_policy = dict(x=self.ITER, y=self.POLICYLOSS, type='custom', mode="lines", name='loss') trace_entropy = dict(x=self.ITER, y=self.ENTROPY, type='custom', mode="lines", name='entropy') trace_spatial_entropy = dict(x=self.ITER, y=self.SPATIALENTROPY, type='custom', mode="lines", name='spatial entropy') trace_value_mean = dict(x=[x + 45 for x in self.ITER[::10]], y=self.VALUELOSS_MEAN, line={ 'color': 'red', 'width': 3 }, type='custom', mode="lines", name='mean loss') trace_policy_mean = dict(x=[x + 45 for x in self.ITER[::10]], y=self.POLICYLOSS_MEAN, line={ 'color': 'red', 'width': 3 }, type='custom', mode="lines", name='mean loss') trace_entropy_mean = dict(x=[x + 45 for x in self.ITER[::10]], y=self.ENTROPY_MEAN, line={ 'color': 'red', 'width': 3 }, type='custom', mode="lines", name='mean entropy') trace_spatial_entropy_mean = dict( x=[x + 45 for x in self.ITER[::10]], y=self.SPATIALENTROPY_MEAN, line={ 'color': 'red', 'width': 3 }, type='custom', mode="lines", name='mean spatial entropy') self.vis._send({ 'data': [trace_value, trace_value_mean], 'layout': self.value_layout, 'win': 'valuewin' }) self.vis._send({ 'data': [trace_policy, trace_policy_mean], 'layout': self.policy_layout, 'win': 'policywin' }) self.vis._send({ 'data': [trace_entropy, trace_entropy_mean], 'layout': self.entropy_layout, 'win': 'entropywin' }) self.vis._send({ 'data': [trace_spatial_entropy, trace_spatial_entropy_mean], 'layout': self.spatial_entropy_layout, 'win': 'spatial_entropywin' }) if done: self.reward_sample.append(reward) if len(self.reward_sample) == 1: self.EPISODES.append(len(self.EPISODES)) self.REWARDS.append(mean(self.reward_sample)) self.reward_sample = [] if len(self.EPISODES) % 10 == 0: self.REWARDS_MEAN.append( mean(self.REWARDS[len(self.REWARDS) - 10:])) trace_reward = dict(x=self.EPISODES, y=self.REWARDS, type='custom', mode="lines", name='reward') trace_reward_mean = dict( x=[x + 4.5 for x in self.EPISODES[::10]], y=self.REWARDS_MEAN, line={ 'color': 'red', 'width': 4 }, type='custom', mode="lines", name='mean reward') self.vis._send({ 'data': [trace_reward, trace_reward_mean], 'layout': self.reward_layout, 'win': 'rewardwin' }) def send_current_data(self): trace_reward = dict(x=self.EPISODES, y=self.REWARDS, type='custom', mode="lines", name='reward') trace_reward_mean = dict(x=[x + 4.5 for x in self.EPISODES[::10]], y=self.REWARDS_MEAN, line={ 'color': 'red', 'width': 4 }, type='custom', mode="lines", name='mean reward') trace_value = dict(x=self.ITER, y=self.VALUELOSS, type='custom', mode="lines", name='loss') trace_policy = dict(x=self.ITER, y=self.POLICYLOSS, type='custom', mode="lines", name='loss') trace_entropy = dict(x=self.ITER, y=self.ENTROPY, type='custom', mode="lines", name='entropy') trace_spatial_entropy = dict(x=self.ITER, y=self.SPATIALENTROPY, type='custom', mode="lines", name='spatial entropy') trace_value_mean = dict(x=[x + 45 for x in self.ITER[::10]], y=self.VALUELOSS_MEAN, line={ 'color': 'red', 'width': 3 }, type='custom', mode="lines", name='mean loss') trace_policy_mean = dict(x=[x + 45 for x in self.ITER[::10]], y=self.POLICYLOSS_MEAN, line={ 'color': 'red', 'width': 3 }, type='custom', mode="lines", name='mean loss') trace_entropy_mean = dict(x=[x + 45 for x in self.ITER[::10]], y=self.ENTROPY_MEAN, line={ 'color': 'red', 'width': 3 }, type='custom', mode="lines", name='mean entropy') trace_spatial_entropy_mean = dict(x=[x + 45 for x in self.ITER[::10]], y=self.SPATIALENTROPY_MEAN, line={ 'color': 'red', 'width': 3 }, type='custom', mode="lines", name='mean spatial entropy') self.vis._send({ 'data': [trace_reward, trace_reward_mean], 'layout': self.reward_layout, 'win': 'rewardwin' }) self.vis._send({ 'data': [trace_value, trace_value_mean], 'layout': self.value_layout, 'win': 'valuewin' }) self.vis._send({ 'data': [trace_policy, trace_policy_mean], 'layout': self.policy_layout, 'win': 'policywin' }) self.vis._send({ 'data': [trace_entropy, trace_entropy_mean], 'layout': self.entropy_layout, 'win': 'entropywin' }) self.vis._send({ 'data': [trace_spatial_entropy, trace_spatial_entropy_mean], 'layout': self.spatial_entropy_layout, 'win': 'spatial_entropywin' })