python类reduce_all()的实例源码

saliency_map.py 文件源码 项目:tensorflow-adversarial 作者: gongzhitaao 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def _jsma2_impl(model, x, yind, epochs, eps, clip_min, clip_max, score_fn):

    def _cond(k, xadv):
        return tf.less(k, epochs)

    def _body(k, xadv):
        ybar = model(xadv)

        dy_dx = tf.gradients(ybar, xadv)[0]

        # gradients of target w.r.t input
        yt = tf.gather_nd(ybar, yind)
        dt_dx = tf.gradients(yt, xadv)[0]

        # gradients of non-targets w.r.t input
        do_dx = dy_dx - dt_dx

        c0 = tf.logical_or(eps < 0, xadv < clip_max)
        c1 = tf.logical_or(eps > 0, xadv > clip_min)
        cond = tf.reduce_all([dt_dx >= 0, do_dx <= 0, c0, c1], axis=0)
        cond = tf.to_float(cond)

        # saliency score for each pixel
        score = cond * score_fn(dt_dx, do_dx)

        shape = score.get_shape().as_list()
        dim = _prod(shape[1:])
        score = tf.reshape(score, [-1, dim])

        a = tf.expand_dims(score, axis=1)
        b = tf.expand_dims(score, axis=2)
        score2 = tf.reshape(a + b, [-1, dim*dim])
        ij = tf.argmax(score2, axis=1)

        i = tf.to_int32(ij / dim)
        j = tf.to_int32(ij) % dim

        dxi = tf.one_hot(i, dim, on_value=eps, off_value=0.0)
        dxj = tf.one_hot(j, dim, on_value=eps, off_value=0.0)
        dx = tf.reshape(dxi + dxj, [-1] + shape[1:])

        xadv = tf.stop_gradient(xadv + dx)
        xadv = tf.clip_by_value(xadv, clip_min, clip_max)

        return k+1, xadv

    _, xadv = tf.while_loop(_cond, _body, (0, tf.identity(x)),
                            back_prop=False, name='_jsma2_batch')
    return xadv
model.py 文件源码 项目:densecap-tensorflow 作者: rampage644 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def split_proposals(proposals, proposals_num, gt, gt_num, iou, scores, cross_boundary_mask):
    '''Generate batches from proposals and ground truth boxes

    Idea is to drastically reduce number of proposals to evaluate. So, we find those
    proposals that have IoU > 0.7 with _any_ ground truth and mark them as positive samples.
    Proposals with IoU < 0.3 with _all_ ground truth boxes are considered negative. All
    other proposals are discarded.

    We generate batch with at most half of examples being positive. We also pad them with negative
    have we not enough positive proposals.

    proposals: N x 4 tensor
    proposal_num: N
    gt: M x 4 tensor
    gt_num: M
    iou: N x M tensor of IoU between every proposal and ground truth
    scores: N x 2 tensor with scores object/not-object
    cross_boundary_mask: N x 1 Tensor masking out-of-image proposals
    '''
    # now let's get rid of non-positive and non-negative samples
    # Sample is considered positive if it has IoU > 0.7 with _any_ ground truth box
    # XXX: maximal IoU ground truth proposal should be treated as positive
    positive_mask = tf.reduce_any(tf.greater(iou, 0.7), axis=1) & cross_boundary_mask

    # Sample would be considered negative if _all_ ground truch box
    # have iou less than 0.3
    negative_mask = tf.reduce_all(tf.less(iou, 0.3), axis=1) & cross_boundary_mask

    # Select only positive boxes and their corresponding predicted scores
    positive_boxes = tf.boolean_mask(proposals, positive_mask)
    positive_scores = tf.boolean_mask(scores, positive_mask)
    positive_labels = tf.reduce_mean(tf.ones_like(positive_scores), axis=1)

    # Same for negative
    negative_boxes = tf.boolean_mask(proposals, negative_mask)
    negative_scores = tf.boolean_mask(scores, negative_mask)
    negative_labels = tf.reduce_mean(tf.zeros_like(negative_scores), axis=1)

    return (
        (positive_boxes, positive_scores, positive_labels),
        (negative_boxes, negative_scores, negative_labels)
    )
layers.py 文件源码 项目:tefla 作者: openAGI 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def merge(tensors_list, mode, axis=1, name='merge', outputs_collections=None, **kwargs):
    """
    Merge op

    Args:
        tensor_list: A list `Tensors` to merge
        mode: str, available modes are
            ['concat', 'elemwise_sum', 'elemwise_mul', 'sum',
                'mean', 'prod', 'max', 'min', 'and', 'or']
        name: a optional scope/name of the layer
        outputs_collections: The collections to which the outputs are added.

    Returns:
        A `Tensor` representing the results of the repetition operation.

    Raises:
        ValueError: If 'kernel_size' is not a 2-D list
    """
    assert len(tensors_list) > 1, "Merge required 2 or more tensors."

    with tf.name_scope(name):
        tensors = [l for l in tensors_list]
        if mode == 'concat':
            output = tf.concat(tensors, axis=axis)
        elif mode == 'elemwise_sum':
            output = tensors[0]
            for i in range(1, len(tensors)):
                output = tf.add(output, tensors[i])
        elif mode == 'elemwise_mul':
            output = tensors[0]
            for i in range(1, len(tensors)):
                output = tf.multiply(output, tensors[i])
        elif mode == 'sum':
            output = tf.reduce_sum(tf.concat(tensors, axis=axis), axis=axis)
        elif mode == 'mean':
            output = tf.reduce_mean(tf.concat(tensors, axis=axis), axis=axis)
        elif mode == 'prod':
            output = tf.reduce_prod(tf.concat(tensors, axis=axis), axis=axis)
        elif mode == 'max':
            output = tf.reduce_max(tf.concat(tensors, axis=axis), axis=axis)
        elif mode == 'min':
            output = tf.reduce_min(tf.concat(tensors, axis=axis), axis=axis)
        elif mode == 'and':
            output = tf.reduce_all(tf.concat(tensors, axis=axis), axis=axis)
        elif mode == 'or':
            output = tf.reduce_any(tf.concat(tensors, axis=axis), axis=axis)
        else:
            raise Exception("Unknown merge mode", str(mode))
        return _collect_named_outputs(outputs_collections, name, output)

    return output
seq2seq_utils.py 文件源码 项目:tefla 作者: openAGI 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def next_inputs(self, time, outputs, state, sample_ids, name=None):
        with tf.name_scope(name, "ScheduledOutputTrainingHelperNextInputs",
                           [time, outputs, state, sample_ids]):
            (finished, base_next_inputs, state) = (
                super(ScheduledOutputTrainingHelper, self).next_inputs(
                    time=time,
                    outputs=outputs,
                    state=state,
                    sample_ids=sample_ids,
                    name=name))

            def maybe_sample():
                """Perform scheduled sampling."""

                def maybe_concatenate_auxiliary_inputs(outputs_, indices=None):
                    """Concatenate outputs with auxiliary inputs, if they exist."""
                    if self._auxiliary_input_tas is None:
                        return outputs_

                    next_time = time + 1
                    auxiliary_inputs = nest.map_structure(
                        lambda ta: ta.read(next_time), self._auxiliary_input_tas)
                    if indices is not None:
                        auxiliary_inputs = tf.gather_nd(
                            auxiliary_inputs, indices)
                    return nest.map_structure(
                        lambda x, y: tf.concat((x, y), -1),
                        outputs_, auxiliary_inputs)

                if self._next_input_layer is None:
                    return tf.where(
                        sample_ids, maybe_concatenate_auxiliary_inputs(outputs),
                        base_next_inputs)

                where_sampling = tf.cast(
                    tf.where(sample_ids), tf.int32)
                where_not_sampling = tf.cast(
                    tf.where(tf.logical_not(sample_ids)), tf.int32)
                outputs_sampling = tf.gather_nd(outputs, where_sampling)
                inputs_not_sampling = tf.gather_nd(base_next_inputs,
                                                   where_not_sampling)
                sampled_next_inputs = maybe_concatenate_auxiliary_inputs(
                    self._next_input_layer(outputs_sampling), where_sampling)

                base_shape = tf.shape(base_next_inputs)
                return (tf.scatter_nd(indices=where_sampling,
                                      updates=sampled_next_inputs,
                                      shape=base_shape)
                        + tf.scatter_nd(indices=where_not_sampling,
                                        updates=inputs_not_sampling,
                                        shape=base_shape))

            all_finished = tf.reduce_all(finished)
            next_inputs = tf.cond(
                all_finished, lambda: base_next_inputs, maybe_sample)
            return (finished, next_inputs, state)
model.py 文件源码 项目:windbag 作者: tongda 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def decode(self, enc_outputs, enc_final_state):
    with tf.variable_scope(self.decoder.scope):
      def condition(time, all_outputs: tf.TensorArray, inputs, states):
        def check_outputs_ends():
          def has_end_word(t):
            return tf.reduce_any(tf.equal(t, ANSWER_MAX))

          output_label = tf.arg_max(all_outputs.stack(), 2)
          output_label = tf.Print(output_label, [output_label], "Output Labels: ")
          # The outputs are time-major, which means time is the first
          # dimension. Here I need to check whether all the generated
          # answers are ends with "</s>", so we need to transpose it
          # to batch-major. Because `map_fn` only map function by the
          # first dimension.
          batch_major_outputs = tf.transpose(output_label, (1, 0))
          all_outputs_ends = tf.reduce_all(tf.map_fn(has_end_word, batch_major_outputs, dtype=tf.bool))
          return all_outputs_ends

        # If the TensorArray has 0 size, stack() will trigger error,
        # so I have to use condition function to check whether the
        # size is 0.
        all_ends = tf.cond(tf.equal(all_outputs.size(), 0),
                           lambda: tf.constant(False, tf.bool),
                           check_outputs_ends)

        condition_result = tf.logical_and(tf.logical_not(all_ends), tf.less(time, ANSWER_MAX))
        return condition_result

      def body(time, all_outputs, inputs, state):
        dec_outputs, dec_state, output_logits, next_input = self.decoder.step(inputs, state)
        all_outputs = all_outputs.write(time, output_logits)
        return time + 1, all_outputs, next_input, dec_state

      output_ta = tensor_array_ops.TensorArray(dtype=tf.float32,
                                               size=0,
                                               dynamic_size=True,
                                               element_shape=(None, config.DEC_VOCAB),
                                               clear_after_read=False)

      # with time-major data input, the batch size is the second dimension
      batch_size = tf.shape(enc_outputs)[1]
      zero_input = tf.ones(tf.expand_dims(batch_size, axis=0), dtype=tf.int32) * ANSWER_START
      res = control_flow_ops.while_loop(
        condition,
        body,
        loop_vars=[0, output_ta, self.decoder.zero_input(zero_input), enc_final_state],
      )
      final_outputs = res[1].stack()
      final_outputs = tf.Print(final_outputs, [final_outputs], "Final Output: ")
      final_state = res[3]
    return final_outputs, final_state
seq2seq.py 文件源码 项目:tensorsoup 作者: ai-guild 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def naive_decoder(cell, enc_states, targets, start_token, end_token, 
        feed_previous=True, training=True, scope='naive_decoder.0'):

    init_state = enc_states[-1]
    timesteps = tf.shape(enc_states)[0]

    # targets time major
    targets_tm = tf.transpose(targets, [1,0,2])

    states = tf.TensorArray(dtype=tf.float32, size=timesteps+1, name='states',
                    clear_after_read=False)
    outputs = tf.TensorArray(dtype=tf.float32, size=timesteps+1, name='outputs',
                    clear_after_read=False)

    def step(i, states, outputs):
        # run one step
        #  read from TensorArray (states)
        state_prev = states.read(i)

        if is_lstm(cell):
            # previous state <tensor> -> <LSTMStateTuple>
            c, h = tf.unstack(state_prev)
            state_prev = rnn.LSTMStateTuple(c,h)

        if feed_previous:
            input_ = outputs.read(i)
        else:
            input_ = targets_tm[i]

        output, state = cell(input_, state_prev)
        # add state, output to list
        states = states.write(i+1, state)
        outputs = outputs.write(i+1, output)
        i = tf.add(i,1)
        return i, states, outputs


    with tf.variable_scope(scope):
        # initial state
        states = states.write(0, init_state)
        # initial input
        outputs = outputs.write(0, start_token)

        i = tf.constant(0)

        # Stop loop condition
        if training:
            c = lambda x, y, z : tf.less(x, timesteps)
        else:
            c = lambda x, y, z : tf.reduce_all(tf.not_equal(tf.argmax(z.read(x), axis=-1), 
                    end_token))
        # body
        b = lambda x, y, z : step(x, y, z)
        # execution 
        _, fstates, foutputs = tf.while_loop(c,b, [i, states, outputs])

    return foutputs.stack()[1:] # add states; but why?
hvpoi.py 文件源码 项目:GPflowOpt 作者: GPflow 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def build_acquisition(self, Xcand):
        outdim = tf.shape(self.data[1])[1]
        num_cells = tf.shape(self.pareto.bounds.lb)[0]
        N = tf.shape(Xcand)[0]

        # Extended Pareto front
        pf_ext = tf.concat([-np.inf * tf.ones([1, outdim], dtype=float_type), self.pareto.front, self.reference], 0)

        # Predictions for candidates, concatenate columns
        preds = [m.build_predict(Xcand) for m in self.models]
        candidate_mean, candidate_var = (tf.concat(moment, 1) for moment in zip(*preds))
        candidate_var = tf.maximum(candidate_var, stability)  # avoid zeros

        # Calculate the cdf's for all candidates for every predictive distribution in the data points
        normal = tf.contrib.distributions.Normal(candidate_mean, tf.sqrt(candidate_var))
        Phi = tf.transpose(normal.cdf(tf.expand_dims(pf_ext, 1)), [1, 0, 2])  # N x pf_ext_size x outdim

        # tf.gather_nd indices for bound points
        col_idx = tf.tile(tf.range(outdim), (num_cells,))
        ub_idx = tf.stack((tf.reshape(self.pareto.bounds.ub, [-1]), col_idx), axis=1)  # (num_cells*outdim x 2)
        lb_idx = tf.stack((tf.reshape(self.pareto.bounds.lb, [-1]), col_idx), axis=1)  # (num_cells*outdim x 2)

        # Calculate PoI
        P1 = tf.transpose(tf.gather_nd(tf.transpose(Phi, perm=[1, 2, 0]), ub_idx))  # N x num_cell*outdim
        P2 = tf.transpose(tf.gather_nd(tf.transpose(Phi, perm=[1, 2, 0]), lb_idx))  # N x num_cell*outdim
        P = tf.reshape(P1 - P2, [N, num_cells, outdim])
        PoI = tf.reduce_sum(tf.reduce_prod(P, axis=2), axis=1, keep_dims=True)  # N x 1

        # Calculate Hypervolume contribution of points Y
        ub_points = tf.reshape(tf.gather_nd(pf_ext, ub_idx), [num_cells, outdim])
        lb_points = tf.reshape(tf.gather_nd(pf_ext, lb_idx), [num_cells, outdim])

        splus_valid = tf.reduce_all(tf.tile(tf.expand_dims(ub_points, 1), [1, N, 1]) > candidate_mean,
                                    axis=2)  # num_cells x N
        splus_idx = tf.expand_dims(tf.cast(splus_valid, dtype=float_type), -1)  # num_cells x N x 1
        splus_lb = tf.tile(tf.expand_dims(lb_points, 1), [1, N, 1])  # num_cells x N x outdim
        splus_lb = tf.maximum(splus_lb, candidate_mean)  # num_cells x N x outdim
        splus_ub = tf.tile(tf.expand_dims(ub_points, 1), [1, N, 1])  # num_cells x N x outdim
        splus = tf.concat([splus_idx, splus_ub - splus_lb], axis=2)  # num_cells x N x (outdim+1)
        Hv = tf.transpose(tf.reduce_sum(tf.reduce_prod(splus, axis=2), axis=0, keep_dims=True))  # N x 1

        # return HvPoI
        return tf.multiply(Hv, PoI)


问题


面经


文章

微信
公众号

扫码关注公众号