In [1]:
import torch
import torch.nn as nn

In [11]:
V = torch.randn(5, 10)
L = V.mm(V.t())

vecs, vals, _ = torch.svd(V)
vals = vals.pow(2)
n = vecs.size(0)
n_vals = vals.size(0)

subset = torch.FloatTensor([0,1,0,1,0])
subset_sum = subset.long().sum()

In [12]:
## Best

grad_vals = 1 / vals
grad_vecs = vecs.new().resize_(n, n_vals).copy_(torch.zeros(n, n_vals))

ix = subset.new().resize_(n).copy_((subset * torch.arange(0,n))).nonzero().squeeze()
Pvecs = vecs[ix,:].squeeze(1)

submatrix = Pvecs.mm(vals.diag()).mm(Pvecs.t())
subinv = torch.inverse(submatrix)

grad_vals += Pvecs.t().mm(subinv).mm(Pvecs).diag()
grad_vecs[ix,:] += subinv.mm(Pvecs).mm(vals.diag())    

print(grad_vals, grad_vecs)


 0.0741
 0.1794
 0.3043
 0.7958
 1.1396
[torch.FloatTensor of size 5]
 
 0.0000  0.0000  0.0000  0.0000  0.0000
 1.0931 -0.4053  0.0728 -0.1016 -0.0090
 0.0000  0.0000  0.0000  0.0000  0.0000
-0.8026 -1.1328  0.4221 -0.3973 -0.2167
 0.0000  0.0000  0.0000  0.0000  0.0000
[torch.FloatTensor of size 5x5]


In [13]:
grad_vals = 1 / vals
grad_vecs = torch.zeros(n, n_vals)

matrix = vecs.mm(vals.diag()).mm(vecs.t())

P = torch.eye(n).masked_select(subset.expand(n,n).t().byte()).view(subset_sum, -1)
submatrix = P.mm(matrix).mm(P.t())
# ix = (subset * torch.arange(0,len(subset))).nonzero()
# submatrix = matrix[ix,].squeeze(1).t()[ix,].squeeze(1)
subinv = torch.inverse(submatrix)
Pvecs = P.mm(vecs)
# Pvecs = vecs[ix,:].squeeze(1)

grad_vals += Pvecs.t().mm(subinv).mm(Pvecs).diag()
grad_vecs += P.t().mm(subinv).mm(Pvecs).mm(vals.diag())
print(grad_vals, grad_vecs)


 0.0741
 0.1794
 0.3043
 0.7958
 1.1396
[torch.FloatTensor of size 5]
 
 0.0000  0.0000  0.0000  0.0000  0.0000
 1.0931 -0.4053  0.0728 -0.1016 -0.0090
 0.0000  0.0000  0.0000  0.0000  0.0000
-0.8026 -1.1328  0.4221 -0.3973 -0.2167
 0.0000  0.0000  0.0000  0.0000  0.0000
[torch.FloatTensor of size 5x5]


In [14]:
grad_vals = 1 / vals
grad_vecs = vecs.new().resize_(n, n_vals).copy_(torch.zeros(n, n_vals))

ix = subset.new().resize_(n).copy_((subset * torch.arange(0,n))).nonzero()
Pvecs = vecs[ix,:].squeeze(1)

submatrix = Pvecs.mm(vals.diag()).mm(Pvecs.t())
subinv = torch.inverse(submatrix)

grad_vals += Pvecs.t().mm(subinv).mm(Pvecs).diag()
grad_vecs += P.t().mm(subinv).mm(Pvecs).mm(vals.diag())    

print(grad_vals, grad_vecs)


 0.0741
 0.1794
 0.3043
 0.7958
 1.1396
[torch.FloatTensor of size 5]
 
 0.0000  0.0000  0.0000  0.0000  0.0000
 1.0931 -0.4053  0.0728 -0.1016 -0.0090
 0.0000  0.0000  0.0000  0.0000  0.0000
-0.8026 -1.1328  0.4221 -0.3973 -0.2167
 0.0000  0.0000  0.0000  0.0000  0.0000
[torch.FloatTensor of size 5x5]


In [ ]:


In [ ]:


In [1]:
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 [2]:
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()



In [34]:
x = np.linspace(0,100)
y = np.sin(x)
y2 = np.cos(x)

f, axarr = plt.subplots(2, 2, sharex='col')
f.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.4, hspace=None)
ax0 = axarr[0,0]
ax1 = axarr[0,1]
ax2 = axarr[1,0]
ax3 = axarr[1,1]

# Loss
ax0.set_ylabel('BCE', size=9)
ax0.set_yticks([0,0.25,0.5,0.75,1])
ax0.set_yticklabels(['0','','','','1'],size=8)
ax0.set_ylim([0,1])

#ax0.plot(x,y2)

# Subset Size
ax1.set_ylabel('Cardinality', size=9)
ax1.set_yticks([0,2.5,5,7.5,10])
ax1.set_yticklabels(['0','','','','10'],size=8)
ax1.set_ylim([0,10])


# Accuracy 
ax2.set_ylabel('Accuracy', size=9)
ax2.set_yticks([0,0.25,0.5,0.75,1])
ax2.set_yticklabels(['0','','','','1'],size=8)
ax2.set_ylim([0,1])

# Precision
ax3.set_ylabel('Recall', size=9)
ax3.set_yticks([0,0.25,0.5,0.75,1])
ax3.set_yticklabels(['0','','','','1'],size=8)
ax3.set_ylim([0,1])

# Set x-axis 
ax3.set_xlabel('t', size=9)
#ax3.set_xticks([])
#ax3.set_xticklabels(['0','','','','1'],size=8)
ax4.set_xlabel('t', size=9)
#ax4
plt.show()



In [9]:
f, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(1, 5, sharey=True)

# Create subplots subplot
rects0 = curate_ax(0, ax0, super_loss)
curate_ax(1, ax1, super_loss)
curate_ax(2, ax2, super_loss)
curate_ax(3, ax3, super_loss)
curate_ax(4, ax4, super_loss)


# Axis and Title Settings
head = plt.suptitle('Learning To Count Clusters')

# y-axis 
ax0.set_yticks([0,5,10,15,20,25])
ax0.set_ylim([0,25])
ax0.set_ylabel('Accuracy')

# x-axis
ax0.set_xticks([],[])
ax1.set_xticks([],[])
ax2.set_xticks([],[])
ax3.set_xticks([],[])
ax4.set_xticks([],[])
#ax5.set_xticks([],[])

ax0.set_xlabel('All Sets')
ax1.set_xlabel('y = 5')
ax2.set_xlabel('y = 10')
ax3.set_xlabel('y = 15')
ax4.set_xlabel('y = 20')
#ax0.set_title('y = 5')

# Legend
# Legend
lgd = plt.legend((rects0[0], rects0[1], rects0[2]), 
           (r'$\lambda = 10$', r'$\lambda = 15$', r'$\lambda = 20$'), 
           loc = 'best', bbox_to_anchor = (0,-0.1,1.1,1),
                 fontsize=9, numpoints=3, handlelength=1,
           bbox_transform = plt.gcf().transFigure)
plt.savefig('odl.pdf', bbox_extra_artists=(lgd,head), bbox_inches='tight')
plt.show()


Out[9]:
array([   0.        ,    2.04081633,    4.08163265,    6.12244898,
          8.16326531,   10.20408163,   12.24489796,   14.28571429,
         16.32653061,   18.36734694,   20.40816327,   22.44897959,
         24.48979592,   26.53061224,   28.57142857,   30.6122449 ,
         32.65306122,   34.69387755,   36.73469388,   38.7755102 ,
         40.81632653,   42.85714286,   44.89795918,   46.93877551,
         48.97959184,   51.02040816,   53.06122449,   55.10204082,
         57.14285714,   59.18367347,   61.2244898 ,   63.26530612,
         65.30612245,   67.34693878,   69.3877551 ,   71.42857143,
         73.46938776,   75.51020408,   77.55102041,   79.59183673,
         81.63265306,   83.67346939,   85.71428571,   87.75510204,
         89.79591837,   91.83673469,   93.87755102,   95.91836735,
         97.95918367,  100.        ])

In [ ]: