def r_loss(communities = 2, group_size = 10, seed=None, p=0.4, q=0.05, r=1.0, projection_dim=2): """testing to see if the loss will decrease backproping through very simple function""" B = np.asarray(balanced_stochastic_blockmodel(communities, group_size, p, q, seed)).astype(np.double) B = tf.cast(B, tf.float64) Diag = tf.diag(tf.reduce_sum(B,0)) Diag = tf.cast(Diag, tf.float64) #r_grid = tf.linspace(r_min, r_max, grid_size) r = tf.cast(r, tf.float64) BH = (tf.square(r)-1)*tf.diag(tf.ones(shape=[communities*group_size], dtype=tf.float64))-tf.mul(r, B)+Diag with tf.Session() as sess: eigenval, eigenvec = tf.self_adjoint_eig(BH) eigenvec_proj = tf.slice(eigenvec, [0,0], [communities*group_size, projection_dim]) true_assignment_a = tf.concat(0, [-1*tf.ones([group_size], dtype=tf.float64), tf.ones([group_size], dtype=tf.float64)]) true_assignment_b = -1*true_assignment_a true_assignment_a = tf.expand_dims(true_assignment_a, 1) true_assignment_b = tf.expand_dims(true_assignment_b, 1) projected_a = tf.matmul(tf.matmul(eigenvec_proj, tf.transpose(eigenvec_proj)), true_assignment_a)#tf.transpose(true_assignment_a)) projected_b = tf.matmul(tf.matmul(eigenvec_proj, tf.transpose(eigenvec_proj)), true_assignment_b)#tf.transpose(true_assignment_b)) loss = tf.minimum(tf.reduce_sum(tf.square(tf.sub(projected_a, true_assignment_a))), tf.reduce_sum(tf.square(tf.sub(projected_b, true_assignment_b)))) d = sess.run(loss) return d
def compute_loss_reg(self, sim_reg_mat, offset_label): sim_score_mat, p_reg_mat, l_reg_mat = tf.split(2, 3, sim_reg_mat) sim_score_mat = tf.reshape(sim_score_mat, [self.batch_size, self.batch_size]) l_reg_mat = tf.reshape(l_reg_mat, [self.batch_size, self.batch_size]) p_reg_mat = tf.reshape(p_reg_mat, [self.batch_size, self.batch_size]) # unit matrix with -2 I_2 = tf.diag(tf.constant(-2.0, shape=[self.batch_size])) all1 = tf.constant(1.0, shape=[self.batch_size, self.batch_size]) # | -1 1 1... | # mask_mat = | 1 -1 -1... | # | 1 1 -1 ... | mask_mat = tf.add(I_2, all1) # loss cls, not considering iou I = tf.diag(tf.constant(1.0, shape=[self.batch_size])) I_half = tf.diag(tf.constant(0.5, shape=[self.batch_size])) batch_para_mat = tf.constant(self.alpha, shape=[self.batch_size, self.batch_size]) para_mat = tf.add(I,batch_para_mat) loss_mat = tf.log(tf.add(all1, tf.exp(tf.mul(mask_mat, sim_score_mat)))) loss_mat = tf.mul(loss_mat, para_mat) loss_align = tf.reduce_mean(loss_mat) # regression loss l_reg_diag = tf.matmul(tf.mul(l_reg_mat, I), tf.constant(1.0, shape=[self.batch_size, 1])) p_reg_diag = tf.matmul(tf.mul(p_reg_mat, I), tf.constant(1.0, shape=[self.batch_size, 1])) offset_pred = tf.concat(1, (p_reg_diag, l_reg_diag)) loss_reg = tf.reduce_mean(tf.abs(tf.sub(offset_pred, offset_label))) loss=tf.add(tf.mul(self.lambda_regression, loss_reg), loss_align) return loss, offset_pred, loss_reg
def multilayer_perceptron(_X_aud, _X_img, _w_aud, _b_aud, _w_img, _b_img, _w_out, _b_out, _dropout, _b_size): #layer 1 aud_layer_1 = tf.nn.relu(tf.add(tf.matmul(_X_aud, _w_aud['h1']), _b_aud['b1'])) img_layer_1 = tf.nn.relu(tf.add(tf.matmul(_X_img, _w_img['h1']), _b_img['b1'])) img_layer_1 = tf.nn.dropout(img_layer_1, _dropout) ''' CA 1 ''' factor = calculatCA(aud_layer_1, img_layer_1, 1000, _b_size) factor = tf.reshape(tf.diag(factor), shape=[_b_size, _b_size]) aud_layer_1 = tf.matmul(factor, aud_layer_1) aud_layer_1 = tf.nn.dropout(aud_layer_1, _dropout) img_layer_1 = tf.matmul(factor, img_layer_1) #img_layer_1 = tf.nn.dropout(img_layer_1, _dropout) #layer 2 aud_layer_2 = tf.nn.relu(tf.add(tf.matmul(aud_layer_1, _w_aud['h2']), _b_aud['b2'])) #Hidden layer with RELU activation img_layer_2 = tf.nn.relu(tf.add(tf.matmul(img_layer_1, _w_img['h2']), _b_img['b2'])) #Hidden layer with RELU activation #drop_2 = tf.nn.dropout(img_layer_2, _dropout) ''' CA 2 & merge ''' factor = calculatCA(aud_layer_2, img_layer_2, 600, _b_size) factor = tf.reshape(tf.diag(factor), shape=[_b_size, _b_size]) merge_sum = tf.add(aud_layer_2, img_layer_2) facmat = tf.matmul(factor, merge_sum) #facmat = tf.nn.dropout(facmat, _dropout) #output layer out_layer_1 = tf.nn.relu(tf.add(tf.matmul(facmat, _w_out['h1']), _b_out['b1'])) #Hidden layer with RELU activation out_layer_2 = tf.nn.relu(tf.add(tf.matmul(out_layer_1, _w_out['h2']), _b_out['b2'])) #Hidden layer with RELU activation return tf.matmul(out_layer_2, _w_out['out']) + _b_out['out']
def __init__(self, mps, mpo, name='InfiniteDMRG', precision=1E-12, precision_canonize=1E-12, nmax=1000, nmax_canonize=1000, ncv=40, numeig=1, pinv=1E-20, power_method=False): # if not isinstance(mps, InfiniteMPSCentralGauge): # raise TypeError( # 'in InfiniteDMRGEngine.__init__(...): mps of type InfiniteMPSCentralGauge expected, got {0}' # .format(type(mps))) mps.restore_form( precision=precision_canonize, ncv=ncv, nmax=nmax_canonize, numeig=numeig, power_method=power_method, pinv=pinv) #this leaves state in left-orthogonal form lb, hl = misc_mps.compute_steady_state_Hamiltonian_GMRES( 'l', mps, mpo, left_dominant=tf.diag(tf.ones(mps.D[-1], dtype=mps.dtype)), right_dominant=ncon.ncon([mps.mat, tf.conj(mps.mat)], [[-1, 1], [-2, 1]]), precision=precision, nmax=nmax) rmps = mps.get_right_orthogonal_imps( precision=precision_canonize, ncv=ncv, nmax=nmax_canonize, numeig=numeig, pinv=pinv, restore_form=False) rb, hr = misc_mps.compute_steady_state_Hamiltonian_GMRES( 'r', rmps, mpo, right_dominant=tf.diag(tf.ones(mps.D[0], dtype=mps.dtype)), left_dominant=ncon.ncon([mps.mat, tf.conj(mps.mat)], [[1, -1], [1, -2]]), precision=precision, nmax=nmax) left_dominant = ncon.ncon([mps.mat, tf.conj(mps.mat)], [[1, -1], [1, -2]]) out = mps.unitcell_transfer_op('l', left_dominant) super().__init__(mps=mps, mpo=mpo, lb=lb, rb=rb, name=name)
def logpdf(self, x, mean=None, cov=1): """Log of the probability density function. Parameters ---------- x : tf.Tensor A 1-D or 2-D tensor. mean : tf.Tensor, optional A 1-D tensor. Defaults to zero mean. cov : tf.Tensor, optional A 1-D or 2-D tensor. Defaults to identity matrix. Returns ------- tf.Tensor A tensor of one dimension less than the input. """ x = tf.cast(x, dtype=tf.float32) x_shape = get_dims(x) if len(x_shape) == 1: d = x_shape[0] else: d = x_shape[1] if mean is None: r = x else: mean = tf.cast(mean, dtype=tf.float32) r = x - mean if cov is 1: L_inv = tf.diag(tf.ones([d])) det_cov = tf.constant(1.0) else: cov = tf.cast(cov, dtype=tf.float32) if len(cov.get_shape()) == 1: # vector L_inv = tf.diag(1.0 / tf.sqrt(cov)) det_cov = tf.reduce_prod(cov) else: # matrix L = tf.cholesky(cov) L_inv = tf.matrix_inverse(L) det_cov = tf.pow(tf.reduce_prod(tf.diag_part(L)), 2) lps = -0.5*d*tf.log(2*np.pi) - 0.5*tf.log(det_cov) if len(x_shape) == 1: # vector r = tf.reshape(r, shape=(d, 1)) inner = tf.matmul(L_inv, r) lps -= 0.5 * tf.matmul(inner, inner, transpose_a=True) return tf.squeeze(lps) else: # matrix # TODO vectorize further out = [] for r_vec in tf.unpack(r): r_vec = tf.reshape(r_vec, shape=(d, 1)) inner = tf.matmul(L_inv, r_vec) out += [tf.squeeze(lps - 0.5 * tf.matmul(inner, inner, transpose_a=True))] return tf.pack(out)
def test_all_finite_raises(self): with self.test_session(): a = np.inf * tf.ones([5]) * np.arange(5) b = tf.diag(tf.ones([5])) with self.assertRaisesOpError('Inf'): dot(a, b).eval() a = tf.ones([5]) * np.arange(5) b = np.inf * tf.diag(tf.ones([5])) with self.assertRaisesOpError('Inf'): dot(a, b).eval()
def times_diag_tf(input_matrix, n_hidden, diag): input_re = input_matrix[:, :n_hidden] #okay so the first left half of the matrix is real numbers input_im = input_matrix[:, n_hidden:] #the right half is the imaginary numbers that correspond Re = tf.diag(tf.cos(diag)) Im = tf.diag(tf.sin(diag)) input_re_times_Re = tf.matmul(input_re, Re) #matmul is the equivalent of dot input_re_times_Im = tf.matmul(input_re, Im) input_im_times_Re = tf.matmul(input_im, Re) input_im_times_Im = tf.matmul(input_im, Im) return tf.concat(1, [input_re_times_Re - input_im_times_Im, input_re_times_Im + input_im_times_Re]) #this will combine two matrixes
def logpdf(self, x, mean=None, cov=1): """ Parameters ---------- x : np.array or tf.Tensor vector or matrix mean : np.array or tf.Tensor, optional vector. Defaults to zero mean. cov : np.array or tf.Tensor, optional vector or matrix. Defaults to identity. """ x = tf.cast(tf.convert_to_tensor(x), dtype=tf.float32) x_shape = get_dims(x) if len(x_shape) == 1: d = x_shape[0] else: d = x_shape[1] if mean is None: r = x else: mean = tf.cast(tf.convert_to_tensor(mean), dtype=tf.float32) r = x - mean if cov is 1: cov_inv = tf.diag(tf.ones([d])) det_cov = tf.constant(1.0) else: cov = tf.cast(tf.convert_to_tensor(cov), dtype=tf.float32) if len(cov.get_shape()) == 1: # vector cov_inv = tf.diag(1.0 / cov) det_cov = tf.reduce_prod(cov) else: # matrix cov_inv = tf.matrix_inverse(cov) det_cov = tf.matrix_determinant(cov) lps = -0.5*d*tf.log(2*np.pi) - 0.5*tf.log(det_cov) if len(x_shape) == 1: r = tf.reshape(r, shape=(d, 1)) lps -= 0.5 * tf.matmul(tf.matmul(r, cov_inv, transpose_a=True), r) return tf.squeeze(lps) else: # TODO vectorize further out = [] for r_vec in tf.unpack(r): r_vec = tf.reshape(r_vec, shape=(d, 1)) out += [tf.squeeze(lps - 0.5 * tf.matmul( tf.matmul(r_vec, cov_inv, transpose_a=True), r_vec))] return tf.pack(out) """
def logpdf(self, x, mean=None, cov=1): """ Arguments ---------- x: tf.Tensor vector mean: tf.Tensor, optional vector. Defaults to zero mean. cov: tf.Tensor, optional vector or matrix. Defaults to identity. Returns ------- tf.Tensor scalar """ x = tf.cast(tf.squeeze(x), dtype=tf.float32) d = get_dims(x)[0] if mean is None: r = tf.ones([d]) * x else: mean = tf.cast(tf.squeeze(mean), dtype=tf.float32) r = x - mean if cov == 1: cov_inv = tf.diag(tf.ones([d])) det_cov = tf.constant(1.0) else: cov = tf.cast(tf.squeeze(cov), dtype=tf.float32) if len(cov.get_shape()) == 1: cov_inv = tf.diag(1.0 / cov) det_cov = tf.reduce_prod(cov) else: cov_inv = tf.matrix_inverse(cov) det_cov = tf.matrix_determinant(cov) r = tf.reshape(r, shape=(d, 1)) lps = -0.5*d*tf.log(2*np.pi) - 0.5*tf.log(det_cov) - \ 0.5 * tf.matmul(tf.matmul(r, cov_inv, transpose_a=True), r) """ # TensorFlow can't reverse-mode autodiff Cholesky L = tf.cholesky(cov) L_inv = tf.matrix_inverse(L) det_cov = tf.pow(tf.matrix_determinant(L), 2) inner = dot(L_inv, r) out = -0.5*d*tf.log(2*np.pi) - \ 0.5*tf.log(det_cov) - \ 0.5*tf.matmul(tf.transpose(inner), inner) """ return tf.squeeze(lps)
def BatchClipByL2norm(t, upper_bound, name=None): """Clip an array of tensors by L2 norm. Shrink each dimension-0 slice of tensor (for matrix it is each row) such that the l2 norm is at most upper_bound. Here we clip each row as it corresponds to each example in the batch. Args: t: the input tensor. upper_bound: the upperbound of the L2 norm. name: optional name. Returns: the clipped tensor. """ assert upper_bound > 0 with tf.op_scope([t, upper_bound], name, "batch_clip_by_l2norm") as name: saved_shape = tf.shape(t) batch_size = tf.slice(saved_shape, [0], [1]) t2 = tf.reshape(t, tf.concat(0, [batch_size, [-1]])) upper_bound_inv = tf.fill(tf.slice(saved_shape, [0], [1]), tf.constant(1.0/upper_bound)) # Add a small number to avoid divide by 0 l2norm_inv = tf.rsqrt(tf.reduce_sum(t2 * t2, [1]) + 0.000001) scale = tf.minimum(l2norm_inv, upper_bound_inv) * upper_bound clipped_t = tf.matmul(tf.diag(scale), t2) clipped_t = tf.reshape(clipped_t, saved_shape, name=name) return clipped_t
def symsqrt(mat, eps=1e-7): """Symmetric square root.""" s, u, v = tf.svd(mat) # sqrt is unstable around 0, just use 0 in such case print("Warning, cutting off at eps") si = tf.where(tf.less(s, eps), s, tf.sqrt(s)) return u @ tf.diag(si) @ tf.transpose(v)
def pseudo_inverse(mat, eps=1e-10): """Computes pseudo-inverse of mat, treating eigenvalues below eps as 0.""" s, u, v = tf.svd(mat) eps = 1e-10 # zero threshold for eigenvalues si = tf.where(tf.less(s, eps), s, 1./s) return u @ tf.diag(si) @ tf.transpose(v)
def backward(obs,m,scope=None): init = np.array([1.]*m*m).astype('float32').reshape(m,m) output = [] output.append(init) obs = tf.split(0,m,obs) obs = [tf.squeeze(obs_) for obs_ in obs] with tf.variable_scope(scope or "Forward") as scope: T = tf.get_variable("Transitions",[m,m], initializer=tf.random_uniform_initializer(-1,1)) #session.run(tf.initialize_all_variables()) for time_step, o_ in enumerate(reversed(obs)): #print(time_step) if time_step > 0: #scope.reuse_variables() init = output[time_step] O = tf.diag(o_) tmp = tf.matmul(O,T) tmp = tf.matmul(tmp,init) tmp = tf.div(tmp,tf.reduce_sum(tmp))*2 #print(tmp.get_shape()) output.append(tmp) return output
def build_predict(self, Xnew, full_cov=False): """ The posterior variance of F is given by q(f) = N(f | K alpha, [K^-1 + diag(lambda**2)]^-1) Here we project this to F*, the values of the GP at Xnew which is given by q(F*) = N ( F* | K_{*F} alpha , K_{**} - K_{*f}[K_{ff} + diag(lambda**-2)]^-1 K_{f*} ) """ #compute kernelly things Kx = self.kern.K(Xnew, self.X) K = self.kern.K(self.X) #predictive mean f_mean = tf.matmul(Kx, self.q_alpha) + self.mean_function(Xnew) #predictive var f_var = [] for d in range(self.num_latent): b = self.q_lambda[:,d] A = K + tf.diag(1./tf.square(b)) L = tf.cholesky(A) LiKx = tf.matrix_triangular_solve(L, tf.transpose(Kx), lower=True) if full_cov: f_var.append( self.kern.K(Xnew)- tf.matmul(tf.transpose(LiKx),LiKx) ) else: f_var.append( self.kern.Kdiag(Xnew) - tf.reduce_sum(tf.square(LiKx),0) ) f_var = tf.pack(f_var) return f_mean, tf.transpose(f_var)
def test_whiten(self): """ make sure that predicting using the whitened representation is the sameas the non-whitened one. """ with self.test_context() as sess: rng = np.random.RandomState(0) Xs, X, F, k, num_data, feed_dict = self.prepare() k.compile(session=sess) F_sqrt = tf.placeholder(settings.float_type, [num_data, 1]) F_sqrt_data = rng.rand(num_data, 1) feed_dict[F_sqrt] = F_sqrt_data K = k.K(X) L = tf.cholesky(K) V = tf.matrix_triangular_solve(L, F, lower=True) V_sqrt = tf.matrix_triangular_solve(L, tf.diag(F_sqrt[:, 0]), lower=True)[None, :, :] Fstar_mean, Fstar_var = gpflow.conditionals.conditional( Xs, X, k, F, q_sqrt=F_sqrt) Fstar_w_mean, Fstar_w_var = gpflow.conditionals.conditional( Xs, X, k, V, q_sqrt=V_sqrt, white=True) mean_difference = sess.run(Fstar_w_mean - Fstar_mean, feed_dict=feed_dict) var_difference = sess.run(Fstar_w_var - Fstar_var, feed_dict=feed_dict) assert_allclose(mean_difference, 0, atol=4) assert_allclose(var_difference, 0, atol=4)
def gate(self, sig): ''' Compute the gating coefficients based on the coordinates of the columns Input: self.coordinates Output: LxLx1 matrix where each row sums to one. The larger the value, the closer you are together. ''' #Step 1: coordinates dot ones transpose -> 3xLxL -> just replicates the 1D vector L times output = tf.batch_matmul(self.coordinates, tf.transpose(tf.ones_like(self.coordinates), perm=[0, 2, 1])) # output = tf.batch_matmul(self.bbb, tf.transpose(tf.ones_like(self.bbb), perm=[0, 2, 1])) #Step 2: minus its tranpose -> 3xLxL -> symmetric difference matrix output = output - tf.transpose(output, perm=[0, 2, 1]) #Step 3: add 1000 to the diagonals, ie dist to itself is 1000 instead of 0 -> so that the weight column x has to predict itself is very low, preferably zero # output = output + tf.reshape(tf.concat(0,[tf.diag(tf.ones([self.L]))*99999, tf.diag(tf.ones([self.L]))*99999, tf.diag(tf.ones([self.L]))*99999]),[self.D,self.L,self.L]) #could use the function tile for the stuff above, or just add to the diagonal of the next stuff #Step 4: square difference and sum over the dimensions -> LxL -> dist of coord to each coord, symmetric output = tf.reduce_sum(tf.square(output),0) #Step 4.1: output = -(sig*output) #Make the diagonal very negative, meaning they are far apart, so it get low gating weight to itself output = output + tf.diag(tf.ones([self.L]))*(-99999) #Step 5: softmax so rows sum to 1 output = tf.nn.softmax(output) #Step 6: reshape so it works later -> LxLx1 output = tf.reshape(output, [self.L, self.L, 1]) return output
def setUp(self): tf.reset_default_graph() self.num_latent = 2 self.num_data = 3 self.k = GPflow.kernels.Matern32(1) + GPflow.kernels.White(1) self.k.white.variance = 0.01 self.X = tf.placeholder(float_type) self.mu = tf.placeholder(float_type) self.Xs = tf.placeholder(float_type) self.sqrt = tf.placeholder(float_type, [self.num_data, self.num_latent]) #make tf array shenanigans self.free_x = tf.placeholder(float_type) self.k.make_tf_array(self.free_x) self.free_x_data = self.k.get_free_state() # NB. with too many random data, numerics suffer self.rng = np.random.RandomState(0) self.X_data = self.rng.randn(self.num_data,1) self.mu_data = self.rng.randn(self.num_data,self.num_latent) self.sqrt_data = self.rng.randn(self.num_data,self.num_latent) self.Xs_data = self.rng.randn(50,1) self.feed_dict = { self.X:self.X_data, self.Xs:self.Xs_data, self.mu:self.mu_data, self.sqrt:self.sqrt_data, self.free_x:self.free_x_data} #the chols are diagonal matrices, with the same entries as the diag representation. self.chol = tf.pack([tf.diag(self.sqrt[:,i]) for i in range(self.num_latent)]) self.chol = tf.transpose(self.chol, perm=[1,2,0])
def dynamic_vae_single(T = 50, d_z = 1, d_hidden=2, d_x = 10): # MODEL transition_mat = np.eye(d_z, dtype=np.float32) #GaussianMatrix(mean=0, std=1.0, output_shape=(D, D), name="transition") transition_bias = np.zeros((d_z,), dtype=np.float32) transition_cov = np.eye(d_z, dtype=np.float32) step_noise = MVGaussianMeanCov(transition_bias, transition_cov) w1, w2, b1, b2 = decoder_params(d_z, d_hidden, d_x) z = LinearGaussian(T, transition_bias, transition_cov, transition_mat, transition_bias, transition_cov, name="z") x = VAEDecoderBernoulli(z, w1, w2, b1, b2, name="x") # SYNTHETIC OBSERVATION x_sampled = x.sample(0) q_x = x.observe(x_sampled) # INFERENCE MODEL upwards_messages = VAEEncoder(q_x.sample, d_hidden, d_z) upwards_means = tf.unpack(upwards_messages.mean) upwards_vars = tf.unpack(upwards_messages.variance) unary_factors = [MVGaussianMeanCov(mean, tf.diag(vs)) for (mean, vs) in zip(upwards_means, upwards_vars)] tmat = tf.constant(transition_mat) q_z = LinearGaussianChainCRF((T, d_z), tmat, step_noise, unary_factors) z.attach_q(q_z) return x, z, x_sampled
def _testDiagOp(self, diag, dtype, expected_ans, use_gpu=False, expected_err_re=None): with self.test_session(use_gpu=use_gpu): tf_ans = tf.diag(tf.convert_to_tensor(diag.astype(dtype))) out = tf_ans.eval() self.assertAllClose(out, expected_ans) self.assertShapeEqual(expected_ans, tf_ans)
def log_prob(self, xs, zs): """Return scalar, the log joint density log p(xs, zs).""" if self.prior == 'Lognormal': log_prior = tf.reduce_sum(lognorm.logpdf(zs['z'], self.prior_std)) elif self.prior == 'Gaussian': log_prior = tf.reduce_sum(norm.logpdf(zs['z'], 0.0, self.prior_std)) else: raise NotImplementedError("prior not available.") z = tf.reshape(zs['z'], [self.N, self.K]) if self.dist == 'euclidean': xp = tf.tile(tf.reduce_sum(tf.pow(z, 2), 1, keep_dims=True), [1, self.N]) xp = xp + tf.transpose(xp) - 2 * tf.matmul(z, z, transpose_b=True) xp = 1.0 / tf.sqrt(xp + tf.diag(tf.zeros(self.N) + 1e3)) elif self.dist == 'cosine': xp = tf.matmul(z, z, transpose_b=True) if self.like == 'Gaussian': log_lik = tf.reduce_sum(norm.logpdf(xs['x'], xp, 1.0)) elif self.like == 'Poisson': if not (self.dist == 'euclidean' or self.prior == "Lognormal"): raise NotImplementedError("Rate of Poisson has to be nonnegatve.") log_lik = tf.reduce_sum(poisson.logpmf(xs['x'], xp)) else: raise NotImplementedError("likelihood not available.") return log_lik + log_prior
def _test_logpdf_scalar(scalar): x = tf.constant(scalar) val_true = stats.norm.logpdf(scalar) _assert_eq(norm.logpdf(x), val_true) _assert_eq(norm.logpdf(x, tf.zeros([1]), tf.constant(1.0)), val_true) _assert_eq(norm.logpdf(x, tf.zeros([1]), tf.ones([1])), val_true) _assert_eq(norm.logpdf(x, tf.zeros([1]), tf.diag(tf.ones([1]))), val_true)
def K(self, X, X2=None): if X2 is None: d = tf.fill(tf.pack([tf.shape(X)[0]]), tf.squeeze(self.variance)) return tf.diag(d) else: shape = tf.pack([tf.shape(X)[0], tf.shape(X2)[0]]) return tf.zeros(shape, tf.float64)
def multilayer_perceptron(_X_aud, _X_img, _w_aud, _b_aud, _w_img, _b_img, _w_out, _b_out, _dropout, _b_size): #aud aud_layer_1 = tf.nn.relu(tf.add(tf.matmul(_X_aud, _w_aud['h1']), _b_aud['b1'])) #Hidden layer with RELU activation aud_layer_2 = tf.nn.relu(tf.add(tf.matmul(aud_layer_1, _w_aud['h2']), _b_aud['b2'])) #Hidden layer with RELU activation #aud_out = tf.matmul(aud_layer_2, _w_aud['out']) + _b_aud['out'] #Image img_layer_1 = tf.nn.relu(tf.add(tf.matmul(_X_img, _w_img['h1']), _b_img['b1'])) #Hidden layer with RELU activation drop_1 = tf.nn.dropout(img_layer_1, _dropout) img_layer_2 = tf.nn.relu(tf.add(tf.matmul(drop_1, _w_img['h2']), _b_img['b2'])) #Hidden layer with RELU activation drop_2 = tf.nn.dropout(img_layer_2, _dropout) #img_out = tf.matmul(drop_2, _w_img['out']) + _b_img['out'] ''' Merge with CA ''' factor = calculatCA(aud_layer_2, drop_2, 600, _b_size) factor = tf.reshape(tf.diag(factor), shape=[_b_size, _b_size]) merge_sum = tf.add(aud_layer_2, drop_2) facmat = tf.nn.relu(tf.matmul(factor, merge_sum)) #out_drop = tf.nn.dropout(merge_sum, _dropout) out_layer_1 = tf.nn.relu(tf.add(tf.matmul(facmat, _w_out['h1']), _b_out['b1'])) #Hidden layer with RELU activation out_layer_2 = tf.nn.relu(tf.add(tf.matmul(out_layer_1, _w_out['h2']), _b_out['b2'])) #Hidden layer with RELU activation #return out_drop return tf.matmul(out_layer_2, _w_out['out']) + _b_out['out']
def buildGraph(self): self.graph = tf.Graph() with self.graph.as_default(): # train_input , [batch_size * embed_size] 一个batch有多条 self.train_input = tf.placeholder(tf.float32,shape=[self.batch_size,self.embed_size],name='train_input') self.train_label = tf.placeholder(tf.int32,shape=[self.batch_size],name='train_label') label_float = tf.cast(self.train_label,tf.float32) # label_matrix = tf.Variable(tf.diag(tf.ones(self.label_size)),trainable=False) label_matrix = tf.diag(tf.ones(self.label_size)) embed_label = tf.nn.embedding_lookup(label_matrix,self.train_label) hidden_unit = 50 self.weight = tf.Variable(tf.truncated_normal(shape=[hidden_unit,self.embed_size],stddev=1.0/math.sqrt(self.embed_size))) self.biase = tf.Variable(tf.zeros([hidden_unit])) y1 = tf.matmul(self.train_input,self.weight,transpose_b=True) + self.biase g1 = tf.nn.sigmoid(y1) # batch_size * label_size weight2 = tf.Variable(tf.truncated_normal(shape=[self.label_size,hidden_unit],stddev=1.0/math.sqrt(hidden_unit))) biase2 = tf.Variable(tf.zeros([self.label_size])) y2 = tf.matmul(g1,weight2,transpose_b=True) + biase2 g2 = tf.nn.sigmoid(y2) self.predict = tf.cast(tf.argmax(g2,axis=1),tf.float32) self.error_num = tf.count_nonzero(label_float-self.predict) self.loss = tf.reduce_mean(-tf.reduce_sum(embed_label*tf.log(g2+0.0001)+(1-embed_label)*tf.log(1+0.0001-g2),axis=1)) # self.train_op = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(self.loss) self.train_op = tf.train.AdagradOptimizer(learning_rate=1).minimize(self.loss) self.init_op = tf.global_variables_initializer()
def getStatsEigen(self, stats=None): if len(self.stats_eigen) == 0: stats_eigen = {} if stats is None: stats = self.stats tmpEigenCache = {} with tf.device('/cpu:0'): for var in stats: for key in ['fprop_concat_stats', 'bprop_concat_stats']: for stats_var in stats[var][key]: if stats_var not in tmpEigenCache: stats_dim = stats_var.get_shape()[1].value e = tf.Variable(tf.ones( [stats_dim]), name='KFAC_FAC/' + stats_var.name.split(':')[0] + '/e', trainable=False) Q = tf.Variable(tf.diag(tf.ones( [stats_dim])), name='KFAC_FAC/' + stats_var.name.split(':')[0] + '/Q', trainable=False) stats_eigen[stats_var] = {'e': e, 'Q': Q} tmpEigenCache[ stats_var] = stats_eigen[stats_var] else: stats_eigen[stats_var] = tmpEigenCache[ stats_var] self.stats_eigen = stats_eigen return self.stats_eigen
def _test_logpdf_scalar(x): xtf = tf.constant(x) val_true = stats.norm.logpdf(x) _assert_eq(norm.logpdf(xtf), val_true) _assert_eq(norm.logpdf(xtf, tf.zeros([1]), tf.constant(1.0)), val_true) _assert_eq(norm.logpdf(xtf, tf.zeros([1]), tf.ones([1])), val_true) _assert_eq(norm.logpdf(xtf, tf.zeros([1]), tf.diag(tf.ones([1]))), val_true)
def _define_distance_to_clusters(self, data): """Defines the Mahalanobis distance to the assigned Gaussian.""" # TODO(xavigonzalvo): reuse (input - mean) * cov^-1 * (input - # mean) from log probability function. self._all_scores = [] for shard in data: all_scores = [] shard = tf.expand_dims(shard, 0) for c in xrange(self._num_classes): if self._covariance_type == FULL_COVARIANCE: cov = self._covs[c, :, :] elif self._covariance_type == DIAG_COVARIANCE: cov = tf.diag(self._covs[c, :]) inverse = tf.matrix_inverse(cov + self._min_var) inv_cov = tf.tile( tf.expand_dims(inverse, 0), tf.pack([self._num_examples, 1, 1])) diff = tf.transpose(shard - self._means[c, :, :], perm=[1, 0, 2]) m_left = tf.batch_matmul(diff, inv_cov) all_scores.append(tf.sqrt(tf.batch_matmul( m_left, tf.transpose(diff, perm=[0, 2, 1]) ))) self._all_scores.append(tf.reshape( tf.concat(1, all_scores), tf.pack([self._num_examples, self._num_classes]))) # Distance to the associated class. self._all_scores = tf.concat(0, self._all_scores) assignments = tf.concat(0, self.assignments()) rows = tf.to_int64(tf.range(0, self._num_examples)) indices = tf.concat(1, [tf.expand_dims(rows, 1), tf.expand_dims(assignments, 1)]) self._scores = tf.gather_nd(self._all_scores, indices)
def test_dot(self): with self.test_session(): a = tf.ones([5]) * np.arange(5) b = tf.diag(tf.ones([5])) self.assertAllEqual(dot(a, b).eval(), a.eval()[np.newaxis].dot(b.eval())) self.assertAllEqual(dot(b, a).eval(), b.eval().dot(a.eval()[:, np.newaxis]))
def target_subspace(adj, groupsize, communities, diag, dim_proj): normalizer = tf.cast(2.0*groupsize*communities, dtype=tf.float64) total_degree = tf.cast(tf.reduce_sum(adj), dtype=tf.float64) r = tf.sqrt(total_degree/normalizer) BH_op = (tf.square(r)-1)*tf.diag(tf.ones(shape=[communities*groupsize], dtype=tf.float64))-r*adj+diag val, vec = tf.self_adjoint_eig(BH_op) #this is already normalized so no need to normalize subspace = tf.slice(vec, [0,0], [communities*groupsize, dim_proj]) return r, subspace
def test_multivariate_normal_tril(self): with self.test_session() as sess: N, D, w_true, X_train, y_train, X, w, b, y = self._setup() # INFERENCE. Initialize scales at identity to verify if we # learned an approximately zero determinant. qw = MultivariateNormalTriL( loc=tf.Variable(tf.random_normal([D])), scale_tril=tf.Variable(tf.diag(tf.ones(D)))) qb = MultivariateNormalTriL( loc=tf.Variable(tf.random_normal([1])), scale_tril=tf.Variable(tf.diag(tf.ones(1)))) inference = ed.Laplace({w: qw, b: qb}, data={X: X_train, y: y_train}) inference.run(n_iter=100) self._test(sess, qw, qb, w_true)
def get(self): return tf.diag(self.d) + tf.matmul(self.W, tf.transpose(self.W))
def build(self, features1, features2, features3, face_feature_size, attention_feature_size, scene_feature_size, hidden_size, num_classes, num_steps, num_face_nodes, num_attention_nodes, edge_features_length, use_bias, keep_prob=0.5, layer_num=1): #Add an extract fully connected layer to shrink the size of features self.face_weights = tf.Variable(glorot_init( [face_feature_size, hidden_size]), name='face_weights') self.face_biases = tf.Variable(np.zeros([hidden_size ]).astype(np.float32), name='face_biases') self.attention_weights = tf.Variable(glorot_init( [attention_feature_size, hidden_size]), name='attention_weights') self.attention_biases = tf.Variable(np.zeros([hidden_size ]).astype(np.float32), name='attention_biases') self.scene_weights = tf.Variable(glorot_init( [scene_feature_size, hidden_size]), name='scene_weights') self.scene_biases = tf.Variable(np.zeros([hidden_size ]).astype(np.float32), name='scene_biases') self.face_features = tf.nn.relu( tf.nn.bias_add(tf.matmul(features1, self.face_weights), self.face_biases)) self.attention_features = tf.nn.relu( tf.nn.bias_add(tf.matmul(features2, self.attention_weights), self.attention_biases)) self.scene_features = tf.nn.relu( tf.nn.bias_add(tf.matmul(features3, self.scene_weights), self.scene_biases)) #define LSTM with tf.variable_scope("lstm_scope"): self.cell = tf.contrib.rnn.GRUCell(hidden_size) self.cell = tf.contrib.rnn.DropoutWrapper( self.cell, output_keep_prob=keep_prob) #self.mlstm_cell = tf.contrib.rnn.MultiRNNCell([self.cell for _ in range(layer_num)]) #define edge weights, edge bias, and mask used to take average of edge features. self.face_edge_weights = tf.Variable(glorot_init( [hidden_size, edge_features_length]), name='face_edge_weights') self.face_edge_biases = tf.Variable(np.zeros([edge_features_length ]).astype(np.float32), name='face_edge_biases') self.attention_edge_weights = tf.Variable( glorot_init([hidden_size, edge_features_length]), name='attention_edge_weights') self.attention_edge_biases = tf.Variable(np.zeros( [edge_features_length]).astype(np.float32), name='attention_edge_biases') self.scene_edge_weights = tf.Variable(glorot_init( [hidden_size, edge_features_length]), name='scene_edge_weights') self.scene_edge_biases = tf.Variable(np.zeros([edge_features_length ]).astype(np.float32), name='scene_edge_biases') with tf.variable_scope("lstm_scope") as scope: mask = tf.ones([ num_face_nodes + num_attention_nodes + 1, num_face_nodes + num_attention_nodes + 1 ]) - tf.diag(tf.ones([num_face_nodes + num_attention_nodes + 1])) for step in range(num_steps): if step > 0: tf.get_variable_scope().reuse_variables() else: self.state = tf.cond( tf.equal(num_face_nodes, 0), lambda: tf.concat( [self.attention_features, self.scene_features], axis=0), lambda: tf.concat([ self.face_features, self.attention_features, self.scene_features ], axis=0)) m_face = tf.matmul( self.state[:num_face_nodes], tf.nn.dropout(self.face_edge_weights, keep_prob=keep_prob)) m_attention = tf.matmul( self.state[num_face_nodes:num_face_nodes + num_attention_nodes], tf.nn.dropout(self.attention_edge_weights, keep_prob=keep_prob)) m_scene = tf.matmul( self.state[num_face_nodes + num_attention_nodes:], tf.nn.dropout(self.scene_edge_weights, keep_prob=keep_prob)) if use_bias: m_face = tf.nn.bias_add(m_face, self.face_edge_biases) m_face = tf.nn.bias_add(m_attention, self.attention_edge_biases) m_scene = tf.nn.bias_add(m_scene, self.scene_edge_biases) m_combine = tf.concat([m_face, m_attention, m_scene], axis=0) acts = tf.multiply( tf.matmul(mask, m_combine), 1 / (tf.cast(num_face_nodes + num_attention_nodes + 1, tf.float32) - 1)) self.rnnoutput, self.state = self.cell(acts, self.state) with tf.variable_scope('softmax'): W = tf.get_variable('W', [hidden_size, num_classes]) b = tf.get_variable('b', [num_classes], initializer=tf.constant_initializer(0.0)) self.logits = tf.matmul(self.rnnoutput, W) + b self.probs = tf.nn.softmax(self.logits) self.data_dict = None print("build model finished")
def get_stats(self, factors, varlist): """ return the stats values from the factors to update and the parameters :param factors: ([TensorFlow Tensor]) The factors to update :param varlist: ([TensorFlow Tensor]) The parameters :return: ([TensorFlow Tensor]) The stats values """ if len(self.stats) == 0: # initialize stats variables on CPU because eigen decomp is # computed on CPU with tf.device('/cpu'): tmp_stats_cache = {} # search for tensor factors and # use block diag approx for the bias units for var in varlist: bprop_factor = factors[var]['bpropFactors_concat'] op_type = factors[var]['opName'] if op_type == 'Conv2D': operator_height = bprop_factor.get_shape()[1] operator_width = bprop_factor.get_shape()[2] if operator_height == 1 and operator_width == 1 and self._channel_fac: # factorization along the channels do not support # homogeneous coordinate var_assn_bias = factors[var]['assnBias'] if var_assn_bias: factors[var]['assnBias'] = None factors[var_assn_bias]['assnWeights'] = None for var in varlist: fprop_factor = factors[var]['fpropFactors_concat'] bprop_factor = factors[var]['bpropFactors_concat'] op_type = factors[var]['opName'] self.stats[var] = {'opName': op_type, 'fprop_concat_stats': [], 'bprop_concat_stats': [], 'assnWeights': factors[var]['assnWeights'], 'assnBias': factors[var]['assnBias'], } if fprop_factor is not None: if fprop_factor not in tmp_stats_cache: if op_type == 'Conv2D': kernel_height = var.get_shape()[0] kernel_width = var.get_shape()[1] n_channels = fprop_factor.get_shape()[-1] operator_height = bprop_factor.get_shape()[1] operator_width = bprop_factor.get_shape()[2] if operator_height == 1 and operator_width == 1 and self._channel_fac: # factorization along the channels # assume independence between input channels and spatial # 2K-1 x 2K-1 covariance matrix and C x C covariance matrix # factorization along the channels do not # support homogeneous coordinate, assnBias # is always None fprop_factor2_size = kernel_height * kernel_width slot_fprop_factor_stats2 = tf.Variable(tf.diag(tf.ones( [fprop_factor2_size])) * self._diag_init_coeff, name='KFAC_STATS/' + fprop_factor.op.name, trainable=False) self.stats[var]['fprop_concat_stats'].append( slot_fprop_factor_stats2) fprop_factor_size = n_channels else: # 2K-1 x 2K-1 x C x C covariance matrix # assume BHWC fprop_factor_size = kernel_height * kernel_width * n_channels else: # D x D covariance matrix fprop_factor_size = fprop_factor.get_shape()[-1] # use homogeneous coordinate if not self._blockdiag_bias and self.stats[var]['assnBias']: fprop_factor_size += 1 slot_fprop_factor_stats = tf.Variable( tf.diag(tf.ones([fprop_factor_size])) * self._diag_init_coeff, name='KFAC_STATS/' + fprop_factor.op.name, trainable=False) self.stats[var]['fprop_concat_stats'].append( slot_fprop_factor_stats) if op_type != 'Conv2D': tmp_stats_cache[fprop_factor] = self.stats[ var]['fprop_concat_stats'] else: self.stats[var][ 'fprop_concat_stats'] = tmp_stats_cache[fprop_factor] if bprop_factor is not None: # no need to collect backward stats for bias vectors if # using homogeneous coordinates if not ((not self._blockdiag_bias) and self.stats[var]['assnWeights']): if bprop_factor not in tmp_stats_cache: slot_bprop_factor_stats = tf.Variable(tf.diag(tf.ones([bprop_factor.get_shape( )[-1]])) * self._diag_init_coeff, name='KFAC_STATS/' + bprop_factor.op.name, trainable=False) self.stats[var]['bprop_concat_stats'].append( slot_bprop_factor_stats) tmp_stats_cache[bprop_factor] = self.stats[ var]['bprop_concat_stats'] else: self.stats[var][ 'bprop_concat_stats'] = tmp_stats_cache[bprop_factor] return self.stats
# Load the data boston = load_boston() # header = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV'] # used_h = ['CRIM', 'INDUS', 'NOX', 'RM', 'AGE', 'DIS', 'TAX', 'PTRATIO', 'B', 'LSTAT'] x_vals = np.delete(boston.data, [1, 3, 8, 13], axis = 1) num_features = x_vals.shape[1] y_vals = np.expand_dims(boston.target, axis = 1) ## Min-Max Scaling x_vals = (x_vals - x_vals.min(axis = 0)) / x_vals.ptp(axis = 0) ## Create distance metric weight matrix weighted by standard deviation # 4. 创建对角权重矩阵,矩阵提供了归一化的距离试题,其值为特征的标准差 weight_diagonal = x_vals.std(axis = 0) weight_matrix = tf.cast(tf.diag(weight_diagonal), dtype = tf.float32) # Split the data into train and test sets train_indices = np.random.choice(len(x_vals), int(round(len(x_vals) * 0.8)), replace = False) test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices))) x_vals_train = x_vals[train_indices] x_vals_test = x_vals[test_indices] y_vals_train = y_vals[train_indices] y_vals_test = y_vals[test_indices] # Declare k-value and batch size k = 4 batch_size = len(x_vals_test) # Placeholders x_data_train = tf.placeholder(shape = [None, num_features], dtype = tf.float32)
def __init__(self, params): """ 构建Graph,不需要sess 其中train时候需要提供的、读取的变量,都需要通过添加self.变成attributes来获取 :param params: """ # --------- PLACEHOLDER -------- # <n hot> because there may be multiple ones self.tag_n_hot = tf.placeholder(tf.int32, shape=[None, params["tag_vocab_size"]], name="tag_n_hot") self.gender_n_hot = tf.placeholder(tf.int32, shape=[None, params["gender_vocab_size"]], name="gender_n_hot") self.city_n_hot = tf.placeholder(tf.int32, shape=[None, params["city_vocab_size"]], name="city_n_hot") self.country_n_hot = tf.placeholder(tf.int32, shape=[None, params["country_vocab_size"]], name="country_n_hot") self.word_n_hot = tf.placeholder(tf.int32, shape=[None, params["word_vocab_size"]], name="word_n_hot") self.friend_n_hot = tf.placeholder(tf.int32, shape=[None, params["friend_vocab_size"]], name="friend_n_hot") self.y_true = tf.convert_to_tensor(tf.placeholder(tf.float32, shape=[None, params["num_classes"]], name="y_true")) # ------------ GRAPH - embeddings -------- tag_embedding_table = tf.Variable( tf.random_uniform([params["tag_vocab_size"], params["tag_emb_size"]], -1.0, 1.0), name="tag_embedding_table" ) # same size as tag_n_hot, so if input is 0, corresponding embed is [0,0,0...] tag_embed = tf.nn.embedding_lookup(tag_embedding_table, self.tag_n_hot) gender_embedding_table = tf.Variable( tf.random_uniform([params["gender_vocab_size"], params["gender_emb_size"]], -1.0, 1.0), name="gender_embedding_table" ) gender_embed = tf.nn.embedding_lookup(gender_embedding_table, self.gender_n_hot) city_embedding_table = tf.Variable( tf.random_uniform([params["city_vocab_size"], params["city_emb_size"]], -1.0, 1.0), name="city_embedding_table" ) city_embed = tf.nn.embedding_lookup(city_embedding_table, self.city_n_hot) country_embedding_table = tf.Variable( tf.random_uniform([params["country_vocab_size"], params["country_emb_size"]], -1.0, 1.0), name="country_embedding_table" ) country_embed = tf.nn.embedding_lookup(country_embedding_table, self.country_n_hot) # init with w2v? word_embedding_table = tf.Variable( tf.random_uniform([params["word_vocab_size"], params["word_emb_size"]], -1.0, 1.0), name="word_embedding_table" ) word_embed = tf.nn.embedding_lookup(word_embedding_table, self.word_n_hot) friend_embedding_table = tf.Variable( tf.random_uniform([params["friend_vocab_size"], params["friend_emb_size"]], -1.0, 1.0), name="friend_embedding_table" ) friend_embed = tf.nn.embedding_lookup(friend_embedding_table, self.friend_n_hot) # flatten feature embeddings tag_embed_flat = tf.reshape(tag_embed, [-1, params["tag_vocab_size"] * params["tag_emb_size"]]) gender_embed_flat = tf.reshape(gender_embed, [-1, params["gender_vocab_size"] * params["gender_emb_size"]]) city_embed_flat = tf.reshape(city_embed, [-1, params["city_vocab_size"] * params["city_emb_size"]]) country_embed_flat = tf.reshape(country_embed, [-1, params["country_vocab_size"] * params["country_emb_size"]]) word_embed_flat = tf.reshape(word_embed, [-1, params["word_vocab_size"] * params["word_emb_size"]]) friend_embed_flat = tf.reshape(friend_embed, [-1, params["friend_vocab_size"] * params["friend_emb_size"]]) # concat embeddings all_feature_embed = tf.concat([tag_embed_flat, gender_embed_flat, city_embed_flat, country_embed_flat, word_embed_flat, friend_embed_flat], axis=1) all_feature_map = tf.reshape(all_feature_embed, [-1, params["img_size"], params["img_size"], params["num_channels"]]) all_feature_map = tf.nn.dropout(all_feature_map, params["dropout_rate"]) # ------------ GRAPH - cnn -------- # [?, 64, 64, 32] layer_conv = self.__create_convolutional_layer(input=all_feature_map, num_input_channels=params["num_channels"], conv_filter_size=params["filter_size_conv"], num_filters=params["num_filters_conv"]) # [?, 16*16*32] layer_flat = self.__create_flatten_layer(layer_conv) # [?, 128],使用relu layer_fc1 = self.__create_fc_layer(input=layer_flat, num_inputs=layer_flat.get_shape()[1:4].num_elements(), num_outputs=params["fc_layer_size"], use_relu=True) # [?, 2] 输出层 self.layer_fc2 = self.__create_fc_layer(input=layer_fc1, num_inputs=params["fc_layer_size"], num_outputs=params["num_classes"], use_relu=False) # [?, 2] self.y_pred = tf.nn.sigmoid(self.layer_fc2, name="y_pred") # ---------- EVAL ------------ top_k = tf.nn.top_k(self.y_pred, params["top_k"]) self.label_prediction = top_k.indices # an embedding matrix for later look_up operation onehot_embedding = tf.diag([1.] * params["num_classes"]) # [batch, k, label_size] multiple_oneHot = tf.nn.embedding_lookup( onehot_embedding, self.label_prediction) # [batch, label_size] multihot = tf.reduce_sum(multiple_oneHot, axis=1) correct = tf.reduce_sum(tf.multiply(multihot, self.y_true), 1) all_pos = tf.reduce_sum(self.y_true, axis=1) self.recall_k = tf.divide(correct, all_pos) self.precision_k = tf.divide(correct, params["top_k"]) self.f1_k = tf.divide(2 * tf.multiply(self.precision_k, self.recall_k), tf.add(self.precision_k, self.recall_k)) # ---------- OPTIMIZATION ---------- # forward & softmax + loss cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(logits=self.layer_fc2, labels=self.y_true) self.loss = tf.reduce_mean(cross_entropy) # optimize # self.optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(self.loss) self.optimizer = tf.train.AdamOptimizer(learning_rate=1e-4) gvs = self.optimizer.compute_gradients(self.loss) clipped_gvs = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gvs] self.train_op = self.optimizer.apply_gradients(clipped_gvs)
def _inference_ops(self): if self.n_classes == 1: highest_prob = tf.reduce_max(self.dt_probs_ini, axis=1) else: # we are considering all classes, skip the backgorund class highest_prob = tf.reduce_max(self.dt_probs_ini[:, 1:], axis=1) _, top_ix = tf.nn.top_k(highest_prob, k=self.top_k_hypotheses) pairwise_coords_features = spatial.construct_pairwise_features_tf( self.dt_coords) spatial_features_list = [] n_pairwise_features = 0 iou_feature = spatial.compute_pairwise_spatial_features_iou_tf( pairwise_coords_features) if self.use_iou_features: spatial_features_list.append(iou_feature) n_pairwise_features += 1 pairwise_obj_features = spatial.construct_pairwise_features_tf( self.dt_features_merged) if self.use_object_features: spatial_features_list.append(pairwise_obj_features) n_pairwise_features += self.dt_features_merged.get_shape().as_list( )[1] * 2 score_diff_sign_feature = tf.sign( pairwise_obj_features[:, :, 0:self.n_dt_features] - pairwise_obj_features[:, :, self.n_dt_features:]) score_diff_feature = pairwise_obj_features[:, :, 0:self.n_dt_features] -\ pairwise_obj_features[:, :, self.n_dt_features:] spatial_features_list.append(score_diff_sign_feature) spatial_features_list.append(score_diff_feature) n_pairwise_features += self.dt_features_merged.get_shape().as_list( )[1] * 2 pairwise_features = tf.concat(axis=2, values=spatial_features_list) diagonals = [] for i in range(0, n_pairwise_features): d = tf.expand_dims(tf.diag(tf.diag_part(pairwise_features[:, :, i])), axis=2) diagonals.append(d) diag = tf.concat(axis=2, values=diagonals) pairwise_features = pairwise_features - diag self.pairwise_obj_features = pairwise_features kernel_features = self._kernel(pairwise_features, n_pairwise_features, hlayer_size=self.knet_hlayer_size, n_kernels=self.n_kernels) kernel_features_sigmoid = tf.nn.sigmoid(kernel_features) kernel_max = tf.reshape(tf.reduce_max(kernel_features_sigmoid, axis=1), [self.n_bboxes, self.n_kernels]) kernel_sum = tf.reshape(tf.reduce_sum(kernel_features_sigmoid, axis=1), [self.n_bboxes, self.n_kernels]) object_and_context_features = tf.concat( axis=1, values=[self.dt_features_merged, kernel_max, kernel_sum]) self.object_and_context_features = object_and_context_features fc1 = slim.layers.fully_connected(object_and_context_features, self.fc_apres_layer_size, activation_fn=tf.nn.relu) fc2 = slim.layers.fully_connected(fc1, self.fc_apres_layer_size, activation_fn=tf.nn.relu) fc2_drop = tf.nn.dropout(fc2, self.keep_prob) logits = slim.fully_connected(fc2_drop, self.n_classes, activation_fn=None) class_scores = tf.nn.sigmoid(logits) return iou_feature, logits, class_scores
def zca_normalization(features): shape = tf.shape(features) # reshape the features to orderless feature vectors mean_features = tf.reduce_mean(features, axis=[1, 2], keep_dims=True) unbiased_features = tf.reshape(features - mean_features, shape=(shape[0], -1, shape[3])) #unbiased_features = tf.reshape(features, shape=(shape[0], -1, shape[3])) # get the covariance matrix gram = tf.matmul(unbiased_features, unbiased_features, transpose_a=True) gram /= tf.reduce_prod(tf.cast(shape[1:3], tf.float32)) # converting the feature spaces s, u, v = tf.svd(gram, compute_uv=True) #s = tf.expand_dims(s, axis=1) # let it be active in the last dimension # get the effective singular values ''' valid_index = tf.cast(s > 0.00001, dtype=tf.float32) s_effective = tf.maximum(s, 0.00001) sqrt_s_effective = tf.sqrt(s_effective) * valid_index sqrt_inv_s_effective = tf.sqrt(1.0/s_effective) * valid_index''' #k = tf.reduce_sum(tf.cast(tf.greater(S, 1e-5), tf.int32)) # noise sh = tf.shape(u) noise = tf.random_normal(shape=(sh[1], sh[2]), mean=0, stddev=1, dtype=tf.float32) s_n, u_n, v_n = tf.svd(noise, compute_uv=True) #s=tf.diag(s) u = tf.reshape(u, shape=(sh[1], sh[2])) u_n = tf.matmul(u_n, u) #u_n = tf.matmul(noise, u) #s=tf.reshape(s,shape=(sh[1],sh[2])) sqrt_s_effective = tf.diag(tf.pow(s, 0.5)) sqrt_s_effective = tf.reshape(sqrt_s_effective, shape=(sh[1], sh[2])) sqrt_inv_s_effective = tf.diag(tf.pow(s, -0.5)) sqrt_inv_s_effective = tf.reshape(sqrt_inv_s_effective, shape=(sh[1], sh[2])) unbiased_features = tf.reshape(unbiased_features, shape=(-1, sh[2])) # colorization functions colorization_kernel_n = tf.matmul(tf.matmul(u_n, sqrt_s_effective), u, transpose_b=True) colorization_kernel = tf.matmul(tf.matmul(u, sqrt_s_effective), u, transpose_b=True) #colorization_kernel_n = tf.random_normal(shape=(tf.shape(colorization_kernel)[0],tf.shape(colorization_kernel)[1]),mean=0,stddev=1,dtype=tf.float32) ratio_n = 0.5 colorization_kernel = ratio_n * colorization_kernel_n + ( 1 - ratio_n) * colorization_kernel colorization_kernel = tf.expand_dims(colorization_kernel, axis=0) # normalized features #normalized_features = tf.matmul(tf.matmul(tf.matmul(u, sqrt_inv_s_effective), u, transpose_b=True), unbiased_features) normalized_features = tf.matmul(unbiased_features, u) normalized_features = tf.matmul(normalized_features, sqrt_inv_s_effective) normalized_features = tf.matmul(normalized_features, u, transpose_b=True) normalized_features = tf.reshape(normalized_features, shape=shape) return normalized_features, colorization_kernel, mean_features
def __init__(self, M, L, A, labels_train, labels_test, labels_val, testing_set_mask, training_set_mask, validation_set_mask, order_chebyshev_row=3, n_conv_feat=32, dropout=0.98, learning_rate=0.01, l2_regu=0.00001, nb_layers=5, idx_gpu='/gpu:1'): """ Neural network architecture. Output classification result for each row of M. Inputs: M: initial matrix with all the known values, A : adjacency matrices, respectively for the age, sex and age and sex graphs, L: laplacian matrices, respectively for the age, sex and age and sex graphs, labels_train, labels_test, labels_val: labels for each set for every subject, training_set_mask, testing_set_mask, validation_set_mask: indexes of subjects that respectively belong to the training, testing and validation sets, order_chebyshev_row: order to use for the Chebyshev polynomials. Default value = 18, n_conv_feat: number of weights to use for the GCNN layer. Default value = 36, l2_regu: coefficient to use in front of the l2 regularization term. Default value = 1, dropout: dropout rate on the GCN output. Default = 0.5, learning_rate: learning rate. Default value = 0.001, nb_layers: number of GCNN layers. Default value: 5 """ self.ord_row = order_chebyshev_row self.n_conv_feat = n_conv_feat with tf.Graph().as_default() as g: tf.logging.set_verbosity(tf.logging.ERROR) self.graph = g tf.set_random_seed(0) with tf.device(idx_gpu): #loading of the laplacians self.L = tf.cast(L, 'float32') self.norm_L = self.L - tf.diag(tf.ones([ L.shape[0], ])) #compute all chebyshev polynomials a priori self.list_row_cheb_pol = list() self.compute_cheb_polynomials(self.norm_L, self.ord_row, self.list_row_cheb_pol) #definition of constant matrices self.A = tf.constant(A, dtype=tf.float32) self.M = tf.constant(M, dtype=tf.float32) self.labels_train = tf.constant(labels_train, dtype=tf.float32) self.labels_test = tf.constant(labels_test, dtype=tf.float32) self.labels_val = tf.constant(labels_val, dtype=tf.float32) self.testing_mask = tf.constant(testing_set_mask, dtype=tf.float32) self.training_mask = tf.constant(training_set_mask, dtype=tf.float32) self.validation_mask = tf.constant(validation_set_mask, dtype=tf.float32) ##################################definition of the NN variables##################################### #definition of the weights for extracting the global features weights = [] bias = [] if nb_layers == 1: weights.append( tf.get_variable( 'weights_W', shape=[ self.ord_row * M.shape[1], self.n_conv_feat ], initializer=tf.contrib.layers.xavier_initializer()) ) #self.n_conv_feat bias.append(tf.Variable(tf.zeros([ self.n_conv_feat, ]))) weights.append( tf.get_variable("weight_final", shape=[self.n_conv_feat, 1], initializer=tf.contrib.layers. xavier_initializer())) bias.append(tf.Variable(tf.zeros([ 1, ]))) else: weights.append( tf.get_variable( 'weights_W', shape=[ self.ord_row * M.shape[1], self.n_conv_feat ], initializer=tf.contrib.layers.xavier_initializer()) ) #self.n_conv_feat bias.append(tf.Variable(tf.zeros([ self.n_conv_feat, ]))) for i in range(nb_layers - 1): weights.append( tf.get_variable( 'weights_%d' % i, shape=[ self.ord_row * self.n_conv_feat, self.n_conv_feat ], initializer=tf.contrib.layers. xavier_initializer())) #self.n_conv_feat bias.append(tf.Variable(tf.zeros([ self.n_conv_feat, ]))) weights.append( tf.get_variable("weight_final", shape=[self.n_conv_feat, 1], initializer=tf.contrib.layers. xavier_initializer())) bias.append(tf.Variable(tf.zeros([ 1, ]))) # GCNN architecture TRAINING if nb_layers == 1: self.final_feat_users = self.mono_conv_cheby( self.list_row_cheb_pol, self.ord_row, self.M, weights[0], bias[0]) self.final_feat_users = tf.nn.relu(self.final_feat_users) self.final_feat_users = tf.nn.dropout( self.final_feat_users, dropout) self.final_feat_users = tf.matmul(self.final_feat_users, weights[-1]) + bias[-1] else: self.final_feat_users = self.mono_conv_cheby( self.list_row_cheb_pol, self.ord_row, self.M, weights[0], bias[0]) #shape 3000*32 self.final_feat_users = tf.nn.relu(self.final_feat_users) self.final_feat_users = tf.nn.dropout( self.final_feat_users, dropout) for i in range(nb_layers - 1): self.final_feat_users = self.mono_conv_cheby( self.list_row_cheb_pol, self.ord_row, self.final_feat_users, weights[i + 1], bias[i + 1]) self.final_feat_users = tf.nn.relu( self.final_feat_users) self.final_feat_users = tf.nn.dropout( self.final_feat_users, dropout) self.final_feat_users = tf.matmul(self.final_feat_users, weights[-1]) + bias[-1] self.classification = tf.sigmoid(self.final_feat_users) #########loss definition #computation of the accuracy term self.classification_train = tf.multiply( self.training_mask, self.classification) self.classification_test = tf.multiply(self.testing_mask, self.classification) self.classification_val = tf.multiply(self.validation_mask, self.classification) self.binary_entropy = tf.losses.sigmoid_cross_entropy( multi_class_labels=self.labels_train, logits=self.classification_train) self.l2 = 0 for i in range(len(weights)): self.l2 += tf.nn.l2_loss(weights[i]) #training loss definition self.loss = self.binary_entropy + l2_regu * self.l2 # GCNN architecture TESTING self.binary_entropy_test = tf.losses.sigmoid_cross_entropy( multi_class_labels=self.labels_test, logits=self.classification_test) self.predictions_error = self.binary_entropy_test #definition of the solver self.optimizer = tf.train.AdamOptimizer( learning_rate=learning_rate).minimize(self.loss) self.var_grad = tf.gradients(self.loss, tf.trainable_variables()) self.norm_grad = self.frobenius_norm_square( tf.concat([tf.reshape(g, [-1]) for g in self.var_grad], 0)) # Create a session for running Ops on the Graph. config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True self.session = tf.Session(config=config) # Run the Op to initialize the variables. init = tf.initialize_all_variables() self.session.run(init)
def forward_backward(self, y): """ runs forward backward algorithm on state probabilities y Arguments --------- y : np.array : shape (T, K) where T is number of timesteps and K is the number of states Returns ------- (posterior, forward, backward) posterior : list of length T of tensorflow graph nodes representing the posterior probability of each state at each time step forward : list of length T of tensorflow graph nodes representing the forward probability of each state at each time step backward : list of length T of tensorflow graph nodes representing the backward probability of each state at each time step """ # set up nT = y.shape[0] posterior = np.zeros((nT, self.K)) forward = [] backward = np.zeros((nT + 1, self.K)) # forward pass forward.append( tf.ones((1, self.K), dtype=tf.float64) * (1.0 / self.K) ) for t in range(nT): # NOTE: np.matrix expands forward[t, :] into 2d and causes * to be # matrix multiplies instead of element wise that an array would be tmp = tf.mul( tf.matmul(forward[t], self.P), y[t] ) forward.append(tmp / tf.reduce_sum(tmp)) # backward pass backward = [None] * (nT + 1) backward[-1] = tf.ones((1, self.K), dtype=tf.float64) * (1.0 / self.K) for t in range(nT, 0, -1): tmp = tf.transpose( tf.matmul( tf.matmul(self.P, tf.diag(y[t - 1])), tf.transpose(backward[t]) ) ) backward[t - 1] = tmp / tf.reduce_sum(tmp) # remove initial/final probabilities forward = forward[1:] backward = backward[:-1] # combine and normalize posterior = [f * b for f, b in zip(forward, backward)] posterior = [p / tf.reduce_sum(p) for p in posterior] return posterior, forward, backward
import tensorflow as tf import numpy as np import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import matplotlib.image as img import matplotlib.pyplot as plt sess = tf.Session() diag = tf.diag([1, 1, 1, 1]) truncated = tf.truncated_normal([2, 3]) fill = tf.fill([2, 3], 5.0) uniform = tf.random_uniform([3, 2]) convert_tensor = tf.convert_to_tensor( np.array([[1., 2., 3.], [-3., -7., -1.], [0., 5., -2.]])) truncatedTwo = tf.truncated_normal([3, 4], mean=0.0, stddev=1.0) input_data = tf.constant([[1., 2., 3.], [1., 5., 3.], [1., 2., 7.], [6., 2., 3.], [8., 2., 3.]]) shuffle = tf.random_shuffle(input_data) crop = tf.random_crop(input_data, [1, 1]) # 指定尺寸,图片随机裁剪 image = img.imread('./resources/test.jpg') plt.imshow(image) plt.show() reshaped_image = tf.cast(image, tf.float32) size = tf.cast(tf.shape(reshaped_image), tf.int32) height = sess.run(size[0] // 2) width = sess.run(size[1] // 2) distorted_image = tf.random_crop(reshaped_image, [height, width, 3]) plt.imshow(sess.run(tf.cast(distorted_image, tf.uint8)))
Returns: y: the output predict tensor shaped [None, y_i] """ if config.USE_HIDDEN_LAYER == True: with tf.name_scope("hidden_layer"): layer_h1 = tf.add(tf.matmul(X, weights["hidden"]), biases["hidden"]) layer_h1 = tf.nn.relu(layer_h1) with tf.name_scope("out_layer"): y = tf.add(tf.matmul(layer_h1, weights["out"]), biases["out"]) else: with tf.name_scope("linear_layer"): y = tf.add(tf.matmul(X, weights["linear"]), biases["linear"]) return y with tf.name_scope("matrices"): identity_mat = tf.diag(tf.ones(tf.shape(tf.squeeze(Y)), dtype=tf.int32)) y = compute_graph(X) # score diff matrix with shape [doc_count, doc_count] # sigma_ij = matrix of sigma(s_i - s_j) # in default RankNet, sigma = Identity, s_i = f(xi) # note: sigma_ij is the logit # thus Pij = sigmoid(sigma_ij) sigma_ij = y - tf.transpose(y) # relevance diff matrix with shape [doc_count, doc_count] Sij_ = Y - tf.transpose(Y) # pairwise label matrix Sij = tf.minimum(1.0, tf.maximum(-1.0, Sij_)) # pairwise label probability matrix Pij_hat = 1.0 / 2.0 * (1 + Sij)
def get(self): return tf.diag(self.d)
def test_Diag(self): t = tf.diag(self.random(3, 3)) self.check(t)
26.67541885, 38.08450317, 20.74983215, 34.94445419, 34.45999146, 29.06485367, 36.01657104, 27.88236427, 20.56035233, 30.20379066, 29.51215172, 33.71149445, 28.59134293, 36.05556488, 28.66994858 ]) x_indices = tf.where(x > 30) out = tf.gather(x, x_indices) ############################################################################### # 1e: Create a diagnoal 2-d tensor of size 6 x 6 with the diagonal values of 1, # 2, ..., 6 # Hint: Use tf.range() and tf.diag(). ############################################################################### output = tf.diag(tf.range(1, 7)) ############################################################################### # 1f: Create a random 2-d tensor of size 10 x 10 from any distribution. # Calculate its determinant. # Hint: Look at tf.matrix_determinant(). ############################################################################### random = tf.random_normal([10, 10], mean=10.0, stddev=1.0) out = tf.matrix_determinant(random) ############################################################################### # 1g: Create tensor x with value [5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9]. # Return the unique elements in x # Hint: use tf.unique(). Keep in mind that tf.unique() returns a tuple.
30.97266006, 26.67541885, 38.08450317, 20.74983215, 34.94445419, 34.45999146, 29.06485367, 36.01657104, 27.88236427, 20.56035233, 30.20379066, 29.51215172, 33.71149445, 28.59134293, 36.05556488, 28.66994858]) indices = tf.where(x > 30) y = tf.gather(x, indices) print(sess.run(y)) ############################################################################### # 1e: Create a diagnoal 2-d tensor of size 6 x 6 with the diagonal values of 1, # 2, ..., 6 # Hint: Use tf.range() and tf.diag(). ############################################################################### x = tf.diag(tf.range(1, 7)) print(sess.run(x)) ############################################################################### # 1f: Create a random 2-d tensor of size 10 x 10 from any distribution. # Calculate its determinant. # Hint: Look at tf.matrix_determinant(). ############################################################################### x = tf.random_uniform([10, 10]) dt = tf.matrix_determinant(x) print(sess.run(dt)) ############################################################################### # 1g: Create tensor x with value [5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9]. # Return the unique elements in x
def invertible_1x1_conv(name, x, reverse=False): """1X1 convolution on x. The 1X1 convolution is parametrized as P*L*(U + sign(s)*exp(log(s))) where 1. P is a permutation matrix. 2. L is a lower triangular matrix with diagonal entries unity. 3. U is a upper triangular matrix where the diagonal entries zero. 4. s is a vector. sign(s) and P are fixed and the remaining are optimized. P, L, U and s are initialized by the PLU decomposition of a random rotation matrix. Args: name: scope x: Input Tensor. reverse: whether the pass is from z -> x or x -> z. Returns: x_conv: x after a 1X1 convolution is applied on x. objective: sum(log(s)) """ _, height, width, channels = common_layers.shape_list(x) w_shape = [channels, channels] # Random rotation-matrix Q random_matrix = np.random.rand(channels, channels) np_w = scipy.linalg.qr(random_matrix)[0].astype("float32") # Initialize P,L,U and s from the LU decomposition of a random rotation matrix np_p, np_l, np_u = scipy.linalg.lu(np_w) np_s = np.diag(np_u) np_sign_s = np.sign(np_s) np_log_s = np.log(np.abs(np_s)) np_u = np.triu(np_u, k=1) with tf.variable_scope(name, reuse=tf.AUTO_REUSE): p = tf.get_variable("P", initializer=np_p, trainable=False) l = tf.get_variable("L", initializer=np_l) sign_s = tf.get_variable("sign_S", initializer=np_sign_s, trainable=False) log_s = tf.get_variable("log_S", initializer=np_log_s) u = tf.get_variable("U", initializer=np_u) # W = P * L * (U + sign_s * exp(log_s)) l_mask = np.tril(np.ones([channels, channels], dtype=np.float32), -1) l = l * l_mask + tf.eye(channels, channels) u = u * np.transpose(l_mask) + tf.diag(sign_s * tf.exp(log_s)) w = tf.matmul(p, tf.matmul(l, u)) # If height or width cannot be statically determined then they end up as # tf.int32 tensors, which cannot be directly multiplied with a floating # point tensor without a cast. objective = tf.reduce_sum(log_s) * tf.cast(height * width, log_s.dtype) if not reverse: w = tf.reshape(w, [1, 1] + w_shape) x = tf.nn.conv2d(x, w, [1, 1, 1, 1], "SAME", data_format="NHWC") else: u_inv = tf.matrix_inverse(u) l_inv = tf.matrix_inverse(l) p_inv = tf.matrix_inverse(p) w_inv = tf.matmul(u_inv, tf.matmul(l_inv, p_inv)) w_inv = tf.reshape(w_inv, [1, 1] + w_shape) x = tf.nn.conv2d(x, w_inv, [1, 1, 1, 1], "SAME", data_format="NHWC") objective *= -1 return x, objective
26.67541885, 38.08450317, 20.74983215, 34.94445419, 34.45999146, 29.06485367, 36.01657104, 27.88236427, 20.56035233, 30.20379066, 29.51215172, 33.71149445, 28.59134293, 36.05556488, 28.66994858 ]) indices = tf.where(x > 30) out_1d = tf.gather(x, indices) ############################################################################### # 1e: Create a diagnoal 2-d tensor of size 6 x 6 with the diagonal values of 1, # 2, ..., 6 # Hint: Use tf.range() and tf.diag(). ############################################################################### # YOUR CODE diag = tf.range(1, 7) out_1e = tf.diag(diag) ############################################################################### # 1f: Create a random 2-d tensor of size 10 x 10 from any distribution. # Calculate its determinant. # Hint: Look at tf.matrix_determinant(). ############################################################################### # YOUR CODE random = tf.random_normal([10, 10], mean=10, stddev=1) out_1f = tf.matrix_determinant(random) ############################################################################### # 1g: Create tensor x with value [5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9]. # Return the unique elements in x # Hint: use tf.unique(). Keep in mind that tf.unique() returns a tuple.
import tensorflow as tf import numpy as np import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' with tf.Session() as sess: identity_matrix = tf.diag([1.0, 1.0, 1.0]) A = tf.truncated_normal([2, 3]) B = tf.fill([2, 3], 5.0) C = tf.random_uniform([3, 2], minval=1, maxval=10) D = tf.convert_to_tensor( np.array([[1., 2., 3.], [-3., -7., -1.], [0., 5., -2.]])) print('A =', sess.run(A)) print('B =', sess.run(B)) print('C =', sess.run(C)) print('D =', sess.run(D)) # 矩阵乘法 tf.matmul(B, identity_matrix) # 转置 tf.transpose(C) # print(sess.run(tf.transpose(C))) # 求行列式 tf.matrix_determinant(D) # print(sess.run(tf.matrix_determinant(D))) # 求逆
def _build(self): self.hidden1 = defaultdict(list) for i, j in self.edge_types: self.hidden1[i].append(GraphConvolutionSparseMulti( input_dim=self.input_dim, output_dim=FLAGS.hidden1, edge_type=(i,j), num_types=self.edge_types[i,j], adj_mats=self.adj_mats, nonzero_feat=self.nonzero_feat, act=lambda x: x, dropout=self.dropout, logging=self.logging)(self.inputs[j])) for i, hid1 in self.hidden1.items(): self.hidden1[i] = tf.nn.relu(tf.add_n(hid1)) self.embeddings = defaultdict(list) for i, j in self.edge_types: self.embeddings[i].append(GraphConvolutionMulti( input_dim=FLAGS.hidden1, output_dim=FLAGS.hidden2, edge_type=(i,j), num_types=self.edge_types[i,j], adj_mats=self.adj_mats, act=lambda x: x, dropout=self.dropout, logging=self.logging)(self.hidden1[j])) for i, embeds in self.embeddings.items(): # self.embeddings[i] = tf.nn.relu(tf.add_n(embeds)) self.embeddings[i] = tf.add_n(embeds) self.row_embeds, self.col_embeds = [None]*self.num_row_types, [None]*self.num_col_types for i, j in self.edge_types: self.row_embeds[i] = self.embeddings[i] self.col_embeds[j] = self.embeddings[j] self.edge_type2decoder = {} for i, j in self.edge_types: decoder = self.decoders[i, j] if decoder == 'innerproduct': self.edge_type2decoder[i, j] = InnerProductDecoder( input_dim=FLAGS.hidden2, logging=self.logging, edge_type=(i, j), num_types=self.edge_types[i, j], act=lambda x: x, dropout=self.dropout) elif decoder == 'distmult': self.edge_type2decoder[i, j] = DistMultDecoder( input_dim=FLAGS.hidden2, logging=self.logging, edge_type=(i, j), num_types=self.edge_types[i, j], act=lambda x: x, dropout=self.dropout) elif decoder == 'bilinear': self.edge_type2decoder[i, j] = BilinearDecoder( input_dim=FLAGS.hidden2, logging=self.logging, edge_type=(i, j), num_types=self.edge_types[i, j], act=lambda x: x, dropout=self.dropout) elif decoder == 'dedicom': self.edge_type2decoder[i, j] = DEDICOMDecoder( input_dim=FLAGS.hidden2, logging=self.logging, edge_type=(i, j), num_types=self.edge_types[i, j], act=lambda x: x, dropout=self.dropout) else: raise ValueError('Unknown decoder type') self.latent_inters = [] self.latent_varies = [] for edge_type in self.edge_types: decoder = self.decoders[edge_type] for k in range(self.edge_types[edge_type]): if decoder == 'innerproduct': glb = tf.eye(FLAGS.hidden2, FLAGS.hidden2) loc = tf.eye(FLAGS.hidden2, FLAGS.hidden2) elif decoder == 'distmult': glb = tf.diag(self.edge_type2decoder[edge_type].vars['relation_%d' % k]) loc = tf.eye(FLAGS.hidden2, FLAGS.hidden2) elif decoder == 'bilinear': glb = self.edge_type2decoder[edge_type].vars['relation_%d' % k] loc = tf.eye(FLAGS.hidden2, FLAGS.hidden2) elif decoder == 'dedicom': glb = self.edge_type2decoder[edge_type].vars['global_interaction'] loc = tf.diag(self.edge_type2decoder[edge_type].vars['local_variation_%d' % k]) else: raise ValueError('Unknown decoder type') self.latent_inters.append(glb) self.latent_varies.append(loc)
def appr_seminmf(M, r): """ Approximate Semi-NMF factorisation. Parameters ---------- M: array-like, shape=(n_features, n_samples) r: number of components to keep during factorisation """ S, A, B = tf.svd(M, full_matrices=False) S = tf.diag(S[0:tf.constant(r - 1)]) A = tf.matmul(A[:, 0:tf.constant(r - 1)], S) B = tf.transpose(B)[0:tf.constant(r - 1), :] m, n = M.get_shape().as_list() ii = tf.constant(0) AA = A[:, ii] BB = B[ii, :] Atemp = tf.cond( tf.less(tf.reduce_min(B[ii, :]), tf.reduce_min(tf.negative(B[ii, :]))), lambda: tf.reshape(tf.negative(AA), [m, 1]), lambda: tf.reshape(AA, [m, 1])) Btemp = tf.cond( tf.less(tf.reduce_min(B[ii, :]), tf.reduce_min(tf.negative(B[ii, :]))), lambda: tf.reshape(tf.negative(BB), [1, n]), lambda: tf.reshape(BB, [1, n])) if r > 2: for i in range(1, r - 1): ii = tf.constant(i) AA = tf.reshape(A[:, ii], [m, 1]) BB = tf.reshape(B[ii, :], [1, n]) Atemp = tf.cond( tf.less(tf.reduce_min(B[ii, :]), tf.reduce_min(tf.negative(B[ii, :]))), lambda: tf.concat([Atemp, tf.negative(AA)], axis=1), lambda: tf.concat([Atemp, AA], axis=1)) Btemp = tf.cond( tf.less(tf.reduce_min(B[ii, :]), tf.reduce_min(tf.negative(B[ii, :]))), lambda: tf.concat([Btemp, tf.negative(BB)], axis=0), lambda: tf.concat([Btemp, BB], axis=0)) if r == 2: U = tf.concat([A, tf.negative(A)], axis=1) else: An = tf.reshape(tf.transpose(tf.negative(tf.reduce_sum(A, 1))), [m, 1]) U = tf.concat([A, An], 1) V = tf.concat([B, tf.zeros((1, n))], 0) if r >= 3: V = tf.subtract(V, tf.minimum(0.0, tf.reduce_min(B, 0))) else: V = tf.subtract(V, tf.minimum(0.0, B)) norm_const = tf.sqrt(tf.cast(tf.multiply(m, n), tf.float32)) norm = tf.norm(U) return tf.multiply(tf.divide(U, norm), norm_const), tf.divide(tf.multiply(V, norm), norm_const)
def K_labs_initialiser(shape, partition_info=None, dtype=None): # initialise lengths to be 0.01 for vitals and 5 for blood tests output = np.ones(shape[0]) output[:7] = 0.01 return tf.diag(tf.convert_to_tensor(output, dtype=tf.float32))
def gate(): L_by_L = tf.batch_matmul(column_2D_positions, tf.transpose(tf.ones_like(column_2D_positions), perm=[0, 2, 1])) #for the next line, if D changes need to chnage number of: tf.diag(tf.ones([L]))*100, one for every dimension dif_matrix = L_by_L - tf.transpose(L_by_L, perm=[0, 2, 1]) + tf.reshape(tf.concat(0,[tf.diag(tf.ones([L]))*100, tf.diag(tf.ones([L]))*100]),[D,L,L]) square = tf.square(dif_matrix) dist_matrix = tf.reduce_sum(square,0) negative = -dist_matrix # exp = tf.exp(negative) sm = tf.nn.softmax(negative) gating = tf.reshape(sm, [L, L, 1]) return gating
N = 7 f = 3 init_v = np.random.randint(0, 10, size=(N, f)).astype(np.float32) C = np.arange(N * N).reshape(N, N).astype(np.float32) B = tf.ones(shape=(f, 1)) ind = tf.constant(np.arange(N).reshape(N, 1).astype(np.int32)) queue = tf.FIFOQueue(1000, ("int32", "float")) enq = queue.enqueue_many((ind, C)) i, c = queue.dequeue() v = tf.Variable(init_v) c_diag = tf.diag(c) vt = tf.transpose(v) v_new = tf.transpose(tf.matmul(tf.matmul(tf.matmul(vt, c_diag), v), B)) update = tf.scatter_update(v, i, v_new) qr = tf.train.QueueRunner(queue, [enq] * 1) sess = tf.InteractiveSession() tf.initialize_all_variables().run() coord = tf.train.Coordinator() threads = qr.create_threads(sess, coord=coord, start=True) try: for step in range(1000): print("step", step) if coord.should_stop():
def __init__(self, M, M_init_final, A_age, A_sex, A_sexage, mask_age, mask_sex, mask_agesex, mask_nosignificance, Lrow_age, Lrow_sex, Lrow_agesex, Otraining, labels, training_set_mask, testing_set_mask, validation_set_mask, mask_features, order_chebyshev_row = 18,cheby=1, n_conv_feat=36,l2_regu=1,dropout=0.5, num_iterations = 10, gamma_age=1, gamma_sex=1, gamma_agesex=1, gamma_W=1, gamma_e=1, learning_rate=0.001, idx_gpu = '/gpu:1'): """ Neural network architecture. Compute an update X of M. Inputs: M: initial matrix with all the known values, M_init_final: initialization of X with the feature values from M and only the labels of the training set, A_age, A_sex, A_sexage : adjacency matrices, respectively for the age, sex and age and sex graphs, mask_age, mask_sex, mask_agesex, mask_nosignificance: masks to apply to X for each one of the graphs, Lrow_age, Lrow_sex, Lrow_agesex: laplacian matrices, respectively for the age, sex and age and sex graphs, Otraining: mask on the training features to know which ones are on the training set for the loss function, labels: labels for every subject, training_set_mask, testing_set_mask, validation_set_mask: indexes of subjects that respectively belong to the training, testing and validation sets, mask_features: mask composed of 1 values for all the features and 0 for the labels to compute the Frobenius loss term on the features, order_chebyshev_row: order to use for the Chebyshev polynomials. Default value = 18, cheby: boolean, use of a GCNN or a GCN layer. 0: GCN, 1: GCNN. Default value = 1, n_conv_feat: number of weights to use for the GCNN layer. Default value = 36, l2_regu: coefficient to use in front of the l2 regularization term. Default value = 1, dropout: dropout rate on the GCN output. Default = 0.5, num_iterations: number of times that the process GCNN+LSTM is done before updating X and computing the loss function. Default value = 10, gamma_age, gamma_sex, gamma_agesex, gamma_W, gamma_e: hyperparameters of the loss function in front of all the terms. Default value = 1, learning_rate: learning rate. Default value = 0.001 """ #order of the spectral filters self.ord_row = order_chebyshev_row self.num_iterations = num_iterations self.n_conv_feat = n_conv_feat with tf.Graph().as_default() as g: tf.logging.set_verbosity(tf.logging.ERROR) self.graph = g tf.set_random_seed(0) with tf.device(idx_gpu): #definition of constant matrices : adjacency matrices and laplacian. Computation of the Chebyshev polynomials self.A_age=tf.constant(A_age, dtype=tf.float32) self.A_sex=tf.constant(A_sex, dtype=tf.float32) self.A_sexage=tf.constant(A_sexage, dtype=tf.float32) self.Lrow_age=tf.constant(Lrow_age, dtype=tf.float32) self.Lrow_sex=tf.constant(Lrow_sex, dtype=tf.float32) self.Lrow_agesex=tf.constant(Lrow_agesex, dtype=tf.float32) self.norm_Lrow_age = self.Lrow_age - tf.diag(tf.ones([Lrow_age.shape[0], ])) self.list_row_cheb_pol_age = list() self.compute_cheb_polynomials(self.norm_Lrow_age, self.ord_row, self.list_row_cheb_pol_age) self.norm_Lrow_sex = self.Lrow_sex - tf.diag(tf.ones([Lrow_sex.shape[0], ])) self.list_row_cheb_pol_sex = list() self.compute_cheb_polynomials(self.norm_Lrow_sex, self.ord_row, self.list_row_cheb_pol_sex) self.norm_Lrow_agesex = self.Lrow_agesex - tf.diag(tf.ones([Lrow_agesex.shape[0], ])) self.list_row_cheb_pol_agesex = list() self.compute_cheb_polynomials(self.norm_Lrow_agesex, self.ord_row, self.list_row_cheb_pol_agesex) self.M = tf.constant(M, dtype=tf.float32) self.Otraining = tf.constant(Otraining, dtype=tf.float32) #training mask self.training_set_mask=tf.constant(training_set_mask, dtype=tf.float32) self.testing_set_mask=tf.constant(testing_set_mask, dtype=tf.float32) self.validation_set_mask =tf.constant(validation_set_mask, dtype=tf.float32) self.mask_age=tf.constant(mask_age, dtype=tf.float32) self.mask_sex=tf.constant(mask_sex, dtype=tf.float32) self.mask_agesex=tf.constant(mask_agesex, dtype=tf.float32) self.mask_nosignificance=tf.constant(mask_nosignificance, dtype=tf.float32) self.mask_features=tf.constant(mask_features, dtype=tf.float32) self.output_nn=tf.zeros([M.shape[0],]) ##################################definition of the NN variables##################################### #definition of the weights for extracting the global features #graph CNN if cheby==0:#no Chebyshev decomposition self.W_conv_age = tf.get_variable("W_conv_age", shape=[M_init_final.shape[1], self.n_conv_feat], initializer=tf.contrib.layers.xavier_initializer()) self.b_conv_age = tf.Variable(tf.zeros([self.n_conv_feat,])) self.W_conv_sex = tf.get_variable("W_conv_sex", shape=[M_init_final.shape[1], self.n_conv_feat], initializer=tf.contrib.layers.xavier_initializer()) self.b_conv_sex = tf.Variable(tf.zeros([self.n_conv_feat,])) self.W_conv_agesex = tf.get_variable("W_conv_agesex", shape=[M_init_final.shape[1], self.n_conv_feat], initializer=tf.contrib.layers.xavier_initializer()) self.b_conv_agesex = tf.Variable(tf.zeros([self.n_conv_feat,])) self.W_conv_nosign = tf.get_variable("W_conv_nosignificance", shape=[M_init_final.shape[1], self.n_conv_feat], initializer=tf.contrib.layers.xavier_initializer()) self.b_conv_nosign = tf.Variable(tf.zeros([self.n_conv_feat,])) else:#using Chebyshev decomposition self.W_conv_age = tf.get_variable("W_conv_age", shape=[self.ord_row*M_init_final.shape[1], self.n_conv_feat], initializer=tf.contrib.layers.xavier_initializer()) self.b_conv_age = tf.Variable(tf.zeros([self.n_conv_feat,])) self.W_conv_sex = tf.get_variable("W_conv_sex", shape=[self.ord_row*M_init_final.shape[1], self.n_conv_feat], initializer=tf.contrib.layers.xavier_initializer()) self.b_conv_sex = tf.Variable(tf.zeros([self.n_conv_feat,])) self.W_conv_agesex = tf.get_variable("W_conv_agesex", shape=[self.ord_row*M_init_final.shape[1], self.n_conv_feat], initializer=tf.contrib.layers.xavier_initializer()) self.b_conv_agesex = tf.Variable(tf.zeros([self.n_conv_feat,])) self.W_conv_nosign = tf.get_variable("W_conv_nosignificance", shape=[M_init_final.shape[1], self.n_conv_feat], initializer=tf.contrib.layers.xavier_initializer()) self.b_conv_nosign = tf.Variable(tf.zeros([self.n_conv_feat,])) #recurrent N parameters self.W_f_u = tf.get_variable("W_f_u", shape=[self.n_conv_feat*4, self.n_conv_feat*4], initializer=tf.contrib.layers.xavier_initializer()) self.W_i_u = tf.get_variable("W_i_u", shape=[self.n_conv_feat*4, self.n_conv_feat*4], initializer=tf.contrib.layers.xavier_initializer()) self.W_o_u = tf.get_variable("W_o_u", shape=[self.n_conv_feat*4, self.n_conv_feat*4], initializer=tf.contrib.layers.xavier_initializer()) self.W_c_u = tf.get_variable("W_c_u", shape=[self.n_conv_feat*4, self.n_conv_feat*4], initializer=tf.contrib.layers.xavier_initializer()) self.U_f_u = tf.get_variable("U_f_u", shape=[self.n_conv_feat*4, self.n_conv_feat*4], initializer=tf.contrib.layers.xavier_initializer()) self.U_i_u = tf.get_variable("U_i_u", shape=[self.n_conv_feat*4, self.n_conv_feat*4], initializer=tf.contrib.layers.xavier_initializer()) self.U_o_u = tf.get_variable("U_o_u", shape=[self.n_conv_feat*4, self.n_conv_feat*4], initializer=tf.contrib.layers.xavier_initializer()) self.U_c_u = tf.get_variable("U_c_u", shape=[self.n_conv_feat*4, self.n_conv_feat*4], initializer=tf.contrib.layers.xavier_initializer()) self.b_f_u = tf.Variable(tf.zeros([self.n_conv_feat*4,])) self.b_i_u = tf.Variable(tf.zeros([self.n_conv_feat*4,])) self.b_o_u = tf.Variable(tf.zeros([self.n_conv_feat*4,])) self.b_c_u = tf.Variable(tf.zeros([self.n_conv_feat*4,])) #output parameters self.W_out_W = tf.get_variable("W_out_W", shape=[self.n_conv_feat*4, M_init_final.shape[1]], initializer=tf.contrib.layers.xavier_initializer()) self.b_out_W = tf.Variable(tf.zeros([M_init_final.shape[1],])) self.X = tf.constant(M_init_final.astype('float32')) self.list_X = list() self.list_X.append(tf.identity(self.X)) #RNN self.h_u = tf.zeros([M.shape[0], self.n_conv_feat*4]) self.c_u = tf.zeros([M.shape[0], self.n_conv_feat*4]) for k in range(self.num_iterations): #GCN or GCNN layer if cheby==0: #GCN layer X1 = tf.multiply(self.mask_age , self.X) self.final_feat_age = self.mono_conv(self.A_age, X1, self.W_conv_age, self.b_conv_age) X2 = tf.multiply(self.mask_sex , self.X) self.final_feat_sex = self.mono_conv(self.A_sex, X2, self.W_conv_sex, self.b_conv_sex) X3 = tf.multiply(self.mask_agesex , self.X) self.final_feat_agesex = self.mono_conv(self.A_sexage, X3, self.W_conv_agesex, self.b_conv_agesex) else: #GCNN layer X1 = tf.multiply(self.mask_age , self.X) self.final_feat_age = self.mono_conv_cheby(self.list_row_cheb_pol_age, self.ord_row, X1, self.W_conv_age, self.b_conv_age) X2 = tf.multiply(self.mask_sex , self.X) self.final_feat_sex = self.mono_conv_cheby(self.list_row_cheb_pol_sex, self.ord_row, X2, self.W_conv_sex, self.b_conv_sex) X3 = tf.multiply(self.mask_agesex , self.X) self.final_feat_agesex = self.mono_conv_cheby(self.list_row_cheb_pol_agesex, self.ord_row, X3, self.W_conv_agesex, self.b_conv_agesex) X4 = tf.multiply(self.mask_nosignificance , self.X) self.final_feat_nosign=tf.nn.relu(tf.matmul(X4, self.W_conv_nosign)+ self.b_conv_nosign) self.final_feat_users= tf.concat([self.final_feat_age, self.final_feat_sex, self.final_feat_agesex, self.final_feat_nosign], 1) self.final_feat_users= tf.nn.dropout(self.final_feat_users, dropout) # LSTM self.f_u = tf.sigmoid(tf.matmul(self.final_feat_users, self.W_f_u) + tf.matmul(self.h_u, self.U_f_u) + self.b_f_u) self.i_u = tf.sigmoid(tf.matmul(self.final_feat_users, self.W_i_u) + tf.matmul(self.h_u, self.U_i_u) + self.b_i_u) self.o_u = tf.sigmoid(tf.matmul(self.final_feat_users, self.W_o_u) + tf.matmul(self.h_u, self.U_o_u) + self.b_o_u) self.update_c_u = tf.sigmoid(tf.matmul(self.final_feat_users, self.W_c_u) + tf.matmul(self.h_u, self.U_c_u) + self.b_c_u) self.c_u = tf.multiply(self.f_u, self.c_u) + tf.multiply(self.i_u, self.update_c_u) self.h_u = tf.multiply(self.o_u, tf.sigmoid(self.c_u)) #compute update of matrix X self.delta_X = tf.tanh(tf.matmul(self.c_u, self.W_out_W) + self.b_out_W) self.X += self.delta_X self.list_X.append(tf.identity(tf.reshape(self.X, [self.M.get_shape().as_list()[0], self.M.get_shape().as_list()[1]]))) #########loss definition #computation of the Frobenius term on the features self.Xnormed= self.norm_tensor(self.X) frob_tensor = tf.multiply(self.Otraining, self.Xnormed - self.M) frob_tensor = tf.multiply(self.mask_features, frob_tensor) self.loss_frob = self.frobenius_norm_square(frob_tensor)/np.sum(Otraining) #computation of the regularization terms for the graphs trace_row_age_tensor = tf.matmul(tf.matmul(tf.multiply(self.mask_age , self.X), self.Lrow_age, transpose_a=True), tf.multiply(self.mask_age , self.X)) self.loss_trace_row_age = tf.trace(trace_row_age_tensor)/tf.cast(tf.shape(self.X)[0]*tf.shape(self.X)[1],'float32') trace_row_sex_tensor = tf.matmul(tf.matmul(tf.multiply(self.mask_sex , self.X), self.Lrow_sex, transpose_a=True), tf.multiply(self.mask_sex , self.X)) self.loss_trace_row_sex = tf.trace(trace_row_sex_tensor)/tf.cast(tf.shape(self.X)[0]*tf.shape(self.X)[1],'float32') trace_row_agesex_tensor = tf.matmul(tf.matmul(tf.multiply(self.mask_agesex , self.X), self.Lrow_agesex, transpose_a=True), tf.multiply(self.mask_agesex , self.X)) self.loss_trace_row_agesex = tf.trace(trace_row_agesex_tensor)/tf.cast(tf.shape(self.X)[0]*tf.shape(self.X)[1],'float32') self.frob_norm_W = self.frobenius_norm_square(tf.multiply(self.mask_nosignificance, self.X))/tf.cast(tf.shape(self.X)[0]*tf.shape(self.X)[1], 'float32') #computation of the cross-entropy self.output_nn = tf.slice(self.X, begin = [0, self.M.get_shape().as_list()[1]-1], size = [self.M.get_shape().as_list()[0], 1]) self.output_nn=tf.sigmoid(self.output_nn) output_nn_train = (tf.multiply(self.training_set_mask , self.output_nn)) self.prediction_train = output_nn_train self.labels_training = tf.multiply(self.training_set_mask , labels) self.binary_entropy = tf.losses.sigmoid_cross_entropy(multi_class_labels = self.labels_training, logits = self.prediction_train) #computation of the l2 regularization term self.l2_regu=tf.nn.l2_loss(self.W_f_u) + tf.nn.l2_loss(self.W_i_u ) + tf.nn.l2_loss( self.W_o_u) + tf.nn.l2_loss(self.W_c_u)+ tf.nn.l2_loss(self.U_f_u ) + tf.nn.l2_loss(self.U_i_u )+ tf.nn.l2_loss(self.U_o_u ) + tf.nn.l2_loss( self.U_c_u )+ tf.nn.l2_loss(self.W_out_W) + tf.nn.l2_loss(self.W_conv_age) + tf.nn.l2_loss(self.W_conv_sex)+ tf.nn.l2_loss(self.W_conv_agesex)+ tf.nn.l2_loss(self.W_conv_nosign) #training loss definition self.loss = self.loss_frob + (gamma_age)*self.loss_trace_row_age+ gamma_sex*self.loss_trace_row_sex+ gamma_agesex*self.loss_trace_row_agesex + (gamma_W)*self.frob_norm_W+ gamma_e* self.binary_entropy +l2_regu*self.l2_regu # output_nn_val = (tf.multiply(self.validation_set_mask, self.output_nn)) self.predictions_val = output_nn_val self.labels_val = tf.multiply(self.validation_set_mask, labels) output_nn_test = (tf.multiply(self.testing_set_mask, self.output_nn)) self.predictions = output_nn_test self.labels_test = tf.multiply(self.testing_set_mask, labels) self.binary_entropy_test = tf.losses.sigmoid_cross_entropy(multi_class_labels = self.labels_test, logits = self.predictions) self.predictions_error = self.binary_entropy_test #definition of the solver self.optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(self.loss) self.var_grad = tf.gradients(self.loss, tf.trainable_variables()) self.norm_grad = self.frobenius_norm_square(tf.concat([tf.reshape(g, [-1]) for g in self.var_grad], 0)) # Create a session for running Ops on the Graph. config = tf.ConfigProto(allow_soft_placement = True) config.gpu_options.allow_growth = True self.session = tf.Session(config=config) # Run the Op to initialize the variables. init = tf.initialize_all_variables() self.session.run(init)
def eye(N): return tf.diag(tf.ones(tf.pack([ N, ]), dtype='float64'))
def inference(self, data): self.context_embedded, self.utterance_embedded = data[0], data[1] self.context_len, self.utterance_len, self.labels = data[2], data[ 3], data[4] with tf.variable_scope('rnn_context'): cell_context = tf.nn.rnn_cell.LSTMCell(self.n_neurons, forget_bias=2.0, use_peepholes=True, state_is_tuple=True) # Run the utterance and context through the RNN outputs_contexts, encoding_context = tf.nn.dynamic_rnn( cell_context, self.context_embedded, dtype=tf.float32, sequence_length=self.context_len) with tf.variable_scope("rnn_response"): cell_response = tf.nn.rnn_cell.LSTMCell(self.n_neurons, forget_bias=2.0, use_peepholes=True, state_is_tuple=True) outputs_responses, encoding_utterance = tf.nn.dynamic_rnn( cell_response, self.utterance_embedded, dtype=tf.float32, sequence_length=self.utterance_len) encoding_context = encoding_context.h encoding_utterance = encoding_utterance.h print("context encoded shape: {0}, utterance encoded shape {1}".format( encoding_context.shape, encoding_utterance.shape)) M = tf.diag([1.0] * self.n_neurons) print("Shape of M {}".format(M.shape)) logits_list = [] poly_kernel_pow_max = 3 for poly_kernel_pow in range(1, poly_kernel_pow_max + 1): bias = tf.get_variable("B_" + str(poly_kernel_pow), shape=None, initializer=0.0) # "Predict" a response: c * M generated_response = tf.matmul(encoding_context, M) #generated_response = tf.expand_dims(generated_response, 2) print("Shape of gen res {}".format(generated_response.shape)) #encoding_utterance = tf.expand_dims(encoding_utterance, 2) print("Shape of enc utt {}".format(encoding_utterance.shape)) # Dot product between generated response and actual response # (c * M) * r res1 = tf.reduce_sum(tf.multiply(generated_response, encoding_utterance), axis=1) res1 = tf.add(res1, bias) print("res1 shape", res1.shape) logits = tf.pow(res1, poly_kernel_pow) logits = tf.add(logits, bias) logits_list.append(logits) logits = tf.add_n(logits_list) self.logits = tf.reshape(logits, [-1, 1]) print("Shape of logits at inference {}".format(self.logits.shape)) return self.logits
30.97266006, 26.67541885, 38.08450317, 20.74983215, 34.94445419, 34.45999146, 29.06485367, 36.01657104, 27.88236427, 20.56035233, 30.20379066, 29.51215172, 33.71149445, 28.59134293, 36.05556488, 28.66994858]) indices = tf.where(x>30.) elements = tf.gather(x, indices) ############################################################################### # 1e: Create a diagnoal 2-d tensor of size 6 x 6 with the diagonal values of 1, # 2, ..., 6 # Hint: Use tf.range() and tf.diag(). ############################################################################### diagonal_val = tf.range(1,7) diagonal = tf.diag(diagonal_val) ############################################################################### # 1f: Create a random 2-d tensor of size 10 x 10 from any distribution. # Calculate its determinant. # Hint: Look at tf.matrix_determinant(). ############################################################################### x = tf.random_uniform([10,10]) determinant = tf.matrix_determinant(x) ############################################################################### # 1g: Create tensor x with value [5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9]. # Return the unique elements in x # Hint: use tf.unique(). Keep in mind that tf.unique() returns a tuple. ###############################################################################
def target_log_prob_fn(event): return tfd.MultivariateNormalFullCovariance( loc=tf.zeros(2), covariance_matrix=tf.diag([1., 10.])).log_prob(event)
def get(self): V = tf.expand_dims(self.v, 1) return tf.diag(self.d) + tf.matmul(V, tf.transpose(V))
def get_transform_matrix_tf_(theta, phi, invert_rot=False, invert_focal=False): #INPUT IN DEGREES #extrinsic matrix: # # RRRD # RRRD # RRRD # 000D sin_phi = tf.sin(phi / 180 * np.pi) cos_phi = tf.cos(phi / 180 * np.pi) sin_theta = tf.sin(-theta / 180.0 * np.pi) #why is theta negative??? cos_theta = tf.cos(-theta / 180.0 * np.pi) rotation_azimuth_flat = [ cos_theta, 0.0, -sin_theta, 0.0, 1.0, 0.0, sin_theta, 0.0, cos_theta ] rotation_elevation_flat = [ cos_phi, sin_phi, 0.0, -sin_phi, cos_phi, 0.0, 0.0, 0.0, 1.0 ] f = lambda x: tf.reshape(tf.stack(x), (3, 3)) rotation_azimuth = f(rotation_azimuth_flat) rotation_elevation = f(rotation_elevation_flat) rotation_matrix = tf.matmul(rotation_azimuth, rotation_elevation) if invert_rot: rotation_matrix = tf.linalg.inv(rotation_matrix) displacement = np.zeros((3, 1), dtype=np.float32) displacement[0, 0] = const.DIST_TO_CAM #because the target has distance 4 displacement = tf.constant(displacement, dtype=np.float32) displacement = tf.matmul(rotation_matrix, displacement) bottom_row = np.zeros((1, 4), dtype=np.float32) bottom_row[0, 3] = 1.0 bottom_row = tf.constant(bottom_row) #print rotation_matrix #print bottom_row #print displacement extrinsic_matrix = tf.concat( [tf.concat([rotation_matrix, -displacement], axis=1), bottom_row], axis=0) if invert_focal: intrinsic_diag = [ 1.0, float(const.focal_length), float(const.focal_length), 1.0 ] else: intrinsic_diag = [ 1.0, 1.0 / float(const.focal_length), 1.0 / float(const.focal_length), 1.0 ] intrinsic_matrix = tf.diag(tf.constant(intrinsic_diag, dtype=tf.float32)) camera_matrix = tf.matmul(extrinsic_matrix, intrinsic_matrix) return camera_matrix