def getImageDescriptors_HOG_cdist(self, all_emb, ref_emb, ref_mask):
# unnormalized cosine distance for HOG
dist = numpy.dot(all_emb, ref_emb.T)
# normalize by length of query descriptor projected on reference
norm = numpy.sqrt(numpy.dot(numpy.square(all_emb), ref_mask.T))
dist /= norm
dist[numpy.isinf(dist)] = 0.
dist[numpy.isnan(dist)] = 0.
# dist[numpy.triu_indices(dist.shape[0], 1)] = numpy.maximum(dist[numpy.triu_indices(dist.shape[0], 1)],
# dist.T[numpy.triu_indices(dist.shape[0], 1)])
# dist[numpy.tril_indices(dist.shape[0], -1)] = 0.
# dist += dist.T
return dist
python类triu_indices()的实例源码
def triu_indices(n, k=0):
m = numpy.ones((n, n), int)
a = triu(m, k)
return numpy.where(a != 0)
def triu_indices(n, k=0):
m = numpy.ones((n, n), int)
a = triu(m, k)
return numpy.where(a != 0)
def triu_indices(n, k=0):
m = numpy.ones((n, n), int)
a = triu(m, k)
return numpy.where(a != 0)
def _fully_random_weights(n_features, lam_scale, prng):
"""Generate a symmetric random matrix with zeros along the diagonal."""
weights = np.zeros((n_features, n_features))
n_off_diag = int((n_features ** 2 - n_features) / 2)
weights[np.triu_indices(n_features, k=1)] =\
0.1 * lam_scale * prng.randn(n_off_diag) + (0.25 * lam_scale)
weights[weights < 0] = 0
weights = weights + weights.T
return weights
def _random_weights(n_features, lam, lam_perturb, prng):
"""Generate a symmetric random matrix with zeros along the diagnoal and
non-zero elements take the value {lam * lam_perturb, lam / lam_perturb}
with probability 1/2.
"""
weights = np.zeros((n_features, n_features))
n_off_diag = int((n_features ** 2 - n_features) / 2)
berns = prng.binomial(1, 0.5, size=n_off_diag)
vals = np.zeros(berns.shape)
vals[berns == 0] = 1. * lam * lam_perturb
vals[berns == 1] = 1. * lam / lam_perturb
weights[np.triu_indices(n_features, k=1)] = vals
weights[weights < 0] = 0
weights = weights + weights.T
return weights
def vech_kh(X, stack_cols=True, conserve_norm=False):
assert X.shape[0] == X.shape[1]
# Scale off-diagonal indexes if norm has to be preserved
d = X.shape[0]
if conserve_norm:
# Scale off-diagonal
tmp = np.copy(X)
triu_scale_idx = np.triu_indices(d, 1)
tmp[triu_scale_idx] = np.sqrt(2) * tmp[triu_scale_idx]
else:
tmp = X
triu_idx_r = []
triu_idx_c = []
# Find appropriate indexes
if stack_cols:
for c in range(0, d):
for r in range(0, c+1):
triu_idx_r.append(r)
triu_idx_c.append(c)
else:
for r in range(0, d):
for c in range(r, d):
triu_idx_r.append(r)
triu_idx_c.append(c)
# Extract and return upper triangular
triu_idx = (triu_idx_r, triu_idx_c)
return tmp[triu_idx]
def unvech_kh(v, cols_stacked=True, norm_conserved=False):
# Restore matrix dimension and add triangular
v = v.flatten()
d = int(0.5 * (np.sqrt(8 * len(v) + 1) - 1))
X = np.empty((d, d))
triu_idx_r = []
triu_idx_c = []
# Find appropriate indexes
if cols_stacked:
for c in range(0, d):
for r in range(0, c+1):
triu_idx_r.append(r)
triu_idx_c.append(c)
else:
for r in range(0, d):
for c in range(r, d):
triu_idx_r.append(r)
triu_idx_c.append(c)
# Restore original matrix
triu_idx = (triu_idx_r, triu_idx_c)
X[triu_idx] = v
X[np.tril_indices(d)] = X.T[np.tril_indices(d)]
# Undo rescaling on off diagonal elements
if norm_conserved:
X[np.triu_indices(d, 1)] /= np.sqrt(2)
X[np.tril_indices(d, -1)] /= np.sqrt(2)
return X
def upper2Full(a, eps = 0):
ind = (a<eps)&(a>-eps)
a[ind] = 0
n = int((-1 + np.sqrt(1+ 8*a.shape[0]))/2)
A = np.zeros([n,n])
A[np.triu_indices(n)] = a
temp = A.diagonal()
A = np.asarray((A + A.T) - np.diag(temp))
return A
def upper2Full(a, eps = 0):
ind = (a<eps)&(a>-eps)
a[ind] = 0
n = int((-1 + np.sqrt(1+ 8*a.shape[0]))/2)
A = np.zeros([n,n])
A[np.triu_indices(n)] = a
temp = A.diagonal()
A = np.asarray((A + A.T) - np.diag(temp))
return A
def upper2Full(a, eps = 0):
ind = (a<eps)&(a>-eps)
a[ind] = 0
n = int((-1 + np.sqrt(1+ 8*a.shape[0]))/2)
A = np.zeros([n,n])
A[np.triu_indices(n)] = a
temp = A.diagonal()
A = np.asarray((A + A.T) - np.diag(temp))
return A
def upper2Full(a, eps = 0):
ind = (a<eps)&(a>-eps)
a[ind] = 0
n = int((-1 + np.sqrt(1+ 8*a.shape[0]))/2)
A = np.zeros([n,n])
A[np.triu_indices(n)] = a
temp = A.diagonal()
A = np.asarray((A + A.T) - np.diag(temp))
return A
def upper2Full(a, eps = 0):
ind = (a<eps)&(a>-eps)
a[ind] = 0
n = int((-1 + np.sqrt(1+ 8*a.shape[0]))/2)
A = np.zeros([n,n])
A[np.triu_indices(n)] = a
temp = A.diagonal()
A = np.asarray((A + A.T) - np.diag(temp))
return A
def upper2Full(a):
n = int((-1 + numpy.sqrt(1+ 8*a.shape[0]))/2)
A = numpy.zeros([n,n])
A[numpy.triu_indices(n)] = a
temp = A.diagonal()
A = (A + A.T) - numpy.diag(temp)
return A
def upper2Full(self, a):
n = int((-1 + numpy.sqrt(1+ 8*a.shape[0]))/2)
A = numpy.zeros([n,n])
A[numpy.triu_indices(n)] = a
temp = A.diagonal()
A = (A + A.T) - numpy.diag(temp)
return A
def Prox_logdet(self, S, A, eta):
d, q = numpy.linalg.eigh(eta*A-S)
q = numpy.matrix(q)
X_var = ( 1/(2*float(eta)) )*q*( numpy.diag(d + numpy.sqrt(numpy.square(d) + (4*eta)*numpy.ones(d.shape))) )*q.T
x_var = X_var[numpy.triu_indices(S.shape[1])] # extract upper triangular part as update variable
return numpy.matrix(x_var).T
def upperToFull(a, eps = 0):
ind = (a<eps)&(a>-eps)
a[ind] = 0
n = int((-1 + np.sqrt(1+ 8*a.shape[0]))/2)
A = np.zeros([n,n])
A[np.triu_indices(n)] = a
temp = A.diagonal()
A = np.asarray((A + A.T) - np.diag(temp))
return A
def spd_to_vector(M):
return M[np.triu_indices(M.shape[0])]
def spd_to_vector_nondiag(M,scale=False):
result=M[np.triu_indices(M.shape[0],k=1)]
if(scale):
result=np.abs(result)/np.max(np.abs(result))
return result
def applyNNBigRandom(Xin,model,reps=10,msize=500,start=150):
#Returns an adjacency matrix
n_features=Xin.shape[1]
X=Xin.copy()
#Center
X -= X.mean(axis=0)
std = X.std(axis=0)
std[std == 0] = 1
X /= std
C_Final=np.zeros((X.shape[1],X.shape[1]))
ind=np.arange(0,X.shape[1])
larger=np.zeros((msize,msize))
for i in xrange(reps):
np.random.shuffle(ind)
I=np.eye(X.shape[1])
P=I[:,ind]
larger[start:start+n_features,start:start+n_features]=P.T.dot(X.T.dot(X)).dot(P)/X.shape[0]
emp_cov_matrix=np.expand_dims(larger,0)
pred=model.predict(np.expand_dims(emp_cov_matrix,0))
pred=pred.reshape(msize,msize)[start:start+n_features,start:start+n_features]
C=np.zeros((X.shape[1],X.shape[1]))
C[np.triu_indices(n_features,k=1)]=pred[np.triu_indices(n_features,k=1)]
C=C+C.T
C=P.dot(C).dot(P.T)
C_Final+=C
C_Final=C_Final/float(reps)
return C_Final