Python tensorflow.diag_part() Examples
The following are 30
code examples of tensorflow.diag_part().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
Example #1
Source File: slicenet.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def rank_loss(sentence_emb, image_emb, margin=0.2): """Experimental rank loss, thanks to kkurach@ for the code.""" with tf.name_scope("rank_loss"): # Normalize first as this is assumed in cosine similarity later. sentence_emb = tf.nn.l2_normalize(sentence_emb, 1) image_emb = tf.nn.l2_normalize(image_emb, 1) # Both sentence_emb and image_emb have size [batch, depth]. scores = tf.matmul(image_emb, tf.transpose(sentence_emb)) # [batch, batch] diagonal = tf.diag_part(scores) # [batch] cost_s = tf.maximum(0.0, margin - diagonal + scores) # [batch, batch] cost_im = tf.maximum( 0.0, margin - tf.reshape(diagonal, [-1, 1]) + scores) # [batch, batch] # Clear diagonals. batch_size = tf.shape(sentence_emb)[0] empty_diagonal_mat = tf.ones_like(cost_s) - tf.eye(batch_size) cost_s *= empty_diagonal_mat cost_im *= empty_diagonal_mat return tf.reduce_mean(cost_s) + tf.reduce_mean(cost_im)
Example #2
Source File: my_layers.py From DAS with Apache License 2.0 | 6 votes |
def _mix_rbf_kernel(X, Y, sigmas=[1.], wts=None): if wts is None: wts = [1] * len(sigmas) XX = tf.matmul(X, X, transpose_b=True) XY = tf.matmul(X, Y, transpose_b=True) YY = tf.matmul(Y, Y, transpose_b=True) X_sqnorms = tf.diag_part(XX) Y_sqnorms = tf.diag_part(YY) r = lambda x: tf.expand_dims(x, 0) c = lambda x: tf.expand_dims(x, 1) K_XX, K_XY, K_YY = 0, 0, 0 for sigma, wt in zip(sigmas, wts): gamma = 1 / (2 * sigma**2) K_XX += wt * tf.exp(-gamma * (-2 * XX + c(X_sqnorms) + r(X_sqnorms))) K_XY += wt * tf.exp(-gamma * (-2 * XY + c(X_sqnorms) + r(Y_sqnorms))) K_YY += wt * tf.exp(-gamma * (-2 * YY + c(Y_sqnorms) + r(Y_sqnorms))) return K_XX, K_XY, K_YY, tf.reduce_sum(wts)
Example #3
Source File: vae.py From disentanglement_lib with Apache License 2.0 | 6 votes |
def regularize_diag_off_diag_dip(covariance_matrix, lambda_od, lambda_d): """Compute on and off diagonal regularizers for DIP-VAE models. Penalize deviations of covariance_matrix from the identity matrix. Uses different weights for the deviations of the diagonal and off diagonal entries. Args: covariance_matrix: Tensor of size [num_latent, num_latent] to regularize. lambda_od: Weight of penalty for off diagonal elements. lambda_d: Weight of penalty for diagonal elements. Returns: dip_regularizer: Regularized deviation from diagonal of covariance_matrix. """ covariance_matrix_diagonal = tf.diag_part(covariance_matrix) covariance_matrix_off_diagonal = covariance_matrix - tf.diag( covariance_matrix_diagonal) dip_regularizer = tf.add( lambda_od * tf.reduce_sum(covariance_matrix_off_diagonal**2), lambda_d * tf.reduce_sum((covariance_matrix_diagonal - 1)**2)) return dip_regularizer
Example #4
Source File: metrics.py From MultiPlanarUNet with MIT License | 6 votes |
def sparse_mean_fg_f1(y_true, y_pred): y_pred = tf.argmax(y_pred, axis=-1) # Get confusion matrix cm = tf.confusion_matrix(tf.reshape(y_true, [-1]), tf.reshape(y_pred, [-1])) # Get precisions TP = tf.diag_part(cm) precisions = TP / tf.reduce_sum(cm, axis=0) # Get recalls TP = tf.diag_part(cm) recalls = TP / tf.reduce_sum(cm, axis=1) # Get F1s f1s = (2 * precisions * recalls) / (precisions + recalls) return tf.reduce_mean(f1s[1:])
Example #5
Source File: bayes.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def call(self, inputs): if self.coeffs_mean is None and self.coeffs_precision_tril_op is None: # p(mean(ynew) | xnew) = Normal(ynew | mean = 0, variance = xnew xnew^T) predictive_mean = 0. predictive_variance = tf.reduce_sum(tf.square(inputs), -1) else: # p(mean(ynew) | xnew, x, y) = Normal(ynew | # mean = xnew (1/noise_variance) (1/noise_variance x^T x + I)^{-1}x^T y, # variance = xnew (1/noise_variance x^T x + I)^{-1} xnew^T) predictive_mean = tf.einsum('nm,m->n', inputs, self.coeffs_mean) predictive_covariance = tf.matmul( inputs, self.coeffs_precision_tril_op.solve( self.coeffs_precision_tril_op.solve(inputs, adjoint_arg=True), adjoint=True)) predictive_variance = tf.diag_part(predictive_covariance) return ed.Normal(loc=predictive_mean, scale=tf.sqrt(predictive_variance))
Example #6
Source File: gaussian_process.py From BERT with Apache License 2.0 | 6 votes |
def call(self, inputs): if self.coeffs_mean is None and self.coeffs_precision_tril_op is None: # p(mean(ynew) | xnew) = Normal(ynew | mean = 0, variance = xnew xnew^T) predictive_mean = 0. predictive_variance = tf.reduce_sum(tf.square(inputs), -1) else: # p(mean(ynew) | xnew, x, y) = Normal(ynew | # mean = xnew (1/noise_variance) (1/noise_variance x^T x + I)^{-1}x^T y, # variance = xnew (1/noise_variance x^T x + I)^{-1} xnew^T) predictive_mean = tf.einsum('nm,m->n', inputs, self.coeffs_mean) predictive_covariance = tf.matmul( inputs, self.coeffs_precision_tril_op.solve( self.coeffs_precision_tril_op.solve(inputs, adjoint_arg=True), adjoint=True)) predictive_variance = tf.diag_part(predictive_covariance) return ed.Normal(loc=predictive_mean, scale=tf.sqrt(predictive_variance))
Example #7
Source File: slicenet.py From BERT with Apache License 2.0 | 6 votes |
def rank_loss(sentence_emb, image_emb, margin=0.2): """Experimental rank loss, thanks to kkurach@ for the code.""" with tf.name_scope("rank_loss"): # Normalize first as this is assumed in cosine similarity later. sentence_emb = tf.nn.l2_normalize(sentence_emb, 1) image_emb = tf.nn.l2_normalize(image_emb, 1) # Both sentence_emb and image_emb have size [batch, depth]. scores = tf.matmul(image_emb, tf.transpose(sentence_emb)) # [batch, batch] diagonal = tf.diag_part(scores) # [batch] cost_s = tf.maximum(0.0, margin - diagonal + scores) # [batch, batch] cost_im = tf.maximum( 0.0, margin - tf.reshape(diagonal, [-1, 1]) + scores) # [batch, batch] # Clear diagonals. batch_size = tf.shape(sentence_emb)[0] empty_diagonal_mat = tf.ones_like(cost_s) - tf.eye(batch_size) cost_s *= empty_diagonal_mat cost_im *= empty_diagonal_mat return tf.reduce_mean(cost_s) + tf.reduce_mean(cost_im)
Example #8
Source File: utils.py From variance-networks with Apache License 2.0 | 6 votes |
def build_graph_with_hess(images, labels, loss_function, inference_function, learning_rate, global_step): optimizer_net = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.95) tf.summary.scalar('learning_rate', learning_rate) with tf.variable_scope(tf.get_variable_scope()) as scope: logits_stoch = inference_function(images, stochastic=True, reuse=False) probs_stoch = tf.nn.softmax(logits_stoch) tf.get_variable_scope().reuse_variables() logits_det = inference_function(images, stochastic=False, reuse=True) probs_det = tf.nn.softmax(logits_det) train_loss = loss_function(logits_stoch, labels) # weights = get_weights() # for v in weights: # hess = tf.diag_part(tf.squeeze(tf.hessians(logits_det, v))) # tf.summary.histogram(v.name + 'hessian', hess) # print v.name, v.get_shape(), hess.get_shape() train_op = optimizer_net.minimize(train_loss, global_step=global_step) return train_op, probs_det, probs_stoch
Example #9
Source File: math_func.py From MMD-GAN with Apache License 2.0 | 6 votes |
def matrix_mean_wo_diagonal(matrix, num_row, num_col=None, name='mu_wo_diag'): """ This function calculates the mean of the matrix elements not in the diagonal 2018.4.9 - replace tf.diag_part with tf.matrix_diag_part tf.matrix_diag_part can be used for rectangle matrix while tf.diag_part can only be used for square matrix :param matrix: :param num_row: :type num_row: float :param num_col: :type num_col: float :param name: :return: """ with tf.name_scope(name): if num_col is None: mu = (tf.reduce_sum(matrix) - tf.reduce_sum(tf.matrix_diag_part(matrix))) / (num_row * (num_row - 1.0)) else: mu = (tf.reduce_sum(matrix) - tf.reduce_sum(tf.matrix_diag_part(matrix))) \ / (num_row * num_col - tf.minimum(num_col, num_row)) return mu ########################################################################
Example #10
Source File: mmd.py From opt-mmd with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _mix_rbf_kernel(X, Y, sigmas, wts=None): if wts is None: wts = [1] * len(sigmas) XX = tf.matmul(X, X, transpose_b=True) XY = tf.matmul(X, Y, transpose_b=True) YY = tf.matmul(Y, Y, transpose_b=True) X_sqnorms = tf.diag_part(XX) Y_sqnorms = tf.diag_part(YY) r = lambda x: tf.expand_dims(x, 0) c = lambda x: tf.expand_dims(x, 1) K_XX, K_XY, K_YY = 0, 0, 0 for sigma, wt in zip(sigmas, wts): gamma = 1 / (2 * sigma**2) K_XX += wt * tf.exp(-gamma * (-2 * XX + c(X_sqnorms) + r(X_sqnorms))) K_XY += wt * tf.exp(-gamma * (-2 * XY + c(X_sqnorms) + r(Y_sqnorms))) K_YY += wt * tf.exp(-gamma * (-2 * YY + c(Y_sqnorms) + r(Y_sqnorms))) return K_XX, K_XY, K_YY, tf.reduce_sum(wts)
Example #11
Source File: slicenet.py From fine-lm with MIT License | 6 votes |
def rank_loss(sentence_emb, image_emb, margin=0.2): """Experimental rank loss, thanks to kkurach@ for the code.""" with tf.name_scope("rank_loss"): # Normalize first as this is assumed in cosine similarity later. sentence_emb = tf.nn.l2_normalize(sentence_emb, 1) image_emb = tf.nn.l2_normalize(image_emb, 1) # Both sentence_emb and image_emb have size [batch, depth]. scores = tf.matmul(image_emb, tf.transpose(sentence_emb)) # [batch, batch] diagonal = tf.diag_part(scores) # [batch] cost_s = tf.maximum(0.0, margin - diagonal + scores) # [batch, batch] cost_im = tf.maximum( 0.0, margin - tf.reshape(diagonal, [-1, 1]) + scores) # [batch, batch] # Clear diagonals. batch_size = tf.shape(sentence_emb)[0] empty_diagonal_mat = tf.ones_like(cost_s) - tf.eye(batch_size) cost_s *= empty_diagonal_mat cost_im *= empty_diagonal_mat return tf.reduce_mean(cost_s) + tf.reduce_mean(cost_im)
Example #12
Source File: matrix_structures.py From VFF with Apache License 2.0 | 5 votes |
def trace_KiX(self, X): """ X is a square matrix of the same size as this one. if self is K, compute tr(K^{-1} X) """ return tf.reduce_sum(tf.diag_part(X) / self.d)
Example #13
Source File: model_dual_encoder_dense.py From google-smart-reply-2017 with MIT License | 5 votes |
def _negative_log_probability_loss(context_channel, utterance_channel): """ This implements the negative log probability using negative sampling where K-1 items in the batch are treated as negative samples where i != j. The overall loss formula is: $$L(x,y,\theta) = -\frac{1}{K}\sum_{i=1}^{K}{[ f(x_i, y_i) - log \sum_{j=1}^{K}{e^{f(x_i,y_i)}}]}$$ :param context_channel: :param utterance_channel: :return: """ # calculate dot product between each pair of inputs and responses # (bs x bs) K = tf.matmul(context_channel, utterance_channel, transpose_b=True) # get the diagonals which are the S(x_i, y_i) # this represents the similarity score between each input x_i and output y_i # out = (bs x 1) S = tf.diag_part(K) S = tf.reshape(S, [-1, 1]) # calculate the log sum(e^x_i, y_j) # here every row has only the negative examples # in = (bs x bs). out = (bs x 1) K = tf.reduce_logsumexp(K, axis=1, keep_dims=True) # compute the mean loss between each x,y pair # and the log sum of each other (K-1) x,y pair return -tf.reduce_mean(S - K)
Example #14
Source File: metrics.py From MultiPlanarUNet with MIT License | 5 votes |
def sparse_mean_fg_recall(y_true, y_pred): y_pred = tf.argmax(y_pred, axis=-1) # Get confusion matrix cm = tf.confusion_matrix(tf.reshape(y_true, [-1]), tf.reshape(y_pred, [-1])) # Get precisions TP = tf.diag_part(cm) recalls = TP / tf.reduce_sum(cm, axis=1) return tf.reduce_mean(recalls[1:])
Example #15
Source File: model.py From personalized-dialog with MIT License | 5 votes |
def _assemble_graph(self): self._create_placeholders() tf.set_random_seed(self._random_seed + 1) A_var = tf.Variable( initial_value=tf.random_uniform( shape=[self._emb_dim, self._vocab_dim], minval=-1, maxval=1, seed=(self._random_seed + 2) ) ) B_var = tf.Variable( initial_value=tf.random_uniform( shape=[self._emb_dim, self._vocab_dim], minval=-1, maxval=1, seed=(self._random_seed + 3) ) ) self.global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') cont_mult = tf.transpose(tf.matmul(A_var, tf.transpose(self.context_batch))) resp_mult = tf.matmul(B_var, tf.transpose(self.response_batch)) neg_resp_mult = tf.matmul(B_var, tf.transpose(self.neg_response_batch)) pos_raw_f = tf.diag_part(tf.matmul(cont_mult, resp_mult)) neg_raw_f = tf.diag_part(tf.matmul(cont_mult, neg_resp_mult)) self.f_pos = pos_raw_f self.f_neg = neg_raw_f self.loss = tf.reduce_sum(tf.nn.relu(self.f_neg - self.f_pos + self._margin))
Example #16
Source File: model_utils.py From gcnn-survey-paper with Apache License 2.0 | 5 votes |
def compute_l2_sim_matrix(node_features): """Compute squared-L2 distance between each pair of nodes.""" # N x N # d_scores = tf.matmul(node_features, tf.transpose(node_features,perm=[1, 0])) # diag = tf.diag_part(d_scores) # d_scores *= -2. # d_scores += tf.reshape(diag, (-1, 1)) + tf.reshape(diag, (1, -1)) l2_norm = tf.reduce_sum(tf.square(node_features), 1) na = tf.reshape(l2_norm, [-1, 1]) nb = tf.reshape(l2_norm, [1, -1]) # return pairwise euclidead difference matrix l2_scores = tf.maximum( na - 2*tf.matmul(node_features, node_features, False, True) + nb, 0.0) return l2_scores
Example #17
Source File: utilities.py From NNCF with MIT License | 5 votes |
def tensorflow_diag(x, size=None): ''' size: square matrix size tf.diag_part is very slow! so when size given, we use faster gather_nd ''' if size is None: return tf.diag_part(x) else: diag_idx = np.vstack((np.arange(size), np.arange(size))).T return tf.gather_nd(x, diag_idx.astype(np.int32))
Example #18
Source File: network.py From CPL with MIT License | 5 votes |
def __init__(self, is_training, word_embeddings, simple_position = False): NN.__init__(self, is_training, word_embeddings, simple_position) with tf.name_scope("conv-maxpool"): input_sentence = tf.expand_dims(self.input_embedding, axis=1) x = tf.layers.conv2d(inputs = input_sentence, filters=FLAGS.hidden_size, kernel_size=[1,3], strides=[1, 1], padding='same', kernel_initializer=tf.contrib.layers.xavier_initializer_conv2d()) x = tf.reduce_max(x, axis=2) x = tf.nn.relu(tf.squeeze(x)) if FLAGS.katt_flag != 0: stack_repre = self.katt(x, is_training) else: stack_repre = self.att(x, is_training) with tf.name_scope("loss"): logits = tf.matmul(stack_repre, tf.transpose(self.relation_matrix)) + self.bias self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.label,logits=logits)) self.loss = tf.losses.softmax_cross_entropy(onehot_labels = self.label, logits = logits, weights = self.weights) self.output = tf.nn.softmax(logits) tf.summary.scalar('loss',self.loss) self.predictions = tf.argmax(logits, 1, name="predictions") self.correct_predictions = tf.equal(self.predictions, tf.argmax(self.label, 1)) self.accuracy = tf.reduce_mean(tf.cast(self.correct_predictions, "float"), name="accuracy") if not is_training: with tf.name_scope("test"): if FLAGS.katt_flag != 0: test_attention_logit = self.katt_test(x) else: test_attention_logit = self.att_test(x) test_tower_output = [] for i in range(FLAGS.test_batch_size): test_attention_score = tf.nn.softmax(tf.transpose(test_attention_logit[self.scope[i]:self.scope[i+1],:])) final_repre = tf.matmul(test_attention_score, x[self.scope[i]:self.scope[i+1]]) logits = tf.matmul(final_repre, tf.transpose(self.relation_matrix)) + self.bias output = tf.diag_part(tf.nn.softmax(logits)) test_tower_output.append(output) test_stack_output = tf.reshape(tf.stack(test_tower_output),[FLAGS.test_batch_size, self.num_classes]) self.test_output = test_stack_output
Example #19
Source File: network.py From CPL with MIT License | 5 votes |
def __init__(self, is_training, word_embeddings, simple_position = False): NN.__init__(self, is_training, word_embeddings, simple_position) with tf.name_scope("conv-maxpool"): mask_embedding = tf.constant([[0,0,0],[1,0,0],[0,1,0],[0,0,1]], dtype=np.float32) pcnn_mask = tf.nn.embedding_lookup(mask_embedding, self.mask) input_sentence = tf.expand_dims(self.input_embedding, axis=1) x = tf.layers.conv2d(inputs = input_sentence, filters=FLAGS.hidden_size, kernel_size=[1,3], strides=[1, 1], padding='same', kernel_initializer=tf.contrib.layers.xavier_initializer_conv2d()) x = tf.reshape(x, [-1, self.max_length, FLAGS.hidden_size, 1]) x = tf.reduce_max(tf.reshape(pcnn_mask, [-1, 1, self.max_length, 3]) * tf.transpose(x,[0, 2, 1, 3]), axis = 2) x = tf.nn.relu(tf.reshape(x,[-1, self.output_size])) if FLAGS.katt_flag != 0: stack_repre = self.katt(x, is_training) else: stack_repre = self.att(x, is_training) with tf.name_scope("loss"): logits = tf.matmul(stack_repre, tf.transpose(self.relation_matrix)) + self.bias self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.label,logits=logits)) self.loss = tf.losses.softmax_cross_entropy(onehot_labels = self.label, logits = logits, weights = self.weights) self.output = tf.nn.softmax(logits) tf.summary.scalar('loss',self.loss) self.predictions = tf.argmax(logits, 1, name="predictions") self.correct_predictions = tf.equal(self.predictions, tf.argmax(self.label, 1)) self.accuracy = tf.reduce_mean(tf.cast(self.correct_predictions, "float"), name="accuracy") if not is_training: with tf.name_scope("test"): if FLAGS.katt_flag != 0: test_attention_logit = self.katt_test(x) else: test_attention_logit = self.att_test(x) test_tower_output = [] for i in range(FLAGS.test_batch_size): test_attention_score = tf.nn.softmax(tf.transpose(test_attention_logit[self.scope[i]:self.scope[i+1],:])) final_repre = tf.matmul(test_attention_score, x[self.scope[i]:self.scope[i+1]]) logits = tf.matmul(final_repre, tf.transpose(relation_matrix)) + bias output = tf.diag_part(tf.nn.softmax(logits)) test_tower_output.append(output) test_stack_output = tf.reshape(tf.stack(test_tower_output),[FLAGS.test_batch_size, self.num_classes]) self.test_output = test_stack_output
Example #20
Source File: dists.py From elbow with BSD 3-Clause "New" or "Revised" License | 5 votes |
def multivariate_gaussian_entropy(Sigma=None, L=None, L_prec=None): if L is None and Sigma is not None: L = tf.cholesky(Sigma) if L is not None: half_logdet = tf.reduce_sum(tf.log(tf.diag_part(L))) n, _ = extract_shape(L) else: half_logdet = -tf.reduce_sum(tf.log(tf.diag_part(L_prec))) n, _ = extract_shape(L_prec) log_2pi = 1.83787706641 entropy = .5*n*(1 + log_2pi) + half_logdet return entropy
Example #21
Source File: ssgp.py From VFF with Apache License 2.0 | 5 votes |
def build_likelihood(self): # w = w./repmat(ell',[m,1]); % scaled model angular frequencies w = self.omega / self.kern.lengthscales m = tf.shape(self.omega)[0] m_float = tf.cast(m, tf.float64) # phi = x_tr*w'; phi = tf.matmul(self.X, tf.transpose(w)) # phi = [cos(phi) sin(phi)]; % design matrix phi = tf.concat([tf.cos(phi), tf.sin(phi)], axis=1) # R = chol((sf2/m)*(phi'*phi) + sn2*eye(2*m)); % calculate some often-used constants A = (self.kern.variance / m_float) * tf.matmul(tf.transpose(phi), phi)\ + self.likelihood.variance * gpflow.tf_wraps.eye(2*m) RT = tf.cholesky(A) R = tf.transpose(RT) # PhiRiphi/R; # RtiPhit = PhiRi'; RtiPhit = tf.matrix_triangular_solve(RT, tf.transpose(phi)) # Rtiphity=RtiPhit*y_tr; Rtiphity = tf.matmul(RtiPhit, self.Y) # % output NLML # out1=0.5/sn2*(sum(y_tr.^2)-sf2/m*sum(Rtiphity.^2))+ ... out = 0.5/self.likelihood.variance*(tf.reduce_sum(tf.square(self.Y)) - self.kern.variance/m_float*tf.reduce_sum(tf.square(Rtiphity))) # +sum(log(diag(R)))+(n/2-m)*log(sn2)+n/2*log(2*pi); n = tf.cast(tf.shape(self.X)[0], tf.float64) out += tf.reduce_sum(tf.log(tf.diag_part(R)))\ + (n/2.-m_float) * tf.log(self.likelihood.variance)\ + n/2*np.log(2*np.pi) return -out
Example #22
Source File: matrix_structures.py From VFF with Apache License 2.0 | 5 votes |
def trace_KiX(self, X): """ X is a square matrix of the same size as this one. if self is K, compute tr(K^{-1} X) """ d_col = tf.expand_dims(self.d, 1) R = self.W / d_col RTX = tf.matmul(tf.transpose(R), X) RTXR = tf.matmul(RTX, R) M = tf.eye(tf.shape(self.W)[1], float_type) + tf.matmul(tf.transpose(R), self.W) Mi = tf.matrix_inverse(M) return tf.reduce_sum(tf.diag_part(X) * 1./self.d) - tf.reduce_sum(RTXR * Mi)
Example #23
Source File: matrix_structures.py From VFF with Apache License 2.0 | 5 votes |
def logdet(self): part1 = tf.reduce_sum(tf.log(self.d)) I = tf.eye(tf.shape(self.W)[1], float_type) M = I + tf.matmul(tf.transpose(self.W) / self.d, self.W) part2 = 2*tf.reduce_sum(tf.log(tf.diag_part(tf.cholesky(M)))) return part1 + part2
Example #24
Source File: tensorflowbk.py From quantumflow with Apache License 2.0 | 5 votes |
def productdiag(tensor: BKTensor) -> BKTensor: N = rank(tensor) tensor = reshape(tensor, [2**(N//2), 2**(N//2)]) tensor = tf.diag_part(tensor) tensor = reshape(tensor, [2]*(N//2)) return tensor
Example #25
Source File: kernels.py From GGP with Apache License 2.0 | 5 votes |
def Kdiag(self, X): return tf.diag_part(self.K(X))
Example #26
Source File: metrics.py From MultiPlanarUNet with MIT License | 5 votes |
def sparse_mean_fg_precision(y_true, y_pred): y_pred = tf.argmax(y_pred, axis=-1) # Get confusion matrix cm = tf.confusion_matrix(tf.reshape(y_true, [-1]), tf.reshape(y_pred, [-1])) # Get precisions TP = tf.diag_part(cm) precisions = TP / tf.reduce_sum(cm, axis=0) return tf.reduce_mean(precisions[1:])
Example #27
Source File: triplet_loss.py From tensorflow-triplet-loss with MIT License | 5 votes |
def _pairwise_distances(embeddings, squared=False): """Compute the 2D matrix of distances between all the embeddings. Args: embeddings: tensor of shape (batch_size, embed_dim) squared: Boolean. If true, output is the pairwise squared euclidean distance matrix. If false, output is the pairwise euclidean distance matrix. Returns: pairwise_distances: tensor of shape (batch_size, batch_size) """ # Get the dot product between all embeddings # shape (batch_size, batch_size) dot_product = tf.matmul(embeddings, tf.transpose(embeddings)) # Get squared L2 norm for each embedding. We can just take the diagonal of `dot_product`. # This also provides more numerical stability (the diagonal of the result will be exactly 0). # shape (batch_size,) square_norm = tf.diag_part(dot_product) # Compute the pairwise distance matrix as we have: # ||a - b||^2 = ||a||^2 - 2 <a, b> + ||b||^2 # shape (batch_size, batch_size) distances = tf.expand_dims(square_norm, 1) - 2.0 * dot_product + tf.expand_dims(square_norm, 0) # Because of computation errors, some distances might be negative so we put everything >= 0.0 distances = tf.maximum(distances, 0.0) if not squared: # Because the gradient of sqrt is infinite when distances == 0.0 (ex: on the diagonal) # we need to add a small epsilon where distances == 0.0 mask = tf.to_float(tf.equal(distances, 0.0)) distances = distances + mask * 1e-16 distances = tf.sqrt(distances) # Correct the epsilon added: set the distances on the mask to be exactly 0.0 distances = distances * (1.0 - mask) return distances
Example #28
Source File: selector.py From CPL with MIT License | 5 votes |
def bag_attention(x, scope, query, rel_tot, is_training, var_scope=None, dropout_before=False, keep_prob=1.0): with tf.variable_scope(var_scope or "attention", reuse=tf.AUTO_REUSE): if is_training: # training if dropout_before: x = __dropout__(x, keep_prob) bag_repre = [] attention_logit = __attention_train_logit__(x, query, rel_tot) for i in range(scope.shape[0]): bag_hidden_mat = x[scope[i][0]:scope[i][1]] attention_score = tf.nn.softmax(attention_logit[scope[i][0]:scope[i][1]], -1) bag_repre.append(tf.squeeze(tf.matmul(tf.expand_dims(attention_score, 0), bag_hidden_mat))) # (1, n') x (n', hidden_size) = (1, hidden_size) -> (hidden_size) bag_repre = tf.stack(bag_repre) if not dropout_before: bag_repre = __dropout__(bag_repre, keep_prob) return __logit__(bag_repre, rel_tot), bag_repre else: # testing attention_logit = __attention_test_logit__(x, rel_tot) # (n, rel_tot) bag_repre = [] bag_logit = [] for i in range(scope.shape[0]): bag_hidden_mat = x[scope[i][0]:scope[i][1]] attention_score = tf.nn.softmax(tf.transpose(attention_logit[scope[i][0]:scope[i][1], :]), -1) # softmax of (rel_tot, n') bag_repre_for_each_rel = tf.matmul(attention_score, bag_hidden_mat) # (rel_tot, n') \dot (n', hidden_size) = (rel_tot, hidden_size) bag_logit_for_each_rel = __logit__(bag_repre_for_each_rel, rel_tot) # -> (rel_tot, rel_tot) bag_repre.append(bag_repre_for_each_rel) bag_logit.append(tf.diag_part(tf.nn.softmax(bag_logit_for_each_rel, -1))) # could be improved by sigmoid? bag_repre = tf.stack(bag_repre) bag_logit = tf.stack(bag_logit) return bag_logit, bag_repre
Example #29
Source File: mmd.py From RGAN with MIT License | 5 votes |
def _mix_rbf_kernel(X, Y, sigmas, wts=None): """ """ if wts is None: wts = [1.0] * sigmas.get_shape()[0] # debug! if len(X.shape) == 2: # matrix XX = tf.matmul(X, X, transpose_b=True) XY = tf.matmul(X, Y, transpose_b=True) YY = tf.matmul(Y, Y, transpose_b=True) elif len(X.shape) == 3: # tensor -- this is computing the Frobenius norm XX = tf.tensordot(X, X, axes=[[1, 2], [1, 2]]) XY = tf.tensordot(X, Y, axes=[[1, 2], [1, 2]]) YY = tf.tensordot(Y, Y, axes=[[1, 2], [1, 2]]) else: raise ValueError(X) X_sqnorms = tf.diag_part(XX) Y_sqnorms = tf.diag_part(YY) r = lambda x: tf.expand_dims(x, 0) c = lambda x: tf.expand_dims(x, 1) K_XX, K_XY, K_YY = 0, 0, 0 for sigma, wt in zip(tf.unstack(sigmas, axis=0), wts): gamma = 1 / (2 * sigma**2) K_XX += wt * tf.exp(-gamma * (-2 * XX + c(X_sqnorms) + r(X_sqnorms))) K_XY += wt * tf.exp(-gamma * (-2 * XY + c(X_sqnorms) + r(Y_sqnorms))) K_YY += wt * tf.exp(-gamma * (-2 * YY + c(Y_sqnorms) + r(Y_sqnorms))) return K_XX, K_XY, K_YY, tf.reduce_sum(wts)
Example #30
Source File: triplet_losses.py From MobileFaceNet_Tensorflow with Apache License 2.0 | 5 votes |
def _pairwise_distances(embeddings, squared=False): """Compute the 2D matrix of distances between all the embeddings. Args: embeddings: tensor of shape (batch_size, embed_dim) squared: Boolean. If true, output is the pairwise squared euclidean distance matrix. If false, output is the pairwise euclidean distance matrix. Returns: pairwise_distances: tensor of shape (batch_size, batch_size) """ # Get the dot product between all embeddings # shape (batch_size, batch_size) dot_product = tf.matmul(embeddings, tf.transpose(embeddings)) # Get squared L2 norm for each embedding. We can just take the diagonal of `dot_product`. # This also provides more numerical stability (the diagonal of the result will be exactly 0). # shape (batch_size,) square_norm = tf.diag_part(dot_product) # Compute the pairwise distance matrix as we have: # ||a - b||^2 = ||a||^2 - 2 <a, b> + ||b||^2 # shape (batch_size, batch_size) distances = tf.expand_dims(square_norm, 1) - 2.0 * dot_product + tf.expand_dims(square_norm, 0) # Because of computation errors, some distances might be negative so we put everything >= 0.0 distances = tf.maximum(distances, 0.0) if not squared: # Because the gradient of sqrt is infinite when distances == 0.0 (ex: on the diagonal) # we need to add a small epsilon where distances == 0.0 mask = tf.to_float(tf.equal(distances, 0.0)) distances = distances + mask * 1e-16 distances = tf.sqrt(distances) # Correct the epsilon added: set the distances on the mask to be exactly 0.0 distances = distances * (1.0 - mask) return distances