In [1]:
import torch
from dpp_nets.my_torch.simulator2 import SimulClassifier
from dpp_nets.helper.plotting import plot_floats, plot_dict
In [29]:
# Global Settings
input_set_size = 15
aspects_n = 5
dtype = torch.DoubleTensor
path = '/Users/Max/Desktop/master thesis/latex/figures/plots'
fname = None
title = 'Loss over Time'
xlabel = 'Training Steps'
ylabel = 'BCE Loss'
reg=10
reg_mean = 5
train_steps = 5000
batch_size = 20
lr = 1e-3
reg=10
reg_mean = aspects_n
sample_iter = 10
In [ ]:
# Multi-Sample + Baseline
# Reg is already scaled by sample_iter :)
torch.manual_seed(0)
best = SimulClassifier(input_set_size, aspects_n, dtype)
best.train(train_steps, batch_size, sample_iter, baseline=True, lr=1e-3, reg=10, reg_mean=reg_mean)
best.evaluate(1000)
In [25]:
# Plots
plot_floats(best.loss_dict, 100, fname, title, xlabel, ylabel)
In [27]:
# Plots
plot_floats(pure_reinforce.loss_dict, 100, fname, title, xlabel, ylabel)
In [26]:
# Pure Reinforce
torch.manual_seed(0)
pure_reinforce = SimulClassifier(30, aspects_n, dtype)
pure_reinforce.train(train_steps, batch_size, 1, baseline=False, lr=lr, reg=reg, reg_mean=reg_mean)
pure_reinforce.evaluate(1000)
In [34]:
plot_floats(pure_reinforce.loss_dict, 10, fname, title, xlabel, ylabel)
In [ ]:
# Multi-Sampling
torch.manual_seed(0)
multi = SimulClassifier(20, aspects_n, dtype)
multi.train(train_steps, batch_size, sample_iter, baseline=False, lr=lr, reg=reg, reg_mean=reg_mean)
multi.evaluate(1000)
plot_floats(multi.loss_dict, 10, fname, title, xlabel, ylabel)
In [ ]:
In [ ]:
"""
========
Barchart
========
A bar plot with errorbars and height labels on individual bars
"""
import numpy as np
import matplotlib.pyplot as plt
N = 5
men_means = (20, 35, 30, 35, 27)
men_std = (2, 3, 4, 1, 2)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, men_means, width, color='r', yerr=men_std)
women_means = (25, 32, 34, 20, 25)
women_std = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, women_means, width, color='y', yerr=women_std)
# add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))
def autolabel(rects):
"""
Attach a text label above each bar displaying its height
"""
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
'%d' % int(height),
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
plt.show()
In [ ]:
"""Examples illustrating the use of plt.subplots().
This function creates a figure and a grid of subplots with a single call, while
providing reasonable control over how the individual plots are created. For
very refined tuning of subplot creation, you can still use add_subplot()
directly on a new figure.
"""
import matplotlib.pyplot as plt
import numpy as np
# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
plt.close('all')
# Just a figure and one subplot
f, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')
# Two subplots, the axes array is 1-d
f, axarr = plt.subplots(2, sharex=True)
axarr[0].plot(x, y)
axarr[0].set_title('Sharing X axis')
axarr[1].scatter(x, y)
# Two subplots, unpack the axes array immediately
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
# Three subplots sharing both x/y axes
f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing both axes')
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
# Fine-tune figure; make subplots close to each other and hide x ticks for
# all but bottom plot.
f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
# row and column sharing
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row')
ax1.plot(x, y)
ax1.set_title('Sharing x per column, y per row')
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
ax4.plot(x, 2 * y ** 2 - 1, color='r')
# Four axes, returned as a 2-d array
f, axarr = plt.subplots(2, 2)
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Axis [0,0]')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Axis [0,1]')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Axis [1,0]')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Axis [1,1]')
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)
# Four polar axes
f, axarr = plt.subplots(2, 2, subplot_kw=dict(projection='polar'))
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Axis [0,0]')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Axis [0,1]')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Axis [1,0]')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Axis [1,1]')
# Fine-tune figure; make subplots farther from each other.
f.subplots_adjust(hspace=0.3)
plt.show()
In [ ]:
f, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')
In [ ]:
plt.show()
In [ ]:
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
plt.suptitle('Global Title')
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
plt.show()
In [ ]:
N = 5
men_means = (20, 35, 30, 35, 27)
men_std = (2, 3, 4, 1, 2)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, men_means, width, color='r', yerr=men_std)
women_means = (25, 32, 34, 20, 25)
women_std = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, women_means, width, color='y', yerr=women_std)
# add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))
plt.show()
In [ ]:
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.01*height,
'%.2f' % float(height),
ha='center', va='bottom')
reg5 = (0.32)
reg6 = (0.3)
reg7 = (0.24)
reg8 = (0.7)
reg9 = (0.75)
reg10 = (0.8)
linc = 1
linm = 0.1
ind = [linc + linm * i for i in range(6)] # the x locations for the groups
width = 0.05 # the width of the bars
fig, ax = plt.subplots()
rects5 = ax.bar(ind[0], reg5, width, color='0.9')
rects6 = ax.bar(ind[1], reg6, width, color='0.75')
rects7 = ax.bar(ind[2], reg7, width, color='0.6')
rects8 = ax.bar(ind[3], reg8, width, color='0.45')
rects9 = ax.bar(ind[4], reg9, width, color='0.3')
rects10 = ax.bar(ind[5], reg10, width, color='0.15')
# add some text for labels, title and axes ticks
ax.set_title('Segmentation Task (higher is better)')
plt.xticks([], [])
#plt.axis('off')
ax.set_xlabel('Overall')
ax.set_ylim([0,1])
ax.set_ylabel('Accuracy')
autolabel(rects5)
autolabel(rects6)
autolabel(rects7)
autolabel(rects8)
autolabel(rects9)
autolabel(rects10)
plt.show()
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.01*height,
'%.2f' % float(height),
ha='center', va='bottom')
def curate_ax(idx, ax, my_data):
x_c = 1
x_m = 0.06
x_positioning = [x_c + x_m*i for i in range(len(my_data))]
width = 0.05 # width of bars
colors = ['0.9', '0.75', '0.6', '0.45', '0.3', '0.15']
rects = []
for i, data in enumerate(my_data):
r = ax.bar(x_positioning[i], data[idx], width, color=colors[i])
rects.append(r)
#autolabel(r)
return rects
In [ ]:
my_data = [[0.32, 0.4, 0.39],[0.30, 0.4, 0.2],[0.24,0.4, 0.1],
[0.7,0.4, 0.3],[0.75,0.4, 0.7],[0.8,0.4,0.6]]
f, (ax0, ax1, ax2) = plt.subplots(1, 3, sharey=True)
# Create subplots subplot
rects0 = curate_ax(0, ax0, my_data)
curate_ax(1, ax1, my_data)
curate_ax(2, ax2, my_data)
# Axis and Title Settings
plt.suptitle('Learning To Count Clusters')
# y-axis
ax0.set_yticks([0,1])
ax0.set_ylim([0,1])
ax0.set_ylabel('Accuracy')
# x-axis
ax0.set_xticks([],[])
ax1.set_xticks([],[])
ax2.set_xticks([],[])
ax0.set_xlabel('All Sets')
#ax0.set_title('y = 5')
# Legend
#ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))
plt.legend((rects0[0], rects0[1], rects0[2], rects0[3], rects0[4], rects0[5]),
('5', '6', '7','8','9','10'),
loc = 'best', bbox_to_anchor = (0,-0.1,1.1,1),
bbox_transform = plt.gcf().transFigure)
plt.show()