mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-07-05 16:02:13 +02:00
xrange fixes for Python 3
This commit is contained in:
parent
5eeb2f18e9
commit
cf1c382acd
14 changed files with 29 additions and 29 deletions
|
|
@ -185,7 +185,7 @@ class Tie(Parameterized):
|
||||||
def _check_change(self):
|
def _check_change(self):
|
||||||
changed = False
|
changed = False
|
||||||
if self.tied_param is not None:
|
if self.tied_param is not None:
|
||||||
for i in xrange(self.tied_param.size):
|
for i in range(self.tied_param.size):
|
||||||
b0 = self.label_buf==self.label_buf[self.buf_idx[i]]
|
b0 = self.label_buf==self.label_buf[self.buf_idx[i]]
|
||||||
b = self._highest_parent_.param_array[b0]!=self.tied_param[i]
|
b = self._highest_parent_.param_array[b0]!=self.tied_param[i]
|
||||||
if b.sum()==0:
|
if b.sum()==0:
|
||||||
|
|
@ -212,11 +212,11 @@ class Tie(Parameterized):
|
||||||
if self.tied_param is not None:
|
if self.tied_param is not None:
|
||||||
self.tied_param.gradient = 0.
|
self.tied_param.gradient = 0.
|
||||||
[np.put(self.tied_param.gradient, i, self._highest_parent_.gradient[self.label_buf==self.label_buf[self.buf_idx[i]]].sum())
|
[np.put(self.tied_param.gradient, i, self._highest_parent_.gradient[self.label_buf==self.label_buf[self.buf_idx[i]]].sum())
|
||||||
for i in xrange(self.tied_param.size)]
|
for i in range(self.tied_param.size)]
|
||||||
|
|
||||||
def propagate_val(self):
|
def propagate_val(self):
|
||||||
if self.tied_param is not None:
|
if self.tied_param is not None:
|
||||||
for i in xrange(self.tied_param.size):
|
for i in range(self.tied_param.size):
|
||||||
self._highest_parent_.param_array[self.label_buf==self.label_buf[self.buf_idx[i]]] = self.tied_param[i]
|
self._highest_parent_.param_array[self.label_buf==self.label_buf[self.buf_idx[i]]] = self.tied_param[i]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -653,7 +653,7 @@ def ssgplvm_simulation_linear():
|
||||||
def sample_X(Q, pi):
|
def sample_X(Q, pi):
|
||||||
x = np.empty(Q)
|
x = np.empty(Q)
|
||||||
dies = np.random.rand(Q)
|
dies = np.random.rand(Q)
|
||||||
for q in xrange(Q):
|
for q in range(Q):
|
||||||
if dies[q] < pi:
|
if dies[q] < pi:
|
||||||
x[q] = np.random.randn()
|
x[q] = np.random.randn()
|
||||||
else:
|
else:
|
||||||
|
|
@ -663,7 +663,7 @@ def ssgplvm_simulation_linear():
|
||||||
Y = np.empty((N, D))
|
Y = np.empty((N, D))
|
||||||
X = np.empty((N, Q))
|
X = np.empty((N, Q))
|
||||||
# Generate data from random sampled weight matrices
|
# Generate data from random sampled weight matrices
|
||||||
for n in xrange(N):
|
for n in range(N):
|
||||||
X[n] = sample_X(Q, pi)
|
X[n] = sample_X(Q, pi)
|
||||||
w = np.random.randn(D, Q)
|
w = np.random.randn(D, Q)
|
||||||
Y[n] = np.dot(w, X[n])
|
Y[n] = np.dot(w, X[n])
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@ class Posterior(object):
|
||||||
if self._precision is None:
|
if self._precision is None:
|
||||||
cov = np.atleast_3d(self.covariance)
|
cov = np.atleast_3d(self.covariance)
|
||||||
self._precision = np.zeros(cov.shape) # if one covariance per dimension
|
self._precision = np.zeros(cov.shape) # if one covariance per dimension
|
||||||
for p in xrange(cov.shape[-1]):
|
for p in range(cov.shape[-1]):
|
||||||
self._precision[:,:,p] = pdinv(cov[:,:,p])[0]
|
self._precision[:,:,p] = pdinv(cov[:,:,p])[0]
|
||||||
return self._precision
|
return self._precision
|
||||||
|
|
||||||
|
|
@ -125,7 +125,7 @@ class Posterior(object):
|
||||||
if self._woodbury_inv is not None:
|
if self._woodbury_inv is not None:
|
||||||
winv = np.atleast_3d(self._woodbury_inv)
|
winv = np.atleast_3d(self._woodbury_inv)
|
||||||
self._woodbury_chol = np.zeros(winv.shape)
|
self._woodbury_chol = np.zeros(winv.shape)
|
||||||
for p in xrange(winv.shape[-1]):
|
for p in range(winv.shape[-1]):
|
||||||
self._woodbury_chol[:,:,p] = pdinv(winv[:,:,p])[2]
|
self._woodbury_chol[:,:,p] = pdinv(winv[:,:,p])[2]
|
||||||
#Li = jitchol(self._woodbury_inv)
|
#Li = jitchol(self._woodbury_inv)
|
||||||
#self._woodbury_chol, _ = dtrtri(Li)
|
#self._woodbury_chol, _ = dtrtri(Li)
|
||||||
|
|
@ -160,7 +160,7 @@ class Posterior(object):
|
||||||
elif self._covariance is not None:
|
elif self._covariance is not None:
|
||||||
B = np.atleast_3d(self._K) - np.atleast_3d(self._covariance)
|
B = np.atleast_3d(self._K) - np.atleast_3d(self._covariance)
|
||||||
self._woodbury_inv = np.empty_like(B)
|
self._woodbury_inv = np.empty_like(B)
|
||||||
for i in xrange(B.shape[-1]):
|
for i in range(B.shape[-1]):
|
||||||
tmp, _ = dpotrs(self.K_chol, B[:,:,i])
|
tmp, _ = dpotrs(self.K_chol, B[:,:,i])
|
||||||
self._woodbury_inv[:,:,i], _ = dpotrs(self.K_chol, tmp.T)
|
self._woodbury_inv[:,:,i], _ = dpotrs(self.K_chol, tmp.T)
|
||||||
return self._woodbury_inv
|
return self._woodbury_inv
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,7 @@ class VarDTC_minibatch(LatentFunctionInference):
|
||||||
psi0_full = 0.
|
psi0_full = 0.
|
||||||
YRY_full = 0.
|
YRY_full = 0.
|
||||||
|
|
||||||
for n_start in xrange(0,num_data,batchsize):
|
for n_start in range(0,num_data,batchsize):
|
||||||
n_end = min(batchsize+n_start, num_data)
|
n_end = min(batchsize+n_start, num_data)
|
||||||
if batchsize==num_data:
|
if batchsize==num_data:
|
||||||
Y_slice = Y
|
Y_slice = Y
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ class HMC:
|
||||||
:rtype: numpy.ndarray
|
:rtype: numpy.ndarray
|
||||||
"""
|
"""
|
||||||
params = np.empty((num_samples,self.p.size))
|
params = np.empty((num_samples,self.p.size))
|
||||||
for i in xrange(num_samples):
|
for i in range(num_samples):
|
||||||
self.p[:] = np.random.multivariate_normal(np.zeros(self.p.size),self.M)
|
self.p[:] = np.random.multivariate_normal(np.zeros(self.p.size),self.M)
|
||||||
H_old = self._computeH()
|
H_old = self._computeH()
|
||||||
theta_old = self.model.optimizer_array.copy()
|
theta_old = self.model.optimizer_array.copy()
|
||||||
|
|
@ -59,7 +59,7 @@ class HMC:
|
||||||
return params
|
return params
|
||||||
|
|
||||||
def _update(self, hmc_iters):
|
def _update(self, hmc_iters):
|
||||||
for i in xrange(hmc_iters):
|
for i in range(hmc_iters):
|
||||||
self.p[:] += -self.stepsize/2.*self.model._transform_gradients(self.model.objective_function_gradients())
|
self.p[:] += -self.stepsize/2.*self.model._transform_gradients(self.model.objective_function_gradients())
|
||||||
self.model.optimizer_array = self.model.optimizer_array + self.stepsize*np.dot(self.Minv, self.p)
|
self.model.optimizer_array = self.model.optimizer_array + self.stepsize*np.dot(self.Minv, self.p)
|
||||||
self.p[:] += -self.stepsize/2.*self.model._transform_gradients(self.model.objective_function_gradients())
|
self.p[:] += -self.stepsize/2.*self.model._transform_gradients(self.model.objective_function_gradients())
|
||||||
|
|
@ -82,7 +82,7 @@ class HMC_shortcut:
|
||||||
|
|
||||||
def sample(self, m_iters=1000, hmc_iters=20):
|
def sample(self, m_iters=1000, hmc_iters=20):
|
||||||
params = np.empty((m_iters,self.p.size))
|
params = np.empty((m_iters,self.p.size))
|
||||||
for i in xrange(m_iters):
|
for i in range(m_iters):
|
||||||
# sample a stepsize from the uniform distribution
|
# sample a stepsize from the uniform distribution
|
||||||
stepsize = np.exp(np.random.rand()*(self.stepsize_range[1]-self.stepsize_range[0])+self.stepsize_range[0])
|
stepsize = np.exp(np.random.rand()*(self.stepsize_range[1]-self.stepsize_range[0])+self.stepsize_range[0])
|
||||||
self.p[:] = np.random.multivariate_normal(np.zeros(self.p.size),self.M)
|
self.p[:] = np.random.multivariate_normal(np.zeros(self.p.size),self.M)
|
||||||
|
|
|
||||||
|
|
@ -166,7 +166,7 @@ class Coregionalize(Kern):
|
||||||
|
|
||||||
def update_gradients_diag(self, dL_dKdiag, X):
|
def update_gradients_diag(self, dL_dKdiag, X):
|
||||||
index = np.asarray(X, dtype=np.int).flatten()
|
index = np.asarray(X, dtype=np.int).flatten()
|
||||||
dL_dKdiag_small = np.array([dL_dKdiag[index==i].sum() for i in xrange(self.output_dim)])
|
dL_dKdiag_small = np.array([dL_dKdiag[index==i].sum() for i in range(self.output_dim)])
|
||||||
self.W.gradient = 2.*self.W*dL_dKdiag_small[:, None]
|
self.W.gradient = 2.*self.W*dL_dKdiag_small[:, None]
|
||||||
self.kappa.gradient = dL_dKdiag_small
|
self.kappa.gradient = dL_dKdiag_small
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,7 @@ class SplitKern(CombinationKernel):
|
||||||
assert len(slices2)<=2, 'The Split kernel only support two different indices'
|
assert len(slices2)<=2, 'The Split kernel only support two different indices'
|
||||||
target = np.zeros((X.shape[0], X2.shape[0]))
|
target = np.zeros((X.shape[0], X2.shape[0]))
|
||||||
# diagonal blocks
|
# diagonal blocks
|
||||||
[[target.__setitem__((s,s2), self.kern.K(X[s,:],X2[s2,:])) for s,s2 in itertools.product(slices[i], slices2[i])] for i in xrange(min(len(slices),len(slices2)))]
|
[[target.__setitem__((s,s2), self.kern.K(X[s,:],X2[s2,:])) for s,s2 in itertools.product(slices[i], slices2[i])] for i in range(min(len(slices),len(slices2)))]
|
||||||
if len(slices)>1:
|
if len(slices)>1:
|
||||||
[target.__setitem__((s,s2), self.kern_cross.K(X[s,:],X2[s2,:])) for s,s2 in itertools.product(slices[1], slices2[0])]
|
[target.__setitem__((s,s2), self.kern_cross.K(X[s,:],X2[s2,:])) for s,s2 in itertools.product(slices[1], slices2[0])]
|
||||||
if len(slices2)>1:
|
if len(slices2)>1:
|
||||||
|
|
@ -135,7 +135,7 @@ class SplitKern(CombinationKernel):
|
||||||
else:
|
else:
|
||||||
assert dL_dK.shape==(X.shape[0],X2.shape[0])
|
assert dL_dK.shape==(X.shape[0],X2.shape[0])
|
||||||
slices2 = index_to_slices(X2[:,self.index_dim])
|
slices2 = index_to_slices(X2[:,self.index_dim])
|
||||||
[[collate_grads(dL_dK[s,s2],X[s],X2[s2]) for s,s2 in itertools.product(slices[i], slices2[i])] for i in xrange(min(len(slices),len(slices2)))]
|
[[collate_grads(dL_dK[s,s2],X[s],X2[s2]) for s,s2 in itertools.product(slices[i], slices2[i])] for i in range(min(len(slices),len(slices2)))]
|
||||||
if len(slices)>1:
|
if len(slices)>1:
|
||||||
[collate_grads(dL_dK[s,s2], X[s], X2[s2], True) for s,s2 in itertools.product(slices[1], slices2[0])]
|
[collate_grads(dL_dK[s,s2], X[s], X2[s2], True) for s,s2 in itertools.product(slices[1], slices2[0])]
|
||||||
if len(slices2)>1:
|
if len(slices2)>1:
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ class SSGPLVM(SparseGP_MPI):
|
||||||
self.link_parameter(self.X, index=0)
|
self.link_parameter(self.X, index=0)
|
||||||
|
|
||||||
if self.group_spike:
|
if self.group_spike:
|
||||||
[self.X.gamma[:,i].tie('tieGamma'+str(i)) for i in xrange(self.X.gamma.shape[1])] # Tie columns together
|
[self.X.gamma[:,i].tie('tieGamma'+str(i)) for i in range(self.X.gamma.shape[1])] # Tie columns together
|
||||||
|
|
||||||
def set_X_gradients(self, X, X_grad):
|
def set_X_gradients(self, X, X_grad):
|
||||||
"""Set the gradients of the posterior distribution of X in its specific form."""
|
"""Set the gradients of the posterior distribution of X in its specific form."""
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,10 @@ class SSMRD(Model):
|
||||||
name='model_'+str(i)) for i,y in enumerate(Ylist)]
|
name='model_'+str(i)) for i,y in enumerate(Ylist)]
|
||||||
self.add_parameters(*(self.models))
|
self.add_parameters(*(self.models))
|
||||||
|
|
||||||
[[[self.models[m].X.mean[i,j:j+1].tie('mean_'+str(i)+'_'+str(j)) for m in xrange(len(self.models))] for j in xrange(self.models[0].X.mean.shape[1])]
|
[[[self.models[m].X.mean[i,j:j+1].tie('mean_'+str(i)+'_'+str(j)) for m in range(len(self.models))] for j in range(self.models[0].X.mean.shape[1])]
|
||||||
for i in xrange(self.models[0].X.mean.shape[0])]
|
for i in range(self.models[0].X.mean.shape[0])]
|
||||||
[[[self.models[m].X.variance[i,j:j+1].tie('var_'+str(i)+'_'+str(j)) for m in xrange(len(self.models))] for j in xrange(self.models[0].X.variance.shape[1])]
|
[[[self.models[m].X.variance[i,j:j+1].tie('var_'+str(i)+'_'+str(j)) for m in range(len(self.models))] for j in range(self.models[0].X.variance.shape[1])]
|
||||||
for i in xrange(self.models[0].X.variance.shape[0])]
|
for i in range(self.models[0].X.variance.shape[0])]
|
||||||
|
|
||||||
self.updates = True
|
self.updates = True
|
||||||
|
|
||||||
|
|
@ -31,4 +31,4 @@ class SSMRD(Model):
|
||||||
self._log_marginal_likelihood = sum([m._log_marginal_likelihood for m in self.models])
|
self._log_marginal_likelihood = sum([m._log_marginal_likelihood for m in self.models])
|
||||||
|
|
||||||
def log_likelihood(self):
|
def log_likelihood(self):
|
||||||
return self._log_marginal_likelihood
|
return self._log_marginal_likelihood
|
||||||
|
|
|
||||||
|
|
@ -50,8 +50,8 @@ def plot_2D_images(figure, arr, symmetric=False, pad=None, zoom=None, mode=None,
|
||||||
|
|
||||||
buf = np.ones((y_size*fig_nrows+pad*(fig_nrows-1), x_size*fig_ncols+pad*(fig_ncols-1), 3),dtype=arr.dtype)
|
buf = np.ones((y_size*fig_nrows+pad*(fig_nrows-1), x_size*fig_ncols+pad*(fig_ncols-1), 3),dtype=arr.dtype)
|
||||||
|
|
||||||
for y in xrange(fig_nrows):
|
for y in range(fig_nrows):
|
||||||
for x in xrange(fig_ncols):
|
for x in range(fig_ncols):
|
||||||
if y*fig_ncols+x<fig_num:
|
if y*fig_ncols+x<fig_num:
|
||||||
buf[y*y_size+y*pad:(y+1)*y_size+y*pad, x*x_size+x*pad:(x+1)*x_size+x*pad] = arr_color[y*fig_ncols+x,:,:,:3]
|
buf[y*y_size+y*pad:(y+1)*y_size+y*pad, x*x_size+x*pad:(x+1)*x_size+x*pad] = arr_color[y*fig_ncols+x,:,:,:3]
|
||||||
img_plot = ax.imshow(buf, interpolation=interpolation)
|
img_plot = ax.imshow(buf, interpolation=interpolation)
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ def plot(shape_records,facecolor='w',edgecolor='k',linewidths=.5, ax=None,xlims=
|
||||||
par = list(sparts) + [points.shape[0]]
|
par = list(sparts) + [points.shape[0]]
|
||||||
|
|
||||||
polygs = []
|
polygs = []
|
||||||
for pj in xrange(len(sparts)):
|
for pj in range(len(sparts)):
|
||||||
polygs.append(Polygon(points[par[pj]:par[pj+1]]))
|
polygs.append(Polygon(points[par[pj]:par[pj+1]]))
|
||||||
ax.add_collection(PatchCollection(polygs,facecolor=facecolor,edgecolor=edgecolor, linewidths=linewidths))
|
ax.add_collection(PatchCollection(polygs,facecolor=facecolor,edgecolor=edgecolor, linewidths=linewidths))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -459,7 +459,7 @@ class mocap_data_show(matplotlib_show):
|
||||||
|
|
||||||
def initialize_axes(self, boundary=0.05):
|
def initialize_axes(self, boundary=0.05):
|
||||||
"""Set up the axes with the right limits and scaling."""
|
"""Set up the axes with the right limits and scaling."""
|
||||||
bs = [(self.vals[:, i].max()-self.vals[:, i].min())*boundary for i in xrange(3)]
|
bs = [(self.vals[:, i].max()-self.vals[:, i].min())*boundary for i in range(3)]
|
||||||
self.x_lim = np.array([self.vals[:, 0].min()-bs[0], self.vals[:, 0].max()+bs[0]])
|
self.x_lim = np.array([self.vals[:, 0].min()-bs[0], self.vals[:, 0].max()+bs[0]])
|
||||||
self.y_lim = np.array([self.vals[:, 1].min()-bs[1], self.vals[:, 1].max()+bs[1]])
|
self.y_lim = np.array([self.vals[:, 1].min()-bs[1], self.vals[:, 1].max()+bs[1]])
|
||||||
self.z_lim = np.array([self.vals[:, 2].min()-bs[2], self.vals[:, 2].max()+bs[2]])
|
self.z_lim = np.array([self.vals[:, 2].min()-bs[2], self.vals[:, 2].max()+bs[2]])
|
||||||
|
|
|
||||||
|
|
@ -66,12 +66,12 @@ def safe_root(N):
|
||||||
# return flat
|
# return flat
|
||||||
|
|
||||||
def triang_to_cov(L):
|
def triang_to_cov(L):
|
||||||
return np.dstack([np.dot(L[:,:,i], L[:,:,i].T) for i in xrange(L.shape[-1])])
|
return np.dstack([np.dot(L[:,:,i], L[:,:,i].T) for i in range(L.shape[-1])])
|
||||||
|
|
||||||
def multiple_dpotri_old(Ls):
|
def multiple_dpotri_old(Ls):
|
||||||
M, _, D = Ls.shape
|
M, _, D = Ls.shape
|
||||||
Kis = np.rollaxis(Ls, -1).copy()
|
Kis = np.rollaxis(Ls, -1).copy()
|
||||||
[dpotri(Kis[i,:,:], overwrite_c=1, lower=1) for i in xrange(D)]
|
[dpotri(Kis[i,:,:], overwrite_c=1, lower=1) for i in range(D)]
|
||||||
code = """
|
code = """
|
||||||
for(int d=0; d<D; d++)
|
for(int d=0; d<D; d++)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ def divide_data(datanum, rank, size):
|
||||||
|
|
||||||
residue = (datanum)%size
|
residue = (datanum)%size
|
||||||
datanum_list = np.empty((size),dtype=np.int32)
|
datanum_list = np.empty((size),dtype=np.int32)
|
||||||
for i in xrange(size):
|
for i in range(size):
|
||||||
if i<residue:
|
if i<residue:
|
||||||
datanum_list[i] = int(datanum/size)+1
|
datanum_list[i] = int(datanum/size)+1
|
||||||
else:
|
else:
|
||||||
|
|
@ -38,4 +38,4 @@ def divide_data(datanum, rank, size):
|
||||||
else:
|
else:
|
||||||
size = datanum/size
|
size = datanum/size
|
||||||
offset = size*rank+residue
|
offset = size*rank+residue
|
||||||
return offset, offset+size, datanum_list
|
return offset, offset+size, datanum_list
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue