def call(self, inputs, training= None):
        atom_embed, protSeq_embed, atom_splits = inputs

        protSeq_len = protSeq_embed.shape[1] # protSeq inshape (batchsize, seqlen, embed_dim)
        # print(protSeq_embed[0, :, :2])
        # print(atom_embed[:10])
        protSeq_embed_gather = tf.gather(protSeq_embed, atom_splits, axis= 0)
        atom_embed_expand = tf.tile(tf.expand_dims(atom_embed, 1), [1, protSeq_len, 1]) # atom_embed (n_atom, d_atom) to ( (n_atom, protSeq_len, d_atom)
        concat_embed = tf.concat([protSeq_embed_gather, atom_embed_expand], axis = -1)
        concat_hidden = self.att_layer_1(concat_embed)
        W = self.att_layer_2(concat_hidden)
        print(W[:2])
        W = tf.squeeze(W, axis= -1) # to reduce the last 1 dimension

        W = 5 * W
        Wc = tf.exp(tf.reduce_max(W, axis= -1 ,keepdims= True))
        Sc = tf.gather(tf.segment_sum(Wc, atom_splits), atom_splits, axis= 0)
        aa = Wc / Sc
        # print(aa[:200])
        atom_embed = tf.segment_sum(aa * atom_embed, atom_splits)

        # print(W[0])
        Wp = tf.segment_max(W, atom_splits)
        # print(Wp[0])
        ap = tf.nn.softmax(Wp, axis= -1)
        # print(ap.shape, protSeq_embed.shape)
        prot_embed = tf.einsum('ij, ijk->ik', ap, protSeq_embed)

        concat_embed = tf.concat([atom_embed, prot_embed], axis = -1)
        for layer in self.dense_layer_list:
            concat_embed = layer(concat_embed)
            # concat_embed = BatchNormalization(axis = -1)(concat_embed, training= training)
            concat_embed = self.dropout_layer(concat_embed, training= training)

        return self.out_layer(concat_embed)
Example #2
0
 def testSegmentMaxGradient(self):
   data = tf.constant([1.0, 2.0, 3.0], dtype=tf.float32)
   segment_ids = tf.constant([0, 0, 1], dtype=tf.int64)
   segment_max = tf.segment_max(data, segment_ids)
   with self.test_session():
     error = tf.test.compute_gradient_error(data, [3], segment_max, [2])
     self.assertLess(error, 1e-4)
    def call(self, inputs, training= None):
        atom_embed, protSeq_embed, atom_splits, protSeq_len = inputs
        # print(atom_embed[:5])
        # print(protSeq_embed[0, :, :2])
        protSeq_embed_T = tf.transpose(protSeq_embed, (0, 2, 1))
        protSeq_embed_gather = tf.gather(protSeq_embed_T, atom_splits, axis= 0)
        protSeq_mask = tf.sequence_mask(protSeq_len, self.PROTSEQ_MAX_LEN)
        protSeq_mask_gather = tf.gather(protSeq_mask, atom_splits, axis = 0) # shape (num_atom, len_protSeq)
        W = tf.einsum('ij, jk, ikl->il', atom_embed, self.W, protSeq_embed_gather)
        # print(W[0])
        W = tf.tanh(W)
        E = -9E15 * tf.ones_like(W)
        W = tf.where(protSeq_mask_gather, W, E)
        # print(W[0])
        Wc = tf.exp(tf.reduce_max(W, axis= -1 ,keepdims= True))
        Sc = tf.gather(tf.segment_sum(Wc, atom_splits), atom_splits, axis= 0)
        aa = Wc / Sc
        # print(aa[:100])
        atom_embed = tf.segment_sum(tf.multiply(aa, atom_embed), atom_splits)

        Wp = tf.segment_max(W, atom_splits)
        ap = tf.nn.softmax(Wp, axis= -1)

        # print(ap[0])
        prot_embed = tf.einsum('ij, ijk->ik', ap, protSeq_embed)

        concat_embed = tf.concat([atom_embed, prot_embed], axis = -1)
        for layer in self.dense_layer_list:
            concat_embed = layer(concat_embed)
            # concat_embed = BatchNormalization(axis = -1)(concat_embed, training= training)
            concat_embed = self.dropout_layer(concat_embed, training= training) # reuse is a problem ? ok for dropout layer but not for batch normalization layer?
        return self.out_layer(concat_embed)
Example #4
0
def matrix_segment():
    """
    :return:
    """
    isses = tf.InteractiveSession()
    # 对角值
    X = tf.constant([5., 1., 7., 2., 3., 4., 1., 3.], dtype=tf.float32)
    s_id = [0, 0, 0, 1, 2, 2, 3, 3]

    logger.info("X\n%s" % X)
    logger.info("s_id\n%s" % s_id)
    logger.info("tf.segment_sum(X,s_id)\n {0}".format(tf.segment_sum(X, s_id)))
    logger.info("tf.segment_mean(X,s_id)\n {0}".format(
        tf.segment_mean(X, s_id).eval()))
    logger.info("tf.segment_max(X, s_id)\n {0}".format(
        tf.segment_max(X, s_id).eval()))
    logger.info("tf.segment_min(X, s_id)\n {0}".format(
        tf.segment_min(X, s_id).eval()))
    logger.info("tf.segment_prod(X, s_id)\n {0}".format(
        tf.segment_prod(X, s_id).eval()))
    logger.info("tf.unsorted_segment_sum(X, s_id)\n {0}".format(
        tf.unsorted_segment_sum(X, s_id, 2)))

    # c = tf.constant([0., 1.], dtype=tf.float32)
    # logger.info("tf.sparse_segment_sum(X, s_id)\n {0}".format(tf.sparse_segment_sum(X, c, s_id)))
    # logger.info("tf.sparse_segment_mean(X, s_id)\n {0}".format(tf.sparse_segment_mean(X, c, s_id)))
    # logger.info("tf.sparse_segment_sqrt_n(X, s_id)\n {0}".format(tf.sparse_segment_sqrt_n(X, c, s_id)))
    isses.close()
Example #5
0
 def testSegmentMaxGradient(self):
     data = tf.constant([1.0, 2.0, 3.0], dtype=tf.float32)
     segment_ids = tf.constant([0, 0, 1], dtype=tf.int64)
     segment_max = tf.segment_max(data, segment_ids)
     with self.test_session():
         error = tf.test.compute_gradient_error(data, [3], segment_max, [2])
         self.assertLess(error, 1e-4)
Example #6
0
def DeepSurv_loss(surv_time, surv_event, pat_ind, Y_hat):
    # Obtain T and E from self.Y
    # NOTE: negtive value means E = 0
    Y = surv_time * surv_event
    Y_c = tf.squeeze(Y)
    Y_hat_c = tf.squeeze(Y_hat)
    Y_hat_c = tf.gather(Y_hat_c,pat_ind)
    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
    _, 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)
    
    return loss_breslow
Example #7
0
  def get_prediction(self, latent, pool='full', device='/gpu:0', output_feat=1):
    '''
    output_feat: in prediction stage
      0: not using attributes 
      1: using attributes, use mean to combine multi-hot features
      2: using attributes, use max  to combine multi-hot features
      3: same as 2, but softmax (instead of max)
    '''
    # compute inner product between item_hidden and {user_feature_embedding}
    # then lookup to compute logits
    with tf.device(device):
      out_layer = self.i_indices[pool]
      indices_cat, indices_mulhot, segids_mulhot, lengths_mulhot = out_layer
      innerps = []

      n1 = 1 if output_feat == 0 else self.item_attributes.num_features_cat
      n2 = 0 if output_feat == 0 else self.item_attributes.num_features_mulhot

      for i in xrange(n1):
        item_emb_cat = self.item_embs2_cat[i] if self.item_output else self.item_embs_cat[i]
        i_biases_cat = self.i_biases2_cat[i] if self.item_output else self.i_biases_cat[i]
        u = latent[i] if isinstance(latent, list) else latent
        inds = indices_cat[i]
        innerp = tf.matmul(item_emb_cat, tf.transpose(u)) + i_biases_cat # Vf by mb
        innerps.append(lookup(innerp, inds)) # V by mb
      offset = self.item_attributes.num_features_cat
      
      for i in xrange(n2):
        item_embs_mulhot = self.item_embs2_mulhot[i] if self.item_output else self.item_embs_mulhot[i]
        item_biases_mulhot = self.i_biases2_mulhot[i] if self.item_output else self.i_biases_mulhot[i]
        u = latent[i+offset] if isinstance(latent, list) else latent
        lengs = lengths_mulhot[i]
        if pool == 'full':
          inds = indices_mulhot[i]
          segids = segids_mulhot[i]
          V = self.logit_size
        else:
          inds = tf.slice(indices_mulhot[i], [0], [self.sampled_mulhot_l[i]])
          segids = tf.slice(segids_mulhot[i], [0], [self.sampled_mulhot_l[i]])
          V = self.n_sampled
        innerp = tf.add(tf.matmul(item_embs_mulhot, tf.transpose(u)), 
          item_biases_mulhot)

        if output_feat == 1:
          innerps.append(tf.div(tf.unsorted_segment_sum(lookup(innerp, 
            inds), segids, V), lengs))
        elif output_feat == 2:
          innerps.append(tf.segment_max(lookup(innerp, inds), segids))  
        elif output_feat == 3:
          score_max = tf.reduce_max(innerp)
          innerp = tf.subtract(innerp, score_max)
          innerps.append(score_max + tf.log(1 + tf.unsorted_segment_sum(tf.exp(
            lookup(innerp, inds)), segids, V)))
        else:
          print('Error: Attribute combination not implemented!')
          exit(1)

      logits = tf.transpose(tf.reduce_mean(innerps, 0))
    return logits
Example #8
0
 def testSegmentMaxGradientWithTies(self):
   inputs = tf.constant([1.0], dtype=tf.float32)
   data = tf.concat_v2([inputs, inputs], 0)
   segment_ids = tf.constant([0, 0], dtype=tf.int64)
   segment_max = tf.segment_max(data, segment_ids)
   with self.test_session():
     error = tf.test.compute_gradient_error(inputs, [1], segment_max, [1])
     self.assertLess(error, 1e-4)
Example #9
0
 def testSegmentMaxGradientWithTies(self):
   inputs = tf.constant([1.0], dtype=tf.float32)
   data = tf.concat(0, [inputs, inputs])
   segment_ids = tf.constant([0, 0], dtype=tf.int64)
   segment_max = tf.segment_max(data, segment_ids)
   with self.test_session():
     error = tf.test.compute_gradient_error(inputs, [1], segment_max, [1])
     self.assertLess(error, 1e-4)
Example #10
0
def triplet_loss(logits, same_partition, diff_partition, margin=0.3):
    logits = tf.segment_mean(logits, same_partition)
    batch_size = tf.reduce_max(diff_partition) + 1
    anchor, same, diff = tf.split(logits, [batch_size, batch_size, -1])
    anchor = tf.gather(anchor, diff_partition)
    same = tf.gather(same, diff_partition)
    losses = triplet_hinge(anchor, same, diff, margin)
    losses = tf.segment_max(losses, diff_partition)
    return tf.reduce_mean(losses)
Example #11
0
def segment_softmax(xs, segments):
    """ Same as segment_logsumexp but computes the softmax values for each element"""
    maxs = tf.stop_gradient(tf.reduce_max(xs, axis=1))
    segment_maxes = tf.segment_max(maxs, segments)
    xs -= tf.expand_dims(tf.gather(segment_maxes, segments), 1)
    sums = tf.reduce_sum(tf.exp(xs), axis=1)
    sums = tf.segment_sum(sums, segments)
    sums = tf.expand_dims(tf.gather(sums, segments), 1)
    return tf.exp(xs) / sums
Example #12
0
def segment_logsumexp(xs, segments):
    """ Similar tf.segment_sum but compute logsumexp rather then sum """
    # Stop gradients following the implementation of tf.reduce_logsumexp
    maxs = tf.stop_gradient(tf.reduce_max(xs, axis=1))
    segment_maxes = tf.segment_max(maxs, segments)
    xs -= tf.expand_dims(tf.gather(segment_maxes, segments), 1)
    sums = tf.reduce_sum(tf.exp(xs), axis=1)
    return tf.log(
        tf.segment_sum(sums, segments)
    ) + segment_maxes  # todo should'nt we add an epsilon to the log?
Example #13
0
    def countss(self):
        weights = filter(lambda x: x.get_shape()[0] != 0, self.weights)
        with tf.name_scope("Counting"):
            maxed_out = []
            val = tf.constant(0.51, dtype=tf.float64)
            t = lambda x: tf.transpose(x)
            #calculate max values for each node
            for c in range(len(self.counting)):
                if c == 0 and self.node_layers[0][0].t == 'c':
                    counts = self.counting[c]*0+1
                else:
                    maxes = tf.mul(tf.transpose(tf.segment_max(tf.transpose(self.counting[c]), self.inds[c*2])), val)
                    back_maxes = tf.transpose(tf.gather(tf.transpose(maxes), self.inds[c*2]))
                    counts = tf.nn.relu(tf.round(tf.div(back_maxes, self.counting[c])))
                maxed_out.append(counts)
            updates = []
            splits = []
            #label
            self.num2 = tf.placeholder(shape=(None, len(self.node_layers[-1])), dtype=tf.float64)
            curr = self.num2

            for i in reversed(range(len(self.node_layers[1:]))):
                L = i+1
                if self.weights[L].get_shape()[0] == 0: #product node
                    curr = tf.transpose(tf.gather(tf.transpose(curr), self.inds[L], name="myprodgather"))
            
                else: #sum node
            
                    curr = tf.transpose(tf.gather(tf.transpose(curr), self.reverse_shuffle[L]))
                    if (self.input_layers[L] > 0):
                        curr, split = curr[:, :-self.input_layers[L]], curr[:, -self.input_layers[L]:]
                        splits = [split] + splits;
                    curr = tf.transpose(tf.gather(tf.transpose(curr), self.inds[L], name="mysumgather"))
                    curr = tf.mul(curr, maxed_out[L//2])
                    updates.append(tf.reduce_sum(curr, reduction_indices=0))
            

            inputs = tf.concat(1, [curr] + splits, name="lolface");
            if self.node_layers[0][0].t == 'b':
                gathered = tf.transpose(tf.gather(tf.transpose(inputs), self.inds[0]))
                updates.append(tf.reduce_sum(tf.mul(gathered, self.counting[0]), reduction_indices=0))
            else:
                important_inputs = tf.reduce_sum(inputs*self.counting[0], reduction_indices=0)
                total = self.cont[2]+tf.reduce_sum(inputs, reduction_indices=0)
                new_mu = (important_inputs + self.cont[0]*self.cont[2])/total
                mu_diff = new_mu - self.cont[0]
                other_diff = self.counting[0] - self.cont[0]
                new_sig = tf.sqrt((self.cont[1]*self.cont[1]*self.cont[2] + tf.reduce_sum(other_diff*other_diff*inputs, reduction_indices=0))/total - mu_diff*mu_diff)
                new_sums = total
                
                updates.append(new_mu)
                updates.append(new_sig)
                updates.append(new_sums)

        self.updates = updates;
    def _load_data(self):
        """
        [L]
        단어->id 및 태그->id 변환 테이블을 텐서 그래프에 추가합니다.
        """

        word2id = tf.contrib.lookup.index_table_from_tensor(
            mapping=tf.constant(self.id2word),
            num_oov_buckets=1,
            name="word2id")

        label2id = tf.contrib.lookup.index_table_from_tensor(
            mapping=tf.constant(self.id2label),
            default_value=self.label2id["O"],
            name="label2id")
        """
        [M]
        입력 데이터 파일을 읽어들여 이를 단어 id로 변환하는 텐서 그래프를 생성합니다.
        """
        input_dataset = tf.data.TextLineDataset(
            os.path.join(self.data_dir, "train.inputs"))
        batched_input_dataset = input_dataset.batch(self.hparams.batch_size)
        input_iterator = batched_input_dataset.make_initializable_iterator()
        batch_input = input_iterator.get_next()
        batch_input.set_shape([self.hparams.batch_size])
        words = tf.string_split(batch_input, " ")
        word_ids = word2id.lookup(words)
        dense_word_ids = tf.sparse_tensor_to_dense(word_ids)
        # shape = [batch_size, time]

        line_number = word_ids.indices[:, 0]
        line_position = word_ids.indices[:, 1]
        lengths = tf.segment_max(data=line_position,
                                 segment_ids=line_number) + 1
        """
        [N]
        태그 데이터 파일을 읽어들여 이를 태그 id로 변환하는 텐서 그래프를 생성합니다.
        """
        label_dataset = tf.data.TextLineDataset(
            os.path.join(self.data_dir, "train.labels"))
        batched_label_dataset = label_dataset.batch(self.hparams.batch_size)
        label_iterator = batched_label_dataset.make_initializable_iterator()
        batch_label_str = label_iterator.get_next()
        batch_label = tf.string_split(batch_label_str, " ")
        label_ids = label2id.lookup(batch_label)
        dense_label_ids = tf.sparse_tensor_to_dense(label_ids)
        # shape = [batch_size, time]

        mask = tf.sequence_mask(lengths)
        dense_label_ids = tf.boolean_mask(dense_label_ids, mask)

        self.iterator_initializers.append(input_iterator.initializer)
        self.iterator_initializers.append(label_iterator.initializer)

        return dense_word_ids, dense_label_ids, lengths
Example #15
0
def get_sequence_length(sparse_tensor):
    """
    help to get sequence length of 2-D sparse_tensor.
    """
    batch_size = tf.cast(sparse_tensor.dense_shape[0], dtype=tf.int32)
    sequence_length = (tf.segment_max(
        data=sparse_tensor.indices[:,1]+1,
        segment_ids=sparse_tensor.indices[:,0]))
    extend_zeros = tf.zeros([batch_size - tf.shape(sequence_length)[0]],
                            dtype=tf.int64)
    sequence_length = tf.concat([sequence_length, extend_zeros], axis=0)
    return sequence_length
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
Example #17
0
    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_max(X, I)
        else:
            return K.max(X, axis=-2, keepdims=(self.data_mode == 'single'))
Example #18
0
    def step(i, probs, counts, images):
        # Sample image patch
        H = sample_homography(shape, **config['homographies'])
        H_inv = invert_homography(H)
        wrapped = H_transform(image, H, interpolation='BILINEAR')
        count = H_transform(tf.ones(shape), H_inv, interpolation='NEAREST')

        # Predict detection probabilities
        input_wrapped = tf.image.resize_images(wrapped, tf.floordiv(shape, 2))
        prob = net(input_wrapped)['prob']
        prob = tf.image.resize_images(tf.expand_dims(prob, axis=-1), shape)[..., 0]

        # In theory, directly inverting the probability map tends to discard many points
        # with high probability. However experiments show that this is not an issue for
        # a large number of homographies, and is 3 times faster than an exact inverse.
        if approximate_inverse:
            prob_proj = H_transform(prob, H_inv, interpolation='BILINEAR')
        else:
            # Select the points to be mapped back to the original image
            pts = tf.where(tf.greater_equal(prob, 0.01))
            selected_prob = tf.gather_nd(prob, pts)

            # Compute the projected coordinates
            pad = tf.ones(tf.stack([tf.shape(pts)[0], tf.constant(1)]))
            pts_homogeneous = tf.concat([tf.reverse(tf.to_float(pts), axis=[1]), pad], 1)
            pts_proj = tf.matmul(pts_homogeneous, tf.transpose(flat2mat(H)[0]))
            pts_proj = pts_proj[:, :2] / tf.expand_dims(pts_proj[:, 2], axis=1)
            pts_proj = tf.to_int32(tf.round(tf.reverse(pts_proj, axis=[1])))

            # Hack: convert 2D coordinates to 1D indices in order to use tf.unique
            pts_idx = pts_proj[:, 0] * shape[1] + pts_proj[:, 1]
            pts_idx_unique, idx = tf.unique(pts_idx)

            # Keep maximum corresponding probability for each projected point
            # Hack: tf.segment_max requires sorted indices
            idx, sort_idx = tf.nn.top_k(idx, k=tf.shape(idx)[0])
            idx = tf.reverse(idx, axis=[0])
            sort_idx = tf.reverse(sort_idx, axis=[0])
            selected_prob = tf.gather(selected_prob, sort_idx)
            with tf.device('/cpu:0'):
                unique_prob = tf.segment_max(selected_prob, idx)

            # Create final probability map
            pts_proj_unique = tf.stack([tf.floordiv(pts_idx_unique, shape[1]),
                                        tf.floormod(pts_idx_unique, shape[1])], axis=1)
            prob_proj = tf.scatter_nd(pts_proj_unique, unique_prob, shape)

        probs = tf.concat([probs, tf.expand_dims(prob_proj, 0)], axis=0)
        counts = tf.concat([counts, tf.expand_dims(count, 0)], axis=0)
        images = tf.concat([images, tf.expand_dims(wrapped, 0)], axis=0)
        return i + 1, probs, counts, images
Example #19
0
 def get_centers(self, point_cloud_4d):
     point_cloud_idx = tf.cast(tf.round(point_cloud_4d), tf.int64)
     point_cloud_lin_idx = tf.squeeze(
         ravel_index(point_cloud_idx, self.grid_shape))
     vox_idx, idx, count = tf.unique_with_counts(point_cloud_lin_idx,
                                                 out_idx=tf.int32)
     vox_idx = tf.cast(vox_idx, dtype=tf.int32)
     vox_mult_idx = tf.transpose(unravel_index(vox_idx, self.grid_shape),
                                 (1, 0))
     batch_idx = tf.squeeze(tf.slice(vox_mult_idx, (0, 0), (-1, 1)))
     max_point_per_vol = tf.segment_max(count, batch_idx)
     max_point_per_vol = tf.gather(max_point_per_vol, batch_idx)
     count_normalized = tf.divide(count, max_point_per_vol)
     return vox_mult_idx, idx, count, count_normalized
Example #20
0
    def _comp_f(self):
        """
        Encodes all queries (including supporting queries)
        :return: encoded queries
        """
        with tf.device("/cpu:0"):
            max_length = tf.cast(tf.reduce_max(self._length), tf.int32)
            context_t = tf.transpose(self._context)
            context_t = tf.slice(context_t, [0, 0], tf.pack([max_length, -1]))
            embedded = tf.nn.embedding_lookup(self.input_embedding, context_t)
            embedded = tf.nn.dropout(embedded, self.keep_prob)
            batch_size = tf.shape(self._context)[0]
            batch_size_32 = tf.reshape(batch_size, [1])
            batch_size_64 = tf.cast(batch_size, tf.int64)

        with tf.device(self._device1):
            #use other device for backward rnn
            with tf.variable_scope("backward"):
                min_end = tf.segment_min(self._ends, self._span_context)
                init_state = tf.get_variable("init_state", [self._size], initializer=self._init)
                init_state = tf.reshape(tf.tile(init_state, batch_size_32), [-1, self._size])
                rev_embedded = tf.reverse_sequence(embedded, self._length, 0, 1)
                # TIME-MAJOR: [T, B, S]
                outs_bw = self._composition_function(rev_embedded, self._length - min_end, init_state)
                # reshape to all possible queries for all sequences. Dim[0]=batch_size*(max_length+1).
                # "+1" because we include the initial state
                outs_bw = tf.reshape(tf.concat(0, [tf.expand_dims(init_state, 0), outs_bw]), [-1, self._size])
                # gather respective queries via their lengths-start (because reversed sequence)
                lengths_aligned = tf.gather(self._length, self._span_context)
                out_bw = tf.gather(outs_bw, (lengths_aligned - self._ends) * batch_size_64 + self._span_context)

        with tf.device(self._device2):
            with tf.variable_scope("forward"):
                #e_inputs = [tf.reshape(e, [-1, self._size]) for e in tf.split(1, self._max_length, embedded)]
                max_start = tf.segment_max(self._starts, self._span_context)
                init_state = tf.get_variable("init_state", [self._size], initializer=self._init)
                init_state = tf.reshape(tf.tile(init_state, batch_size_32), [-1, self._size])
                # TIME-MAJOR: [T, B, S]
                outs_fw = self._composition_function(embedded, max_start, init_state)
                # reshape to all possible queries for all sequences. Dim[0]=batch_size*(max_length+1).
                # "+1" because we include the initial state
                outs_fw = tf.reshape(tf.concat(0, [tf.expand_dims(init_state, 0), outs_fw]), [-1, self._size])
                # gather respective queries via their positions (with offset of batch_size*ends)
                out_fw = tf.gather(outs_fw, self._starts * batch_size_64 + self._span_context)
            # form query from forward and backward compositions
            query = tf.contrib.layers.fully_connected(tf.concat(1, [out_fw, out_bw]), self._size,
                                                      activation_fn=None, weights_initializer=None, biases_initializer=None)
            query = tf.add_n([query, out_bw, out_fw])

        return query
Example #21
0
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 call(self, inputs, training= None):
        """

        :param inputs: [atom_embed, protSeq_embed, atom_split] in shape ( n_atoms, atom_hidden), (batchsize, seqlen, hidden), (n_atoms, )
        :return:
        """
        atom_embed, protSeq_embed, atom_split = inputs
        mol_embed = tf.segment_max(atom_embed, atom_split)
        prot_embed = tf.reduce_max(protSeq_embed, axis = 1)
        hidden = tf.concat([mol_embed, prot_embed], axis = -1)
        for layer in self.hidden_layers:
            hidden = layer(hidden)
            # hidden = BatchNormalization(axis= -1)(hidden, training = training)
            hidden = self.dropout_layer(hidden, training= training)

        output = self.output_layer(hidden)
        return output
Example #23
0
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
Example #24
0
    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)
Example #25
0
def padded_segment_reduce(vecs, segment_inds, num_segments, reduction_mode):
    """
    Reduce the vecs with segment_inds and reduction_mode
    Input:
        vecs: A Tensor of shape (batch_size, vec_dim)
        segment_inds: A Tensor containing the segment index of each
        vec row, should agree with vecs in shape[0]
    Output:
        A tensor of shape (vec_dim)
    """
    if reduction_mode == 'max':
        print('USING MAX POOLING FOR REDUCTION!')
        vecs_reduced = tf.segment_max(vecs, segment_inds)
    elif reduction_mode == 'mean':
        print('USING AVG POOLING FOR REDUCTION!')
        vecs_reduced = tf.segment_mean(vecs, segment_inds)
    vecs_reduced.set_shape([num_segments, vecs.get_shape()[1]])
    return vecs_reduced
Example #26
0
 def get_pooling_item_latent(pooling_item_embedding, pooling_item_input,
                             pooling_index_input, dimension, time_steps):
     '''
     Description: Input Pooling (need to be verified)
         time_steps: T
         dimension: D
         num_users: B
         input itemid: list by user(0 ~ B-1) by time (0 ~ T-1)
         input index: 0~T-1, T~2T-1, 2T~3T-1 ... (N-2)T~(N-1)T-1
         output: T's B * D
     '''
     pooling_item_latent = tf.gather_nd(pooling_item_embedding,
                                        pooling_item_input)
     pooling_item_latent = tf.segment_max(pooling_item_latent,
                                          pooling_index_input)
     pooling_item_latent = tf.concat([pooling_item_latent, \
         tf.zeros([time_steps - tf.mod(pooling_index_input[-1], time_steps) - 1, dimension])], 0)
     pooling_item_latent = tf.reshape(pooling_item_latent,
                                      [-1, time_steps, dimension])
     pooling_item_latent = tf.unstack(pooling_item_latent, time_steps, 1)
     return pooling_item_latent
Example #27
0
def aggregate( x, mode, minibatch_size, n_lf_permodel ) :
    # xは[ minibatch_size * n_lf_permodel, n_dim_in ]の2D tensor
    shape = x.get_shape().as_list();
    n_dim_in = shape[ 1 ];
    
    segment_ids = tf.range( minibatch_size );
    segment_ids = tf.reshape( segment_ids, [ -1, 1 ] );
    segment_ids = tf.tile( segment_ids, [ 1, n_lf_permodel ] );
    segment_ids = tf.squeeze( tf.reshape( segment_ids, [ -1, 1 ] ) );

    if( mode == "A" ) : # average pooling
        y = tf.segment_mean( x, segment_ids );
        y = tf.reshape( y, [ minibatch_size, n_dim_in ] ); # segment_mean/segment_maxするとtensorのshapeが不定になるので明らかにしておく
    elif( mode == "M" ) : # max pooling
        y = tf.segment_max( x, segment_ids );
        y = tf.reshape( y, [ minibatch_size, n_dim_in ] ); # segment_mean/segment_maxするとtensorのshapeが不定になるので明らかにしておく

    elif( mode == "B" ) : # bi-linear pooling
        # TODO
        pass;        

    return y;
Example #28
0
def build_infer_placeholder(src_ph, vocab_table):
    src_eos_id = tf.cast(vocab_table.lookup(tf.constant(EOS)), tf.int32)

    inputs = tf.string_split(src_ph)
    inputs = tf.cast(vocab_table.lookup(inputs), tf.int32)
    shape = tf.shape(inputs)
    slice_size = tf.cast(tf.stack([shape[0], MAX_SRC_LEN]), tf.int64)
    slice_start = tf.constant([0, 0], dtype=tf.int64)
    inputs = tf.sparse_slice(inputs, start=slice_start, size=slice_size)
    line_number = inputs.indices[:, 0]
    line_position = inputs.indices[:, 1]
    lengths = tf.segment_max(data=line_position, segment_ids=line_number) + 1
    inputs = tf.sparse_tensor_to_dense(inputs, src_eos_id)

    src = inputs
    src_len = lengths

    batchedInput = namedtuple("batchedInput",
                              ('initializer', 'source', 'source_length'))
    return batchedInput(initializer=None,
                        source=tf.identity(src, 'src'),
                        source_length=tf.identity(src_len, 'src_len'))
Example #29
0
    def _calculate_marginal_x_by_y_axis(self, indices):
        xs = indices[..., 1]
        ys = indices[..., 0]

        x_mins = tf.segment_min(xs, ys)
        x_maxs = tf.segment_max(xs, ys)
        y_pos = tf.range(0, tf.shape(x_mins)[0])
        y_pos = tf.cast(y_pos, tf.int64)
        left_marginal = tf.gather_nd(tf.stack([y_pos, x_mins], axis=-1),
                                     tf.where(tf.not_equal(x_mins, x_maxs)))
        right_marginal = tf.gather_nd(tf.stack([y_pos, x_maxs], axis=-1),
                                      tf.where(tf.not_equal(x_mins, x_maxs)))

        # 노이즈을 줄이기 위해 앞뒤 15% drop
        valid_counts = tf.cast(tf.shape(left_marginal)[0], tf.float32)
        drop_counts = tf.clip_by_value(tf.cast(valid_counts * 0.15, tf.int32),
                                       1, 2**31)

        left_marginal = tf.cast(left_marginal[drop_counts:-drop_counts],
                                tf.float32)
        right_marginal = tf.cast(right_marginal[drop_counts:-drop_counts],
                                 tf.float32)
        return left_marginal, right_marginal
Example #30
0
 def _get_constrained_steps(self, indices, mode):
     batch_idx = tf.reshape(
         tf.slice(indices, begin=[0, 0], size=[tf.shape(indices)[0], 1]),
         [-1])
     uniq_batch_idx, ori_batch_idx = tf.unique(batch_idx)
     step_idx = tf.reshape(
         tf.slice(indices, begin=[0, 1], size=[tf.shape(indices)[0], 1]),
         [-1])
     start_limit = None
     end_limit = None
     if mode == 'train':
         split_limit = tf.gather(tf.segment_max(step_idx, batch_idx),
                                 uniq_batch_idx)
         end_limit = tf.gather(split_limit, ori_batch_idx)
         start_limit = end_limit - self._train_steps + 1
     elif mode == 'eval':
         split_limit = tf.gather(tf.segment_min(step_idx, batch_idx),
                                 uniq_batch_idx)
         start_limit = tf.gather(split_limit, ori_batch_idx)
         end_limit = start_limit + self._eval_steps - 1
     return tf.boolean_mask(
         indices,
         tf.logical_and(step_idx >= start_limit, step_idx <= end_limit))
Example #31
0
    def _tf_dec_attention_decoder(self, enc_out, decoder_input, last_state, cell,
                          output_size=None, num_heads=1, dtype=dtypes.float32, scope=None,
                          src_mask=None, maxout_layer=False, embedding_size=None, 
                          encoder="reverse", start=None, init_const=False, bow_mask=None):
        """Decode single step version of tensorflow.models.rnn.seq2seq.attention_decoder
        """
        if num_heads < 1:
            raise ValueError("With less than 1 heads, use a non-attention decoder.")
        if output_size is None:
            output_size = cell.output_size
            
        with tf.variable_scope(scope or "attention_decoder"):
            # enc_out is a list [last_enc_state, hidden, num_heads*hidden_features, num_heads*v]
            # Note that these are computation graphs. We only use them to get the
            # shape right. During computation time, we use placeholders instead
            # because we want to specify them via input feed   
            hidden_shape = enc_out[1].get_shape()
            hidden_feature_shape = enc_out[2].get_shape()
            v_shape = enc_out[num_heads+2].get_shape()

            hidden = tf.placeholder(dtypes.float32,
                shape=[d.value for d in hidden_shape], 
                name="enc_hidden")            
            hidden_features = [tf.placeholder(dtypes.float32,
                shape=[d.value for d in hidden_feature_shape],
                name="enc_hidden_features_%d" % a) for a in xrange(num_heads)]
            v = [tf.placeholder(dtypes.float32,
                shape=[d.value for d in v_shape],
                name="enc_v_%d" % a) for a in xrange(num_heads)]
            self.enc_hidden.append(hidden)
            self.enc_hidden_features.append(hidden_features)
            self.enc_v.append(v)

            batch_size = 1
            attn_length = hidden.get_shape()[1].value
            attn_size = hidden.get_shape()[3].value
            attention_vec_size = attn_size  # Size of query vectors for attention.
            logging.info("Attn_length=%d attn_size=%d" % (attn_length, attn_size))

            def is_LSTM_cell(cell):
              if isinstance(cell, rnn_cell.LSTMCell) or \
                 isinstance(cell, rnn_cell.BasicLSTMCell):
                   return True
              return False

            def is_LSTM_cell_with_dropout(cell):
              if isinstance(cell, rnn_cell.DropoutWrapper):
                if is_LSTM_cell(cell._cell):
                   return True
              return False

            def init_state():
              logging.info("Init decoder state for bow")
              for a in xrange(num_heads):
                s = array_ops.ones(array_ops.pack([batch_size, attn_length]), dtype=dtype)
                s.set_shape([None, attn_length])
              
              # multiply with source mask, then do softmax
              if src_mask is not None:
                s = s * src_mask
              a = nn_ops.softmax(s)
              
              if isinstance(cell, BOWCell) and \
                 (is_LSTM_cell(cell.get_cell()) or \
                  is_LSTM_cell_with_dropout(cell.get_cell()) or \
                  (isinstance(cell.get_cell(), rnn_cell.MultiRNNCell) and \
                  (is_LSTM_cell(cell.get_cell()._cells[0]) or \
                   is_LSTM_cell_with_dropout(cell.get_cell()._cells[0])))):
                  # C = SUM_t i_t * C~_t (ignore i_t for now)
                  C = math_ops.reduce_sum(
                    array_ops.reshape(a, [-1, attn_length, 1, 1]) * hidden, [1, 2])                         
                  h = tanh(C)

                  if is_LSTM_cell(cell.get_cell()) or \
                    is_LSTM_cell_with_dropout(cell.get_cell()):
                    # single LSTM cell
                    return array_ops.concat(1, [C, h])
                  else:
                    # MultiRNNCell (multi LSTM cell)
                    unit = array_ops.concat(1, [C, h])
                    state = unit
                    count = 1
                    while (count < cell.get_cell().num_layers):
                      state = array_ops.concat(1, [state, unit])
                      count += 1
                    return state
              else:
                raise NotImplementedError("Need to implement decoder state initialization for non-LSTM cells")              

            def init_state_const():
              # TODO: don't hardcode (training) batch size
              b_size = 80              
              state_batch = variable_scope.get_variable("DecInit", [b_size, cell.state_size])
              state = math_ops.reduce_sum(state_batch, [0])
              state = array_ops.reshape(state, [1, cell.state_size])
              logging.info("Init decoder state: {} * {} matrix".format(1, cell.state_size))
              state = init_state()
              return state

            def keep_state():
              logging.info("Keep decoder state for bow")
              return last_state

            if encoder == "bow" and start is not None:
              if init_const:                
                last_state = control_flow_ops.cond(start, init_state_const, keep_state)
                last_state.set_shape([None, cell.state_size])
              else:
                last_state = control_flow_ops.cond(start, init_state, keep_state)
        
            def attention(query):
              """Put attention masks on hidden using hidden_features and query."""
              ds = []  # Results of attention reads will be stored here.
              if nest.is_sequence(query):  # If the query is a tuple, flatten it.
                query_list = nest.flatten(query)
                for q in query_list:  # Check that ndims == 2 if specified.
                  ndims = q.get_shape().ndims
                  if ndims:
                    assert ndims == 2
                query = array_ops.concat(1, query_list)
              for i in xrange(num_heads):
                with variable_scope.variable_scope("Attention_%d" % i):                  
                  y = linear(query, attention_vec_size, True)
                  y = array_ops.reshape(y, [-1, 1, 1, attention_vec_size])
                  # Attention mask is a softmax of v^T * tanh(...).
                  s = math_ops.reduce_sum(
                      v[i] * math_ops.tanh(hidden_features[i] + y), [2, 3])
                  # multiply with source mask, then do softmax
                  if src_mask is not None:
                    s = s * src_mask
                  a = nn_ops.softmax(s)
                  # Now calculate the attention-weighted vector d.
                  d = math_ops.reduce_sum(
                      array_ops.reshape(a, [-1, attn_length, 1, 1]) * hidden,
                      [1, 2])                  
                  ds.append(array_ops.reshape(d, [-1, attn_size]))
              return ds            

            attns = [tf.placeholder(dtypes.float32,
                shape=[1, attn_size],
                name="dec_attns_%d" % i) for i in xrange(num_heads)]
            for a in attns:  # Ensure the second shape of attention vectors is set.
                a.set_shape([None, attn_size])

            self.dec_attns.append(attns)
            
            variable_scope.get_variable_scope().reuse_variables()

            # Merge input and previous attentions into one vector of the right size.
            input_size = decoder_input.get_shape().with_rank(2)[1]
            if input_size.value is None:
              raise ValueError("Could not infer input size from input: %s" % decoder_input.name)

            x = linear([decoder_input] + attns, input_size, True)
            # Run the RNN.
            cell_output, new_state = cell(x, last_state) # run cell on combination of input and previous attn masks
            # Run the attention mechanism.
            new_attns = attention(new_state) # calculate new attention masks (attention-weighted src vector)

            if maxout_layer:
              # This tries to imitate the blocks Readout layer, consisting of Merge, Bias, Maxout, Linear, Linear
              logging.info("Output layer consists of: Merge, Bias, Maxout, Linear, Linear")
              # Merge
              with tf.variable_scope("AttnMergeProjection"):
                merge_output = linear([cell_output] + [decoder_input] + new_attns, cell.output_size, True)

              # Bias
              b = tf.get_variable("maxout_b", [cell.output_size])
              merge_output_plus_b = tf.nn.bias_add(merge_output, b)

              # Maxout
              maxout_size = cell.output_size // 2
              segment_id_list = [ [i,i] for i in xrange(maxout_size) ] # make pairs of segment ids to be max-ed over
              segment_id_list = list(chain(*segment_id_list)) # flatten list
              segment_ids = tf.constant(segment_id_list, dtype=tf.int32)
              maxout_output = tf.transpose(tf.segment_max(tf.transpose(merge_output_plus_b), segment_ids)) # transpose to get shape (cell.output_size, batch_size) and reverse         
              maxout_output.set_shape([None, maxout_size])

              # Linear, softmax0 (maxout_size --> embedding_size ), without bias
              with tf.variable_scope("MaxoutOutputProjection_0"):
                output_embed = linear([maxout_output], embedding_size, False)

              # Linear, softmax1 (embedding_size --> vocab_size), with bias
              with tf.variable_scope("MaxoutOutputProjection_1"):
                output = linear([output_embed], output_size, True)
            else:
              with variable_scope.variable_scope("AttnOutputProjection"):
                output = linear([cell_output] + new_attns, output_size, True) # calculate the output

            if bow_mask is not None:
              # Normalize output layer over subset of target words found in input bag-of-words.
              # To do this without changing the architecture, apply a mask over the output layer
              # that sets all logits for words outside the bag to zero.
              logging.info("Use bow mask to locally normalize output layer wrt bow vocabulary")
              output = output * bow_mask

            return [output] + [new_state] + new_attns
Example #32
0
    def _block(block_idx, infeats, weights_init, biases_init, pair_c_idxs,
               pair_n_idxs, pw_feats, weight_reg):
        with tf.variable_scope('block{}'.format(block_idx)):
            feats = tf.contrib.layers.fully_connected(
                inputs=infeats,
                num_outputs=cfg.gnet.reduced_dim,
                activation_fn=tf.nn.relu,
                weights_initializer=weights_init,
                weights_regularizer=weight_reg,
                biases_initializer=biases_init,
                scope='reduce_dim')

            if cfg.gnet.neighbor_feats:
                neighbor_feats = tf.contrib.layers.fully_connected(
                    inputs=infeats,
                    num_outputs=cfg.gnet.reduced_dim,
                    activation_fn=tf.nn.relu,
                    weights_initializer=weights_init,
                    weights_regularizer=weight_reg,
                    biases_initializer=biases_init,
                    scope='reduce_dim_neighbor')
            else:
                neighbor_feats = feats

            with tf.variable_scope('build_context'):
                c_feats = tf.gather(feats, pair_c_idxs)
                n_feats = tf.gather(neighbor_feats, pair_n_idxs)

                # zero out features where c_idx == n_idx
                is_id_row = tf.equal(pair_c_idxs, pair_n_idxs)
                zeros = tf.zeros(tf.shape(n_feats), dtype=feats.dtype)
                n_feats = tf.where(is_id_row, zeros, n_feats)

                feats = tf.concat([pw_feats, c_feats, n_feats], 1)

            for i in range(1, cfg.gnet.num_block_pw_fc + 1):
                feats = tf.contrib.layers.fully_connected(
                    inputs=feats,
                    num_outputs=cfg.gnet.pairfeat_dim,
                    activation_fn=tf.nn.relu,
                    weights_initializer=weights_init,
                    weights_regularizer=weight_reg,
                    biases_initializer=biases_init,
                    scope='pw_fc{}'.format(i))

            with tf.variable_scope('pooling'):
                feats = tf.segment_max(feats, pair_c_idxs, name='max')

            for i in range(1, cfg.gnet.num_block_fc):
                feats = tf.contrib.layers.fully_connected(
                    inputs=feats,
                    num_outputs=cfg.gnet.pairfeat_dim,
                    activation_fn=tf.nn.relu,
                    weights_initializer=weights_init,
                    weights_regularizer=weight_reg,
                    biases_initializer=biases_init,
                    scope='fc{}'.format(i))

            feats = tf.contrib.layers.fully_connected(
                inputs=feats,
                num_outputs=cfg.gnet.shortcut_dim,
                activation_fn=None,
                weights_initializer=weights_init,
                weights_regularizer=weight_reg,
                biases_initializer=biases_init,
                scope='fc{}'.format(cfg.gnet.num_block_fc))

            with tf.variable_scope('shortcut'):
                outfeats = tf.nn.relu(infeats + feats)
        return outfeats
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 
Example #34
0
 def test_SegmentMax(self):
     t = tf.segment_max(self.random(4, 2, 3), np.array([0, 1, 1, 2]))
     self.check(t)
Example #35
0
    def _retrieve_answer(self, query):
        """
        Retrieves answer based on the specified query. Implements consecutive updates to the query and answer.
        :return: answer, if num_hops is 0, returns query itself
        """
        query, supp_queries = tf.dynamic_partition(query, self._query_partition, 2)
        with tf.variable_scope("support"):
            num_queries = tf.shape(query)[0]

            with tf.device("/cpu:0"):
                _, supp_answer_output_ids = tf.dynamic_partition(self._answer_input, self._query_partition, 2)
                _, supp_answer_input_ids = tf.dynamic_partition(self._answer_word_input, self._query_partition, 2)
                supp_answers = tf.nn.embedding_lookup(self.output_embedding, supp_answer_output_ids)
                aligned_supp_answers = tf.gather(supp_answers, self._support_ids)  # and with respective answers

                if self._max_hops > 1:
                    # used in multihop
                    answer_words = tf.nn.embedding_lookup(self.input_embedding, supp_answer_input_ids)
                    aligned_answers_input = tf.gather(answer_words, self._support_ids)

            self.support_scores = []
            query_as_answer = tf.contrib.layers.fully_connected(query, self._size,
                                                                activation_fn=None, weights_initializer=None,
                                                                biases_initializer=None, scope="query_to_answer")
            query_as_answer = query_as_answer * tf.sigmoid(tf.get_variable("query_as_answer_gate", tuple(),
                                                                           initializer=tf.constant_initializer(0.0)))
            current_answer = query_as_answer
            current_query = query

            aligned_support = tf.gather(supp_queries, self._support_ids)  # align supp_queries with queries
            collab_support = tf.gather(query, self._collab_support_ids)  # align supp_queries with queries
            aligned_support = tf.concat(0, [aligned_support, collab_support])

            query_ids = tf.concat(0, [self._query_ids, self._collab_query_ids])
            self.answer_weights = []


            for i in range(self._max_hops):
                if i > 0:
                    tf.get_variable_scope().reuse_variables()
                collab_queries = tf.gather(current_query, self._collab_query_ids)  # align supp_queries with queries
                aligned_queries = tf.gather(current_query, self._query_ids)  # align queries
                aligned_queries = tf.concat(0, [aligned_queries, collab_queries])

                with tf.variable_scope("support_scores"):
                    scores = tf_util.batch_dot(aligned_queries, aligned_support)
                    self.support_scores.append(scores)
                    score_max = tf.gather(tf.segment_max(scores, query_ids), query_ids)
                    e_scores = tf.exp(scores - score_max)
                    norm = tf.unsorted_segment_sum(e_scores, query_ids, num_queries) + 0.00001 # for zero norms
                    norm = tf.expand_dims(norm, 1)
                    e_scores = tf.expand_dims(e_scores, 1)

                with tf.variable_scope("support_answers"):
                    aligned_supp_answers_with_collab = tf.concat(0, [aligned_supp_answers, collab_queries])
                    weighted_supp_answers = tf.unsorted_segment_sum(e_scores * aligned_supp_answers_with_collab,
                                                               query_ids, num_queries) / norm

                with tf.variable_scope("support_queries"):
                    weighted_supp_queries = tf.unsorted_segment_sum(e_scores * aligned_support, query_ids, num_queries) / norm
                
                with tf.variable_scope("answer_accumulation"):
                    answer_p_max = tf.reduce_max(tf.nn.softmax(self._score_candidates(weighted_supp_answers)), [1], keep_dims=True)
                    answer_weight = tf.contrib.layers.fully_connected(tf.concat(1, [query_as_answer * weighted_supp_answers,
                                                                                    weighted_supp_queries * current_query,
                                                                                    answer_p_max]),
                                                                      1,
                                                                      activation_fn=tf.nn.sigmoid,
                                                                      weights_initializer=tf.constant_initializer(0.0),
                                                                      biases_initializer=tf.constant_initializer(0.0),
                                                                      scope="answer_weight")

                    new_answer = answer_weight * weighted_supp_answers + current_answer

                    # this condition allows for setting varying number of hops
                    current_answer = tf.cond(tf.greater(self.num_hops, i),
                                             lambda: new_answer,
                                             lambda: current_answer)

                    self.answer_weights.append(answer_weight)

                if i < self._max_hops - 1:
                    with tf.variable_scope("query_update"):
                        # prepare subsequent query
                        aligned_answers_input_with_collab = tf.concat(0, [aligned_answers_input, collab_queries])
                        weighted_answer_words = tf.unsorted_segment_sum(e_scores * aligned_answers_input_with_collab,
                                                                        query_ids, num_queries) / norm

                        c = tf.contrib.layers.fully_connected(tf.concat(1, [current_query, weighted_supp_queries, weighted_answer_words]),
                                                              self._size, activation_fn=tf.tanh, scope="update_candidate",
                                                              weights_initializer=None, biases_initializer=None)

                        gate = tf.contrib.layers.fully_connected(tf.concat(1, [current_query, weighted_supp_queries]),
                                                                 self._size, activation_fn=tf.sigmoid,
                                                                 weights_initializer=None, scope="update_gate",
                                                                 biases_initializer=tf.constant_initializer(1))
                        current_query = gate * current_query + (1-gate) * c

            return current_answer
import tensorflow as tf
import numpy as np

input_a = np.array([[1, 1, 2], [2, 3, 4], [3, 1, 1], [2, 4, 6]])
a_seg_sum = tf.segment_sum(data=input_a, segment_ids=[0, 1, 1, 1])
a_seg_prod = tf.segment_prod(data=input_a, segment_ids=[0, 0, 1, 1])
a_seg_max = tf.segment_max(data=input_a, segment_ids=[0, 0, 0, 1])
a_seg_min = tf.segment_min(data=input_a, segment_ids=[1, 1, 1, 1])
a_seg_mean = tf.segment_mean(data=input_a, segment_ids=[0, 0, 0, 1])
a_seg_sum_num = tf.unsorted_segment_sum(data=input_a,
                                        segment_ids=[0, 1, 1, 0],
                                        num_segments=2)
a_sparse_seg_sum = tf.sparse_segment_sum(data=input_a,
                                         indices=[0, 1, 2],
                                         segment_ids=[0, 0, 1])

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(a_seg_sum), '\n', sess.run(a_seg_prod), '\n',
          sess.run(a_seg_max), '\n', sess.run(a_seg_min))
    print(sess.run(a_seg_mean), '\n', sess.run(a_seg_sum_num), '\n',
          sess.run(a_sparse_seg_sum))
Example #37
0
#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
Example #38
0
 def test_SegmentMax(self):
     t = tf.segment_max(self.random(4, 2, 3), np.array([0, 1, 1, 2]))
     self.check(t)