def _grad_helper(z): #import pdb; pdb.set_trace() zeros = z.zeros_like().astype(theano.config.floatX) zeros = zeros.sum(axis=self.axis,keepdims=True) pre = basic.concatenate([zeros, z],axis=self.axis) app = basic.concatenate([z, zeros],axis=self.axis) return pre - app
def _grad_helper(z): #import pdb; pdb.set_trace() zeros = z.zeros_like().astype(theano.config.floatX) zeros = zeros.sum(axis=self.axis, keepdims=True) pre = basic.concatenate([zeros, z], axis=self.axis) app = basic.concatenate([z, zeros], axis=self.axis) return pre - app
def Generate_theta_p(x, s, I, N, K, prod_f): #print(s,N,len(x)) T = len(x) D = len(x[0]) s = [one_hot(ss, N) for ss in s] # x = [[xt for _ in range(N)] for xt in x] x = np.array(x) s = np.array(s) model = pm.Model() with model: # Priors for unknown model parameters theta = pm.Normal("theta", mu=0, sigma=1, shape=(D, K)) / np.sqrt( K * D) p_list = [] for t in range(T): #print(prod_f) #print(np.transpose(theta)) wt = dot(dot(prod_f, transpose(theta)), x[t]) swt = s[t] * wt sum_sw = sum(swt) p = exp(swt) / (1 + sum_sw) p0 = 1 / (1 + sum_sw) p_list.append(concatenate(([p0], p))) I_obs = pm.Categorical("I_obs", p=stack(p_list, axis=0), observed=I) with model: step = pm.Metropolis() trace1 = pm.sample(tune=2000, chains=1, step=step) return trace1["theta"][-1]
def _grad_helper(z): pre = basic.concatenate([[0.], z]) app = basic.concatenate([z, [0.]]) return pre - app