예제 #1
0
def compute_nats_and_bits_per_dim(data_dim, latent_dim, average_reconstruction,
                                  average_prior):
    """Computes negative ELBO, which is an upper bound on the negative likelihood.

  Args:
    data_dim: int-like indicating data dimensionality.
    latent_dim: int-like indicating latent dimensionality.
    average_reconstruction: Scalar Tensor indicating the reconstruction cost
      averaged over all data dimensions and any data batches.
    average_prior: Scalar Tensor indicating the negative log-prior probability
      averaged over all latent dimensions and any data batches.

  Returns:
    Tuple of scalar Tensors, representing the nats and bits per data dimension
    (e.g., subpixels) respectively.
  """
    with tf.name_scope(None, default_name="compute_nats_per_dim"):
        data_dim = tf.cast(data_dim, average_reconstruction.dtype)
        latent_dim = tf.cast(latent_dim, average_prior.dtype)
        negative_log_likelihood = data_dim * average_reconstruction
        negative_log_prior = latent_dim * average_prior
        negative_elbo = negative_log_likelihood + negative_log_prior
        nats_per_dim = tf.divide(negative_elbo, data_dim, name="nats_per_dim")
        bits_per_dim = tf.divide(nats_per_dim, tf.log(2.), name="bits_per_dim")
        return nats_per_dim, bits_per_dim
예제 #2
0
def TSS(y_true, y_pred):
    """
    TSS

    import tensorflow as tf
    sess = tf.Session()
    a=tf.contrib.metrics.confusion_matrix([1, 0, 0, 0, 0], [1, 0, 1, 0, 0])
    a.eval(session=sess)
    array([[3, 1],
          [0, 1]], dtype=int32)
    a[0][0].eval(session=sess)
    3 -> tn
    a[0][1].eval(session=sess)
    1 -> fp
    """
    confusion_matrix = tf.confusion_matrix(labels=tf.argmax(y_true, 1),
                                           predictions=tf.argmax(y_pred, 1),
                                           num_classes=2,
                                           dtype=tf.float32)
    tp = confusion_matrix[1][1]
    fn = confusion_matrix[1][0]
    fp = confusion_matrix[0][1]
    tn = confusion_matrix[0][0]
    tmp1 = tf.divide(tp, tf.add(tp, fn))
    tmp2 = tf.divide(fp, tf.add(fp, tn))
    tss = tf.subtract(tmp1, tmp2)
    return tss
예제 #3
0
    def dpp_style(self, submethod):
        """Computes the DPP of a matrix."""
        det_entries = []
        if submethod == "inverse_dist":
            for i in range(self.total_CFs):
                for j in range(self.total_CFs):
                    det_temp_entry = tf.divide(
                        1.0,
                        tf.add(
                            1.0,
                            self.compute_dist(self.cfs_frozen[i],
                                              self.cfs_frozen[j])))
                    if i == j:
                        det_temp_entry = tf.add(det_temp_entry, 0.0001)
                    det_entries.append(det_temp_entry)

        elif submethod == "exponential_dist":
            for i in range(self.total_CFs):
                for j in range(self.total_CFs):
                    det_temp_entry = tf.divide(
                        1.0,
                        tf.exp(
                            self.compute_dist(self.cfs_frozen[i],
                                              self.cfs_frozen[j])))
                    det_entries.append(det_temp_entry)

        det_entries = tf.reshape(det_entries, [self.total_CFs, self.total_CFs])
        loss_part3 = tf.matrix_determinant(det_entries)
        return loss_part3
예제 #4
0
def clip_eta(eta, ord, eps):
    """
  Helper function to clip the perturbation to epsilon norm ball.
  :param eta: A tensor with the current perturbation.
  :param ord: Order of the norm (mimics Numpy).
              Possible values: np.inf, 1 or 2.
  :param eps: Epsilon, bound of the perturbation.
  """

    # Clipping perturbation eta to self.ord norm ball
    if ord not in [np.inf, 1, 2]:
        raise ValueError('ord must be np.inf, 1, or 2.')
    reduc_ind = list(xrange(1, len(eta.get_shape())))
    avoid_zero_div = 1e-12
    if ord == np.inf:
        eta = clip_by_value(eta, -eps, eps)
    elif ord == 1:
        # Implements a projection algorithm onto the l1-ball from
        # (Duchi et al. 2008) that runs in time O(d*log(d)) where d is the
        # input dimension.
        # Paper link (Duchi et al. 2008): https://dl.acm.org/citation.cfm?id=1390191

        eps = tf.cast(eps, eta.dtype)

        dim = tf.reduce_prod(tf.shape(eta)[1:])
        eta_flat = tf.reshape(eta, (-1, dim))
        abs_eta = tf.abs(eta_flat)

        if 'sort' in dir(tf):
            mu = -tf.sort(-abs_eta, axis=-1)
        else:
            # `tf.sort` is only available in TF 1.13 onwards
            mu = tf.nn.top_k(abs_eta, k=dim, sorted=True)[0]
        cumsums = tf.cumsum(mu, axis=-1)
        js = tf.cast(tf.divide(1, tf.range(1, dim + 1)), eta.dtype)
        t = tf.cast(tf.greater(mu - js * (cumsums - eps), 0), eta.dtype)

        rho = tf.argmax(t * cumsums, axis=-1)
        rho_val = tf.reduce_max(t * cumsums, axis=-1)
        theta = tf.divide(rho_val - eps, tf.cast(1 + rho, eta.dtype))

        eta_sgn = tf.sign(eta_flat)
        eta_proj = eta_sgn * tf.maximum(abs_eta - theta[:, tf.newaxis], 0)
        eta_proj = tf.reshape(eta_proj, tf.shape(eta))

        norm = tf.reduce_sum(tf.abs(eta), reduc_ind)
        eta = tf.where(tf.greater(norm, eps), eta_proj, eta)

    elif ord == 2:
        # avoid_zero_div must go inside sqrt to avoid a divide by zero
        # in the gradient through this operation
        norm = tf.sqrt(
            tf.maximum(avoid_zero_div,
                       reduce_sum(tf.square(eta), reduc_ind, keepdims=True)))
        # We must *clip* to within the norm ball, not *normalize* onto the
        # surface of the ball
        factor = tf.minimum(1., div(eps, norm))
        eta = eta * factor
    return eta
예제 #5
0
    def _create_variables(self):
        """
        Creates/instantiates variables for model such that we can look them up when learning and making predictions
        """
        # Weights to map uid into embeddings representing shared knowledge in item and social domains (u^c in paper)
        self.uw_c = tf.Variable(tf.random.truncated_normal(shape=[self.n_users, self.embedding_size],
                                                                mean=0.0, stddev=0.01, dtype = tf.float32, name='uc'))
        # Weights to map uid into embeddings representing shared knowledge in item and question domains
        self.uw_c2 = tf.Variable(tf.random.truncated_normal(shape=[self.n_users, self.embedding_size],
                                                           mean=0.0, stddev=0.01, dtype=tf.float32, name='uc2'))
        # Weights to map uid into embeddings representing preferences to items (u^i in paper)
        self.uw_i = tf.Variable(tf.random.truncated_normal(shape=[self.n_users, self.embedding_size],
                                                              mean=0.0, stddev=0.01, dtype=tf.float32, name='ui'))
        # Weights to map uid into embeddings preferences to items (u^s in paper)
        self.uw_s = tf.Variable(tf.random.truncated_normal(shape=[self.n_users, self.embedding_size],
                                                                mean=0.0, stddev=0.01, dtype=tf.float32, name='us'))
        # Weights to map uid into embeddings for questionnaires
        self.uw_q = tf.Variable(tf.random.truncated_normal(shape=[self.n_users, self.embedding_size],
                                                                mean=0.0, stddev=0.01, dtype=tf.float32, name='uq'))
        # TODO: figure out why they +1 in sizes
        # Embeddings for items (M x D)
        self.Q = tf.Variable(tf.random.truncated_normal(shape=[self.n_items + 1, self.embedding_size],
                                                        mean=0.0, stddev=0.01, dtype=tf.float32, name='Q'))
        # Embeddings for social interactions (N x D)
        self.G = tf.Variable(tf.random.truncated_normal(shape=[self.n_users + 1, self.embedding_size],
                                                        mean=0.0, stddev=0.01, dtype=tf.float32, name='G'))
        # Embeddings for questionnaires (M x D)
        self.V = tf.Variable(tf.random.truncated_normal(shape=[self.n_items + 1, self.embedding_size],
                                                        mean=0.0, stddev=0.01, dtype=tf.float32, name='V'))


        # Weights used in item domain prediction layer
        self.H_i = tf.Variable(tf.constant(0.01, shape=[self.embedding_size, 1]), name='hi')
        # Weights used in social domain prediction layer
        self.H_s = tf.Variable(tf.constant(0.01, shape=[self.embedding_size, 1]), name='hf')
        # Weights used in questionnaire domain prediction layer
        self.H_q = tf.Variable(tf.constant(0.01, shape=[self.embedding_size, 1]), name='hq')

        # Item domain attention network parameters
        self.W_item = tf.Variable(
            tf.random.truncated_normal(shape=[self.embedding_size, self.attention_size], mean=0.0, stddev=tf.sqrt(
                tf.divide(2.0, self.attention_size + self.embedding_size))), dtype=tf.float32, name='Witem')
        self.B_item = tf.Variable(tf.constant(0.00, shape=[self.attention_size]), name='Bitem') # 1 x k
        self.H_item = tf.Variable(tf.constant(0.01, shape=[self.attention_size, 1], name='Hitem')) # k x 1

        # Social domain attention network parameters
        self.W_social = tf.Variable(
            tf.random.truncated_normal(shape=[self.embedding_size, self.attention_size], mean=0.0, stddev=tf.sqrt(
                tf.divide(2.0, self.attention_size + self.embedding_size))), dtype=tf.float32, name='Wsocial')
        self.B_social = tf.Variable(tf.constant(0.00, shape=[self.attention_size]), name='Bsocial')  # 1 x k
        self.H_social = tf.Variable(tf.constant(0.01, shape=[self.attention_size, 1], name='Hsocial'))  # k x 1

        # Questionnaire domain attention network parameters
        self.W_question = tf.Variable(
            tf.random.truncated_normal(shape=[self.embedding_size, self.attention_size], mean=0.0, stddev=tf.sqrt(
                tf.divide(2.0, self.attention_size + self.embedding_size))), dtype=tf.float32, name='Wquestion')
        self.B_question = tf.Variable(tf.constant(0.00, shape=[self.attention_size]), name='Bquestion')  # 1 x k
        self.H_question = tf.Variable(tf.constant(0.01, shape=[self.attention_size, 1], name='Hquestion'))  # k x 1
예제 #6
0
def read_record(data_path, batch_size=1, size=512):
	feature = {'image': tf.FixedLenFeature(shape=(), dtype=tf.string),
				'wall': tf.FixedLenFeature(shape=(), dtype=tf.string),
				'close': tf.FixedLenFeature(shape=(), dtype=tf.string),
				'room': tf.FixedLenFeature(shape=(), dtype=tf.string),
				'close_wall': tf.FixedLenFeature(shape=(), dtype=tf.string)}

	# Create a list of filenames and pass it to a queue
	filename_queue = tf.train.string_input_producer([data_path], num_epochs=None, shuffle=False, capacity=batch_size*128)
	
	# Define a reader and read the next record
	reader = tf.TFRecordReader()
	_, serialized_example = reader.read(filename_queue)

	# Decode the record read by the reader
	features = tf.parse_single_example(serialized_example, features=feature)

	# Convert the image data from string back to the numbers
	image = tf.decode_raw(features['image'], tf.uint8)
	wall = tf.decode_raw(features['wall'], tf.uint8)
	close = tf.decode_raw(features['close'], tf.uint8)
	room = tf.decode_raw(features['room'], tf.uint8)
	close_wall = tf.decode_raw(features['close_wall'], tf.uint8)

	# Cast data
	image = tf.cast(image, dtype=tf.float32)
	wall = tf.cast(wall, dtype=tf.float32)
	close = tf.cast(close, dtype=tf.float32)
	# room = tf.cast(room, dtype=tf.float32)
	close_wall = tf.cast(close_wall, dtype=tf.float32)

	# Reshape image data into the original shape
	image = tf.reshape(image, [size, size, 3])
	wall = tf.reshape(wall, [size, size, 1])
	close = tf.reshape(close, [size, size, 1])
	room = tf.reshape(room, [size, size])
	close_wall = tf.reshape(close_wall, [size, size, 1])


	# Any preprocessing here ...
	# normalize 
	image = tf.divide(image, tf.constant(255.0))
	wall = tf.divide(wall, tf.constant(255.0))
	close = tf.divide(close, tf.constant(255.0))
	close_wall = tf.divide(close_wall, tf.constant(255.0))

	# Genereate one hot room label
	room_one_hot = tf.one_hot(room, 9, axis=-1)

	# Creates batches by randomly shuffling tensors
	images, walls, closes, rooms, close_walls = tf.train.shuffle_batch([image, wall, close, room_one_hot, close_wall], 
						batch_size=batch_size, capacity=batch_size*128, num_threads=1, min_after_dequeue=batch_size*32)	

	# images, walls = tf.train.shuffle_batch([image, wall], 
						# batch_size=batch_size, capacity=batch_size*128, num_threads=1, min_after_dequeue=batch_size*32)	

	return {'images': images, 'walls': walls, 'closes': closes, 'rooms': rooms, 'close_walls': close_walls}
예제 #7
0
        def _attention(sent1, sent2):
            """
            Compute attention of sentences, positional encoding
             """
            with tf.variable_scope('attend_scope') as self.attend_scope:
                num_units = self.attend_layer_sizes

                repr1 = _feedforwad(sent1, self.attend_scope, num_units)
                repr2 = _feedforwad(sent2,
                                    self.attend_scope,
                                    num_units,
                                    reuse_weights=True)

                m1_m2 = tf.multiply(tf.expand_dims(self.s1_m, 2),
                                    tf.expand_dims(self.s2_m, 1))

                repr2 = tf.transpose(repr2, [0, 2, 1])
                origin_attention = tf.matmul(repr1, repr2)
                origin_attention = tf.multiply(origin_attention, m1_m2)

                attention1 = tf.exp(
                    origin_attention -
                    tf.reduce_max(origin_attention, axis=2, keep_dims=True))
                attention2 = tf.exp(
                    origin_attention -
                    tf.reduce_max(origin_attention, axis=1, keep_dims=True))

                attention1 = tf.multiply(attention1,
                                         tf.expand_dims(self.s2_m, 1))
                attention2 = tf.multiply(attention2,
                                         tf.expand_dims(self.s1_m, 2))

                attention1 = tf.divide(
                    attention1,
                    tf.reduce_sum(attention1, axis=2, keep_dims=True))
                attention2 = tf.divide(
                    attention2,
                    tf.reduce_sum(attention2, axis=1, keep_dims=True))

                attention1 = tf.multiply(attention1, m1_m2)
                attention2 = tf.multiply(attention2, m1_m2)

                alpha = tf.matmul(attention1, sent2, name='alpha')
                beta = tf.matmul(tf.transpose(attention2, [0, 2, 1]),
                                 sent1,
                                 name='beta')

            return alpha, beta
예제 #8
0
파일: metrics.py 프로젝트: yutoc/tflearn
def weighted_r2_op(predictions, targets, inputs):
    """ weighted_r2_op.

    An op that calculates the standard error.

    Examples:
        ```python
        input_data = placeholder(shape=[None, 784])
        y_pred = my_network(input_data) # Apply some ops
        y_true = placeholder(shape=[None, 10]) # Labels
        stderr_op = weighted_r2_op(y_pred, y_true, input_data)

        # Calculate standard error by feeding data X and labels Y
        std_error = sess.run(stderr_op, feed_dict={input_data: X, y_true: Y})
        ```

    Arguments:
        predictions: `Tensor`.
        targets: `Tensor`.
        inputs: `Tensor`.

    Returns:
        `Float`. The standard error.

    """
    with tf.name_scope('WeightedStandardError'):
        if hasattr(inputs, '__len__'):
            inputs = tf.add_n(inputs)
        if inputs.get_shape().as_list() != targets.get_shape().as_list():
            raise Exception(
                "Weighted R2 metric requires Inputs and Targets to "
                "have same shape.")
        a = tf.reduce_sum(tf.square(predictions - inputs))
        b = tf.reduce_sum(tf.square(targets - inputs))
        return tf.divide(a, b)
예제 #9
0
파일: metrics.py 프로젝트: yutoc/tflearn
def r2_op(predictions, targets):
    """ r2_op.

    An op that calculates the standard error.

    Examples:
        ```python
        input_data = placeholder(shape=[None, 784])
        y_pred = my_network(input_data) # Apply some ops
        y_true = placeholder(shape=[None, 10]) # Labels
        stderr_op = r2_op(y_pred, y_true)

        # Calculate standard error by feeding data X and labels Y
        std_error = sess.run(stderr_op, feed_dict={input_data: X, y_true: Y})
        ```

    Arguments:
        predictions: `Tensor`.
        targets: `Tensor`.

    Returns:
        `Float`. The standard error.

    """
    with tf.name_scope('StandardError'):
        a = tf.reduce_sum(tf.square(tf.subtract(targets, predictions)))
        b = tf.reduce_sum(
            tf.square(tf.subtract(targets, tf.reduce_mean(targets))))
        return tf.subtract(1.0, tf.divide(a, b))
    def update_fisher_diag(self, n_task):

        # Reset is mandatory
        print('Mandatory fisher diagonal reset')
        self.reset_fisher_diag()

        print("Reset fishers computed")
        reset_ops = []
        for fdc in self.objs['fisher_diagcs']:
            reset_ops += [tf.assign(fdc, tf.zeros_like(fdc))]
        self.objs['sess'].run(reset_ops)

        n_minibatches = self.it.n // self.fisher_batch_size
        self.it.i = 0
        orig = self.objs['sess'].run(utils.sum_up(self.objs['fisher_diagcs']))
        # imgs_sum = []
        for batch in range(n_minibatches):
            # print("Batch %d" % batch)
            nX, nY = next(self.it)
            # imgs_sum += [np.sum(nY)]
            train_data = {self.phs['fisher_X']: nX, self.phs['fisher_Y']: nY}
            self.objs['sess'].run(self.objs['fisher_sum_up_ops'],
                                  feed_dict=train_data)
            # print(self.objs['sess'].run(self.objs['fisher_diagcs'][0])[0][0])
        newv = self.objs['sess'].run(utils.sum_up(self.objs['fisher_diagcs']))
        # print(orig, newv, n_minibatches, self.fisher_batch_size)
        # print(imgs_sum)
        print('Ran fisher_sum_up_ops (examples: %d)' %
              (n_minibatches * self.fisher_batch_size))

        division_ops = []
        for fdc in self.objs['fisher_diagcs']:
            division_ops += [
                tf.assign(
                    fdc, tf.divide(fdc,
                                   n_minibatches * self.fisher_batch_size))
            ]
        self.objs['sess'].run(division_ops)

        shown_vars = self.objs['fisher_diags']
        orig = self.objs['sess'].run(utils.sum_up(self.objs['fisher_diags']))
        origs = ["%.2f" % orig]
        assign_ops = []
        for fdc, fd in zip(self.objs['fisher_diagcs'],
                           self.objs['fisher_diags']):
            assign_ops += [tf.assign_add(fd, fdc)]
        self.objs['sess'].run(assign_ops)
        newv = self.objs['sess'].run(utils.sum_up(self.objs['fisher_diags']))
        newvs = ["%.2f" % newv]
        print("changed %s => %s" % (" , ".join(origs), " , ".join(newvs)))
        # print("SHOWN:")
        # self.print_vars(shown_vars)

        self.saved_fishers[n_task - 1] = []  # say task 0
        save_ops = []
        for fd in self.objs['fisher_diags']:
            self.saved_fishers[n_task - 1] += [tf.Variable(tf.zeros_like(fd))]
            save_ops += [tf.assign(self.saved_fishers[n_task - 1][-1], fd)]
        self.objs['sess'].run(save_ops)
        print("Saved fishers for task %d" % (n_task - 1))
예제 #11
0
    def create_similarity_model(self):
        this = self._this_mem
        target = self._target_mem
        feed_dict = self._feed_dict

        _main_norm = tf.norm(this, name='this_norm')
        _row_norm = tf.norm(target, name='target_norm')

        expr_dot = tf.tensordot(this, target, 1, name='member_dot')
        expr_div = tf.divide(this, target, name='member_div')

        with tf.Session() as sess:
            _ = tf.Variable(initial_value='fake_variable')
            sess.run(tf.global_variables_initializer())

            main_m = sess.run(_main_norm, {this: feed_dict['this_member']})
            row_mem = sess.run(_row_norm, {target: feed_dict['target_member']})
            print(f'main_m: {main_m}')
            print(f'row_mem: {row_mem}')

            prod = sess.run(expr_dot, {this: feed_dict['this_member'], target: feed_dict['target_member']})
            print(f'PROD: {prod}')
            similarity = sess.run(expr_div, {this: prod, target: (main_m*row_mem)})
            print(f'SIMILARITY: {similarity}')
            
            checkpoint_path = os.path.join(self.path, 'recommend_stock_checkpoint', 'similarity.ckpt')
            saver = tf.train.Saver()
            saver.save(sess, checkpoint_path, global_step=1000)
예제 #12
0
    def _questionnaire_attentive_transfer(self):
        """
        Transferring knowledge from questionnaires. Our contribution.
        Works the same way, as the attentive networks in paper.
        """
        # eq. 3 for computing attentions
        q_attention = tf.exp(
            tf.matmul(
                tf.nn.relu(
                    tf.matmul(self.u_q, self.W_question) + self.B_question),
                self.H_question))
        common_attention = tf.exp(
            tf.matmul(
                tf.nn.relu(
                    tf.matmul(self.u_c, self.W_question) + self.B_question),
                self.H_question))
        # eq. 4 for computing weights
        q_weight = tf.divide(q_attention, q_attention + common_attention)
        common_weight = 1.0 - q_weight
        # eq. 5 for computing transferred user embedding
        user_embedding = q_weight * self.u_q + common_weight * self.u_c

        # returning user embedding, so that we can make predictions. Social weight such that we can analyse,
        # when it's low or high
        return user_embedding, q_weight
예제 #13
0
    def _make_activity_op(self, input_tensor):
        """ Creates the op for calculating the activity of a SOM
        :param input_tensor: A tensor to calculate the activity of. Must be of shape `[batch_size, dim]` where `dim` is
        the dimensionality of the SOM's weights.
        :return A handle to the newly created activity op:
        """
        with self._graph.as_default():
            with tf.name_scope("Activity"):
                # This constant controls the width of the gaussian.
                # The closer to 0 it is, the wider it is.
                c = tf.constant(self._c, dtype="float32")
                # Get the euclidean distance between each neuron and the input vectors
                dist = tf.norm(tf.subtract(
                    tf.expand_dims(self._weights, axis=0),
                    tf.expand_dims(input_tensor, axis=1)),
                               name="Distance",
                               axis=2)  # [batch_size, neurons]

                # Calculate the Gaussian of the activity. Units with distances closer to 0 will have activities
                # closer to 1.
                activity = tf.exp(tf.multiply(tf.pow(dist, 2), c),
                                  name="Gaussian")

                # Convert the activity into a softmax probability distribution
                if self._softmax_activity:
                    activity = tf.divide(tf.exp(activity),
                                         tf.expand_dims(tf.reduce_sum(
                                             tf.exp(activity), axis=1),
                                                        axis=-1),
                                         name="Softmax")

                return tf.identity(activity, name="Output")
예제 #14
0
def lnlike_ellflatpriormarginalized(
    F_obs,  # (nobj, ..., numBands)
    F_obs_var,  # (nobj, ..., numBands)
    F_mod,  #  (nobj, ..., numBands)
):
    """
    Fit linear model to one Gaussian data set (formulation 3)

    Parameters
    ----------
    F_obs, F_obs_var : ndarray (nobj, ..., n_pix_y)
        data and data variances
    F_mod : ndarray (..., n_components, n_pix_y)
        design matrix of linear model

    Returns
    -------
    logfml : ndarray (nobj, )
        log likelihood values with parameters marginalised and at best fit
    ellML : ndarray (nobj, ndim)
        Best fit MAP parameters

    """
    FOT = tf.reduce_sum(F_mod * F_obs / F_obs_var, axis=-1)  # (nobj, ..., )
    FOO = tf.reduce_sum(tf.square(F_obs) / F_obs_var, axis=-1)  # (nobj, ..., )
    FTT = tf.reduce_sum(tf.square(F_mod) / F_obs_var, axis=-1)  # (nobj, ..., )
    LogSigma_det = tf.reduce_sum(tf.math.log(F_obs_var), axis=-1)  # (nobj, ..., )
    Chi2 = FOO - tf.multiply(tf.divide(FOT, FTT), FOT)  # (nobj, ..., )
    LogDenom = LogSigma_det + tf.math.log(FTT)
    LnMarglike = -0.5 * Chi2 - 0.5 * LogDenom  # (nobj, ..., )
    ellML = FOT / FTT
    return LnMarglike, ellML
예제 #15
0
파일: metrics.py 프로젝트: Rory602/gcn
def masked_softmax_cross_entropy(preds, labels, mask):
    """Softmax cross-entropy loss with masking."""
    loss = tf.nn.softmax_cross_entropy_with_logits(logits=preds, labels=labels)
    mask = tf.cast(mask, dtype=tf.float32)
    mask = tf.divide(mask, tf.reduce_mean(mask))
    loss = tf.multiply(loss, mask)
    return tf.reduce_mean(loss)
예제 #16
0
    def compute_x(self, param_name, param, m, prev_w_norm, prev_eta,
                  prev_beta):
        """Compute prev x value on the fly.

    Alternatively, we can store this as a slot but that would double the
    memory usage of our parameters. We don't like that!

    Args:
      param_name: Name of the parameter. Used to check whether to normalize the
        gradients for this layer.
      param: The parameter `Tensor`.
      m: Accumulated momentum `Tensor` of shape same as param.
      prev_w_norm: Scalar tracking norm of the param tensor at previous
        iteration.
      prev_eta: Scalar tracking the learning rate applied at previous iteration.
      prev_beta: Scalar tracking momentum applied at previous iteration.

    Returns:
      x: An intermediate `Tensor` of shape same as param. Will be used for the
        final update.
    """
        prev_ratio = 1.0
        if self._do_layer_adaptation(param_name):
            prev_g_norm = tf.norm(m, ord=2)
            prev_ratio = self.gamma * tf.where(
                tf.math.greater(prev_w_norm, 0),
                tf.where(tf.math.greater(prev_g_norm, 0),
                         (prev_w_norm / prev_g_norm), 1.0), 1.0)
        prev_normalized_m_with_lr = prev_ratio * prev_eta * m

        x = param - tf.divide(
            tf.multiply(prev_beta, prev_normalized_m_with_lr), prev_beta - 1.0)
        return x
예제 #17
0
def mask_logits(logits, mask):
    """Masks logits and writes them to predictions dict.

  Args:
    logits: Float tensor with shape [batch_size, num_classes].
    mask: Boolean tensor with shape [batch_size, num_classes].

  Returns:
    Dict of tensors. It contains keys
    'unmasked_probabilities' and 'masked_probabilities'. Both of them are float
    tensor with shape [batch_size, num_classes].
  """
    with tf.variable_scope('mask_logits'):
        unmasked_probabilities = tf.nn.softmax(logits,
                                               name='unmasked_probabilities')
        unnormalized_masked_probabilities = tf.multiply(
            unmasked_probabilities, tf.cast(mask,
                                            unmasked_probabilities.dtype))
        masked_probabilities = tf.divide(unnormalized_masked_probabilities,
                                         tf.reduce_sum(
                                             unnormalized_masked_probabilities,
                                             axis=1,
                                             keepdims=True),
                                         name='masked_probabilities')
        return {
            'unmasked_probabilities': unmasked_probabilities,
            'masked_probabilities': masked_probabilities,
        }
예제 #18
0
    def read_tensor_from_image_file(self,
                                    file_name,
                                    input_height=299,
                                    input_width=299,
                                    input_mean=0,
                                    input_std=255):
        input_name = "file_reader"
        output_name = "normalized"
        file_reader = tf.read_file(file_name, input_name)
        if file_name.endswith(".png"):
            image_reader = tf.image.decode_png(file_reader,
                                               channels=3,
                                               name='png_reader')
        elif file_name.endswith(".gif"):
            image_reader = tf.squeeze(
                tf.image.decode_gif(file_reader, name='gif_reader'))
        elif file_name.endswith(".bmp"):
            image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
        else:
            image_reader = tf.image.decode_jpeg(file_reader,
                                                channels=3,
                                                name='jpeg_reader')
        float_caster = tf.cast(image_reader, tf.float32)
        dims_expander = tf.expand_dims(float_caster, 0)
        resized = tf.image.resize_bilinear(dims_expander,
                                           [input_height, input_width])
        normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
        sess = tf.Session()
        result = sess.run(normalized)

        return result
예제 #19
0
    def read_tensor_from_image_file(self,
                                    url,
                                    input_height=299,
                                    input_width=299,
                                    input_mean=0,
                                    input_std=255):
        input_name = "file_reader"
        output_name = "normalized"
        imageData = requests.get(url).content

        if ".png" in url:
            image_reader = tf.image.decode_png(imageData,
                                               channels=3,
                                               name='png_reader')
        elif ".gif" in url:
            image_reader = tf.squeeze(
                tf.image.decode_gif(imageData, name='gif_reader'))
        elif ".bmp" in url:
            image_reader = tf.image.decode_bmp(imageData, name='bmp_reader')
        else:
            image_reader = tf.image.decode_jpeg(imageData,
                                                channels=3,
                                                name='jpeg_reader')
        float_caster = tf.cast(image_reader, tf.float32)
        dims_expander = tf.expand_dims(float_caster, 0)
        resized = tf.image.resize_bilinear(dims_expander,
                                           [input_height, input_width])
        normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
        sess = tf.Session()
        result = sess.run(normalized)

        return result
예제 #20
0
파일: losses.py 프로젝트: Asharib90/OCR
    def _compute_loss(self, prediction_tensor, target_tensor, weights):
        """Compute loss function.

    Args:
      prediction_tensor: A float tensor of shape [batch_size, num_anchors,
        num_classes] representing the predicted logits for each class
      target_tensor: A float tensor of shape [batch_size, num_anchors,
        num_classes] representing logit classification targets
      weights: a float tensor of shape, either [batch_size, num_anchors,
        num_classes] or [batch_size, num_anchors, 1]. If the shape is
        [batch_size, num_anchors, 1], all the classses are equally weighted.

    Returns:
      loss: a float tensor of shape [batch_size, num_anchors]
        representing the value of the loss function.
    """
        weights = tf.reduce_mean(weights, axis=2)
        num_classes = prediction_tensor.get_shape().as_list()[-1]
        target_tensor = self._scale_and_softmax_logits(target_tensor)
        prediction_tensor = tf.divide(prediction_tensor,
                                      self._logit_scale,
                                      name='scale_logits')

        per_row_cross_ent = (tf.nn.softmax_cross_entropy_with_logits(
            labels=tf.reshape(target_tensor, [-1, num_classes]),
            logits=tf.reshape(prediction_tensor, [-1, num_classes])))
        return tf.reshape(per_row_cross_ent, tf.shape(weights)) * weights
예제 #21
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)
예제 #22
0
    def contrast_normalize(self, I):
        dist = tf.distributions.Normal(loc=0., scale=self.sigma)

        W = (self.kernel_size - 1) / 2.0
        box_x = tf.lin_space(-W, W, self.kernel_size)
        box_y = tf.lin_space(-W, W, self.kernel_size)

        prob_x = dist.prob(box_x)
        prob_y = dist.prob(box_y)

        gaussian_box = tf.matmul(tf.reshape(prob_x, [self.kernel_size, 1]),
                                 tf.reshape(prob_y, [1, self.kernel_size]))
        gaussian_box = tf.reshape(gaussian_box,
                                  [self.kernel_size, self.kernel_size, 1, 1])
        gaussian_box = tf.divide(gaussian_box, tf.reduce_sum(gaussian_box))

        avg_I = tf.nn.conv2d(I,
                             gaussian_box,
                             strides=[1, 1, 1, 1],
                             padding='SAME')
        normalized_I = I - avg_I

        img_H = tf.shape(I)[1]
        img_W = tf.shape(I)[2]

        normalized_I = tf.slice(
            normalized_I, [0, self.border, self.border, 0],
            [-1, img_H - 2 * self.border, img_W - 2 * self.border, -1])

        return [normalized_I]
예제 #23
0
def get_D(l, u, Ip, I):

    # D matrix for each layer
    D = Ip + tf.where(tf.greater(I, 0.5), tf.divide(u, u - l),
                      tf.zeros_like(I))

    return D
예제 #24
0
def attention_func(Q, K, V):
    Z = tf.matmul(Q, K, transpose_b=True)
    dk = tf.cast(tf.shape(K)[-1], dtype=tf.float32)
    Z = tf.divide(Z, tf.sqrt(dk))
    Z = tf.nn.softmax(Z, dim=-1)
    res = tf.matmul(Z, V)
    res = tf.reduce_mean(res, axis=0)
    return res
예제 #25
0
    def _item_attentive_transfer(self):
        """
        Item attentive transfer (eq. 5)
        """
        # eq. 3 for computing attentions
        item_attention = tf.exp(tf.matmul(tf.nn.relu(tf.matmul(self.u_i, self.W_item) + self.B_item), self.H_item))
        common_attention = tf.exp(tf.matmul(tf.nn.relu(tf.matmul(self.u_c, self.W_item) + self.B_item), self.H_item))
        common2_attention = tf.exp(tf.matmul(tf.nn.relu(tf.matmul(self.u_c2, self.W_item) + self.B_item), self.H_item))
        # eq. 4 for computing weights
        item_weight = tf.divide(item_attention, item_attention + common_attention + common2_attention)
        common_weight = tf.divide(common_attention, item_attention + common_attention + common2_attention)
        common2_weight = tf.divide(common2_attention, item_attention + common_attention + common2_attention)
        # eq. 5 for computing transferred user embedding
        user_embedding = item_weight * self.u_i + common_weight * self.u_c + common2_weight * self.u_c2

        # returning user embedding, so that we can make predictions. Item weight such that we can analyse,
        # when it's low or high
        return user_embedding, item_weight
예제 #26
0
def file_to_tensor(file_path):
    image_string = tf.read_file(file_path)
    image = tf.image.decode_image(image_string, channels=3)

    image.set_shape([None, None, None])
    image = tf.image.resize_images(image, [image_size, image_size])
    image = tf.divide(tf.subtract(image, [0]), [255])
    image.set_shape([image_size, image_size, num_channel])
    return image
예제 #27
0
 def _link(self, x, **kwargs):
     y = x
     if self._mu is not None:
         self._mu = tf.constant(self._mu, dtype=hub.dtype)
         y = tf.subtract(x, self._mu)
     if self._sigma is not None:
         self._sigma = tf.constant(self._sigma, dtype=hub.dtype)
         y = tf.divide(y, self._sigma)
     return y
예제 #28
0
 def compute_second_part_of_loss(self):
     """Compute the second part (distance from x1) of the loss function."""
     loss_part2 = 0.0
     for i in range(self.total_CFs):
         loss_part2 = tf.add(loss_part2,
                             self.compute_dist(self.cfs_frozen[i], self.x1))
     return tf.divide(
         loss_part2,
         tf.cast(tf.multiply(len(self.minx[0]), self.total_CFs),
                 dtype=tf.float32))
예제 #29
0
파일: NN1.py 프로젝트: hbcbh1999/PINN--NUS
    def net_NS(self, P_back, x):
        P_U1_U2_U3 = self.neural_net(tf.concat([P_back, x], 1), self.weights, self.biases)
        P = P_U1_U2_U3[:,0:1]
        U1 = P_U1_U2_U3[:,1:2]
        U2 = P_U1_U2_U3[:,2:3]
        U3 = P_U1_U2_U3[:,3:4]

        # autodiff gradient #1
        mass_flow_grad = tf.gradients(U2, x)[0]
        # autodiff gradient #2
        S = 1 + 2.2*(x-1.5)*(x-1.5)
        momentum_grad = tf.add(tf.gradients(U2*U2/U1 + P*S, x)[0], - tf.multiply(tf.gradients(S, x)[0], P))
        # autodiff gradient #3
        rho_u_E_S = tf.divide(tf.multiply(U2, U3), U1)
        p_u_S = tf.divide(tf.multiply(tf.multiply(P, S), U2), U1)
        net_energy_expression = tf.add(rho_u_E_S, p_u_S)
        energy_grad = tf.gradients(net_energy_expression, x)[0]

        return P, U1, U2, U3, mass_flow_grad, momentum_grad, energy_grad
예제 #30
0
    def compute_first_part_of_loss(self, method):
        """Computes the first part (y-loss) of the loss function."""
        loss_part1 = 0.0
        for i in range(self.total_CFs):
            if method == "l2_loss":
                temp_loss = tf.square(
                    tf.subtract(self.model.get_output(self.cfs_frozen[i]),
                                self.target_cf))
            elif method == "log_loss":
                temp_logits = tf.log(
                    tf.divide(
                        tf.abs(
                            tf.subtract(
                                self.model.get_output(self.cfs_frozen[i]),
                                0.000001)),
                        tf.subtract(
                            1.0,
                            tf.abs(
                                tf.subtract(
                                    self.model.get_output(self.cfs_frozen[i]),
                                    0.000001)))))
                temp_loss = tf.nn.sigmoid_cross_entropy_with_logits(
                    logits=temp_logits, labels=self.target_cf)
            elif method == "hinge_loss":
                temp_logits = tf.log(
                    tf.divide(
                        tf.abs(
                            tf.subtract(
                                self.model.get_output(self.cfs_frozen[i]),
                                0.000001)),
                        tf.subtract(
                            1.0,
                            tf.abs(
                                tf.subtract(
                                    self.model.get_output(self.cfs_frozen[i]),
                                    0.000001)))))
                temp_loss = tf.losses.hinge_loss(logits=temp_logits,
                                                 labels=self.target_cf)

            loss_part1 = tf.add(loss_part1, temp_loss)

        return tf.divide(loss_part1, tf.cast(self.total_CFs, dtype=tf.float32))