How to use d_exp2 method in fMBT

Best Python code snippet using fMBT_python

ssrbf_psi_comp.py

Source:ssrbf_psi_comp.py Github

copy

Full Screen

1# Copyright (c) 2012, GPy authors (see AUTHORS.txt).2# Licensed under the BSD 3-clause license (see LICENSE.txt)3"""4The package for the psi statistics computation5"""6import numpy as np7try:8 from scipy import weave9 def _psicomputations(variance, lengthscale, Z, variational_posterior):10 """11 Z - MxQ12 mu - NxQ13 S - NxQ14 gamma - NxQ15 """16 # here are the "statistics" for psi0, psi1 and psi217 # Produced intermediate results:18 # _psi1 NxM19 mu = variational_posterior.mean20 S = variational_posterior.variance21 gamma = variational_posterior.binary_prob22 N,M,Q = mu.shape[0],Z.shape[0],mu.shape[1]23 l2 = np.square(lengthscale)24 log_denom1 = np.log(S/l2+1)25 log_denom2 = np.log(2*S/l2+1)26 log_gamma = np.log(gamma)27 log_gamma1 = np.log(1.-gamma)28 variance = float(variance)29 psi0 = np.empty(N)30 psi0[:] = variance31 psi1 = np.empty((N,M))32 psi2n = np.empty((N,M,M))33 from ....util.misc import param_to_array34 S = param_to_array(S)35 mu = param_to_array(mu)36 gamma = param_to_array(gamma)37 Z = param_to_array(Z)38 support_code = """39 #include <math.h>40 """41 code = """42 for(int n=0; n<N; n++) {43 for(int m1=0;m1<M;m1++) {44 double log_psi1=0;45 for(int m2=0;m2<=m1;m2++) {46 double log_psi2_n=0;47 for(int q=0;q<Q;q++) {48 double Snq = S(n,q);49 double lq = l2(q);50 double Zm1q = Z(m1,q);51 double Zm2q = Z(m2,q);52 if(m2==0) {53 // Compute Psi_154 double muZ = mu(n,q)-Z(m1,q);55 double psi1_exp1 = log_gamma(n,q) - (muZ*muZ/(Snq+lq) +log_denom1(n,q))/2.;56 double psi1_exp2 = log_gamma1(n,q) -Zm1q*Zm1q/(2.*lq);57 log_psi1 += (psi1_exp1>psi1_exp2)?psi1_exp1+log1p(exp(psi1_exp2-psi1_exp1)):psi1_exp2+log1p(exp(psi1_exp1-psi1_exp2));58 }59 // Compute Psi_260 double muZhat = mu(n,q) - (Zm1q+Zm2q)/2.;61 double Z2 = Zm1q*Zm1q+ Zm2q*Zm2q;62 double dZ = Zm1q - Zm2q;63 double psi2_exp1 = dZ*dZ/(-4.*lq)-muZhat*muZhat/(2.*Snq+lq) - log_denom2(n,q)/2. + log_gamma(n,q);64 double psi2_exp2 = log_gamma1(n,q) - Z2/(2.*lq);65 log_psi2_n += (psi2_exp1>psi2_exp2)?psi2_exp1+log1p(exp(psi2_exp2-psi2_exp1)):psi2_exp2+log1p(exp(psi2_exp1-psi2_exp2));66 }67 double exp_psi2_n = exp(log_psi2_n);68 psi2n(n,m1,m2) = variance*variance*exp_psi2_n;69 if(m1!=m2) { psi2n(n,m2,m1) = variance*variance*exp_psi2_n;}70 }71 psi1(n,m1) = variance*exp(log_psi1);72 }73 }74 """75 weave.inline(code, support_code=support_code, arg_names=['psi1','psi2n','N','M','Q','variance','l2','Z','mu','S','gamma','log_denom1','log_denom2','log_gamma','log_gamma1'], type_converters=weave.converters.blitz)76 psi2 = psi2n.sum(axis=0)77 return psi0,psi1,psi2,psi2n78 from GPy.util.caching import Cacher79 psicomputations = Cacher(_psicomputations, limit=1)80 def psiDerivativecomputations(dL_dpsi0, dL_dpsi1, dL_dpsi2, variance, lengthscale, Z, variational_posterior):81 ARD = (len(lengthscale)!=1)82 _,psi1,_,psi2n = psicomputations(variance, lengthscale, Z, variational_posterior)83 mu = variational_posterior.mean84 S = variational_posterior.variance85 gamma = variational_posterior.binary_prob86 N,M,Q = mu.shape[0],Z.shape[0],mu.shape[1]87 l2 = np.square(lengthscale)88 log_denom1 = np.log(S/l2+1)89 log_denom2 = np.log(2*S/l2+1)90 log_gamma = np.log(gamma)91 log_gamma1 = np.log(1.-gamma)92 variance = float(variance)93 dvar = np.zeros(1)94 dmu = np.zeros((N,Q))95 dS = np.zeros((N,Q))96 dgamma = np.zeros((N,Q))97 dl = np.zeros(Q)98 dZ = np.zeros((M,Q))99 dvar += np.sum(dL_dpsi0)100 from ....util.misc import param_to_array101 S = param_to_array(S)102 mu = param_to_array(mu)103 gamma = param_to_array(gamma)104 Z = param_to_array(Z)105 support_code = """106 #include <math.h>107 """108 code = """109 for(int n=0; n<N; n++) {110 for(int m1=0;m1<M;m1++) {111 double log_psi1=0;112 for(int m2=0;m2<M;m2++) {113 double log_psi2_n=0;114 for(int q=0;q<Q;q++) {115 double Snq = S(n,q);116 double lq = l2(q);117 double Zm1q = Z(m1,q);118 double Zm2q = Z(m2,q);119 double gnq = gamma(n,q);120 double mu_nq = mu(n,q);121 if(m2==0) {122 // Compute Psi_1123 double lpsi1 = psi1(n,m1)*dL_dpsi1(n,m1);124 if(q==0) {dvar(0) += lpsi1/variance;}125 double Zmu = Zm1q - mu_nq;126 double denom = Snq+lq;127 double Zmu2_denom = Zmu*Zmu/denom;128 double exp1 = log_gamma(n,q)-(Zmu*Zmu/(Snq+lq)+log_denom1(n,q))/(2.);129 double exp2 = log_gamma1(n,q)-Zm1q*Zm1q/(2.*lq);130 double d_exp1,d_exp2;131 if(exp1>exp2) {132 d_exp1 = 1.;133 d_exp2 = exp(exp2-exp1);134 } else {135 d_exp1 = exp(exp1-exp2);136 d_exp2 = 1.;137 }138 double exp_sum = d_exp1+d_exp2;139 dmu(n,q) += lpsi1*Zmu*d_exp1/(denom*exp_sum);140 dS(n,q) += lpsi1*(Zmu2_denom-1.)*d_exp1/(denom*exp_sum)/2.;141 dgamma(n,q) += lpsi1*(d_exp1/gnq-d_exp2/(1.-gnq))/exp_sum;142 dl(q) += lpsi1*((Zmu2_denom+Snq/lq)/denom*d_exp1+Zm1q*Zm1q/(lq*lq)*d_exp2)/(2.*exp_sum);143 dZ(m1,q) += lpsi1*(-Zmu/denom*d_exp1-Zm1q/lq*d_exp2)/exp_sum;144 }145 // Compute Psi_2146 double lpsi2 = psi2n(n,m1,m2)*dL_dpsi2(m1,m2);147 if(q==0) {dvar(0) += lpsi2*2/variance;}148 double dZm1m2 = Zm1q - Zm2q;149 double Z2 = Zm1q*Zm1q+Zm2q*Zm2q;150 double muZhat = mu_nq - (Zm1q + Zm2q)/2.;151 double denom = 2.*Snq+lq;152 double muZhat2_denom = muZhat*muZhat/denom;153 double exp1 = dZm1m2*dZm1m2/(-4.*lq)-muZhat*muZhat/(2.*Snq+lq) - log_denom2(n,q)/2. + log_gamma(n,q);154 double exp2 = log_gamma1(n,q) - Z2/(2.*lq);155 double d_exp1,d_exp2;156 if(exp1>exp2) {157 d_exp1 = 1.;158 d_exp2 = exp(exp2-exp1);159 } else {160 d_exp1 = exp(exp1-exp2);161 d_exp2 = 1.;162 }163 double exp_sum = d_exp1+d_exp2;164 dmu(n,q) += -2.*lpsi2*muZhat/denom*d_exp1/exp_sum;165 dS(n,q) += lpsi2*(2.*muZhat2_denom-1.)/denom*d_exp1/exp_sum;166 dgamma(n,q) += lpsi2*(d_exp1/gnq-d_exp2/(1.-gnq))/exp_sum;167 dl(q) += lpsi2*(((Snq/lq+muZhat2_denom)/denom+dZm1m2*dZm1m2/(4.*lq*lq))*d_exp1+Z2/(2.*lq*lq)*d_exp2)/exp_sum;168 dZ(m1,q) += 2.*lpsi2*((muZhat/denom-dZm1m2/(2*lq))*d_exp1-Zm1q/lq*d_exp2)/exp_sum;169 }170 }171 }172 }173 """174 weave.inline(code, support_code=support_code, arg_names=['dL_dpsi1','dL_dpsi2','psi1','psi2n','N','M','Q','variance','l2','Z','mu','S','gamma','log_denom1','log_denom2','log_gamma','log_gamma1','dvar','dl','dmu','dS','dgamma','dZ'], type_converters=weave.converters.blitz)175 dl *= 2.*lengthscale176 if not ARD:177 dl = dl.sum()178 return dvar, dl, dZ, dmu, dS, dgamma179except:180 def psicomputations(variance, lengthscale, Z, variational_posterior):181 """182 Z - MxQ183 mu - NxQ184 S - NxQ185 gamma - NxQ186 """187 # here are the "statistics" for psi0, psi1 and psi2188 # Produced intermediate results:189 # _psi1 NxM190 mu = variational_posterior.mean191 S = variational_posterior.variance192 gamma = variational_posterior.binary_prob193 psi0 = np.empty(mu.shape[0])194 psi0[:] = variance195 psi1 = _psi1computations(variance, lengthscale, Z, mu, S, gamma)196 psi2 = _psi2computations(variance, lengthscale, Z, mu, S, gamma)197 return psi0, psi1, psi2198 def _psi1computations(variance, lengthscale, Z, mu, S, gamma):199 """200 Z - MxQ201 mu - NxQ202 S - NxQ203 gamma - NxQ204 """205 # here are the "statistics" for psi1206 # Produced intermediate results:207 # _psi1 NxM208 lengthscale2 = np.square(lengthscale)209 # psi1210 _psi1_denom = S[:, None, :] / lengthscale2 + 1. # Nx1xQ211 _psi1_denom_sqrt = np.sqrt(_psi1_denom) #Nx1xQ212 _psi1_dist = Z[None, :, :] - mu[:, None, :] # NxMxQ213 _psi1_dist_sq = np.square(_psi1_dist) / (lengthscale2 * _psi1_denom) # NxMxQ214 _psi1_common = gamma[:,None,:] / (lengthscale2*_psi1_denom*_psi1_denom_sqrt) #Nx1xQ215 _psi1_exponent1 = np.log(gamma[:,None,:]) - (_psi1_dist_sq + np.log(_psi1_denom))/2. # NxMxQ216 _psi1_exponent2 = np.log(1.-gamma[:,None,:]) - (np.square(Z[None,:,:])/lengthscale2)/2. # NxMxQ217 _psi1_exponent_max = np.maximum(_psi1_exponent1,_psi1_exponent2)218 _psi1_exponent = _psi1_exponent_max+np.log(np.exp(_psi1_exponent1-_psi1_exponent_max) + np.exp(_psi1_exponent2-_psi1_exponent_max)) #NxMxQ219 _psi1_exp_sum = _psi1_exponent.sum(axis=-1) #NxM220 _psi1 = variance * np.exp(_psi1_exp_sum) # NxM221 return _psi1222 def _psi2computations(variance, lengthscale, Z, mu, S, gamma):223 """224 Z - MxQ225 mu - NxQ226 S - NxQ227 gamma - NxQ228 """229 # here are the "statistics" for psi2230 # Produced intermediate results:231 # _psi2 MxM232 lengthscale2 = np.square(lengthscale)233 _psi2_Zhat = 0.5 * (Z[:, None, :] + Z[None, :, :]) # M,M,Q234 _psi2_Zdist = 0.5 * (Z[:, None, :] - Z[None, :, :]) # M,M,Q235 _psi2_Zdist_sq = np.square(_psi2_Zdist / lengthscale) # M,M,Q236 _psi2_Z_sq_sum = (np.square(Z[:,None,:])+np.square(Z[None,:,:]))/lengthscale2 # MxMxQ237 # psi2238 _psi2_denom = 2.*S[:, None, None, :] / lengthscale2 + 1. # Nx1x1xQ239 _psi2_denom_sqrt = np.sqrt(_psi2_denom)240 _psi2_mudist = mu[:,None,None,:]-_psi2_Zhat #N,M,M,Q241 _psi2_mudist_sq = np.square(_psi2_mudist)/(lengthscale2*_psi2_denom)242 _psi2_common = gamma[:,None,None,:]/(lengthscale2 * _psi2_denom * _psi2_denom_sqrt) # Nx1x1xQ243 _psi2_exponent1 = -_psi2_Zdist_sq -_psi2_mudist_sq -0.5*np.log(_psi2_denom)+np.log(gamma[:,None,None,:]) #N,M,M,Q244 _psi2_exponent2 = np.log(1.-gamma[:,None,None,:]) - 0.5*(_psi2_Z_sq_sum) # NxMxMxQ245 _psi2_exponent_max = np.maximum(_psi2_exponent1, _psi2_exponent2)246 _psi2_exponent = _psi2_exponent_max+np.log(np.exp(_psi2_exponent1-_psi2_exponent_max) + np.exp(_psi2_exponent2-_psi2_exponent_max))247 _psi2_exp_sum = _psi2_exponent.sum(axis=-1) #NxM248 _psi2 = variance*variance * (np.exp(_psi2_exp_sum).sum(axis=0)) # MxM249 return _psi2250 def psiDerivativecomputations(dL_dpsi0, dL_dpsi1, dL_dpsi2, variance, lengthscale, Z, variational_posterior):251 ARD = (len(lengthscale)!=1)252 dvar_psi1, dl_psi1, dZ_psi1, dmu_psi1, dS_psi1, dgamma_psi1 = _psi1compDer(dL_dpsi1, variance, lengthscale, Z, variational_posterior.mean, variational_posterior.variance, variational_posterior.binary_prob)253 dvar_psi2, dl_psi2, dZ_psi2, dmu_psi2, dS_psi2, dgamma_psi2 = _psi2compDer(dL_dpsi2, variance, lengthscale, Z, variational_posterior.mean, variational_posterior.variance, variational_posterior.binary_prob)254 dL_dvar = np.sum(dL_dpsi0) + dvar_psi1 + dvar_psi2255 dL_dlengscale = dl_psi1 + dl_psi2256 if not ARD:257 dL_dlengscale = dL_dlengscale.sum()258 dL_dgamma = dgamma_psi1 + dgamma_psi2259 dL_dmu = dmu_psi1 + dmu_psi2260 dL_dS = dS_psi1 + dS_psi2261 dL_dZ = dZ_psi1 + dZ_psi2262 return dL_dvar, dL_dlengscale, dL_dZ, dL_dmu, dL_dS, dL_dgamma263 def _psi1compDer(dL_dpsi1, variance, lengthscale, Z, mu, S, gamma):264 """265 dL_dpsi1 - NxM266 Z - MxQ267 mu - NxQ268 S - NxQ269 gamma - NxQ270 """271 # here are the "statistics" for psi1272 # Produced intermediate results: dL_dparams w.r.t. psi1273 # _dL_dvariance 1274 # _dL_dlengthscale Q275 # _dL_dZ MxQ276 # _dL_dgamma NxQ277 # _dL_dmu NxQ278 # _dL_dS NxQ279 lengthscale2 = np.square(lengthscale)280 # psi1281 _psi1_denom = S / lengthscale2 + 1. # NxQ282 _psi1_denom_sqrt = np.sqrt(_psi1_denom) #NxQ283 _psi1_dist = Z[None, :, :] - mu[:, None, :] # NxMxQ284 _psi1_dist_sq = np.square(_psi1_dist) / (lengthscale2 * _psi1_denom[:,None,:]) # NxMxQ285 _psi1_common = gamma / (lengthscale2*_psi1_denom*_psi1_denom_sqrt) #NxQ286 _psi1_exponent1 = np.log(gamma[:,None,:]) -0.5 * (_psi1_dist_sq + np.log(_psi1_denom[:, None,:])) # NxMxQ287 _psi1_exponent2 = np.log(1.-gamma[:,None,:]) -0.5 * (np.square(Z[None,:,:])/lengthscale2) # NxMxQ288 _psi1_exponent_max = np.maximum(_psi1_exponent1,_psi1_exponent2)289 _psi1_exponent = _psi1_exponent_max+np.log(np.exp(_psi1_exponent1-_psi1_exponent_max) + np.exp(_psi1_exponent2-_psi1_exponent_max)) #NxMxQ290 _psi1_exp_sum = _psi1_exponent.sum(axis=-1) #NxM291 _psi1_exp_dist_sq = np.exp(-0.5*_psi1_dist_sq) # NxMxQ292 _psi1_exp_Z = np.exp(-0.5*np.square(Z[None,:,:])/lengthscale2) # 1xMxQ293 _psi1_q = variance * np.exp(_psi1_exp_sum[:,:,None] - _psi1_exponent) # NxMxQ294 _psi1 = variance * np.exp(_psi1_exp_sum) # NxM295 _dL_dvariance = np.einsum('nm,nm->',dL_dpsi1, _psi1)/variance # 1296 _dL_dgamma = np.einsum('nm,nmq,nmq->nq',dL_dpsi1, _psi1_q, (_psi1_exp_dist_sq/_psi1_denom_sqrt[:,None,:]-_psi1_exp_Z)) # NxQ297 _dL_dmu = np.einsum('nm, nmq, nmq, nmq, nq->nq',dL_dpsi1,_psi1_q,_psi1_exp_dist_sq,_psi1_dist,_psi1_common) # NxQ298 _dL_dS = np.einsum('nm,nmq,nmq,nq,nmq->nq',dL_dpsi1,_psi1_q,_psi1_exp_dist_sq,_psi1_common,(_psi1_dist_sq-1.))/2. # NxQ299 _dL_dZ = np.einsum('nm,nmq,nmq->mq',dL_dpsi1,_psi1_q, (- _psi1_common[:,None,:] * _psi1_dist * _psi1_exp_dist_sq - (1-gamma[:,None,:])/lengthscale2*Z[None,:,:]*_psi1_exp_Z))300 _dL_dlengthscale = lengthscale* np.einsum('nm,nmq,nmq->q',dL_dpsi1,_psi1_q,(_psi1_common[:,None,:]*(S[:,None,:]/lengthscale2+_psi1_dist_sq)*_psi1_exp_dist_sq + (1-gamma[:,None,:])*np.square(Z[None,:,:]/lengthscale2)*_psi1_exp_Z))301 return _dL_dvariance, _dL_dlengthscale, _dL_dZ, _dL_dmu, _dL_dS, _dL_dgamma302 def _psi2compDer(dL_dpsi2, variance, lengthscale, Z, mu, S, gamma):303 """304 Z - MxQ305 mu - NxQ306 S - NxQ307 gamma - NxQ308 dL_dpsi2 - MxM309 """310 # here are the "statistics" for psi2311 # Produced the derivatives w.r.t. psi2:312 # _dL_dvariance 1313 # _dL_dlengthscale Q314 # _dL_dZ MxQ315 # _dL_dgamma NxQ316 # _dL_dmu NxQ317 # _dL_dS NxQ318 lengthscale2 = np.square(lengthscale)319 _psi2_Zhat = 0.5 * (Z[:, None, :] + Z[None, :, :]) # M,M,Q320 _psi2_Zdist = 0.5 * (Z[:, None, :] - Z[None, :, :]) # M,M,Q321 _psi2_Zdist_sq = np.square(_psi2_Zdist / lengthscale) # M,M,Q322 _psi2_Z_sq_sum = (np.square(Z[:,None,:])+np.square(Z[None,:,:]))/lengthscale2 # MxMxQ323 # psi2324 _psi2_denom = 2.*S / lengthscale2 + 1. # NxQ325 _psi2_denom_sqrt = np.sqrt(_psi2_denom)326 _psi2_mudist = mu[:,None,None,:]-_psi2_Zhat #N,M,M,Q327 _psi2_mudist_sq = np.square(_psi2_mudist)/(lengthscale2*_psi2_denom[:,None,None,:])328 _psi2_common = gamma/(lengthscale2 * _psi2_denom * _psi2_denom_sqrt) # NxQ329 _psi2_exponent1 = -_psi2_Zdist_sq -_psi2_mudist_sq -0.5*np.log(_psi2_denom[:,None,None,:])+np.log(gamma[:,None,None,:]) #N,M,M,Q330 _psi2_exponent2 = np.log(1.-gamma[:,None,None,:]) - 0.5*(_psi2_Z_sq_sum) # NxMxMxQ331 _psi2_exponent_max = np.maximum(_psi2_exponent1, _psi2_exponent2)332 _psi2_exponent = _psi2_exponent_max+np.log(np.exp(_psi2_exponent1-_psi2_exponent_max) + np.exp(_psi2_exponent2-_psi2_exponent_max))333 _psi2_exp_sum = _psi2_exponent.sum(axis=-1) #NxM334 _psi2_q = variance*variance * np.exp(_psi2_exp_sum[:,:,:,None]-_psi2_exponent) # NxMxMxQ335 _psi2_exp_dist_sq = np.exp(-_psi2_Zdist_sq -_psi2_mudist_sq) # NxMxMxQ336 _psi2_exp_Z = np.exp(-0.5*_psi2_Z_sq_sum) # MxMxQ337 _psi2 = variance*variance * (np.exp(_psi2_exp_sum).sum(axis=0)) # MxM338 _dL_dvariance = np.einsum('mo,mo->',dL_dpsi2,_psi2)*2./variance339 _dL_dgamma = np.einsum('mo,nmoq,nmoq->nq',dL_dpsi2,_psi2_q,(_psi2_exp_dist_sq/_psi2_denom_sqrt[:,None,None,:] - _psi2_exp_Z))340 _dL_dmu = -2.*np.einsum('mo,nmoq,nq,nmoq,nmoq->nq',dL_dpsi2,_psi2_q,_psi2_common,_psi2_mudist,_psi2_exp_dist_sq)341 _dL_dS = np.einsum('mo,nmoq,nq,nmoq,nmoq->nq',dL_dpsi2,_psi2_q, _psi2_common, (2.*_psi2_mudist_sq-1.), _psi2_exp_dist_sq)342 _dL_dZ = 2.*np.einsum('mo,nmoq,nmoq->mq',dL_dpsi2,_psi2_q,(_psi2_common[:,None,None,:]*(-_psi2_Zdist*_psi2_denom[:,None,None,:]+_psi2_mudist)*_psi2_exp_dist_sq - (1-gamma[:,None,None,:])*Z[:,None,:]/lengthscale2*_psi2_exp_Z))343 _dL_dlengthscale = 2.*lengthscale* np.einsum('mo,nmoq,nmoq->q',dL_dpsi2,_psi2_q,(_psi2_common[:,None,None,:]*(S[:,None,None,:]/lengthscale2+_psi2_Zdist_sq*_psi2_denom[:,None,None,:]+_psi2_mudist_sq)*_psi2_exp_dist_sq+(1-gamma[:,None,None,:])*_psi2_Z_sq_sum*0.5/lengthscale2*_psi2_exp_Z))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fMBT automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful