Python tensorflow.reduce_prod() Examples

The following are 30 code examples of tensorflow.reduce_prod(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: common_attention.py    From fine-lm with MIT License 6 votes vote down vote up
def gather_indices_2d(x, block_shape, block_stride):
  """Getting gather indices."""
  # making an identity matrix kernel
  kernel = tf.eye(block_shape[0] * block_shape[1])
  kernel = reshape_range(kernel, 0, 1, [block_shape[0], block_shape[1], 1])
  # making indices [1, h, w, 1] to appy convs
  x_shape = common_layers.shape_list(x)
  indices = tf.range(x_shape[2] * x_shape[3])
  indices = tf.reshape(indices, [1, x_shape[2], x_shape[3], 1])
  indices = tf.nn.conv2d(
      tf.cast(indices, tf.float32),
      kernel,
      strides=[1, block_stride[0], block_stride[1], 1],
      padding="VALID")
  # making indices [num_blocks, dim] to gather
  dims = common_layers.shape_list(indices)[:3]
  if all([isinstance(dim, int) for dim in dims]):
    num_blocks = functools.reduce(operator.mul, dims, 1)
  else:
    num_blocks = tf.reduce_prod(dims)
  indices = tf.reshape(indices, [num_blocks, -1])
  return tf.cast(indices, tf.int32) 
Example #2
Source File: modellib.py    From rec-attend-public with MIT License 6 votes vote down vote up
def f_inter_box(top_left_a, bot_right_a, top_left_b, bot_right_b):
  """Computes intersection area with boxes.
  Args:
    top_left_a: [B, T, 2] or [B, 2]
    bot_right_a: [B, T, 2] or [B, 2]
    top_left_b: [B, T, 2] or [B, 2]
    bot_right_b: [B, T, 2] or [B, 2]
  Returns:
    area: [B, T]
  """
  top_left_max = tf.maximum(top_left_a, top_left_b)
  bot_right_min = tf.minimum(bot_right_a, bot_right_b)
  ndims = tf.shape(tf.shape(top_left_a))

  # Check if the resulting box is valid.
  overlap = tf.to_float(top_left_max < bot_right_min)
  overlap = tf.reduce_prod(overlap, ndims - 1)
  area = tf.reduce_prod(bot_right_min - top_left_max, ndims - 1)
  area = overlap * tf.abs(area)
  return area 
Example #3
Source File: embeddings.py    From inferbeddings with MIT License 6 votes vote down vote up
def bilinear_diagonal_walk_embedding(predicate_embeddings):
    """
    Takes a walk, represented by a 3D Tensor with shape (batch_size, walk_length, embedding_length),
    and computes its embedding using a simple bilinear diagonal models.
    This method is roughly equivalent to:
    > walk_embedding = tf.reduce_prod(predicate_embeddings, axis=1)

    :param predicate_embeddings: 3D Tensor containing the embedding of the predicates in the walk.
    :return: 2D tensor of size (batch_size, embedding_length) containing the walk embeddings.
    """
    batch_size, embedding_len = tf.shape(predicate_embeddings)[0], tf.shape(predicate_embeddings)[2]

    # Transpose the (batch_size, walk_length, n) Tensor in a (walk_length, batch_size, n) Tensor
    transposed_embedding_matrix = tf.transpose(predicate_embeddings, perm=[1, 0, 2])

    # Define the initializer of the scan procedure - an all-ones matrix
    # where one is the neutral element wrt. the element-wise product
    initializer = tf.ones((batch_size, embedding_len), dtype=predicate_embeddings.dtype)

    # The walk embeddings are given by the element-wise product of the predicate embeddings
    walk_embedding = tf.scan(lambda x, y: x * y, transposed_embedding_matrix, initializer=initializer)

    # Add the initializer as the first step in the scan sequence, in case the walk has zero-length
    return tf.concat(values=[tf.expand_dims(initializer, 0), walk_embedding], axis=0)[-1] 
Example #4
Source File: modellib.py    From rec-attend-public with MIT License 6 votes vote down vote up
def f_iou_box_old(top_left_a, bot_right_a, top_left_b, bot_right_b):
  """Computes IoU of boxes.
  Args:
    top_left_a: [B, T, 2] or [B, 2]
    bot_right_a: [B, T, 2] or [B, 2]
    top_left_b: [B, T, 2] or [B, 2]
    bot_right_b: [B, T, 2] or [B, 2]
  Returns:
    iou: [B, T]
  """
  inter_area = f_inter_box(top_left_a, bot_right_a, top_left_b, bot_right_b)
  inter_area = tf.maximum(inter_area, 1e-6)
  ndims = tf.shape(tf.shape(top_left_a))
  # area_a = tf.reduce_prod(bot_right_a - top_left_a, ndims - 1)
  # area_b = tf.reduce_prod(bot_right_b - top_left_b, ndims - 1)
  check_a = tf.reduce_prod(tf.to_float(top_left_a < bot_right_a), ndims - 1)
  area_a = check_a * tf.reduce_prod(bot_right_a - top_left_a, ndims - 1)
  check_b = tf.reduce_prod(tf.to_float(top_left_b < bot_right_b), ndims - 1)
  area_b = check_b * tf.reduce_prod(bot_right_b - top_left_b, ndims - 1)
  union_area = (area_a + area_b - inter_area + 1e-5)
  union_area = tf.maximum(union_area, 1e-5)
  iou = inter_area / union_area
  iou = tf.maximum(iou, 1e-5)
  iou = tf.minimum(iou, 1.0)
  return iou 
Example #5
Source File: modellib.py    From rec-attend-public with MIT License 6 votes vote down vote up
def get_filled_box_idx(idx, top_left, bot_right):
  """Fill a box with top left and bottom right coordinates.
  Args:
    idx: [B, T, H, W, 2] or [B, H, W, 2] or [H, W, 2]
    top_left: [B, T, 2] or [B, 2] or [2]
    bot_right: [B, T, 2] or [B, 2] or [2]
  """
  ss = tf.shape(idx)
  ndims = tf.shape(ss)
  batch = tf.slice(ss, [0], ndims - 3)
  coord_shape = tf.concat(0, [batch, tf.constant([1, 1, 2])])
  top_left = tf.reshape(top_left, coord_shape)
  bot_right = tf.reshape(bot_right, coord_shape)
  lower = tf.reduce_prod(tf.to_float(idx >= top_left), ndims - 1)
  upper = tf.reduce_prod(tf.to_float(idx <= bot_right), ndims - 1)
  box = lower * upper

  return box 
Example #6
Source File: tfutil.py    From disentangling_conditional_gans with MIT License 6 votes vote down vote up
def _create_autosummary_var(name, value_expr):
    assert not _autosummary_finalized
    v = tf.cast(value_expr, tf.float32)
    if v.shape.ndims is 0:
        v = [v, np.float32(1.0)]
    elif v.shape.ndims is 1:
        v = [tf.reduce_sum(v), tf.cast(tf.shape(v)[0], tf.float32)]
    else:
        v = [tf.reduce_sum(v), tf.reduce_prod(tf.cast(tf.shape(v), tf.float32))]
    v = tf.cond(tf.is_finite(v[0]), lambda: tf.stack(v), lambda: tf.zeros(2))
    with tf.control_dependencies(None):
        var = tf.Variable(tf.zeros(2)) # [numerator, denominator]
    update_op = tf.cond(tf.is_variable_initialized(var), lambda: tf.assign_add(var, v), lambda: tf.assign(var, v))
    if name in _autosummary_vars:
        _autosummary_vars[name].append(var)
    else:
        _autosummary_vars[name] = [var]
    return update_op

#----------------------------------------------------------------------------
# Call filewriter.add_summary() with all summaries in the default graph,
# automatically finalizing and merging them on the first call. 
Example #7
Source File: ssim.py    From Multiview2Novelview with MIT License 6 votes vote down vote up
def tf_ms_ssim(img1, img2, mean_metric=True, level=5):
    weight = tf.constant([0.0448, 0.2856, 0.3001, 0.2363, 0.1333], dtype=tf.float32)
    mssim = []
    mcs = []
    for l in range(level):
        ssim_map, cs_map = tf_ssim(img1, img2, cs_map=True, mean_metric=False)
        mssim.append(tf.reduce_mean(ssim_map))
        mcs.append(tf.reduce_mean(cs_map))
        filtered_im1 = tf.nn.avg_pool(img1, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')
        filtered_im2 = tf.nn.avg_pool(img2, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')
        img1 = filtered_im1
        img2 = filtered_im2

    # list to tensor of dim D+1
    mssim = tf.pack(mssim, axis=0)
    mcs = tf.pack(mcs, axis=0)

    value = (tf.reduce_prod(
        mcs[0:level-1]**weight[0:level-1]) * (mssim[level-1]**weight[level-1]))

    if mean_metric:
        value = tf.reduce_mean(value)
    return value 
Example #8
Source File: sampler.py    From addons with Apache License 2.0 6 votes vote down vote up
def _call_sampler(sample_n_fn, sample_shape, name=None):
    """Reshapes vector of samples."""
    with tf.name_scope(name or "call_sampler"):
        sample_shape = tf.convert_to_tensor(
            sample_shape, dtype=tf.int32, name="sample_shape"
        )
        # Ensure sample_shape is a vector (vs just a scalar).
        pad = tf.cast(tf.equal(tf.rank(sample_shape), 0), tf.int32)
        sample_shape = tf.reshape(
            sample_shape,
            tf.pad(tf.shape(sample_shape), paddings=[[pad, 0]], constant_values=1),
        )
        samples = sample_n_fn(tf.reduce_prod(sample_shape))
        batch_event_shape = tf.shape(samples)[1:]
        final_shape = tf.concat([sample_shape, batch_event_shape], 0)
        return tf.reshape(samples, final_shape) 
Example #9
Source File: pareto.py    From GPflowOpt with Apache License 2.0 6 votes vote down vote up
def hypervolume(self, reference):
        """
        Autoflow method to calculate the hypervolume indicator

        The hypervolume indicator is the volume of the dominated region.

        :param reference: reference point to use
            Should be equal or bigger than the anti-ideal point of the Pareto set
            For comparing results across runs the same reference point must be used
        :return: hypervolume indicator (the higher the better)
        """

        min_pf = tf.reduce_min(self.front, 0, keep_dims=True)
        R = tf.expand_dims(reference, 0)
        pseudo_pf = tf.concat((min_pf, self.front, R), 0)
        D = tf.shape(pseudo_pf)[1]
        N = tf.shape(self.bounds.ub)[0]

        idx = tf.tile(tf.expand_dims(tf.range(D), -1),[1, N])
        ub_idx = tf.reshape(tf.stack([tf.transpose(self.bounds.ub), idx], axis=2), [N * D, 2])
        lb_idx = tf.reshape(tf.stack([tf.transpose(self.bounds.lb), idx], axis=2), [N * D, 2])
        ub = tf.reshape(tf.gather_nd(pseudo_pf, ub_idx), [D, N])
        lb = tf.reshape(tf.gather_nd(pseudo_pf, lb_idx), [D, N])
        hv = tf.reduce_sum(tf.reduce_prod(ub - lb, 0))
        return tf.reduce_prod(R - min_pf) - hv 
Example #10
Source File: layers.py    From astroNN with MIT License 6 votes vote down vote up
def build(self, input_shape=None):
        self.layer.input_spec = InputSpec(shape=input_shape)
        if hasattr(self.layer, 'built') and not self.layer.built:
            self.layer.build(input_shape)
            self.layer.built = True

        # initialise p
        self.p_logit = self.add_weight(name='p_logit', shape=(1,),
                                       initializer=initializers.RandomUniform(self.init_min, self.init_max),
                                       dtype=tf.float32, trainable=True)
        self.p = tf.nn.sigmoid(self.p_logit)
        tf.compat.v1.add_to_collection("LAYER_P", self.p)

        # initialise regularizer / prior KL term
        input_dim = tf.reduce_prod(input_shape[1:])  # we drop only last dim
        weight = self.layer.kernel
        kernel_regularizer = self.weight_regularizer * tf.reduce_sum(tf.square(weight)) / (1. - self.p)
        dropout_regularizer = self.p * tf.math.log(self.p)
        dropout_regularizer += (1. - self.p) * tf.math.log(1. - self.p)
        dropout_regularizer *= self.dropout_regularizer * tf.cast(input_dim, tf.float32)
        regularizer = tf.reduce_sum(kernel_regularizer + dropout_regularizer)
        self.layer.add_loss(regularizer)
        # Add the regularisation loss to collection.
        tf.compat.v1.add_to_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES, regularizer) 
Example #11
Source File: __init__.py    From astroNN with MIT License 6 votes vote down vote up
def intpow_avx2(x, n):
    """
    Calculate integer power of float (including negative) even with Tensorflow compiled with AVX2 since --fast-math
    compiler flag aggressively optimize float operation which is common with AVX2 flag

    :param x: identifier
    :type x: tf.Tensor
    :param n: an integer power (a float will be casted to integer!!)
    :type n: int
    :return: powered float(s)
    :rtype: tf.Tensor
    :History: 2018-Aug-13 - Written - Henry Leung (University of Toronto)
    """
    import tensorflow as tf

    # expand inputs to prepare to be tiled
    expanded_inputs = tf.expand_dims(x, 1)
    # we want [1, self.n]
    return tf.reduce_prod(tf.tile(expanded_inputs, [1, n]), axis=-1) 
Example #12
Source File: embeddings.py    From inferbeddings with MIT License 6 votes vote down vote up
def additive_walk_embedding(predicate_embeddings):
    """
    Takes a walk, represented by a 3D Tensor with shape (batch_size, walk_length, embedding_length),
    and computes its embedding using a simple additive models.
    This method is roughly equivalent to:
    > walk_embedding = tf.reduce_prod(predicate_embeddings, axis=1)

    :param predicate_embeddings: 3D Tensor containing the embedding of the predicates in the walk.
    :return: 2D tensor of size (batch_size, embedding_length) containing the walk embeddings.
    """
    batch_size, embedding_len = tf.shape(predicate_embeddings)[0], tf.shape(predicate_embeddings)[2]

    # Transpose the (batch_size, walk_length, n) Tensor in a (walk_length, batch_size, n) Tensor
    transposed_embedding_matrix = tf.transpose(predicate_embeddings, perm=[1, 0, 2])

    # Define the initializer of the scan procedure - an all-zeros matrix
    initializer = tf.zeros((batch_size, embedding_len), dtype=predicate_embeddings.dtype)

    # The walk embeddings are given by the sum of the predicate embeddings
    # where zero is the neutral element wrt. the element-wise sum
    walk_embedding = tf.scan(lambda x, y: x + y, transposed_embedding_matrix, initializer=initializer)

    # Add the initializer as the first step in the scan sequence, in case the walk has zero-length
    return tf.concat(values=[tf.expand_dims(initializer, 0), walk_embedding], axis=0)[-1] 
Example #13
Source File: ops.py    From ICML2019-TREX with MIT License 6 votes vote down vote up
def __call__(self,input_var,name=None,**kwargs) :
        if( input_var.shape.ndims > 2 ) :
            dims = tf.reduce_prod(tf.shape(input_var)[1:])
            input_var = tf.reshape(input_var,[-1,dims])

        def _init():
            v_norm = tf.nn.l2_normalize(self.v,axis=0)
            t = tf.matmul(input_var,v_norm)
            mu,var = tf.nn.moments(t,axes=[0])
            std = tf.sqrt(var+self.epsilon)
            return [tf.assign(self.g,1/std),tf.assign(self.b,-1.*mu/std)]

        require_init = tf.reduce_any(tf.is_nan(self.g))
        init_ops = tf.cond(require_init,_init,lambda : [self.g,self.b])

        with tf.control_dependencies(init_ops):
            w = tf.expand_dims(self.g,axis=0) * tf.nn.l2_normalize(self.v,axis=0)
            return tf.matmul(input_var,w)+self.b 
Example #14
Source File: utls.py    From MBLLEN with Apache License 2.0 6 votes vote down vote up
def tf_ms_ssim(img1, img2, mean_metric=True, level=5):
    weight = tf.constant([0.0448, 0.2856, 0.3001, 0.2363, 0.1333], dtype=tf.float32)
    mssim = []
    mcs = []
    for l in range(level):
        ssim_map, cs_map = tf_ssim(img1, img2, cs_map=True, mean_metric=False)
        mssim.append(tf.reduce_mean(ssim_map))
        mcs.append(tf.reduce_mean(cs_map))
        filtered_im1 = tf.nn.avg_pool(img1, [1,2,2,1], [1,2,2,1], padding='SAME')
        filtered_im2 = tf.nn.avg_pool(img2, [1,2,2,1], [1,2,2,1], padding='SAME')
        img1 = filtered_im1
        img2 = filtered_im2

    # list to tensor of dim D+1
    mssim = tf.stack(mssim, axis=0)
    mcs = tf.stack(mcs, axis=0)

    value = (tf.reduce_prod(mcs[0:level-1]**weight[0:level-1])*
                                    (mssim[level-1]**weight[level-1]))

    if mean_metric:
        value = tf.reduce_mean(value)
    return value 
Example #15
Source File: loss_utils.py    From BERT with Apache License 2.0 6 votes vote down vote up
def dmi_loss(config, logits, labels, **kargs):
	# N x C
	probs = tf.exp(tf.nn.log_softmax(logits, axis=-1))
	input_shape_list = bert_utils.get_shape_list(logits, expected_rank=[2])
	# N x C
	one_hot_labels = tf.one_hot(labels, depth=kargs.get('num_classes', 2), dtype=tf.float32)

	# C x N matmul N x C
	mat = tf.matmul(tf.stop_gradient(one_hot_labels), probs, transpose_a=True) #
	print('==mutul informaton shape==', mat.get_shape())
	per_example_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
												logits=logits, 
												labels=tf.stop_gradient(labels))

	mat_det = tf.reduce_prod(tf.abs((tf.linalg.svd(mat, compute_uv=False))))
	loss = -tf.reduce_sum(tf.log(1e-10+mat_det))
	return loss, per_example_loss 
Example #16
Source File: graph_convolutional_matrix_completion.py    From redshells with MIT License 5 votes vote down vote up
def _cross_feature_layer(cls, hidden_size: int, size: int, input_data: List[np.ndarray]):
        layers = [cls._feature_layer(hidden_size, data) + 1.0 for data in input_data]
        x = tf.reduce_prod(layers, axis=0)
        y = tf.keras.layers.Dense(size, use_bias=False, activation=None, kernel_initializer='glorot_normal')(x)
        return y 
Example #17
Source File: test_model.py    From dket with GNU General Public License v3.0 5 votes vote down vote up
def _build_graph(self):
        shape = [self._params['x'], self._params['y']]
        self._data = tf.get_variable('DATA', shape=shape, dtype=tf.float32)
        coeff = tf.reduce_prod(self._data)
        self._predictions = coeff * tf.one_hot(
            self.inputs.get(model.ModelInputs.FORMULA_KEY), 
            self._params['num_classes']) 
Example #18
Source File: test_computations.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def test_sum_prod_broadcast(self):
        # placeholder
        a = tf.placeholder(tf.float32, shape=[3, 4, 5, 6])
        b = tf.placeholder(tf.float32, shape=[3, 4, 5])
        a_sum = tf.reduce_sum(a, reduction_indices=[0, 3])  # shape (4, 5)
        b_prod = tf.reduce_prod(b, reduction_indices=[0, 1])  # shape (5,)
        f = a_sum + b_prod + b  # (4, 5) + (5,) + (3, 4, 5) -> (3, 4, 5)

        # value
        feed_dict = dict()
        for x in [a, b]:
            feed_dict[x] = np.random.rand(*tf_obj_shape(x))

        # test
        self.run(f, tf_feed_dict=feed_dict) 
Example #19
Source File: bernoulli.py    From garage with MIT License 5 votes vote down vote up
def likelihood_ratio_sym(self,
                             x_var,
                             old_dist_info_vars,
                             new_dist_info_vars,
                             name='likelihood_ratio_sym'):
        with tf.name_scope(name):
            old_p = old_dist_info_vars['p']
            new_p = new_dist_info_vars['p']
            ndims = old_p.get_shape().ndims
            return tf.reduce_prod(x_var * new_p / (old_p + TINY) +
                                  (1 - x_var) * (1 - new_p) /
                                  (1 - old_p + TINY),
                                  axis=ndims - 1) 
Example #20
Source File: modellib.py    From rec-attend-public with MIT License 5 votes vote down vote up
def get_normalized_gamma(size, filter_height, filter_width):
  """Get normalized gamma.
  Args:
    size: [B, T, 2] or [B, 2] or [2]
    filter_height: int
    filter_width: int
  Returns:
    lg_gamma: [B, T] or [B] or float
  """
  rank = tf.shape(tf.shape(size))
  filter_area = filter_height * filter_width
  area = tf.reduce_prod(size, rank - 1)
  lg_gamma = tf.log(float(filter_area)) - tf.log(area)
  return lg_gamma 
Example #21
Source File: train_tools.py    From hart with GNU General Public License v3.0 5 votes vote down vote up
def minimize_clipped(optimizer, loss, clip_value, return_gvs=False, soft=False, **kwargs):
    """Computes a train_op with clipped gradients in the range [-clip_value, clip_value]

    :param optimizer: Tensorflow optimizer object
    :param loss: tensor
    :param clip_value: scalar value
    :param return_gvs: returns list of tuples of (gradient, parameter) for trainable variables
    :param kwargs: kwargs for optimizer.compute_gradients function
    :return: train_step
    """

    gvs = optimizer.compute_gradients(loss, **kwargs)
    clipped_gvs = [(g, v) for (g, v) in gvs if g is not None]

    if not soft:
        clipped_gvs = [(tf.clip_by_value(g, -clip_value, clip_value), v) for (g, v) in clipped_gvs]

    else:
        n_elems = 0
        norm_squared = 0.
        for g, v in gvs:
            n_elems += tf.reduce_prod(tf.shape(g))
            norm_squared += tf.reduce_sum(g ** 2)

        norm_squared /= tf.to_float(n_elems)
        inv_norm = gen_math_ops.rsqrt(norm_squared)
        cond = tf.greater(norm_squared, clip_value ** 2)

        def clip(x):
            return tf.cond(cond, lambda: clip_value * x * inv_norm, lambda: x)

        clipped_gvs = [(clip(g), v) for (g, v) in clipped_gvs]

    train_step = optimizer.apply_gradients(clipped_gvs)

    if return_gvs:
        train_step = (train_step, gvs)
    return train_step 
Example #22
Source File: eval_tools.py    From hart with GNU General Public License v3.0 5 votes vote down vote up
def log_norm(expr_list, name):
    """

    :param expr_list:
    :param name:
    :return:
    """
    n_elems = 0
    norm = 0.
    for e in expr_list:
        n_elems += tf.reduce_prod(tf.shape(e))
        norm += tf.reduce_sum(e**2)
    norm /= tf.to_float(n_elems)
    tf.summary.scalar(name, norm)
    return norm 
Example #23
Source File: tracker.py    From hart with GNU General Public License v3.0 5 votes vote down vote up
def _area_loss(pred_bbox, img_size, presence):
    area = pred_bbox[..., 2] * pred_bbox[..., 3]
    ratio = area / tf.reduce_prod(tf.to_float(img_size))
    weights = tf.clip_by_value(ratio, 1., 10.)
    ratio = tf.clip_by_value(ratio, 0., 1.)
    return _time_weighted_nll(1 - ratio, presence, weights) 
Example #24
Source File: tf_ops.py    From nucleus7 with Mozilla Public License 2.0 5 votes vote down vote up
def squash_dims(tensor: tf.Tensor, dims: list) -> tf.Tensor:
    """
    Squash, e.g. combine, multiple dimensions from tensor

    Parameters
    ----------
    tensor
        tensor to squash
    dims
        dimensions to squash

    Returns
    -------
    squashed_tensor
        tensor with squashed dimensions

    """
    tensor_shape = tf.shape(tensor)
    squash_dimension_size = tf.reduce_prod(
        tf.gather(tensor_shape,
                  np.arange(dims[0], dims[-1] + 1)), keep_dims=True)
    tensor_new_shape_list = [
        s for s in [tensor_shape[:dims[0]], squash_dimension_size,
                    tensor_shape[dims[1] + 1:]]
        if len(s.shape.dims) > 0]
    tensor_new_shape = tf.concat(tensor_new_shape_list, 0)

    tensor_reshaped = tf.reshape(tensor, tensor_new_shape)
    return tensor_reshaped 
Example #25
Source File: reversible_layers.py    From BERT with Apache License 2.0 5 votes vote down vote up
def log_det_jacobian(self, inputs):
    """Returns log det | dx / dy | = num_events * sum log | scale |."""
    del inputs  # unused
    # Number of events is number of all elements excluding the batch and
    # channel dimensions.
    num_events = tf.reduce_prod(tf.shape(inputs)[1:-1])
    log_det_jacobian = num_events * tf.reduce_sum(self.log_scale)
    return log_det_jacobian 
Example #26
Source File: common_layers.py    From BERT with Apache License 2.0 5 votes vote down vote up
def weight_targeting(w, k):
  """Weight-level magnitude pruning."""
  k = tf.to_int32(k)
  w_shape = shape_list(w)
  size = tf.to_int32(tf.reduce_prod(w_shape[:-1]))
  w = tf.reshape(w, [size, w_shape[-1]])

  transpose_w = tf.transpose(w)
  thres = tf.contrib.framework.sort(tf.abs(transpose_w), axis=1)[:, k]
  mask = to_float(thres[None, :] >= tf.abs(w))

  return tf.reshape(mask, w_shape) 
Example #27
Source File: common_layers.py    From BERT with Apache License 2.0 5 votes vote down vote up
def apply_spectral_norm(x):
  """Normalizes x using the spectral norm.

  The implementation follows Algorithm 1 of
  https://arxiv.org/abs/1802.05957. If x is not a 2-D Tensor, then it is
  reshaped such that the number of channels (last-dimension) is the same.

  Args:
    x: Tensor with the last dimension equal to the number of filters.

  Returns:
    x: Tensor with the same shape as x normalized by the spectral norm.
    assign_op: Op to be run after every step to update the vector "u".
  """
  weights_shape = shape_list(x)
  other, num_filters = tf.reduce_prod(weights_shape[:-1]), weights_shape[-1]

  # Reshape into a 2-D matrix with outer size num_filters.
  weights_2d = tf.reshape(x, (other, num_filters))

  # v = Wu / ||W u||
  with tf.variable_scope("u", reuse=tf.AUTO_REUSE):
    u = tf.get_variable(
        "u", [num_filters, 1],
        initializer=tf.truncated_normal_initializer(),
        trainable=False)
  v = tf.nn.l2_normalize(tf.matmul(weights_2d, u))

  # u_new = vW / ||v W||
  u_new = tf.nn.l2_normalize(tf.matmul(tf.transpose(v), weights_2d))

  # s = v*W*u
  spectral_norm = tf.squeeze(
      tf.matmul(tf.transpose(v), tf.matmul(weights_2d, tf.transpose(u_new))))

  # set u equal to u_new in the next iteration.
  assign_op = tf.assign(u, tf.transpose(u_new))
  return tf.divide(x, spectral_norm), assign_op 
Example #28
Source File: modalities.py    From BERT with Apache License 2.0 5 votes vote down vote up
def video_pixel_noise_bottom(x, model_hparams, vocab_size):
  """Bottom transformation for video."""
  input_noise = getattr(model_hparams, "video_modality_input_noise", 0.25)
  inputs = x
  if model_hparams.mode == tf.estimator.ModeKeys.TRAIN:
    background = tfp.stats.percentile(inputs, 50., axis=[0, 1, 2, 3])
    input_shape = common_layers.shape_list(inputs)
    input_size = tf.reduce_prod(input_shape[:-1])
    input_mask = tf.multinomial(
        tf.log([[input_noise, 1.-input_noise]]), input_size)
    input_mask = tf.reshape(tf.cast(input_mask, tf.int32),
                            input_shape[:-1]+[1])
    inputs = inputs * input_mask + background * (1 - input_mask)
  return video_bottom(inputs, model_hparams, vocab_size) 
Example #29
Source File: spatial_pyramid_pooling.py    From addons with Apache License 2.0 5 votes vote down vote up
def compute_output_shape(self, input_shape):
        pooled_shape = 0
        for bin in self.bins:
            pooled_shape += tf.reduce_prod(bin)
        if self.data_format == "channels_last":
            return tf.TensorShape([input_shape[0], pooled_shape, input_shape[-1]])
        else:
            return tf.TensorShape([input_shape[0], input_shape[1], pooled_shape]) 
Example #30
Source File: shape_ops.py    From TensorflowFramework with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def reshape(tensor, dims_list):
  """Reshape the given tensor by collapsing dimensions."""
  shape = get_shape(tensor)
  dims_prod = []
  for dims in dims_list:
    if isinstance(dims, numbers.Number):
      dims_prod.append(shape[dims])
    elif all([isinstance(shape[d], int) for d in dims]):
      dims_prod.append(np.prod([shape[d] for d in dims]))
    else:
      dims_prod.append(tf.reduce_prod([shape[d] for d in dims]))
  tensor = tf.reshape(tensor, dims_prod)
  return tensor