示例#1
0
def drop_connect(inputs, is_training, survival_prob):
  """Drop the entire conv with given survival probability."""
  # "Deep Networks with Stochastic Depth", https://arxiv.org/pdf/1603.09382.pdf
  if not is_training:
    return inputs

  # Compute tensor.
  batch_size = tf.shape(inputs)[0]
  random_tensor = survival_prob
  random_tensor += tf.random_uniform([batch_size, 1, 1, 1], dtype=inputs.dtype)
  binary_tensor = tf.floor(random_tensor)
  # Unlike conventional way that multiply survival_prob at test time, here we
  # divide survival_prob at training time, such that no addition compute is
  # needed at test time.
  output = tf.div(inputs, survival_prob) * binary_tensor
  return output
示例#2
0
文件: utils.py 项目: yichenj/tpu
def drop_connect(inputs, is_training, drop_connect_rate):
  """Apply drop connect."""
  if not is_training:
    return inputs

  # Compute keep_prob
  # TODO(tanmingxing): add support for training progress.
  keep_prob = 1.0 - drop_connect_rate

  # Compute drop_connect tensor
  batch_size = tf.shape(inputs)[0]
  random_tensor = keep_prob
  random_tensor += tf.random_uniform([batch_size, 1, 1, 1], dtype=inputs.dtype)
  binary_tensor = tf.floor(random_tensor)
  output = tf.div(inputs, keep_prob) * binary_tensor
  return output
示例#3
0
    def mean_log_pdf(self, samples):
        """
        Mean log PDF for normal distribution

        Note that ``term1`` is a constant offset when the prior variance is fixed and hence
        in earlier versions of the code this was neglected, along with other constant offsets
        such as factors of pi. However when this code is inherited by spatial priors and ARD
        the variance is no longer fixed and this term must be included.
        """
        dx = tf.subtract(samples, tf.reshape(self.mean, [self.nvertices, 1, 1])) # [W, 1, N]
        z = tf.div(tf.square(dx), tf.reshape(self.var, [self.nvertices, 1, 1])) # [W, 1, N]
        term1 = self.log_tf(-0.5*tf.log(tf.reshape(self.var, [self.nvertices, 1, 1])), name="term1")
        term2 = self.log_tf(-0.5*z, name="term2")
        log_pdf = term1 + term2 # [W, 1, N]
        mean_log_pdf = tf.reshape(tf.reduce_mean(log_pdf, axis=-1), [self.nvertices]) # [W]
        return mean_log_pdf
示例#4
0
def ls(x):
    # 0.8
    threshold = 1 / 256 / 2
    x_coef = 2 / threshold
    a = tf.constant(-0.001)
    # a = tf.constant(1.0)

    nom = tf.abs(tf.subtract(tf.constant(2.0), a))
    mul1 = tf.divide(nom, a)
    x = tf.multiply(x, tf.constant(x_coef))
    x = tf.multiply(x, x)
    mul2 = tf.subtract(
        tf.pow(tf.add(tf.divide(tf.multiply(x, x), nom), tf.constant(1.0)),
               tf.div(a, tf.constant(2.0))), tf.constant(1.0))

    return tf.multiply(mul1, mul2)
def max_scoring_span(start_scores, end_scores, max_length, no_answer_bias=0):
    """Compute max scoring span, using the sum of start and end scores.

  Args:
    start_scores: <float32> [batch_size, seq_len]
    end_scores: <float32> [batch_size, seq_len]
    max_length: <int32> Max answer length.
    no_answer_bias: <float32> Log-odds threshold for "no-answer" selection. I.e.
      if log p(span=i,j)/p(span=NULL) > no_answer_bias, then select i, j as the
      span, and NULL otherwise.

  Returns:
    start: <int32> [batch_size]
    end: <int32> [batch_size]
  """
    # Create sparse tensor of size [seq_len].
    seq_len = tensor_utils.shape(start_scores, -1)
    no_answer_bias = tf.scatter_nd([[0]], [no_answer_bias], [seq_len])
    no_answer_bias = tf.cast(no_answer_bias, tf.float32)

    # Apply bias to CLS token logits.
    no_answer_bias = tf.div(no_answer_bias, 2)
    start_scores += tf.expand_dims(no_answer_bias, 0)
    end_scores += tf.expand_dims(no_answer_bias, 0)

    # Compute outer sum, and mask to be upper triangular.
    # This gives a matrix of start[i] + end[j] scores, where j >= i.
    scores = tf.expand_dims(start_scores, 2) + tf.expand_dims(end_scores, 1)
    mask = (1 - tf.matrix_band_part(tf.ones_like(scores), 0, max_length - 1))
    scores -= mask * 1e-4

    def map_fn(inputs):
        flattened = tf.reshape(inputs, [-1])
        argmax = tf.argmax(flattened, output_type=tf.int32)
        indices = tensor_utils.unravel_index_2d(argmax, inputs.shape)
        score = flattened[argmax]
        return indices, score

    # Return i, j indices of max-scoring entry.
    with tf.device("/cpu"):
        endpoints, span_scores = tf.map_fn(fn=map_fn,
                                           elems=scores,
                                           dtype=(tf.int32, tf.float32))
    start = endpoints[:, 0]
    end = endpoints[:, 1]

    return start, end, span_scores
 def _accuracy(self):
     # shape: [batch_size]
     aff = self.link_pred_layer.affinity(self.outputs1, self.outputs2)
     # shape : [batch_size x num_neg_samples]
     self.neg_aff = self.link_pred_layer.neg_cost(self.outputs1,
                                                  self.neg_outputs)
     #self.neg_aff = tf.reshape(self.neg_aff, [self.batch_size, FLAGS.neg_sample_size])
     self.neg_aff = tf.reshape(self.neg_aff,
                               [self.batch_size, input_neg_sample_size])
     _aff = tf.expand_dims(aff, axis=1)
     self.aff_all = tf.concat(axis=1, values=[self.neg_aff, _aff])
     size = tf.shape(self.aff_all)[1]
     _, indices_of_ranks = tf.nn.top_k(self.aff_all, k=size)
     _, self.ranks = tf.nn.top_k(-indices_of_ranks, k=size)
     self.mrr = tf.reduce_mean(
         tf.div(1.0, tf.cast(self.ranks[:, -1] + 1, tf.float32)))
     tf.summary.scalar('mrr', self.mrr)
示例#7
0
def stochastic_depth(inputs, is_training, stochastic_depth_rate):
    '''Apply stochastic depth.'''
    if not is_training:
        return inputs

    # Compute keep_prob
    # TODO(tanmingxing): add support for training progress.
    keep_prob = 1.0 - stochastic_depth_rate

    # Compute stochastic_depth tensor
    batch_size = tf.shape(inputs)[0]
    random_tensor = keep_prob
    random_tensor += tf.random_uniform([batch_size, 1, 1, 1],
                                       dtype=inputs.dtype)
    binary_tensor = tf.floor(random_tensor)
    output = tf.div(inputs, keep_prob) * binary_tensor
    return output
示例#8
0
def rainbow_network(num_actions, num_atoms, support, network_type, state):
    """The convolutional network used to compute agent's Q-value distributions.

  Args:
    num_actions: int, number of actions.
    num_atoms: int, the number of buckets of the value function distribution.
    support: tf.linspace, the support of the Q-value distribution.
    network_type: namedtuple, collection of expected values to return.
    state: `tf.Tensor`, contains the agent's current state.

  Returns:
    net: _network_type object containing the tensors output by the network.
  """
    weights_initializer = layers.variance_scaling_initializer(factor=1.0 /
                                                              np.sqrt(3.0),
                                                              mode='FAN_IN',
                                                              uniform=True)

    net = tf.cast(state, tf.float32)
    net = tf.div(net, 255.)
    net = layers.conv2d(net,
                        32, [8, 8],
                        stride=4,
                        weights_initializer=weights_initializer)
    net = layers.conv2d(net,
                        64, [4, 4],
                        stride=2,
                        weights_initializer=weights_initializer)
    net = layers.conv2d(net,
                        64, [3, 3],
                        stride=1,
                        weights_initializer=weights_initializer)
    net = layers.flatten(net)
    net = layers.fully_connected(net,
                                 512,
                                 weights_initializer=weights_initializer)
    net = layers.fully_connected(net,
                                 num_actions * num_atoms,
                                 activation_fn=None,
                                 weights_initializer=weights_initializer)

    logits = tf.reshape(net, [-1, num_actions, num_atoms])
    probabilities = layers.softmax(logits)
    q_values = tf.reduce_sum(support * probabilities, axis=2)
    return network_type(q_values, logits, probabilities)
示例#9
0
    def __init__(self, sess, state_dim, action_dim, action_bound,
                 learning_rate, tau, batch_size):
        self.sess = sess
        self.s_dim = state_dim
        self.a_dim = action_dim
        self.action_bound = action_bound
        self.learning_rate = learning_rate
        self.tau = tau
        self.batch_size = batch_size

        # Actor Network
        self.inputs, self.out, self.scaled_out = self.create_actor_network()

        self.network_params = tf.trainable_variables()

        # Target Network
        self.target_inputs, self.target_out, self.target_scaled_out = self.create_actor_network(
        )

        self.target_network_params = tf.trainable_variables(
        )[len(self.network_params):]

        # Op for periodically updating target network with online network
        # weights
        self.update_target_network_params = \
            [self.target_network_params[i].assign(tf.multiply(self.network_params[i], self.tau) +
                                                  tf.multiply(self.target_network_params[i], 1. - self.tau))
             for i in range(len(self.target_network_params))]

        # This gradient will be provided by the critic network
        self.action_gradient = tf.placeholder(tf.float32, [None, self.a_dim])

        # Combine the gradients here
        self.unnormalized_actor_gradients = tf.gradients(
            self.scaled_out, self.network_params, -self.action_gradient)
        self.actor_gradients = list(
            map(lambda x: tf.div(x, self.batch_size),
                self.unnormalized_actor_gradients))

        # Optimization Op
        self.optimize = tf.train.AdamOptimizer(self.learning_rate). \
            apply_gradients(zip(self.actor_gradients, self.network_params))

        self.num_trainable_vars = len(self.network_params) + len(
            self.target_network_params)
示例#10
0
    def call(self, state, num_quantiles):
        """Creates the output tensor/op given the state tensor as input.

    See https://www.tensorflow.org/api_docs/python/tf/keras/Model for more
    information on this. Note that tf.keras.Model implements `call` which is
    wrapped by `__call__` function by tf.keras.Model.

    Args:
      state: `tf.Tensor`, contains the agent's current state.
      num_quantiles: int, number of quantile inputs.
    Returns:
      collections.namedtuple, that contains (quantile_values, quantiles).
    """
        batch_size = state.get_shape().as_list()[0]
        x = tf.cast(state, tf.float32)
        x = tf.div(x, 255.)
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        x = self.flatten(x)
        state_vector_length = x.get_shape().as_list()[-1]
        state_net_tiled = tf.tile(x, [num_quantiles, 1])
        quantiles_shape = [num_quantiles * batch_size, 1]
        quantiles = tf.random_uniform(quantiles_shape,
                                      minval=0,
                                      maxval=1,
                                      dtype=tf.float32)
        quantile_net = tf.tile(quantiles, [1, self.quantile_embedding_dim])
        pi = tf.constant(math.pi)
        quantile_net = tf.cast(tf.range(1, self.quantile_embedding_dim + 1, 1),
                               tf.float32) * pi * quantile_net
        quantile_net = tf.cos(quantile_net)
        # Create the quantile layer in the first call. This is because
        # number of output units depends on the input shape. Therefore, we can only
        # create the layer during the first forward call, not during `.__init__()`.
        if not hasattr(self, 'dense_quantile'):
            self.dense_quantile = tf.keras.layers.Dense(
                state_vector_length,
                activation=self.activation_fn,
                kernel_initializer=self.kernel_initializer)
        quantile_net = self.dense_quantile(quantile_net)
        x = tf.multiply(state_net_tiled, quantile_net)
        x = self.dense1(x)
        quantile_values = self.dense2(x)
        return ImplicitQuantileNetworkType(quantile_values, quantiles)
def h_nonlin(X, fnc, eps=1e-12, name='b'):
    """Apply the nonlinearity described by the function handle fnc: R -> R+ to
    the magnitude of X. CAVEAT: fnc must map to the non-negative reals R+.

    Output U + iV = fnc(R+b) * (A+iB)
    where  A + iB = Z/|Z|

    X: dict of channels {rotation order: (real, imaginary)}
    fnc: function handle for a nonlinearity. MUST map to non-negative reals R+
    eps: regularization since grad |Z| is infinite at zero (default 1e-8)
    """
    magnitude = stack_magnitudes(X, eps)
    msh = magnitude.get_shape()
    b = tf.get_variable('b' + name, shape=[1, 1, 1, msh[3], 1, msh[5]])

    Rb = tf.add(magnitude, b)
    c = tf.div(fnc(Rb), magnitude)
    return c * X
示例#12
0
    def _get_sparsity(self, weight_name):
        """Returns target sparsity for the given layer/weight name."""
        target_sparsity = [
            sparsity for regexp, sparsity in self._weight_sparsity_map.items()
            if regexp.search(weight_name)
        ]
        if not target_sparsity:
            return self._sparsity

        if len(target_sparsity) > 1:
            raise ValueError(
                'Multiple matches in weight_sparsity_map for weight %s' %
                weight_name)
        # TODO(suyoggupta): This will work when initial_sparsity = 0. Generalize
        # to handle other cases as well.
        return tf.multiply(
            self._sparsity,
            tf.div(target_sparsity[0], self._spec.target_sparsity))
示例#13
0
 def _compute_top_k(x):
     """Compute top-k values for each row in a batch."""
     cls_outputs_per_sample, box_outputs_per_sample = x
     cls_outputs_per_sample_reshape = tf.reshape(cls_outputs_per_sample,
                                                 [-1])
     _, cls_topk_indices = tf.nn.top_k(cls_outputs_per_sample_reshape,
                                       k=anchors.MAX_DETECTION_POINTS)
     # Gets top-k class and box scores.
     indices = tf.div(cls_topk_indices, params['num_classes'])
     classes = tf.mod(cls_topk_indices, params['num_classes'])
     cls_indices = tf.stack([indices, classes], axis=1)
     cls_outputs_after_topk = tf.gather_nd(cls_outputs_per_sample,
                                           cls_indices)
     box_outputs_after_topk = tf.gather_nd(box_outputs_per_sample,
                                           tf.expand_dims(indices, 1))
     return [
         indices, classes, cls_outputs_after_topk, box_outputs_after_topk
     ]
示例#14
0
def reduce_nanmean(tensor, axes=None, keepdims=False, name=None):
  """Take the mean of a tensor, skipping NaNs.

  Args:
    tensor: tensor to reduce.
    axes: optional list of axes to reduce.
    keepdims: optional boolean indicating whether to keep dimensions or not.
    name: optional op name.

  Returns:
    tf.Tensor with reduce values.
  """
  masked = tf.is_nan(tensor)
  valid_tensor = tf.where(masked, tf.zeros_like(tensor), tensor)
  total = tf.reduce_sum(valid_tensor, axes, keepdims=keepdims)
  counts = tf.reduce_sum(tf.cast(tf.logical_not(masked), tensor.dtype),
                         axes, keepdims=keepdims)
  return tf.div(total, counts, name=name)
示例#15
0
def _gather_clone_loss(clone, num_clones, regularization_losses):
    """Gather the loss for a single clone.

  Args:
    clone: A Clone namedtuple.
    num_clones: The number of clones being deployed.
    regularization_losses: Possibly empty list of regularization_losses
      to add to the clone losses.

  Returns:
    A tensor for the total loss for the clone.  Can be None.
  """
    # The return value.
    sum_loss = None
    # Individual components of the loss that will need summaries.
    clone_loss = None
    regularization_loss = None
    # Compute and aggregate losses on the clone device.
    with tf.device(clone.device):
        all_losses = []
        clone_losses = tf.get_collection(tf.compat.v1.GraphKeys.LOSSES,
                                         clone.scope)
        if clone_losses:
            clone_loss = tf.add_n(clone_losses, name='clone_loss')
            if num_clones > 1:
                clone_loss = tf.div(clone_loss,
                                    1.0 * num_clones,
                                    name='scaled_clone_loss')
            all_losses.append(clone_loss)
        if regularization_losses:
            regularization_loss = tf.add_n(regularization_losses,
                                           name='regularization_loss')
            all_losses.append(regularization_loss)
        if all_losses:
            sum_loss = tf.add_n(all_losses)
    # Add the summaries out of the clone device block.
    if clone_loss is not None:
        tf.summary.scalar(
            '/'.join(filter(None, ['Losses', clone.scope, 'clone_loss'])),
            clone_loss)
    if regularization_loss is not None:
        tf.summary.scalar('Losses/regularization_loss', regularization_loss)
    return sum_loss
示例#16
0
def drop_path(x, prob):
    """Drop path operation.

    :param x: input feature map
    :type x: torch tensor
    :param prob: dropout probability
    :type prob: float
    :return: output feature map after dropout
    :rtype: torch tensor
    """
    if prob <= 0.:
        return x
    keep = 1. - prob

    bernoulli_random = tf.random.uniform([int(x.get_shape()[0]), 1, 1, 1])
    mask = tf.cast(bernoulli_random < keep, tf.float32)
    x = tf.div(x, keep)
    x = tf.multiply(x, mask)
    return x
    def _network_template(self, state):
        """Builds the convolutional network used to compute the agent's Q-values.

    Args:
      state: `tf.Tensor`, contains the agent's current state.

    Returns:
      net: _network_type object containing the tensors output by the network.
    """
        weights_initializer = slim.variance_scaling_initializer(factor=1.0 /
                                                                np.sqrt(3.0),
                                                                mode='FAN_IN',
                                                                uniform=True)

        net = tf.cast(state, tf.float32)
        net = tf.div(net, 255.)
        net = slim.conv2d(net,
                          32, [8, 8],
                          stride=4,
                          weights_initializer=weights_initializer)
        net = slim.conv2d(net,
                          64, [4, 4],
                          stride=2,
                          weights_initializer=weights_initializer)
        net = slim.conv2d(net,
                          64, [3, 3],
                          stride=1,
                          weights_initializer=weights_initializer)
        net = slim.flatten(net)
        net = noisy_dqn_agent.fully_connected(net,
                                              512,
                                              scope='fully_connected')
        net = noisy_dqn_agent.fully_connected(net,
                                              self.num_actions *
                                              self._num_atoms,
                                              activation_fn=None,
                                              scope='fully_connected_1')

        logits = tf.reshape(net, [-1, self.num_actions, self._num_atoms])
        probabilities = contrib_layers.softmax(logits)
        q_values = tf.reduce_sum(self._support * probabilities, axis=2)
        return self._get_network_type()(q_values, logits, probabilities)
示例#18
0
def nature_dqn_network(num_actions, network_type, state):
    """The convolutional network used to compute the agent's Q-values.

  Args:
    num_actions: int, number of actions.
    network_type: namedtuple, collection of expected values to return.
    state: `tf.Tensor`, contains the agent's current state.

  Returns:
    net: _network_type object containing the tensors output by the network.
  """
    net = tf.cast(state, tf.float32)
    net = tf.div(net, 255.)
    net = layers.conv2d(net, 32, [8, 8], stride=4)
    net = layers.conv2d(net, 64, [4, 4], stride=2)
    net = layers.conv2d(net, 64, [3, 3], stride=1)
    net = layers.flatten(net)
    net = layers.fully_connected(net, 512)
    q_values = layers.fully_connected(net, num_actions, activation_fn=None)
    return network_type(q_values)
  def _address_content(self, x):
    """Address the memory based on content similarity.

    Args:
      x: a tensor in the shape of [batch_size, length, depth].
    Returns:
      the logits for each memory entry [batch_size, length, memory_size].
    """
    mem_keys = tf.layers.dense(self.mem_vals, self.key_depth,
                               bias_initializer=tf.constant_initializer(1.0),
                               name="mem_key")
    mem_query = tf.layers.dense(x, self.key_depth,
                                bias_initializer=tf.constant_initializer(1.0),
                                name="mem_query")
    norm = tf.matmul(self._norm(mem_query), self._norm(mem_keys),
                     transpose_b=True)
    dot_product = tf.matmul(mem_query, mem_keys, transpose_b=True)
    cos_dist = tf.div(dot_product, norm + 1e-7, name="cos_dist")
    access_logits = self.sharpen_factor * cos_dist
    return access_logits
示例#20
0
    def _setup_sparsity(self):
        begin_step = self._spec.sparsity_function_begin_step
        end_step = self._spec.sparsity_function_end_step
        initial_sparsity = self._spec.initial_sparsity
        target_sparsity = self._spec.target_sparsity
        exponent = self._spec.sparsity_function_exponent

        with tf.name_scope(self._spec.name):
            p = tf.minimum(
                1.0,
                tf.maximum(
                    0.0,
                    tf.div(tf.cast(self._global_step - begin_step, tf.float32),
                           end_step - begin_step)))
            sparsity = tf.add(tf.multiply(initial_sparsity - target_sparsity,
                                          tf.pow(1 - p, exponent)),
                              target_sparsity,
                              name='sparsity')

        return sparsity
示例#21
0
    def optimize(self, v):
        ''' Optimization step. Gibbs sampling, calculating of gradients and doing an update operation.

        @param v: visible nodes
        @return update operation
        @return accuracy
        '''

        with tf.name_scope('optimization'):
            v0, vk, ph0, phk, _ = self._gibbs_sampling(v)
            dW,db_h,db_v=self._compute_gradients(v0, vk, ph0, phk)
            update_op =self._update_parameter(dW,db_h,db_v)

        with tf.name_scope('accuracy'):
            mask=tf.where(tf.less(v0,0.0),x=tf.zeros_like(v0),y=tf.ones_like(v0))
            bool_mask=tf.cast(tf.where(tf.less(v0,0.0),x=tf.zeros_like(v0),y=tf.ones_like(v0)), dtype=tf.bool)
            acc=tf.where(bool_mask,x=tf.abs(tf.subtract(v0,vk)),y=tf.zeros_like(v0))
            n_values=tf.reduce_sum(mask)
            acc=tf.subtract(1.0,tf.div(tf.reduce_sum(acc), n_values))

        return update_op, acc
示例#22
0
    def update_neighbour(self, bmu):
        learning_rate = 0.1
        sigma = tf.to_float(tf.maximum(self.width, self.height) / 2)
        # Calculate distance for each cluster from winning node
        square_difference = tf.square(self.location - bmu)
        distance = tf.sqrt(tf.reduce_mean(square_difference, axis=1))
        neighbour_strength = tf.exp(
            tf.div(tf.negative(tf.square(distance)), 2 * tf.square(sigma)))
        rate = neighbour_strength * learning_rate

        total_node = self.width * self.height
        rate_stacked = tf.stack([
            tf.tile(tf.slice(rate, [i], [1]), [self.input_dimension])
            for i in range(total_node)
        ])

        input_weight_difference = tf.subtract(self.input, self.weight)
        weight_difference = tf.multiply(rate_stacked, input_weight_difference)
        weight_new = tf.add(self.weight, weight_difference)

        return tf.assign(self.weight, weight_new), rate_stacked
示例#23
0
def GMM_M_Step(X, Gama, ClusterNo, name='GMM_Statistics', **kwargs):

    D, h, s = tf.split(X, [1,1,1], axis=3)
    
    WXd = tf.multiply(Gama, tf.tile(D ,[1,1,1,ClusterNo]))
    WXa = tf.multiply(Gama, tf.tile(h ,[1,1,1,ClusterNo]))
    WXb = tf.multiply(Gama, tf.tile(s ,[1,1,1,ClusterNo]))
    
    S = tf.reduce_sum(tf.reduce_sum(Gama, axis=1), axis=1)
    S = tf.add(S, tensorflow.keras.backend.epsilon())
    S = tf.reshape(S,[1, ClusterNo])
    
    M_d = tf.div(tf.reduce_sum(tf.reduce_sum(WXd, axis=1), axis=1) , S)
    M_a = tf.div(tf.reduce_sum(tf.reduce_sum(WXa, axis=1), axis=1) , S)
    M_b = tf.div(tf.reduce_sum(tf.reduce_sum(WXb, axis=1), axis=1) , S)
    
    Mu = tf.split(tf.concat([M_d, M_a, M_b],axis=0), ClusterNo, 1)  
    
    Norm_d = tf.squared_difference(D, tf.reshape(M_d,[1, ClusterNo]))
    Norm_h = tf.squared_difference(h, tf.reshape(M_a,[1, ClusterNo]))
    Norm_s = tf.squared_difference(s, tf.reshape(M_b,[1, ClusterNo]))
    
    WSd = tf.multiply(Gama, Norm_d)
    WSh = tf.multiply(Gama, Norm_h)
    WSs = tf.multiply(Gama, Norm_s)
    
    S_d = tf.sqrt(tf.div(tf.reduce_sum(tf.reduce_sum(WSd, axis=1), axis=1) , S))
    S_h = tf.sqrt(tf.div(tf.reduce_sum(tf.reduce_sum(WSh, axis=1), axis=1) , S))
    S_s = tf.sqrt(tf.div(tf.reduce_sum(tf.reduce_sum(WSs, axis=1), axis=1) , S))
    
    Std = tf.split(tf.concat([S_d, S_h, S_s],axis=0), ClusterNo, 1)  
    
    dist = list()
    for k in range(0, ClusterNo):
        dist = tfp.distributions.MultivariateNormalDiag(tf.reshape(Mu[k],[1,3]), tf.reshape(Std[k],[1,3]))

    PI = tf.split(Gama, ClusterNo, axis=3)
    Prob0 = list()
    ds = tf.expand_dims(dataset_tf(X), -2)
    for k in range(0, ClusterNo):
        Prob0.append(tf.multiply(tf.squeeze(dist.prob(ds[:, 0, :])), tf.squeeze(PI[k])))

    Prob = tf.convert_to_tensor(Prob0, dtype=tf.float32)
    Prob = tf.minimum(tf.add(tf.reduce_sum(Prob, axis=0), tensorflow.keras.backend.epsilon()), tf.constant(1.0, tf.float32))
    Log_Prob = tf.negative(tf.log(Prob))
    Log_Likelihood = tf.reduce_mean(Log_Prob)
    
    return Log_Likelihood, Mu, Std
示例#24
0
def drop_connect(inputs, is_training, drop_connect_rate):
  """Apply drop connect.

  Args:
    inputs: `Tensor` input tensor.
    is_training: `bool` if True, the model is in training mode.
    drop_connect_rate: `float` drop connect rate.

  Returns:
    A output tensor, which should have the same shape as input.
  """
  if not is_training or drop_connect_rate is None or drop_connect_rate == 0:
    return inputs

  keep_prob = 1.0 - drop_connect_rate
  batch_size = tf.shape(inputs)[0]
  random_tensor = keep_prob
  random_tensor += tf.random_uniform([batch_size, 1, 1, 1], dtype=inputs.dtype)
  binary_tensor = tf.floor(random_tensor)
  output = tf.div(inputs, keep_prob) * binary_tensor
  return output
示例#25
0
def _create_average_ops(params):
    """Build moving average ops."""
    tf.logging.info('Creating moving average ops')

    with tf.variable_scope('moving_average'):
        moving_average_step = tf.get_variable('step', [],
                                              dtype=tf.float32,
                                              trainable=False)

    all_vars = tf.trainable_variables()
    average_pairs = []
    with tf.variable_scope('average'):
        for v in all_vars:
            v_name = utils.strip_var_name(v.name)
            average_v = tf.get_variable(v_name,
                                        shape=v.shape,
                                        dtype=v.dtype,
                                        initializer=tf.initializers.zeros(),
                                        trainable=False)
            average_pairs.append([v, average_v])

    with tf.control_dependencies([tf.assign_add(moving_average_step, 1.0)]):
        update_average_op = []
        mu = tf.cond(
            tf.greater(moving_average_step, params.start_moving_average),
            lambda: tf.div(1., moving_average_step - params.
                           start_moving_average), lambda: 0.)
        for v, average_v in average_pairs:
            new_average = mu * v + (1 - mu) * average_v
            with tf.control_dependencies([new_average]):
                update_average_op.append(tf.assign(average_v, new_average))

    assert len(average_pairs) == len(all_vars)
    use_average_op = []
    for i in range(len(average_pairs)):
        v, average_v = average_pairs[i]
        use_average_op.append(tf.assign(v, average_v))

    return update_average_op, mu, use_average_op
示例#26
0
def padded_where(condition, length):
    """TPU friendly version of tf.where(cond) with fixed length and padding.

  This is a wrapper around tf.where(cond) that returns the coordinates of the
  True elements of cond (case where x and y are None). This version, however,
  returns a fixed length tensor of coordinates, determined by `length`.  If the
  number of True elements in `condition` is less than `length`, then the
  returned tensor is right-padded with zeros. Otherwise, the returned tensor is
  truncated to `length` size.

  Args:
    condition: tf.Tensor of type boolean; any shape.
    length: Length of (last dimension of) the returned tensor.

  Returns:
    Two tensors:
    - a tensor of type int32, with same shape as `condition`, representing
      coordinates of the last dimension of `condition` tensor where values are
      True.
    - a mask tensor of type int32 with 1s in valid indices of the first tensor,
      and 0s for padded indices.
  """
    condition_shape = shape(condition)
    n = condition_shape[-1]

    # Build a tensor that counts indices from 0 to length of condition.
    ixs = tf.broadcast_to(tf.range(n, dtype=tf.int32), condition_shape)

    # Build tensor where True condition values get their index value or
    # n (== len(condition)) otherwise.
    ixs = tf.where(condition, ixs, tf.ones_like(condition, dtype=tf.int32) * n)

    # Sort indices (so that indices for False values == n, will be placed last),
    # and get the desired number of entries, truncating by `length`.
    ixs = tf.sort(ixs)[Ellipsis, 0:length]

    # For first tensor, zero-out values == n. For second tensor, put 1s where
    # values are < n, and 0s where values are == 0.
    return tf.mod(ixs, n), (1 - tf.div(ixs, n))
    def _network_template(self, state):
        """Builds a convolutional network that outputs Q-value distributions.

    Args:
      state: `tf.Tensor`, contains the agent's current state.

    Returns:
      net: _network_type object containing the tensors output by the network.
    """
        net = tf.cast(state, tf.float32)
        net = tf.div(net, 255.)
        net = slim.conv2d(net,
                          int(32 * self.network_size_expansion), [8, 8],
                          stride=4)
        net = slim.conv2d(net,
                          int(64 * self.network_size_expansion), [4, 4],
                          stride=2)
        net = slim.conv2d(net,
                          int(64 * self.network_size_expansion), [3, 3],
                          stride=1)
        net = slim.flatten(net)
        net = slim.fully_connected(net, int(512 * self.network_size_expansion))

        q_values = []
        for _ in range(self.number_of_gammas):
            gamma_q_value = slim.fully_connected(net,
                                                 self.num_actions,
                                                 activation_fn=None)
            q_values.append(gamma_q_value)

        # Estimate the hyperbolic discounted q-values
        hyp_q_value = agent_utils.integrate_q_values(q_values,
                                                     self.integral_estimate,
                                                     self.eval_gammas,
                                                     self.number_of_gammas,
                                                     self.gammas)

        return self._get_network_type()(hyp_q_value, q_values)
示例#28
0
def batch_rodrigues(theta, name=None):
    """
    Theta is N x 3
    """
    with tf.name_scope(name, "batch_rodrigues", [theta]):
        batch_size = theta.shape.as_list()[0]

        # angle = tf.norm(theta, axis=1)
        # r = tf.expand_dims(tf.div(theta, tf.expand_dims(angle + 1e-8, -1)), -1)
        # angle = tf.expand_dims(tf.norm(theta, axis=1) + 1e-8, -1)
        angle = tf.expand_dims(tf.norm(theta + 1e-8, axis=1), -1)
        r = tf.expand_dims(tf.div(theta, angle), -1)

        angle = tf.expand_dims(angle, -1)
        cos = tf.cos(angle)
        sin = tf.sin(angle)

        outer = tf.matmul(r, r, transpose_b=True, name="outer")

        eyes = tf.tile(tf.expand_dims(tf.eye(3), 0), [batch_size, 1, 1])
        R = cos * eyes + (1 - cos) * outer + sin * batch_skew(
            r, batch_size=batch_size)
        return R
示例#29
0
    def call(self, state):
        """Creates the output tensor/op given the state tensor as input.

    See https://www.tensorflow.org/api_docs/python/tf/keras/Model for more
    information on this. Note that tf.keras.Model implements `call` which is
    wrapped by `__call__` function by tf.keras.Model.

    Parameters created here will have scope according to the `name` argument
    given at `.__init__()` call.
    Args:
      state: Tensor, input tensor.
    Returns:
      collections.namedtuple, output ops (graph mode) or output tensors (eager).
    """
        x = tf.cast(state, tf.float32)
        x = tf.div(x, 255.)
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        x = self.flatten(x)
        x = self.dense1(x)

        return DQNNetworkType(self.dense2(x))
示例#30
0
def cross_entropy_seq_with_mask(logits, target_seqs, input_mask, return_details=False, name=None):
    """Returns the expression of cross-entropy of two sequences, implement
    softmax internally. Normally be used for Dynamic RNN outputs.

    Parameters
    -----------
    logits : network identity outputs
        2D tensor, ``network.outputs``, [batch_size, number of output units].
    target_seqs : int of tensor, like word ID.
        [batch_size, ?]
    input_mask : the mask to compute loss
        The same size with target_seqs, normally 0 and 1.
    return_details : boolean
        - If False (default), only returns the loss.
        - If True, returns the loss, losses, weights and targets (reshape to one vetcor).

    Examples
    --------
    - see Image Captioning Example.
    """
    targets = tf.reshape(target_seqs, [-1])   # to one vector
    weights = tf.to_float(tf.reshape(input_mask, [-1]))   # to one vector like targets
    losses = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=targets, name=name) * weights
    #losses = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=targets, name=name)) # for TF1.0 and others

    try: ## TF1.0
        loss = tf.divide(tf.reduce_sum(losses),   # loss from mask. reduce_sum before element-wise mul with mask !!
                        tf.reduce_sum(weights),
                        name="seq_loss_with_mask")
    except: ## TF0.12
        loss = tf.div(tf.reduce_sum(losses),   # loss from mask. reduce_sum before element-wise mul with mask !!
                        tf.reduce_sum(weights),
                        name="seq_loss_with_mask")
    if return_details:
        return loss, losses, weights, targets
    else:
        return loss