当前位置: 首页>>代码示例>>Python>>正文


Python tensorflow.newaxis方法代码示例

本文整理汇总了Python中tensorflow.newaxis方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.newaxis方法的具体用法?Python tensorflow.newaxis怎么用?Python tensorflow.newaxis使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tensorflow的用法示例。


在下文中一共展示了tensorflow.newaxis方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _top_k_logits

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def _top_k_logits(logits, k):
    """Adapted from
    https://github.com/openai/gpt-2/blob/master/src/sample.py#L63-L77
    """
    if k == 0:
        # no truncation
        return logits

    def _top_k():
        values, _ = tf.nn.top_k(logits, k=k)
        min_values = values[:, -1, tf.newaxis]
        return tf.where(
            logits < min_values,
            tf.ones_like(logits, dtype=logits.dtype) * -1e10,
            logits,
        )
    return tf.cond(
        tf.equal(k, 0),
        lambda: logits,
        lambda: _top_k(),
    ) 
开发者ID:qkaren,项目名称:Counterfactual-StoryRW,代码行数:23,代码来源:rnn_decoder_helpers.py

示例2: take_top_p_logits

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def take_top_p_logits(logits, p):
    """Nucleus sampling"""
    batch, sequence, _ = logits.shape.as_list()
    sorted_logits = tf.sort(logits, direction='DESCENDING', axis=-1)
    cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits, axis=-1), axis=-1)
    indices = tf.stack([
        tf.range(0, batch)[:, tf.newaxis],
        tf.range(0, sequence)[tf.newaxis, :],
        # number of indices to include
        tf.maximum(tf.reduce_sum(tf.cast(cumulative_probs <= p, tf.int32), axis=-1) - 1, 0),
    ], axis=-1)
    min_values = tf.gather_nd(sorted_logits, indices)
    return tf.where(
        logits < min_values,
        tf.ones_like(logits) * -1e10,
        logits,
    ) 
开发者ID:openai,项目名称:lm-human-preferences,代码行数:19,代码来源:core.py

示例3: _add_jittered_boxes

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def _add_jittered_boxes(rois, scores, batch_inds, gt_boxes, jitter=0.1):
    ws = gt_boxes[:, 2] - gt_boxes[:, 0]
    hs = gt_boxes[:, 3] - gt_boxes[:, 1]
    shape = tf.shape(gt_boxes)[0]
    jitter = tf.random_uniform([shape, 1], minval = -jitter, maxval = jitter)
    jitter = tf.reshape(jitter, [-1])
    ws_offset = ws * jitter
    hs_offset = hs * jitter
    x1s = gt_boxes[:, 0] + ws_offset
    x2s = gt_boxes[:, 2] + ws_offset
    y1s = gt_boxes[:, 1] + hs_offset
    y2s = gt_boxes[:, 3] + hs_offset
    boxes = tf.concat(
            values=[
                x1s[:, tf.newaxis],
                y1s[:, tf.newaxis],
                x2s[:, tf.newaxis],
                y2s[:, tf.newaxis]],
            axis=1)
    new_scores = tf.ones([shape], tf.float32)
    new_batch_inds = tf.zeros([shape], tf.int32)

    return tf.concat(values=[rois, boxes], axis=0), \
           tf.concat(values=[scores, new_scores], axis=0), \
           tf.concat(values=[batch_inds, new_batch_inds], axis=0) 
开发者ID:CharlesShang,项目名称:FastMaskRCNN,代码行数:27,代码来源:pyramid_network.py

示例4: top_k_logits

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def top_k_logits(logits, k):
	if k == 0:
		# no truncation
		return logits

	def _top_k():
		values, _ = tf.nn.top_k(logits, k=k)
		min_values = values[:, -1, tf.newaxis]
		return tf.where(
			logits < min_values,
			tf.ones_like(logits, dtype=logits.dtype) * -1e10,
			logits,
		)
	return tf.cond(
	   tf.equal(k, 0),
	   lambda: logits,
	   lambda: _top_k(),
	) 
开发者ID:yyht,项目名称:BERT,代码行数:20,代码来源:token_generator.py

示例5: top_k_logits

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def top_k_logits(logits, k):
	if k == 0:
		# no truncation
		return logits

	def _top_k():
		values, _ = tf.nn.top_k(logits, k=k)
		min_values = values[:, -1, tf.newaxis]
		return tf.where(
			logits < min_values,
			tf.ones_like(logits, dtype=logits.dtype) * -1e10,
			logits,
		)
	return tf.cond(
		 tf.equal(k, 0),
		 lambda: logits,
		 lambda: _top_k(),
	) 
开发者ID:yyht,项目名称:BERT,代码行数:20,代码来源:bert_seq_sample_utils.py

示例6: testDiscreteAutoregressiveFlowSample

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def testDiscreteAutoregressiveFlowSample(self, loc_only):
    batch_size = 5
    length = 2
    vocab_size = 2
    if loc_only:
      units = vocab_size
      network = reversible.MADE(units, [])
    else:
      units = 2 * vocab_size
      mask = tf.reshape([0] * vocab_size + [-1e10] + [0] * (vocab_size - 1),
                        [1, 1, 2 * vocab_size])
      network_ = reversible.MADE(units, [])
      network = lambda inputs: mask + network_(inputs)
    layer = reversible.DiscreteAutoregressiveFlow(network, 1.)
    logits = tf.tile(tf.random_normal([length, vocab_size])[tf.newaxis],
                     [batch_size, 1, 1])
    base = tfp.edward2.OneHotCategorical(logits=logits, dtype=tf.float32)
    outputs = layer(base)
    _ = outputs.value  # need to do this to instantiate tf.variables
    self.evaluate(tf.global_variables_initializer())
    res = self.evaluate(outputs)
    self.assertEqual(res.shape, (batch_size, length, vocab_size))
    self.assertAllGreaterEqual(res, 0)
    self.assertAllLessEqual(res, vocab_size - 1) 
开发者ID:yyht,项目名称:BERT,代码行数:26,代码来源:reversible_layers_test.py

示例7: testOneHotMinusExactSoft

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def testOneHotMinusExactSoft(self):
    inputs = tf.constant([[0., 1., 0.],
                          [0., 0., 1.]])
    shift = tf.constant([[0.1, 0.6, 0.3],
                         [0.2, 0.4, 0.4]])

    outputs = reversible.one_hot_minus(inputs, shift)

    shift_zero = inputs
    shift_one = np.array([[1., 0., 0.],
                          [0., 1., 0.]])
    shift_two = np.array([[0., 0., 1.],
                          [1., 0., 0.]])
    expected_outputs = (shift[..., 0][..., tf.newaxis] * shift_zero +
                        shift[..., 1][..., tf.newaxis] * shift_one +
                        shift[..., 2][..., tf.newaxis] * shift_two)

    actual_outputs_val, expected_outputs_val = self.evaluate([
        outputs, expected_outputs])
    self.assertAllEqual(actual_outputs_val, expected_outputs_val) 
开发者ID:yyht,项目名称:BERT,代码行数:22,代码来源:reversible_layers_test.py

示例8: testOneHotMultiplyExactSoft

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def testOneHotMultiplyExactSoft(self):
    inputs = tf.constant([[0., 1., 0.],
                          [0., 0., 1.]])
    scale = tf.constant([[0.1, 0.6, 0.3],
                         [0.2, 0.4, 0.4]])

    outputs = reversible.one_hot_multiply(inputs, scale)

    scale_zero = np.array([[0., 0., 0.],
                           [0., 0., 0.]])
    scale_one = inputs
    scale_two = np.array([[0., 0., 1.],
                          [0., 1., 0.]])
    expected_outputs = (scale[..., 0][..., tf.newaxis] * scale_zero +
                        scale[..., 1][..., tf.newaxis] * scale_one +
                        scale[..., 2][..., tf.newaxis] * scale_two)

    actual_outputs_val, expected_outputs_val = self.evaluate([
        outputs, expected_outputs])
    self.assertAllEqual(actual_outputs_val, expected_outputs_val) 
开发者ID:yyht,项目名称:BERT,代码行数:22,代码来源:reversible_layers_test.py

示例9: call

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def call(self, inputs):
    batch_shape = tf.shape(inputs)[:-1]
    length = tf.shape(inputs)[-1]
    ngram_range_counts = []
    for n in range(self.minval, self.maxval):
      # Reshape inputs from [..., length] to [..., 1, length // n, n], dropping
      # remainder elements. Each n-vector is an ngram.
      reshaped_inputs = tf.reshape(
          inputs[..., :(n * (length // n))],
          tf.concat([batch_shape, [1], (length // n)[tf.newaxis], [n]], 0))
      # Count the number of times each ngram appears in the input. We do so by
      # checking whether each n-vector in the input is equal to each n-vector
      # in a Tensor of all possible ngrams. The comparison is batched between
      # the input Tensor of shape [..., 1, length // n, n] and the ngrams Tensor
      # of shape [..., input_dim**n, 1, n].
      ngrams = tf.reshape(
          list(np.ndindex((self.input_dim,) * n)),
          [1] * (len(inputs.shape)-1) + [self.input_dim**n, 1, n])
      is_ngram = tf.equal(
          tf.reduce_sum(tf.cast(tf.equal(reshaped_inputs, ngrams), tf.int32),
                        axis=-1),
          n)
      ngram_counts = tf.reduce_sum(tf.cast(is_ngram, tf.float32), axis=-1)
      ngram_range_counts.append(ngram_counts)
    return tf.concat(ngram_range_counts, axis=-1) 
开发者ID:yyht,项目名称:BERT,代码行数:27,代码来源:ngram.py

示例10: _compute_auxiliary_structure

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def _compute_auxiliary_structure(self, contents_and_mask):
    """Compute segment and position metadata."""
    contents = contents_and_mask[:, :self._num_sequences]
    start_mask = tf.cast(contents_and_mask[:, self._num_sequences:],
                         dtype=INDEX_DTYPE)

    segment = tf.cumsum(start_mask, axis=0)
    uniform_count = tf.ones_like(segment[:, 0])
    position = []
    for i in range(self._num_sequences):
      segment_slice = segment[:, i]
      counts = tf.math.segment_sum(uniform_count, segment[:, i])
      position.append(tf.range(self._packed_length) -  tf.cumsum(
          tf.gather(counts, segment_slice - 1) * start_mask[:, i]))
    position = tf.concat([i[:, tf.newaxis] for i in position], axis=1)

    # Correct for padding tokens.
    pad_mask = tf.cast(tf.not_equal(contents, 0), dtype=INDEX_DTYPE)
    segment *= pad_mask
    position *= pad_mask

    return segment, position 
开发者ID:yyht,项目名称:BERT,代码行数:24,代码来源:generator_utils.py

示例11: vec_transformationByMat

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def vec_transformationByMat(poses, input_capsule_dim, input_capsule_num, output_capsule_dim, output_capsule_num, shared=True):                        
    inputs_poses_shape = poses.get_shape().as_list()
    poses = poses[..., tf.newaxis, :]        
    poses = tf.tile(
              poses, [1, 1, output_capsule_num, 1]
            )    
    if shared:
        kernel = capsule_utils._get_weights_wrapper(
          name='weights', shape=[1, 1, output_capsule_num, output_capsule_dim, input_capsule_dim], weights_decay_factor=0.0
        )
        kernel = tf.tile(
                  kernel, [inputs_poses_shape[0], input_capsule_num, 1, 1, 1]
                )
    else:
        kernel = capsule_utils._get_weights_wrapper(
          name='weights', shape=[1, input_capsule_num, output_capsule_num, output_capsule_dim, input_capsule_dim], weights_decay_factor=0.0
        )
        kernel = tf.tile(
                  kernel, [inputs_poses_shape[0], 1, 1, 1, 1]
                )
    tf.logging.info('poses: {}'.format(poses[...,tf.newaxis].get_shape()))   
    tf.logging.info('kernel: {}'.format(kernel.get_shape()))
    u_hat_vecs = tf.squeeze(tf.matmul(kernel, poses[...,tf.newaxis]),axis=-1)
    u_hat_vecs = tf.transpose(u_hat_vecs, (0, 2, 1, 3))
    return u_hat_vecs 
开发者ID:yyht,项目名称:BERT,代码行数:27,代码来源:capsule_layers.py

示例12: enum_ratios_and_thetas

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def enum_ratios_and_thetas(anchors, anchor_ratios, anchor_angles):
    '''
    ratio = h /w
    :param anchors:
    :param anchor_ratios:
    :return:
    '''
    ws = anchors[:, 2]  # for base anchor: w == h
    hs = anchors[:, 3]
    anchor_angles = tf.constant(anchor_angles, tf.float32)
    sqrt_ratios = tf.sqrt(tf.constant(anchor_ratios))

    ws = tf.reshape(ws / sqrt_ratios[:, tf.newaxis], [-1])
    hs = tf.reshape(hs * sqrt_ratios[:, tf.newaxis], [-1])

    ws, _ = tf.meshgrid(ws, anchor_angles)
    hs, anchor_angles = tf.meshgrid(hs, anchor_angles)

    anchor_angles = tf.reshape(anchor_angles, [-1, 1])
    ws = tf.reshape(ws, [-1, 1])
    hs = tf.reshape(hs, [-1, 1])

    return ws, hs, anchor_angles 
开发者ID:Thinklab-SJTU,项目名称:R3Det_Tensorflow,代码行数:25,代码来源:generate_rotate_anchors.py

示例13: _create_masks

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def _create_masks(x, input_length, y):
        r""" Generate a square mask for the sequence. The masked positions are
        filled with float(1.0). Unmasked positions are filled with float(0.0).
        """
        input_mask, output_mask = None, None
        if x is not None:
            input_mask = 1.0 - tf.sequence_mask(
                input_length, tf.shape(x)[1], dtype=tf.float32
            )
            input_mask = input_mask[:, tf.newaxis, tf.newaxis, :]
            input_mask.set_shape([None, None, None, None])
        if y is not None:
            output_mask = tf.cast(tf.math.equal(y, 0), tf.float32)
            output_mask = output_mask[:, tf.newaxis, tf.newaxis, :]
            look_ahead_mask = generate_square_subsequent_mask(tf.shape(y)[1])
            output_mask = tf.maximum(output_mask, look_ahead_mask)
            output_mask.set_shape([None, None, None, None])
        return input_mask, output_mask 
开发者ID:athena-team,项目名称:athena,代码行数:20,代码来源:speech_transformer.py

示例14: top_k_logits

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def top_k_logits(logits, k):
    if k == 0:
        # no truncation
        return logits

    def _top_k():
        values, _ = tf.nn.top_k(logits, k=k)
        min_values = values[:, -1, tf.newaxis]
        return tf.where(
            logits < min_values,
            tf.ones_like(logits, dtype=logits.dtype) * -1e10,
            logits,
        )
    return tf.cond(
       tf.equal(k, 0),
       lambda: logits,
       lambda: _top_k(),
    ) 
开发者ID:ConnorJL,项目名称:GPT2,代码行数:20,代码来源:sample.py

示例15: get_balanced_distribution_for_mote_carlo_sampling

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import newaxis [as 别名]
def get_balanced_distribution_for_mote_carlo_sampling(self, ground_truth):
        N = self._placeholder_global_features[:, self.dim_num_vertices] # [b]
        NN = tf.tile(N[..., tf.newaxis], multiples=[1, self.max_vertices]) # [b]

        M = tf.sequence_mask(tf.cast(N, dtype=tf.int64), maxlen=self.max_vertices) # [b, v]
        M = tf.cast(M, dtype=tf.float32)
        MM = tf.cast(tf.sequence_mask(tf.cast(NN, dtype=tf.int64), maxlen=self.max_vertices), tf.float32)* M[...,tf.newaxis] #[b, v, v]

        P = tf.cast(ground_truth, dtype=tf.float32)
        X = tf.reduce_sum(P, axis=2)
        Y = tf.reduce_sum(P, axis=2)

        G_0 = tf.cast(tf.equal(ground_truth,0), tf.float32)
        G_1 = tf.cast(tf.equal(ground_truth,1), tf.float32)

        X = tf.reduce_sum(G_0*MM, axis=2)
        Y = tf.reduce_sum(G_1*MM, axis=2)

        P_0 = G_0 * 0.5 * ((X+Y)/X)[..., tf.newaxis] * MM
        P_1 = G_1 * 0.5 * ((X+Y)/Y)[..., tf.newaxis] * MM

        P = P_0 + P_1

        return P 
开发者ID:shahrukhqasim,项目名称:TIES-2.0,代码行数:26,代码来源:basic_model.py


注:本文中的tensorflow.newaxis方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。