def create_tensor(self, in_layers=None, set_tensors=True, **kwargs): """ parent layers: atom_features, distance, distance_membership_i, distance_membership_j """ if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) self.build() atom_features = in_layers[0].out_tensor distance = in_layers[1].out_tensor distance_membership_i = in_layers[2].out_tensor distance_membership_j = in_layers[3].out_tensor distance_hidden = tf.matmul(distance, self.W_df) + self.b_df atom_features_hidden = tf.matmul(atom_features, self.W_cf) + self.b_cf outputs = tf.multiply( distance_hidden, tf.gather(atom_features_hidden, distance_membership_j)) # for atom i in a molecule m, this step multiplies together distance info of atom pair(i,j) # and embeddings of atom j(both gone through a hidden layer) outputs = tf.matmul(outputs, self.W_fc) outputs = self.activation(outputs) output_ii = tf.multiply(self.b_df, atom_features_hidden) output_ii = tf.matmul(output_ii, self.W_fc) output_ii = self.activation(output_ii) # for atom i, sum the influence from all other atom j in the molecule outputs = tf.segment_sum(outputs, distance_membership_i) - output_ii + atom_features out_tensor = outputs if set_tensors: self.trainable_variables = self.trainable_weights self.out_tensor = out_tensor return out_tensor
def create_tensor(self, in_layers=None, set_tensors=True, **kwargs): """ Perform M steps of set2set gather, detailed descriptions in: https://arxiv.org/abs/1511.06391 """ if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) self.build() # Extract atom_features atom_features = in_layers[0].out_tensor atom_split = in_layers[1].out_tensor self.c = tf.zeros((self.batch_size, self.n_hidden)) self.h = tf.zeros((self.batch_size, self.n_hidden)) for i in range(self.M): q_expanded = tf.gather(self.h, atom_split) e = tf.reduce_sum(atom_features * q_expanded, 1) e_mols = tf.dynamic_partition(e, atom_split, self.batch_size) # Add another value(~-Inf) to prevent error in softmax e_mols = [ tf.concat([e_mol, tf.constant([-1000.])], 0) for e_mol in e_mols ] a = tf.concat([tf.nn.softmax(e_mol)[:-1] for e_mol in e_mols], 0) r = tf.segment_sum(tf.reshape(a, [-1, 1]) * atom_features, atom_split) # Model using this layer must set pad_batches=True q_star = tf.concat([self.h, r], axis=1) self.h, self.c = self.LSTMStep(q_star, self.c) out_tensor = q_star if set_tensors: self.variables = self.trainable_weights self.out_tensor = out_tensor return out_tensor
def model(self, dataset, labels, isEval=None): with tf.variable_scope('softmax_linear', reuse=isEval): embeddings = tf.get_variable("embeddings", [self.vocabulary_size, self.embedding_size], initializer=tf.random_uniform_initializer(minval=-1.0, maxval=1.0, seed=self.SEED)) segments = tf.constant([x // self.context_window for x in range(self.cbowbatch_size)]) weights = tf.get_variable("weights", [self.vocabulary_size, self.embedding_size], initializer=tf.truncated_normal_initializer(0.0, 1.0 / math.sqrt(float(self.embedding_size)), seed=self.SEED)) biases = tf.get_variable("biases", [self.vocabulary_size], initializer=tf.constant_initializer(0.0)) # Look up embeddings for inputs. embed = tf.nn.embedding_lookup(embeddings, dataset) compressed_embeddings = tf.segment_sum(embed, segments) # merging couple of embeded words into one input # Compute the softmax loss, using a sample of the negative labels each time. loss = tf.reduce_mean( tf.nn.sampled_softmax_loss(weights, biases, compressed_embeddings, labels, self.num_sampled, self.vocabulary_size)) similarity, normalized_embeddings, embeddings = self.similarity(embeddings, dataset) if isEval == None: return loss if isEval == True: return similarity, normalized_embeddings, embeddings
def create_tensor(self, in_layers=None, set_tensors=True, **kwargs): """ parent layers: atom_features, atom_split """ if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) self.build() outputs = in_layers[0].out_tensor atom_split = in_layers[1].out_tensor if self.gaussian_expand: outputs = self.gaussian_histogram(outputs) output_molecules = tf.segment_sum(outputs, atom_split) if self.gaussian_expand: output_molecules = tf.matmul(output_molecules, self.W) + self.b output_molecules = self.activation(output_molecules) out_tensor = output_molecules if set_tensors: self.variables = self.trainable_weights self.out_tensor = out_tensor return out_tensor
def testGradientMatchesSegmentSum(self): # Strategy: compute the gradient for UnsortedSegmentSum and SegmentSum # and compare the outputs, which should be identical. # NB: for this test to work, indices must be valid for SegmentSum, namely # it must be sorted, the indices must be contiguous, and num_segments # must be max(indices) + 1. indices = [0, 0, 1, 1, 1, 2, 3, 4, 5] n = len(indices) num_cols = 2 shape = [n, num_cols] num_segments = max(indices) + 1 with self.test_session(): tf_x, np_x = self._input(shape, dtype=tf.float64) # Results from UnsortedSegmentSum unsorted_s = tf.unsorted_segment_sum(data=tf_x, segment_ids=indices, num_segments=num_segments) unsorted_jacob_t, unsorted_jacob_n = gradient_checker.ComputeGradient( tf_x, shape, unsorted_s, [num_segments, num_cols], x_init_value=np_x.astype(np.double), delta=1) # Results from SegmentSum sorted_s = tf.segment_sum(data=tf_x, segment_ids=indices) sorted_jacob_t, sorted_jacob_n = gradient_checker.ComputeGradient( tf_x, shape, sorted_s, [num_segments, num_cols], x_init_value=np_x.astype(np.double), delta=1) self.assertAllClose(unsorted_jacob_t, sorted_jacob_t, rtol=1e-3, atol=1e-3) self.assertAllClose(unsorted_jacob_n, sorted_jacob_n, rtol=1e-3, atol=1e-3)
def testSegmentIdsSize(self): shape = [4, 4] with self.test_session(): tf_x, _ = self._input(shape) indices = [0, 1] s = tf.segment_sum(data=tf_x, segment_ids=indices) with self.assertRaisesOpError("segment_ids should be the same size"): s.eval()
def testSegmentIdsInvalid7(self): shape = [4, 4] with self.test_session(): tf_x, _ = self._input(shape) indices = [0, 0, 0, -2] s = tf.segment_sum(data=tf_x, segment_ids=indices) with self.assertRaisesOpError("segment ids must be >= 0"): s.eval()
def testSegmentIdsValid(self): # This is a baseline for the following SegmentIdsInvalid* tests. shape = [4, 4] with self.test_session(): tf_x, _ = self._input(shape) indices = [0, 0, 0, 1] result = tf.segment_sum(data=tf_x, segment_ids=indices).eval() self.assertAllEqual([[15, 18, 21, 24], [13, 14, 15, 16]], result)
def testSegmentIdsInvalid2(self): shape = [4, 4] with self.test_session(): tf_x, _ = self._input(shape) indices = [1, 1, 2, 2] s = tf.segment_sum(data=tf_x, segment_ids=indices) with self.assertRaisesOpError("segment ids do not start at 0"): s.eval()
def testSegmentIdsInvalid4(self): shape = [4, 4] with self.test_session(): tf_x, _ = self._input(shape) indices = [0, 1, 0, 1] s = tf.segment_sum(data=tf_x, segment_ids=indices) with self.assertRaisesOpError("segment ids are not increasing by 1"): s.eval()
def remap_keys(sparse_tensor): # Current indices of our SparseTensor that we need to fix bad_indices = sparse_tensor.indices # Current values of our SparseTensor that we need to fix bad_values = sparse_tensor.values # Group by the batch_indices and get the count for each size = tf.segment_sum(data = tf.ones_like(bad_indices[:,0], dtype = tf.int64), segment_ids = bad_indices[:,0]) - 1 # The number of batch_indices (this should be batch_size unless it is a partially full batch) length = tf.shape(size, out_type = tf.int64)[0] # Finds the cumulative sum which we can use for indexing later cum = tf.cumsum(size) # The offsets between each example in the batch due to our concatentation of the keys in the decode_example method length_range = tf.range(start = 0, limit = length, delta = 1, dtype = tf.int64) # Indices of the SparseTensor's indices member of the rows we added by the concatentation of our keys in the decode_example method cum_range = cum + length_range # The keys that we have extracted back out of our concatentated SparseTensor gathered_indices = tf.squeeze(tf.gather(bad_indices, cum_range)[:,1]) # The enumerated row indices of the SparseTensor's indices member sparse_indices_range = tf.range(tf.shape(bad_indices, out_type = tf.int64)[0], dtype = tf.int64) # We want to find here the row indices of the SparseTensor's indices member that are of our actual data and not the concatentated rows # So we want to find the intersection of the two sets and then take the opposite of that x = sparse_indices_range s = cum_range # Number of multiples we are going to tile x, which is our sparse_indices_range tile_multiples = tf.concat([tf.ones(tf.shape(tf.shape(x)), dtype=tf.int64), tf.shape(s, out_type = tf.int64)], axis = 0) # Expands x, our sparse_indices_range, into a rank 2 tensor and then multiplies the rows by 1 (no copying) and the columns by the number of examples in the batch x_tile = tf.tile(tf.expand_dims(x, -1), tile_multiples) # Essentially a vectorized logical or, that we then negate x_not_in_s = ~tf.reduce_any(tf.equal(x_tile, s), -1) # The SparseTensor's indices that are our actual data by using the boolean_mask we just made above applied to the entire indices member of our SparseTensor selected_indices = tf.boolean_mask(tensor = bad_indices, mask = x_not_in_s, axis = 0) # Apply the same boolean_mask to the entire values member of our SparseTensor to get the actual values data selected_values = tf.boolean_mask(tensor = bad_values, mask = x_not_in_s, axis = 0) # Need to replace the first column of our selected_indices with keys, so we first need to tile our gathered_indices tiling = tf.tile(input = tf.expand_dims(gathered_indices[0], -1), multiples = tf.expand_dims(size[0] , -1)) # We have to repeatedly apply the tiling to each example in the batch # Since it is jagged we cannot use tf.map_fn due to the stacking of the TensorArray, so we have to create our own custom version def loop_body(i, tensor_grow): return i + 1, tf.concat(values = [tensor_grow, tf.tile(input = tf.expand_dims(gathered_indices[i], -1), multiples = tf.expand_dims(size[i] , -1))], axis = 0) _, result = tf.while_loop(lambda i, tensor_grow: i < length, loop_body, [tf.constant(1, dtype = tf.int64), tiling]) # Concatenate tiled keys with the 2nd column of selected_indices selected_indices_fixed = tf.concat([tf.expand_dims(result, -1), tf.expand_dims(selected_indices[:, 1], -1)], axis = 1) # Combine everything together back into a SparseTensor remapped_sparse_tensor = tf.SparseTensor(indices = selected_indices_fixed, values = selected_values, dense_shape = sparse_tensor.dense_shape) return remapped_sparse_tensor
def testSegmentIdsInvalid5(self): shape = [4, 4] with self.test_session(): tf_x, _ = self._input(shape) indices = [0, 1, 2, 0] s = tf.segment_sum(data=tf_x, segment_ids=indices) with self.assertRaisesOpError( r"Segment id 1 out of range \[0, 1\), probably " "because 'segment_ids' input is not sorted."): s.eval()
def edge_normalize(edge_states, sender_node_ids, n_nodes=None, sorted=True): """ Args: edge_states: batch_size x n_edges x n_edge_dims sender_node_ids: n_edges sorted: the list sender_node_ids is sorted or not Returns: edge_states_norm: batch_size x n_nodes x n_edge_dims """ edge_states = tf.transpose(edge_states, perm=[1, 0, 2]) # n_edges x batch_size x n_edge_dims if sorted: edge_states_max = tf.segment_max( edge_states, sender_node_ids) # n_nodes x batch_size x n_edge_dims edge_states_max = tf.gather( edge_states_max, sender_node_ids) # n_edges x batch_size x n_edge_dims edge_states_exp = tf.exp( edge_states - edge_states_max) # n_edges x batch_size x n_edge_dims edge_states_sumexp = tf.segment_sum( edge_states_exp, sender_node_ids) # n_nodes x batch_size x n_edge_dims edge_states_sumexp = tf.gather( edge_states_sumexp, sender_node_ids) # n_edges x batch_size x n_edge_dims edge_states_norm = edge_states_exp / edge_states_sumexp # n_edges x batch_size x n_edge_dims else: if n_nodes is None: raise ValueError('`n_nodes` should not be None') edge_states_max = tf.unsorted_segment_max( tf.transpose(edge_states, perm=[1, 0, 2]), sender_node_ids, n_nodes) # n_nodes x batch_size x n_edge_dims edge_states_max = tf.gather( edge_states_max, sender_node_ids) # n_edges x batch_size x n_edge_dims edge_states_exp = tf.exp( edge_states - edge_states_max) # n_edges x batch_size x n_edge_dims edge_states_sumexp = tf.unsorted_segment_sum( edge_states_exp, sender_node_ids, n_nodes) # n_nodes x batch_size x n_edge_dims edge_states_sumexp = tf.gather( edge_states_sumexp, sender_node_ids) # n_edges x batch_size x n_edge_dims edge_states_norm = edge_states_exp / edge_states_sumexp # n_edges x batch_size x n_edge_dims edge_states_norm = tf.transpose( edge_states_norm, perm=[1, 0, 2]) # batch_size x n_edges x n_edge_dims return edge_states_norm
def create_tensor(self, in_layers=None, set_tensors=True, **kwargs): """Creates weave tensors. parent layers: [atom_features, pair_features], pair_split, atom_to_pair """ activation = activations.get(self.activation) # Get activations if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) self.build() atom_features = in_layers[0].out_tensor pair_features = in_layers[1].out_tensor pair_split = in_layers[2].out_tensor atom_to_pair = in_layers[3].out_tensor AA = tf.matmul(atom_features, self.W_AA) + self.b_AA AA = activation(AA) PA = tf.matmul(pair_features, self.W_PA) + self.b_PA PA = activation(PA) PA = tf.segment_sum(PA, pair_split) A = tf.matmul(tf.concat([AA, PA], 1), self.W_A) + self.b_A A = activation(A) if self.update_pair: AP_ij = tf.matmul( tf.reshape(tf.gather(atom_features, atom_to_pair), [-1, 2 * self.n_atom_input_feat]), self.W_AP) + self.b_AP AP_ij = activation(AP_ij) AP_ji = tf.matmul( tf.reshape( tf.gather(atom_features, tf.reverse(atom_to_pair, [1])), [-1, 2 * self.n_atom_input_feat]), self.W_AP) + self.b_AP AP_ji = activation(AP_ji) PP = tf.matmul(pair_features, self.W_PP) + self.b_PP PP = activation(PP) P = tf.matmul(tf.concat([AP_ij + AP_ji, PP], 1), self.W_P) + self.b_P P = activation(P) else: P = pair_features self.out_tensors = [A, P] if set_tensors: self.variables = self.trainable_weights self.out_tensor = A return self.out_tensors
def call(self, inputs): outputs, atom_split = inputs if self.gaussian_expand: outputs = self.gaussian_histogram(outputs) output_molecules = tf.segment_sum(outputs, atom_split) if self.gaussian_expand: output_molecules = tf.matmul(output_molecules, self.W) + self.b output_molecules = self.activation(output_molecules) return output_molecules
def call(self, inputs): if self.data_mode == 'graph': X = inputs[0] I = inputs[1] if K.ndim(I) == 2: I = I[:, 0] else: X = inputs if self.data_mode == 'graph': return tf.segment_sum(X, I) else: return K.sum(X, axis=-2, keepdims=(self.data_mode == 'single'))
def create_tensor(self, in_layers=None, set_tensors=True, **kwargs): """Creates weave tensors. parent layers: [atom_features, pair_features], pair_split, atom_to_pair """ activation = activations.get(self.activation) # Get activations if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) self.build() atom_features = in_layers[0].out_tensor pair_features = in_layers[1].out_tensor pair_split = in_layers[2].out_tensor atom_to_pair = in_layers[3].out_tensor AA = tf.matmul(atom_features, self.W_AA) + self.b_AA AA = activation(AA) PA = tf.matmul(pair_features, self.W_PA) + self.b_PA PA = activation(PA) PA = tf.segment_sum(PA, pair_split) A = tf.matmul(tf.concat([AA, PA], 1), self.W_A) + self.b_A A = activation(A) if self.update_pair: AP_ij = tf.matmul( tf.reshape( tf.gather(atom_features, atom_to_pair), [-1, 2 * self.n_atom_input_feat]), self.W_AP) + self.b_AP AP_ij = activation(AP_ij) AP_ji = tf.matmul( tf.reshape( tf.gather(atom_features, tf.reverse(atom_to_pair, [1])), [-1, 2 * self.n_atom_input_feat]), self.W_AP) + self.b_AP AP_ji = activation(AP_ji) PP = tf.matmul(pair_features, self.W_PP) + self.b_PP PP = activation(PP) P = tf.matmul(tf.concat([AP_ij + AP_ji, PP], 1), self.W_P) + self.b_P P = activation(P) else: P = pair_features self.out_tensors = [A, P] if set_tensors: self.variables = self.trainable_weights self.out_tensor = A return self.out_tensors
def map_features_binary(state,action_,user_fbin_baseline,Fs=250,L=1000): #generate the number of fft points per freq bin pp_fbin = generate_frequency_bins(L,Fs,fbin_min,fbin_max,fbin_steps) #generate a segment mapping for the tensor - like pp_fbin but each element in this array corresponds to a single element of the tensor segmap = generate_segment_map(pp_fbin,1) with tf.name_scope("Map_Binary_Features"): #generate fft spectro=tf.fft(tf.slice(state,[0,0],[-1, L]),name="raw_fft") #transpose the sensor so that we can use segment_sum below spectro=tf.transpose(spectro,name="pre_binning_transpose") #reduce the tensor using binning spectro_binned = tf.segment_sum(spectro,segmap,name="f_binning") #remove the first and last two bins, corresponding to <fbin_min, >fbin_max, and other fft half spectro_binned = spectro_binned[1:-2] #make the tensor spectro_binned = tf.transpose(spectro_binned,name="post_binning_transpose") #take fft power, transfer to new variable (no longer processing fft) state_features_tmp = tf.abs(spectro_binned) with tf.name_scope("Generate_Binary_State_Features"): #Extend this out so we can do a simple max with user baseline bins state_features_tmp = tf.tile(state_features_tmp,[1,feat_per_fbin_per_ch],name="expand_state_feat") #flatten the state features #NOTE: state_features_tmp should be #elec x #feat_per_fbin x #fbins state_features_tmp = tf.reshape(state_features_tmp,[-1,feat_per_fbin_per_ch,fbin_steps],name="mfb_reshape_state_feat") #TODO ensure theat GTE is right order on arguments GTE=tf.greater_equal(user_fbin_baseline,state_features_tmp) GTE=tf.cast(GTE,dtype=tf.int8) GTE_shifted=tf.pad(GTE, [[0,0],[1,0],[0,0]], mode='CONSTANT') GTE_shifted=tf.slice(GTE_shifted,[0,0,0],GTE.get_shape()) state_features_bin=tf.not_equal(GTE,GTE_shifted,"isolate_tf") state_features_bin = tf.reshape(state_features_bin,[-1],name='flatten_state_space_tensor') with tf.name_scope("Generate_Binary_Action_Features"): action_features_bin = act_to_actbin(action_) action_features_bin = tf.reshape(action_features_bin,[-1],name='flatten_act_space_tensor') features = tf.concat([state_features_bin,action_features_bin],0) return features
def Social_influence_one_cal(batch_size,u_fellows,u_fellows_user,user_emb_p,user_emb_q,vision_beta_a,vision_beta_b,e_ab_w,e_ab_b,gather_social): #social influence fellow_pa=(tf.nn.embedding_lookup(user_emb_p, u_fellows)) fellow_qa=(tf.nn.embedding_lookup(user_emb_q, u_fellows)) fellow_pb=(tf.nn.embedding_lookup(user_emb_p, u_fellows_user)) fellow_qb=(tf.nn.embedding_lookup(user_emb_q, u_fellows_user)) x_fellow=tf.concat([fellow_pa,fellow_pb,fellow_qa,fellow_qb,vision_beta_a,vision_beta_b],1) #size_up*60 e_ab_temp=tf.nn.elu(tf.matmul(x_fellow,e_ab_w)+e_ab_b)#(size_up*60 * 60*20)+20=size_up*20 e_ab_temp_sum=(tf.reduce_sum(e_ab_temp,1, keep_dims=True)) e_ab_temp_sum=tf.where(e_ab_temp_sum>88,tf.ones_like(e_ab_temp_sum)*88,e_ab_temp_sum) e_ab=e_ab_temp_sum#tf.exp(e_ab_temp_sum)+0.001#tf.exp((tf.reduce_sum(e_ab_temp,1, keep_dims=True)))#size_up*1 molecular_e_ab=tf.multiply(e_ab,fellow_qb)#size_up*15 denominator_e_ab=e_ab#size_up*1 part_mole=tf.segment_sum(molecular_e_ab,gather_social) part_denom=tf.segment_sum(denominator_e_ab,gather_social) beta_all=tf.multiply(part_mole,tf.reciprocal(part_denom)) beta_all=tf.where(tf.is_nan(beta_all),tf.ones_like(beta_all)*0.001,beta_all) return beta_all
def create_tensor(self, in_layers=None, set_tensors=True, **kwargs): """ description and explanation refer to deepchem.nn.WeaveLayer parent layers: [atom_features, pair_features], pair_split, atom_to_pair """ if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) self.build() atom_features = in_layers[0].out_tensor[0] pair_features = in_layers[0].out_tensor[1] pair_split = in_layers[1].out_tensor atom_to_pair = in_layers[2].out_tensor AA = tf.matmul(atom_features, self.W_AA) + self.b_AA AA = self.activation(AA) PA = tf.matmul(pair_features, self.W_PA) + self.b_PA PA = self.activation(PA) PA = tf.segment_sum(PA, pair_split) A = tf.matmul(tf.concat([AA, PA], 1), self.W_A) + self.b_A A = self.activation(A) if self.update_pair: AP_ij = tf.matmul( tf.reshape(tf.gather(atom_features, atom_to_pair), [-1, 2 * self.n_atom_input_feat]), self.W_AP) + self.b_AP AP_ij = self.activation(AP_ij) AP_ji = tf.matmul( tf.reshape( tf.gather(atom_features, tf.reverse(atom_to_pair, [1])), [-1, 2 * self.n_atom_input_feat]), self.W_AP) + self.b_AP AP_ji = self.activation(AP_ji) PP = tf.matmul(pair_features, self.W_PP) + self.b_PP PP = self.activation(PP) P = tf.matmul(tf.concat([AP_ij + AP_ji, PP], 1), self.W_P) + self.b_P P = self.activation(P) else: P = pair_features out_tensor = [A, P] if set_tensors: self.variables = self.trainable_weights self.out_tensor = out_tensor return out_tensor
def layer_wise_loss(cluster_vocab_size, cluster_labels, item_labels, item2cluster, weights, biases, used_model): uniq_clusters, _ = tf.unique(cluster_labels) whether_include_clusters = tf.sparse_to_dense( uniq_clusters, [cluster_vocab_size], tf.ones_like(uniq_clusters, dtype=tf.bool), default_value=False, validate_indices=False) whether_include_items = tf.gather(whether_include_clusters, item2cluster) included_items = tf.reshape(tf.where(whether_include_items), [-1]) included_clusters = tf.gather(item2cluster, included_items) included_weights = tf.gather(weights, included_items) included_biases = tf.gather(biases, included_items) cluster_included_indices = tf.where( tf.equal( tf.tile(tf.expand_dims(cluster_labels, 1), [1, tf.shape(included_clusters)[0]]), tf.tile(tf.expand_dims(included_clusters, 0), [tf.shape(cluster_labels)[0], 1]) ) ) cluster_idx = tf.reshape( tf.slice( cluster_included_indices, begin=[0, 0], size=[tf.shape(cluster_included_indices)[0], 1] ), [-1] ) included_idx = tf.reshape( tf.slice( cluster_included_indices, begin=[0, 1], size=[tf.shape(cluster_included_indices)[0], 1] ), [-1] ) included_model = tf.gather(used_model, cluster_idx) included_logits = tf.add( tf.reduce_sum( included_model * tf.gather(included_weights, included_idx), 1), tf.gather(included_biases, included_idx)) exp_included_logits = tf.exp(included_logits) label_logits = tf.add( tf.reduce_sum( used_model * tf.gather(weights, item_labels), 1), tf.gather(biases, item_labels)) included_sum_exp_logits = tf.segment_sum(exp_included_logits, cluster_idx) item_label_probs = tf.exp(label_logits) / included_sum_exp_logits item_label_losses = -tf.log(tf.maximum(item_label_probs, 1e-07)) return item_label_probs, item_label_losses
def sampling_typed_SNIS_rs(nodes_nbrs, nbr_segment, edge_features, num_sample): unique_nbrs = tf.unique_with_counts(nbr_segment) p = 1.0 / tf.cast(tf.gather(unique_nbrs.count, unique_nbrs.idx), tf.float32) num_nbrs = tf.size(unique_nbrs.y) q = tf.gather( tf.ones(num_nbrs) / tf.cast(num_nbrs, dtype=tf.float32), unique_nbrs.idx) samples = tf.unique( tf.cast(tf.multinomial(tf.log([q]), num_sample)[0], tf.int32)).y infos = tf.sparse_to_dense(tf.reshape(tf.contrib.framework.sort(samples), [-1, 1]), output_shape=tf.shape(unique_nbrs.idx), sparse_values=tf.ones_like(samples, dtype=tf.int32)) partitions = tf.gather(infos, unique_nbrs.idx) samples_to_gather = tf.cast( tf.dynamic_partition(tf.range(tf.size(partitions)), partitions, 2)[1], tf.int32) sampled_p = tf.gather(p, samples_to_gather) sampled_q = tf.gather(tf.gather(q, unique_nbrs.idx), samples_to_gather) sampled_unique_nodes = tf.unique_with_counts( tf.gather(nbr_segment, samples_to_gather)) # weight1 = tf.cast(tf.gather(sampled_unique_nodes.count, sampled_unique_nodes.idx), dtype=tf.float32) w0 = sampled_p / sampled_q wpq = w0 / tf.gather(tf.segment_sum(w0, sampled_unique_nodes.idx), sampled_unique_nodes.idx) weight = wpq num_sampled_edges = tf.size(samples_to_gather) num_sampled_nbrs = tf.size(samples) sampled_nbrs = tf.gather(nodes_nbrs, samples_to_gather) sampled_segments = tf.cast(tf.gather(nbr_segment, samples_to_gather), tf.int32) sampled_features = tf.gather(edge_features, samples_to_gather) return [ weight, num_sampled_edges, num_sampled_nbrs, sampled_nbrs, sampled_segments, sampled_features ]
def test_segment_sum(): a = np.arange(16).reshape((4, 4)) # b = np.array([0,1,0,1]) ''' segment ids are not increasing, 如果ids不是增序就用unsorted_segment_sum ''' b = np.array([0, 0, 2, 2]) result = tf.segment_sum(a, b) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) r = sess.run(result) print(r)
def test_segment(): seg_ids = tf.constant([0, 1, 1, 2, 2]) x = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [4, 3, 5, 3], [6, 1, 4, 0], [6, 1, 4, 0]]) with tf.Session() as sess: # 按seg_ids进行加法 print(tf.segment_sum(x, seg_ids).eval()) # 按seg_ids进行乘法 print(tf.segment_prod(x, seg_ids).eval()) # 按seg_ids进行min运算 print(tf.segment_min(x, seg_ids).eval()) # 按seg_ids进行max运算 print(tf.segment_max(x, seg_ids).eval()) # 按seg_ids进行mean运算 print(tf.segment_mean(x, seg_ids).eval())
def segment_top_k(x, I, ratio, top_k_var): """ Returns indices to get the top K values in x segment-wise, according to the segments defined in I. K is not fixed, but it is defined as a ratio of the number of elements in each segment. :param x: a rank 1 tensor; :param I: a rank 1 tensor with segment IDs for x; :param ratio: float, ratio of elements to keep for each segment; :param top_k_var: a tf.Variable created without shape validation (i.e., `tf.Variable(0.0, validate_shape=False)`); :return: a rank 1 tensor containing the indices to get the top K values of each segment in x. """ num_nodes = tf.segment_sum(tf.ones_like(I), I) # Number of nodes in each graph cumsum = tf.cumsum(num_nodes) # Cumulative number of nodes (A, A+B, A+B+C) cumsum_start = cumsum - num_nodes # Start index of each graph n_graphs = tf.shape(num_nodes)[0] # Number of graphs in batch max_n_nodes = tf.reduce_max(num_nodes) # Order of biggest graph in batch batch_n_nodes = tf.shape(I)[0] # Number of overall nodes in batch to_keep = tf.ceil(ratio * tf.cast(num_nodes, tf.float32)) to_keep = tf.cast(to_keep, tf.int32) # Nodes to keep in each graph index = tf.range(batch_n_nodes) index = (index - tf.gather(cumsum_start, I)) + (I * max_n_nodes) y_min = tf.reduce_min(x) dense_y = tf.ones((n_graphs * max_n_nodes, )) # subtract 1 to ensure that filler values do not get picked dense_y = dense_y * tf.cast(y_min - 1, tf.float32) # top_k_var is a variable with unknown shape defined in the elsewhere dense_y = tf.assign(top_k_var, dense_y, validate_shape=False) dense_y = tf.scatter_update(dense_y, index, x) dense_y = tf.reshape(dense_y, (n_graphs, max_n_nodes)) perm = tf.argsort(dense_y, direction='DESCENDING') perm = perm + cumsum_start[:, None] perm = tf.reshape(perm, (-1, )) to_rep = tf.tile(tf.constant([1., 0.]), (n_graphs, )) rep_times = tf.reshape( tf.concat((to_keep[:, None], (max_n_nodes - to_keep)[:, None]), -1), (-1, )) mask = repeat(to_rep, rep_times) perm = tf.boolean_mask(perm, mask) return perm
def create_tensor(self, in_layers=None, **kwargs): """description and explanation refer to deepchem.nn.DTNNGather parent layers: atom_features, atom_membership """ if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) self.build() output = in_layers[0].out_tensor atom_membership = in_layers[1].out_tensor for i, W in enumerate(self.W_list): output = tf.matmul(output, W) + self.b_list[i] output = self.activation(output) output = tf.segment_sum(output, atom_membership) self.out_tensor = output
def sigmoid_start_loss(self, model): correct_start_indices = tf.transpose( tf.stack([ tf.cast(model.answer_context_indices, tf.int32), self.answer_starts ])) correct_start_values = tf.ones([tf.shape(self.answer_starts)[0]], dtype=tf.float32) is_start_correct = tf.scatter_nd(correct_start_indices, correct_start_values, tf.shape(model.start_scores)) # Get relevant scores correct_start_scores = tf.gather_nd(model.start_scores, correct_start_indices) correct_start_mask = -1000.0 * is_start_correct incorrect_start_scores = model.start_scores + correct_start_mask if SIGMOID_NEGATIVE_SAMPLES > 0: incorrect_start_scores, _ = tf.nn.top_k(incorrect_start_scores, k=SIGMOID_NEGATIVE_SAMPLES) # Compute Cross Entropy Loss correct_start_loss = tf.nn.sigmoid_cross_entropy_with_logits( logits=correct_start_scores, labels=tf.ones(tf.shape(correct_start_scores))) incorrect_start_loss = tf.nn.sigmoid_cross_entropy_with_logits( logits=incorrect_start_scores, labels=tf.zeros(tf.shape(incorrect_start_scores))) # Bring incorrect_start_loss into [Q] shape incorrect_start_loss = tf.segment_sum( tf.reduce_sum(incorrect_start_loss, axis=1), model.context_partition) # Now, expand to [len(answers)] shape to match correct_start_loss incorrect_start_loss = tf.gather(incorrect_start_loss, self.question_partition) with tf.name_scope("summaries"): self._train_summaries += [ tf.summary.scalar("sigmoid_correct_start_loss", tf.reduce_mean(correct_start_loss)), tf.summary.scalar("sigmoid_incorrect_start_loss", tf.reduce_mean(incorrect_start_loss)) ] return correct_start_loss + incorrect_start_loss
def _load_vertex_embeddings(labels, embeddings): """ Creates a sparse tensor which describes the labels attached to each vertex. Parameters ---------- labels: dictionary of label data. """ packed_labels = labels['packed_labels'] packed_labels_lengths = labels['packed_labels_lengths'] packed_labels_shape = tf.shape(packed_labels) packed_labels_lengths_shape = tf.shape(packed_labels_lengths) packed_labels_flat = tf.reshape(packed_labels, [-1]) label_embeddings_flat = tf.nn.embedding_lookup(embeddings, packed_labels_flat) if len(packed_labels_lengths.shape) > 1: segments_flat = tf.reshape( array_ops.batch_length_to_segment(packed_labels_lengths, packed_labels_shape[-1]), [-1]) else: segments_flat = array_ops.repeat( tf.range(tf.size(packed_labels_lengths), dtype=tf.int32), packed_labels_lengths) embeddings_flat = tf.segment_sum(label_embeddings_flat, segments_flat) output_embedding_shape = tf.stack( (packed_labels_lengths_shape[0], packed_labels_lengths_shape[1] + 1, embeddings.shape[1]), axis=0, name='vertex_embedding_shape') if len(packed_labels_lengths.shape) > 1: expected_num_segments = packed_labels_lengths_shape[0] * ( packed_labels_lengths_shape[1] + 1) length_to_pad = expected_num_segments - tf.shape(embeddings_flat)[0] embeddings_flat = tf.pad( embeddings_flat, tf.reshape(tf.stack((0, length_to_pad, 0, 0), axis=0), [2, 2])) embeddings = tf.reshape(embeddings_flat, output_embedding_shape) return embeddings
def call(self, inputs): if self.data_mode == 'graph': X, I = inputs if K.ndim(I) == 2: I = I[:, 0] else: X = inputs inputs_linear = K.dot(X, self.lg_kernel) + self.lg_bias attn_map = K.dot(X, self.attn_kernel) + self.attn_bias attn_map = K.sigmoid(attn_map) masked_inputs = inputs_linear * attn_map if self.data_mode in {'single', 'batch'}: output = K.sum(masked_inputs, axis=-2, keepdims=self.data_mode=='single') else: output = tf.segment_sum(masked_inputs, I) return output
def eval(self, Dx): with tf.name_scope(self.name): with tf.name_scope("eval"): if self.partition is None: Dx_sumsq = tf.segment_sum(Dx**2, self.grpidx) #Dx_sumsq = tf.sparse_tensor_dense_matmul(self.grpmat, Dx**2) Dx_norms = tf.sqrt(Dx_sumsq) return tf.reshape( self.lam * tf.matmul(tf.transpose(self.sqrt_sizes), Dx_norms), ()) else: Dx_sumsq = (Dx**2).apply_binary(self.grpidx, tf.segment_sum) Dx_norms = Dx_sumsq.apply(tf.sqrt) product = self.sqrt_sizes.apply_binary( Dx_norms, lambda x, y: tf.matmul(tf.transpose(x), y)) return tf.reshape(self.lam * tf.add_n(product.tensors), ())
def segment_softmax(scores, partition): """Given scores and a partition, converts scores to probs by performing softmax over all rows within a partition.""" # Subtract max max_per_partition = tf.segment_max(tf.reduce_max(scores, axis=1), partition) scores -= tf.expand_dims(tf.gather(max_per_partition, partition), axis=1) # Compute probs scores_exp = tf.exp(scores) scores_exp_sum_per_partition = tf.segment_sum( tf.reduce_sum(scores_exp, axis=1), partition) probs = scores_exp / tf.expand_dims( tf.gather(scores_exp_sum_per_partition, partition), axis=1) return probs
def TextExtract(text, win_size, dict_handle, weight, dim_input, dim_output, max_term_count=12): indices, ids, values, offsets = mstf.dssm_xletter( input=text, win_size=win_size, dict_handle=dict_handle, max_term_count=max_term_count) offsets_to_dense = tf.segment_sum(tf.ones_like(offsets), offsets) batch_id = tf.cumsum(offsets_to_dense[:-1]) #dense offset lei jia index_tensor = tf.concat( [tf.expand_dims(batch_id, axis=-1), tf.expand_dims(indices, axis=-1)], axis=-1) value_tensor = ids dense_shape = tf.concat([ tf.shape(offsets), tf.expand_dims(tf.reduce_max(indices) + 1, axis=-1) ], axis=0) text_tensor = tf.SparseTensor(indices=tf.cast(index_tensor, tf.int64), values=value_tensor, dense_shape=tf.cast(dense_shape, tf.int64)) text_padding = tf.reduce_max(indices) + 1 text_tensor = tf.sparse_reshape(text_tensor, [-1]) text_tensor, text_mask = tf.sparse_fill_empty_rows(text_tensor, dim_input - 1) text_vecs = tf.nn.embedding_lookup_sparse(weight, text_tensor, None, combiner='sum') text_vecs = tf.transpose( tf.multiply(tf.transpose(text_vecs), 1 - tf.cast(text_mask, dtype=tf.float32))) text_vecs = tf.reshape(text_vecs, [-1, text_padding, dim_output]) step_mask = tf.equal(tf.reduce_sum(text_vecs, axis=2), 0) step_mask = tf.where(step_mask, -math.inf * tf.ones_like(step_mask, dtype=tf.float32), tf.zeros_like(step_mask, dtype=tf.float32)) return text_vecs, text_padding, step_mask
def xletter_feature_extractor(text,model_prefix,input_mode, op_dict=None,xletter_cnt=None,win_size=None,dim_xletter_emb=None): with tf.variable_scope("xletter_layer", reuse=tf.AUTO_REUSE): if input_mode=='mstf': xletter_emb = tf.get_variable(name='xletter_emb_' + model_prefix, shape = [xletter_cnt * win_size, dim_xletter_emb]) indices, ids, values, offsets = mstf.dssm_xletter(input=text, win_size=win_size, dict_handle=op_dict) offsets_to_dense = tf.segment_sum(tf.ones_like(offsets), offsets) batch_id = tf.cumsum(offsets_to_dense[:-1]) index_tensor = tf.concat([tf.expand_dims(batch_id,axis=-1), tf.expand_dims(indices,axis=-1)],axis=-1) value_tensor = ids dense_shape = tf.concat([tf.shape(offsets),tf.expand_dims(tf.reduce_max(indices) + 1,axis=-1)],axis=0) text_tensor = tf.SparseTensor(indices=tf.cast(index_tensor,tf.int64), values = value_tensor, dense_shape=tf.cast(dense_shape,tf.int64)) #conv text_tensor = tf.sparse_reshape(text_tensor,[-1]) text_tensor,text_mask = tf.sparse_fill_empty_rows(text_tensor,0) text_vecs = tf.nn.embedding_lookup_sparse(xletter_emb,text_tensor,None,combiner='sum') text_vecs = tf.where(~text_mask, text_vecs, tf.zeros_like(text_vecs)) text_vecs = tf.reshape(text_vecs,[-1,tf.reduce_max(indices) + 1,dim_xletter_emb]) step_mask = ~tf.equal(tf.reduce_sum(text_vecs,axis=2),0) sequence_length = tf.cast(tf.count_nonzero(step_mask,axis=1),tf.int32) elif input_mode=='pyfunc': query_split = tf.string_split(text,';') term_split = tf.string_split(query_split.values,',') xletter_tensor_indices = tf.transpose(tf.stack([tf.gather(query_split.indices[:,0],term_split.indices[:,0]),tf.gather(query_split.indices[:,1],term_split.indices[:,0])])) xletter_tensor = tf.SparseTensor(indices = xletter_tensor_indices, values = tf.string_to_number(term_split.values,out_type=tf.int32), dense_shape = query_split.dense_shape) xletter_emb = tf.get_variable(name='xletter_emb_' + model_prefix, shape = [xletter_cnt * win_size, dim_xletter_emb]) xletter_tensor_reshape = tf.sparse_reshape(xletter_tensor,[-1]) xletter_tensor,text_mask = tf.sparse_fill_empty_rows(xletter_tensor_reshape,0) xletter_vecs = tf.nn.embedding_lookup_sparse(xletter_emb, xletter_tensor, None, combiner='sum') xletter_vecs = tf.where(~text_mask, xletter_vecs, tf.zeros_like(xletter_vecs)) text_vecs = tf.reshape(xletter_vecs, shape=tf.stack([-1,tf.reduce_max(query_split.indices[:,1])+1,dim_xletter_emb])) step_mask = ~tf.equal(tf.reduce_sum(text_vecs,axis=2),0) sequence_length = tf.cast(tf.count_nonzero(step_mask,axis=1),tf.int32) elif input_mode=='pyfunc_batch': indices, values, dense_shape = tf.py_func(op_dict.batch_xletter_extractor,[text],[tf.int64,tf.int32,tf.int64]) xletter_tensor = tf.SparseTensor(indices = indices, values = values, dense_shape = dense_shape) xletter_emb = tf.get_variable(name='xletter_emb_' + model_prefix, shape = [xletter_cnt * win_size, dim_xletter_emb]) xletter_tensor_reshape = tf.sparse_reshape(xletter_tensor,[-1]) xletter_tensor,text_mask = tf.sparse_fill_empty_rows(xletter_tensor_reshape,0) xletter_vecs = tf.nn.embedding_lookup_sparse(xletter_emb, xletter_tensor, None, combiner='sum') xletter_vecs = tf.where(~text_mask, xletter_vecs, tf.zeros_like(xletter_vecs)) text_vecs = tf.reshape(xletter_vecs, shape=tf.stack([-1,dense_shape[1],dim_xletter_emb])) step_mask = ~tf.equal(tf.reduce_sum(text_vecs,axis=2),0) sequence_length = tf.cast(tf.count_nonzero(step_mask,axis=1),tf.int32) else: NotImplementedError return text_vecs, step_mask, sequence_length
def _create_loss(self): """ Define the loss function. Notes ----- The loss function definded here is negative log of Breslow Approximation partial likelihood function. See more in "Breslow N., 'Covariance analysis of censored survival data, ' Biometrics 30.1(1974):89-99.". """ with tf.name_scope("loss"): # Obtain T and E from self.Y # NOTE: negtive value means E = 0 Y_c = tf.squeeze(self.Y) Y_hat_c = tf.squeeze(self.Y_hat) Y_label_T = tf.abs(Y_c) Y_label_E = tf.cast(tf.greater(Y_c, 0), dtype=tf.float32) Obs = tf.reduce_sum(Y_label_E) Y_hat_hr = tf.exp(Y_hat_c) Y_hat_cumsum = tf.log(tf.cumsum(Y_hat_hr)) # Start Computation of Loss function # Get Segment from T unique_values, segment_ids = tf.unique(Y_label_T) # Get Segment_max loss_s2_v = tf.segment_max(Y_hat_cumsum, segment_ids) # Get Segment_count loss_s2_count = tf.segment_sum(Y_label_E, segment_ids) # Compute S2 loss_s2 = tf.reduce_sum(tf.multiply(loss_s2_v, loss_s2_count)) # Compute S1 loss_s1 = tf.reduce_sum(tf.multiply(Y_hat_c, Y_label_E)) # Compute Breslow Loss loss_breslow = tf.divide(tf.subtract(loss_s2, loss_s1), Obs) # Compute Regularization Term Loss reg_item = tf.contrib.layers.l1_l2_regularizer( self.config["L1_reg"], self.config["L2_reg"]) loss_reg = tf.contrib.layers.apply_regularization( reg_item, tf.get_collection("var_weight")) # Loss function = Breslow Function + Regularization Term self.loss = tf.add(loss_breslow, loss_reg)
def call(self, x): """Execute this layer on input tensors. Parameters ---------- x: list of Tensor should be [atom_features: n_atoms*n_embedding, distance_matrix: n_pairs*n_distance, atom_membership: n_atoms distance_membership_i: n_pairs, distance_membership_j: n_pairs, ] Returns ------- tf.Tensor new embeddings for atoms, same shape as x[0] """ self.build() atom_features = x[0] distance = x[1] distance_membership_i = x[3] distance_membership_j = x[4] distance_hidden = tf.matmul(distance, self.W_df) + self.b_df #distance_hidden = self.activation(distance_hidden) atom_features_hidden = tf.matmul(atom_features, self.W_cf) + self.b_cf #atom_features_hidden = self.activation(atom_features_hidden) outputs = tf.multiply(distance_hidden, tf.gather(atom_features_hidden, distance_membership_j)) # for atom i in a molecule m, this step multiplies together distance info of atom pair(i,j) # and embeddings of atom j(both gone through a hidden layer) outputs = tf.matmul(outputs, self.W_fc) outputs = self.activation(outputs) output_ii = tf.multiply(self.b_df, atom_features_hidden) output_ii = tf.matmul(output_ii, self.W_fc) output_ii = self.activation(output_ii) # for atom i, sum the influence from all other atom j in the molecule outputs = tf.segment_sum(outputs, distance_membership_i) - output_ii + atom_features return outputs
def call(self, inputs, training=False): f_ = inputs shape = tf.stack([f_['n_links'], self.hparams.link_state_dim], axis=0) link_state = tf.zeros(shape) shape = tf.stack([f_['n_paths'], self.hparams.path_state_dim - 1], axis=0) path_state = tf.concat( [tf.expand_dims(f_['traffic'], axis=1), tf.zeros(shape)], axis=1) links = f_['links'][0:f_["n_total"]] paths = f_['paths'][0:f_["n_total"]] seqs = f_['sequances'][0:f_["n_total"]] for _ in range(self.hparams.T): h_tild = tf.gather(link_state, links) ids = tf.stack([paths, seqs], axis=1) max_len = tf.reduce_max(seqs) + 1 shape = tf.stack( [f_['n_paths'], max_len, self.hparams.link_state_dim]) lens = tf.segment_sum(data=tf.ones_like(paths), segment_ids=paths) link_inputs = tf.scatter_nd(ids, h_tild, shape) outputs, path_state = tf.nn.dynamic_rnn(self.path_update, link_inputs, sequence_length=lens, initial_state=path_state, dtype=tf.float32) m = tf.gather_nd(outputs, ids) m = tf.unsorted_segment_sum(m, links, f_['n_links']) _, link_state = self.edge_update(m, link_state) # wait for tf 1.11 #link_state,_ = self.edge_update(m, [link_state]) r = self.readout(path_state, training=training) # remove to have inference from path state # Thsi forces additive model for delay #r = tf.gather(r,links) #r = tf.segment_sum(r,segment_ids=paths) return r
def call(self, inputs, training=False): f_ = inputs shape = tf.stack([f_['n_links'], self.hparams.link_state_dim], axis=0) link_state = tf.zeros(shape) shape = tf.stack([f_['n_paths'], self.hparams.path_state_dim - 1], axis=0) path_state = tf.concat([ tf.expand_dims(f_['traffic'][0:f_["n_paths"]], axis=1), tf.zeros(shape) ], axis=1) links = f_['links'] paths = f_['paths'] seqs = f_['sequances'] for _ in range(self.hparams.T): h_tild = tf.gather(link_state, links) ids = tf.stack([paths, seqs], axis=1) max_len = tf.reduce_max(seqs) + 1 shape = tf.stack( [f_['n_paths'], max_len, self.hparams.link_state_dim]) lens = tf.segment_sum(data=tf.ones_like(paths), segment_ids=paths) link_inputs = tf.scatter_nd(ids, h_tild, shape) outputs, path_state = tf.nn.dynamic_rnn(self.path_update, link_inputs, sequence_length=lens, initial_state=path_state, dtype=tf.float32) m = tf.gather_nd(outputs, ids) m = tf.unsorted_segment_sum(m, links, f_['n_links']) #Keras cell expects a list link_state, _ = self.edge_update(m, [link_state]) if self.hparams.learn_embedding: r = self.readout(path_state, training=training) else: r = self.readout(tf.stop_gradient(path_state), training=training) return r
def __call__(self, nodes, segments): nodes_size = int(nodes.get_shape()[1]) pre_sum_fn = lambda x: msgnet.defaults.mlp( x, [nodes_size, nodes_size // 2], activation=msgnet.defaults.nonlinearity, weights_initializer=msgnet.defaults.initializer, ) post_sum_fn = lambda x: msgnet.defaults.mlp( x, [nodes_size // 2, self.output_size], activation=msgnet.defaults.nonlinearity, weights_initializer=msgnet.defaults.initializer, ) pre_sum = tf.identity(pre_sum_fn(nodes), name="node_contribution") tf.add_to_collection("node_contribution", pre_sum) post_sum = tf.segment_sum(pre_sum, segments) graph_out = post_sum_fn(post_sum) return graph_out
def create_tensor(self, in_layers=None, **kwargs): """description and explanation refer to deepchem.nn.DAGGather parent layers: atom_features, membership """ if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) # Add trainable weights self.build() # Extract atom_features atom_features = in_layers[0].out_tensor membership = in_layers[1].out_tensor # Extract atom_features graph_features = tf.segment_sum(atom_features, membership) # sum all graph outputs outputs = self.DAGgraph_step(graph_features, self.W_list, self.b_list) self.out_tensor = outputs
def call(self, inputs): if self.data_mode == 'graph': X, I = inputs if K.ndim(I) == 2: I = I[:, 0] else: X = inputs attn_coeff = K.dot(X, self.attn_kernel) attn_coeff = K.squeeze(attn_coeff, -1) attn_coeff = K.softmax(attn_coeff) if self.data_mode == 'single': output = K.dot(attn_coeff[None, ...], X) elif self.data_mode == 'batch': output = K.batch_dot(attn_coeff, X) else: output = attn_coeff[:, None] * X output = tf.segment_sum(output, I) return output
def forward(self, self_vecs, neigh_vecs, segment_ids=None): """ Update node's embedding based on its neighbors. Args: self_vecs: batch nodes' embeddings with shape [B, D] neigh_vecs: neighbor nodes' embeddings with shape [total_nbrs, D] segment_ids: segment ids that indicates neighbor nodes' belonging, shape [total_nbrs] Returns: updated batch nodes' embedding vector [B, H] """ if segment_ids is None: # sampled GCN neigh_vecs = tf.reduce_sum(neigh_vecs, axis=1) else: # full neighbor GCN neigh_vecs = tf.segment_sum(data=neigh_vecs, segment_ids=segment_ids) updated_vecs = tf.reduce_sum([self_vecs, neigh_vecs], axis=0) return self._fc(updated_vecs)
def _compute_vert_context_soft(self, edge_factor, vert_factor, reuse=False): """ attention-based vertex(node) message pooling """ out_edge = utils.pad_and_gather( edge_factor, self.edge_pair_mask_inds[:, 0] ) # from rel_pair_segment_inds and contains the indices of the relations which are going out in_edge = utils.pad_and_gather( edge_factor, self.edge_pair_mask_inds[:, 1] ) # 100 x 512 i.e. edge_factor[self.edge_pair_mask_inds[:,1],:] self.edge_pair_mask_inds[:,1] is 100,1 # gather correspounding vert factors vert_factor_gathered = tf.gather( vert_factor, self.edge_pair_segment_inds ) # will tell you which vert_factor correspond to which segment id maybe? # concat outgoing edges and ingoing edges with gathered vert_factors out_edge_w_input = tf.concat(concat_dim=1, values=[out_edge, vert_factor_gathered ]) # 100 x 1024 in_edge_w_input = tf.concat(concat_dim=1, values=[in_edge, vert_factor_gathered]) # compute compatibility scores (self.feed(out_edge_w_input).fc( 1, relu=False, reuse=reuse, name='out_edge_w_fc').sigmoid(name='out_edge_score')) (self.feed(in_edge_w_input).fc( 1, relu=False, reuse=reuse, name='in_edge_w_fc').sigmoid(name='in_edge_score')) out_edge_w = self.get_output('out_edge_score') in_edge_w = self.get_output('in_edge_score') # weight the edge factors with computed weigths out_edge_weighted = tf.mul(out_edge, out_edge_w) in_edge_weighted = tf.mul(in_edge, in_edge_w) edge_sum = out_edge_weighted + in_edge_weighted vert_ctx = tf.segment_sum(edge_sum, self.edge_pair_segment_inds) return vert_ctx
def mathOp(self): x = tf.constant([[1, 2], [3, 4]], dtype=tf.int32) y = tf.constant([[4, 3], [3, 2]], dtype=tf.int32) x_add_y = tf.add(x, y) x_mul_y = tf.matmul(x, y) log_x = tf.log(x) x_sum_1 = tf.reduce_sum(x, axis=[1], keepdims=False) x_sum_2 = tf.reduce_sum(x, axis=[0], keepdims=True) #Segments the tensor acccording to segment_id(item with same id in the same segment) #and computes a segmented sum of the data data = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=tf.int32) segment_id = tf.constant([0, 0, 0, 1, 1, 2, 2, 2, 2, 2], dtype=tf.int32) x_seg_sum = tf.segment_sum(data, segment_id)
def create_tensor(self, in_layers=None, set_tensors=True, **kwargs): """ parent layers: atom_features, atom_membership """ if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) self.build() output = in_layers[0].out_tensor atom_membership = in_layers[1].out_tensor for i, W in enumerate(self.W_list[:-1]): output = tf.matmul(output, W) + self.b_list[i] output = self.activation(output) output = tf.matmul(output, self.W_list[-1]) + self.b_list[-1] if self.output_activation: output = self.activation(output) output = tf.segment_sum(output, atom_membership) out_tensor = output if set_tensors: self.variables = self.trainable_weights self.out_tensor = out_tensor return out_tensor
def create_tensor(self, in_layers=None, set_tensors=True, **kwargs): """ parent layers: atom_features, membership """ if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) # Add trainable weights self.build() # Extract atom_features atom_features = in_layers[0].out_tensor membership = in_layers[1].out_tensor # Extract atom_features graph_features = tf.segment_sum(atom_features, membership) # sum all graph outputs outputs = self.DAGgraph_step(graph_features, self.W_list, self.b_list, **kwargs) out_tensor = outputs if set_tensors: self.variables = self.trainable_weights self.out_tensor = out_tensor return out_tensor
def forward(self, atom_features, atom_to_pair): out = tf.expand_dims(tf.gather(atom_features, atom_to_pair[:, 1]), 2) out = tf.squeeze(tf.matmul(self.A, out), axis=2) out = tf.segment_sum(out, atom_to_pair[:, 0]) return out
# Variables. embeddings = tf.Variable( tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0)) softmax_weights = tf.Variable( tf.truncated_normal([vocabulary_size, embedding_size], stddev=1.0 / math.sqrt(embedding_size))) softmax_biases = tf.Variable(tf.zeros([vocabulary_size])) # Model. # Look up embeddings for inputs. embed = tf.nn.embedding_lookup(embeddings, train_dataset) # sum every `context_window` word embedding into one segment_ids = tf.constant([i // context_window for i in range(batch_size)]) embed = tf.segment_sum(embed, segment_ids) # Compute the softmax loss, using a sample of the negative labels each time. loss = tf.reduce_mean( tf.nn.sampled_softmax_loss(softmax_weights, softmax_biases, embed, train_labels, num_sampled, vocabulary_size)) # Optimizer. optimizer = tf.train.AdagradOptimizer(1.0).minimize(loss) # Compute the similarity between minibatch examples and all embeddings. # We use the cosine distance: norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True)) normalized_embeddings = embeddings / norm valid_embeddings = tf.nn.embedding_lookup( normalized_embeddings, valid_dataset)
tf_conv1 = tf.nn.conv2d(tf_input,ww,strides=[1,1,1,1],padding='VALID') tf_deconv1 = tf.nn.conv2d_transpose(value=tf_conv1,filter=ww,output_shape=[batch_size,ny,nx,nl],strides=[1,1,1,1],padding='VALID') tf_error = tf.reduce_mean(tf.square(tf_deconv1 - tf_input)) ## tf_error = tf.nn.l2_loss(tf_deconv1 - tf_input) qqq = tf.square(tf_conv1) ooo = tf.reduce_sum(qqq,3,keep_dims=True) rrr = qqq / (tf.tile(ooo,[1,1,1,nf_risa])+1e-16) tf_local_entropy1 = tf.reduce_sum(rrr * (-tf.log(rrr+1e-16)),3) tf_entropy = tf.reduce_mean(tf_local_entropy1) tf_simple1 = tf.square(tf_conv1) seg24 = tf.constant([0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11]) tf_t_simple1 = tf.transpose(tf_simple1) tf_sparce1 = tf.reduce_mean(tf.sqrt(tf.segment_sum(tf_t_simple1,seg24))) # tf_score = tf_error # tf_score = tf_error * lambda_s + tf_sparce1 tf_score = lambda_s * tf_error + tf_entropy optimizer = tf.train.AdagradOptimizer(learning_rate=learning_rate) train = optimizer.minimize(tf_score) sess.run(tf.initialize_all_variables()) iii_bin = np.arange(batch_size,nn,batch_size) iii_nn = np.arange(nn) iii_batches = np.split(iii_nn,iii_bin) for tt in range(tmax):
#Segmentation Examples import tensorflow as tf sess = tf.InteractiveSession() seg_ids = tf.constant([0,1,1,2,2]); # Group indexes : 0|1,2|3,4 tens1 = tf.constant([[2, 5, 3, -5], [0, 3,-2, 5], [4, 3, 5, 3], [6, 1, 4, 0], [6, 1, 4, 0]]) # A sample constant matrix tf.segment_sum(tens1, seg_ids).eval() # Sum segmentation tf.segment_prod(tens1, seg_ids).eval() # Product segmantation tf.segment_min(tens1, seg_ids).eval() # minimun value goes to group tf.segment_max(tens1, seg_ids).eval() # maximum value goes to group tf.segment_mean(tens1, seg_ids).eval() # mean value goes to group
#!/usr/bin/env python import tensorflow as tf data = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]]) result = tf.segment_sum(data, tf.constant([0, 0, 1])) with tf.Session() as sess: print(sess.run(result))
import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import tensorflow as tf sess = tf.InteractiveSession() seg_ids = tf.constant([0,1,1,2,2]) # Group indexes : 0|1,2|3,4 tens1 = tf.constant([[2, 5, 3, -5], [0, 3,-2, 5], [4, 3, 5, 3], [6, 1, 4, 0], [6, 1, 4, 0]]) # A sample constant m print('\nseg_ids->', seg_ids.eval()) print('tens1->', tens1.eval()) print("\ntf.segment_sum(tens1, seg_ids).eval() ") # Sum segmen print(tf.segment_sum(tens1, seg_ids).eval() ) # Sum segmen print("\ntf.segment_prod(tens1, seg_ids).eval() ") # Product segmen print(tf.segment_prod(tens1, seg_ids).eval() ) # Product segmen print(tf.segment_min(tens1, seg_ids).eval() ) # minimun value goes to print(tf.segment_max(tens1, seg_ids).eval() ) # maximum value goes to print(tf.segment_mean(tens1, seg_ids).eval() ) # mean value goes to group
# Model. # Look up embeddings for inputs. embed = tf.nn.embedding_lookup(embeddings, train_dataset) # seq_ids only needs to be generated once so do this as a numpy array rather than a tensor. seq_ids = np.zeros(batch_size, dtype=np.int32) cur_id = -1 for i in range(batch_size): if i % context_window == 0: cur_id = cur_id + 1 seq_ids[i] = cur_id print (seq_ids) # use segment_sum to add together the related words and reduce the output to be num_labels in size. final_embed = tf.segment_sum(embed, seq_ids) # Compute the softmax loss, using a sample of the negative labels each time. loss = tf.reduce_mean( tf.nn.sampled_softmax_loss(softmax_weights, softmax_biases, final_embed, train_labels, num_sampled, vocabulary_size)) # Optimizer. optimizer = tf.train.AdagradOptimizer(1.0).minimize(loss) # Compute the similarity between minibatch examples and all embeddings. # We use the cosine distance: norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True)) normalized_embeddings = embeddings / norm valid_embeddings = tf.nn.embedding_lookup( normalized_embeddings, valid_dataset)
def testSegmentIdsShape(self): shape = [4, 4] tf_x, _ = self._input(shape) indices = tf.constant([0, 1, 2, 2], shape=[2, 2]) with self.assertRaises(ValueError): tf.segment_sum(data=tf_x, segment_ids=indices)
def test_SegmentSum(self): t = tf.segment_sum(self.random(4, 2, 3), np.array([0, 1, 1, 2])) self.check(t)