Ejemplo n.º 1
0
def one_run(my_x, my_y, my_z,
            my_u, my_v, my_w,
            my_weight,
            my_heat, my_albedo, my_microns_per_shell):

    # move
    random = rng.uniform(low=0.00003, high=1.)
    t = -T.log(random)

    x_moved = my_x + my_u*t
    y_moved = my_y + my_v*t
    z_moved = my_z + my_w*t

    # absorb
    shell = T.cast(T.sqrt(T.sqr(x_moved) + T.sqr(y_moved) + T.sqr(z_moved))
                   * my_microns_per_shell, 'int32')
    shell = T.clip(shell, 0, SHELL_MAX-1)

    new_weight = my_weight * my_albedo

    # new direction
    xi1 = rng.uniform(low=-1., high=1.)
    xi2 = rng.uniform(low=-1., high=1.)
    xi_norm = T.sqrt(T.sqr(xi1) + T.sqr(xi2))

    t_xi = rng.uniform(low=0.000000001, high=1.)

    # rescale xi12 to fit t_xi as norm
    xi1 = xi1/xi_norm * T.sqr(t_xi)
    xi2 = xi2/xi_norm * T.sqr(t_xi)

    u_new_direction = 2. * t_xi - 1.
    v_new_direction = xi1 * T.sqrt((1. - T.sqr(u_new_direction)) / t_xi)
    w_new_direction = xi2 * T.sqrt((1. - T.sqr(u_new_direction)) / t_xi)

    # roulette
    weight_for_starting_roulette = 0.001
    CHANCE = 0.1
    partakes_roulette = T.switch(T.lt(new_weight, weight_for_starting_roulette),
                                 1,
                                 0)
    roulette = rng.uniform(low=0., high=1.)
    loses_roulette = T.gt(roulette, CHANCE)
    # if roulette decides to terminate the photon: set weight to 0
    weight_after_roulette = ifelse(T.and_(partakes_roulette, loses_roulette),
                                     0.,
                                     new_weight)
    # if partakes in roulette but does not get terminated
    weight_after_roulette = ifelse(T.and_(partakes_roulette, T.invert(loses_roulette)),
                                     weight_after_roulette / CHANCE,
                                     weight_after_roulette)

    new_heat = (1.0 - my_albedo) * my_weight
    heat_i = my_heat[shell]

    return (x_moved, y_moved, z_moved,\
           u_new_direction, v_new_direction, w_new_direction,\
           weight_after_roulette),\
           OrderedDict({my_heat: T.inc_subtensor(heat_i, new_heat)})
Ejemplo n.º 2
0
    def _loss(self, nbrs, nbrs_y, query, query_y):
        """Builds a theano graph for computing memory triplet loss."""

        def get_idx(q_nbrs, q_mem):
            """Gets the index of sample in memory for computing loss.

            We first look to see if the query label can be found in the
            retrieved neighbours, and if not, look to memory for a key with
            the same value.

            We keep track of a boolean mask, which indicates whether or not we
            were able to find a sample with a label that matches the query.
            """

            # Whether a matching sample can be found in neighbours or memory
            any_match_nbrs = T.any(q_nbrs, axis=1)
            any_match_mem = T.any(q_mem, axis=1)
            any_match = T.or_(any_match_nbrs, any_match_mem)

            # Look in neighbours then memory for corresponding sample.
            # If from neighbours, we need to retrieve the full mem idx.
            rows = T.arange(nbrs.shape[0])
            idx = T.switch(any_match_nbrs,
                           nbrs[rows, tensor_choose_k(q_nbrs, self.rng, k=1)],
                           tensor_choose_k(q_mem, self.rng, k=1, random=True))

            return (idx, any_match)

        # Make the labels broadcastable for indexing
        query_y_2d = T.reshape(query_y, (-1, 1))

        query_in_nbrs = T.eq(query_y_2d, nbrs_y) #(n_queries, self.k_nbrs)
        query_in_mem = T.eq(query_y_2d, T.reshape(self.V, (1, -1)))

        positive = get_idx(query_in_nbrs, query_in_mem)
        pos_loss = T.sum(query*self.K[positive[0]], axis=1)*positive[1]

        negative = get_idx(T.invert(query_in_nbrs), T.invert(query_in_mem))
        neg_loss = T.sum(query*self.K[negative[0]], axis=1)*negative[1]

        # Only return the positive components
        return T.maximum(0, neg_loss - pos_loss + self.alpha)
Ejemplo n.º 3
0
def pairwise_hinge(pred, target, margin, kernel):
    n = pred.shape[0]
    delta_pred = pred.reshape((n, 1)) - pred.reshape((1, n))
    delta_target = target.reshape((n, 1)) - target.reshape((1, n))
    if kernel == 'sign':
        delta_target = T.sgn(delta_target)
    elif kernel == 'linear':
        pass
    else:
        raise KeyError("Unknown kernel %s" % kernel)
    losses = T.maximum(0, margin - delta_pred * delta_target) * T.invert(
        T.eye(n, dtype='bool'))
    norm_loss = T.sum(losses) / n / (n - 1)
    return norm_loss
Ejemplo n.º 4
0
    # squared hinge loss
    fp_multiplier = 1
    loss = T.mean(
        T.sqr(T.maximum(0., 1. - target * train_output)) *
        np.asarray([fp_multiplier, 1]))
    err = T.mean(T.neq(T.argmax(train_output, axis=1), T.argmax(target,
                                                                axis=1)),
                 dtype=theano.config.floatX)
    train_1_when_0 = T.sum(T.gt(T.argmax(train_output, axis=1),
                                T.argmax(target, axis=1)),
                           dtype=theano.config.floatX)  # face = 0, bg = 1 : fn
    train_0_when_1 = T.sum(T.lt(T.argmax(train_output, axis=1),
                                T.argmax(target, axis=1)),
                           dtype=theano.config.floatX)  # fp
    # the T.invert function seems to react differently depending on theano versions...
    train_0_when_0 = T.sum(T.invert(
        T.or_(T.argmax(train_output, axis=1), T.argmax(target, axis=1))),
                           dtype=theano.config.floatX)
    # if this does not work, try
    # train_0_when_0 = batch_size - T.sum(T.or_(T.argmax(train_output,axis=1),T.argmax(target,axis=1))),dtype=theano.config.floatX)
    train_precision = train_0_when_0 / (train_0_when_0 + train_0_when_1
                                        )  # TP/(TP+FP)
    train_recall = train_0_when_0 / (train_0_when_0 + train_1_when_0
                                     )  # TP/(TP+FN)

    if binary:

        # W updates
        W = lasagne.layers.get_all_params(cnn, binary=True)
        W_grads = binary_net.compute_grads(loss, cnn)
        updates = lasagne.updates.adam(loss_or_grads=W_grads,
                                       params=W,
Ejemplo n.º 5
0
#theano.printing.Print('vw')(vw_new_direction)
#theano.printing.Print('uvw')(uvw_new_direction)
# roulette
weight_for_starting_roulette = 0.001
CHANCE = 0.1
partakes_roulette = T.switch(T.lt(new_weight, weight_for_starting_roulette),
                             1,
                             0)
roulette = rng.uniform((photons,1), low=0., high=1.)
loses_roulette = T.gt(roulette, CHANCE)
# if roulette decides to ter+minate the photon: set weight to 0
weight_after_roulette = T.switch(T.and_(partakes_roulette, loses_roulette),
                                 0.,
                                 new_weight)
# if partakes in roulette but does not get terminated
weight_after_roulette = T.switch(T.and_(partakes_roulette, T.invert(loses_roulette)),
                                 weight_after_roulette / CHANCE,
                                 weight_after_roulette)
#theano.printing.Print('new weight')(new_weight)
#theano.printing.Print('partakes_roulette')(partakes_roulette)
#theano.printing.Print('loses_roulette')(loses_roulette)
#theano.printing.Print('weight_after_roulette')(weight_after_roulette)


one_cycle = theano.function(inputs=[mu_a, mu_s, microns_per_shell],
                            outputs=[shells, new_heats],
                            updates=OrderedDict({xyz: xyz_moved, uvw: uvw_new_direction,
                                                 weight: weight_after_roulette,
                                                 finished: T.allclose(weight, 0.)}))

Ejemplo n.º 6
0
 def tru_neg(self, y):
     return T.nonzero(T.invert(T.or_(y, self.y_out)))
Ejemplo n.º 7
0
#theano.printing.Print('t_xi')(t_xi)
#theano.printing.Print('vw')(vw_new_direction)
#theano.printing.Print('uvw')(uvw_new_direction)
# roulette
weight_for_starting_roulette = 0.001
CHANCE = 0.1
partakes_roulette = T.switch(T.lt(new_weight, weight_for_starting_roulette), 1,
                             0)
roulette = rng.uniform((photons, 1), low=0., high=1.)
loses_roulette = T.gt(roulette, CHANCE)
# if roulette decides to ter+minate the photon: set weight to 0
weight_after_roulette = T.switch(T.and_(partakes_roulette, loses_roulette), 0.,
                                 new_weight)
# if partakes in roulette but does not get terminated
weight_after_roulette = T.switch(
    T.and_(partakes_roulette, T.invert(loses_roulette)),
    weight_after_roulette / CHANCE, weight_after_roulette)
#theano.printing.Print('new weight')(new_weight)
#theano.printing.Print('partakes_roulette')(partakes_roulette)
#theano.printing.Print('loses_roulette')(loses_roulette)
#theano.printing.Print('weight_after_roulette')(weight_after_roulette)

one_cycle = theano.function(inputs=[mu_a, mu_s, microns_per_shell],
                            outputs=[shells, new_heats],
                            updates=OrderedDict({
                                xyz:
                                xyz_moved,
                                uvw:
                                uvw_new_direction,
                                weight:
                                weight_after_roulette,
Ejemplo n.º 8
0
 def invert(self, t):
     return T.invert(t)
Ejemplo n.º 9
0
    def tru_neg(self, y):
	return T.nonzero(T.invert(T.or_(y,self.y_out)))
Ejemplo n.º 10
0
i2r = i2[:, :, 0].flatten()
i2g = i2[:, :, 1].flatten()
i2b = i2[:, :, 2].flatten()
d2f = d2.flatten()
d1d2ltepsilon_cond = T.and_(T.abs_(d1f - d2f) < 0.05, T.and_(d2f > 0, d1f > 0))
d1d2ltepsilon = d1d2ltepsilon_cond.nonzero()
d1f = T.set_subtensor(d1f[d1d2ltepsilon],
                      (d1f[d1d2ltepsilon] + d2f[d1d2ltepsilon]) / 2)
i1r = T.set_subtensor(i1r[d1d2ltepsilon],
                      (i1r[d1d2ltepsilon] + i2r[d1d2ltepsilon]) / 2)
i1g = T.set_subtensor(i1g[d1d2ltepsilon],
                      (i1g[d1d2ltepsilon] + i2g[d1d2ltepsilon]) / 2)
i1b = T.set_subtensor(i1b[d1d2ltepsilon],
                      (i1b[d1d2ltepsilon] + i2b[d1d2ltepsilon]) / 2)
d2ltd1 = T.and_(d2f < d1f, T.and_(d2f > 0,
                                  T.invert(d1d2ltepsilon_cond))).nonzero()
d1f = T.set_subtensor(d1f[d2ltd1], d2f[d2ltd1])
i1r = T.set_subtensor(i1r[d2ltd1], i2r[d2ltd1])
i1g = T.set_subtensor(i1g[d2ltd1], i2g[d2ltd1])
i1b = T.set_subtensor(i1b[d2ltd1], i2b[d2ltd1])
i1f = T.as_tensor_variable([i1r, i1g, i1b, i1[:, :, 3].flatten()])
i1f = T.set_subtensor(i1f[3, :], 255)
_combineTwoImages = theano.function([i1, d1, i2, d2], (T.transpose(i1f), d1f),
                                    on_unused_input='ignore')

i1r = i1[:, :, 0].flatten()
i1g = i1[:, :, 1].flatten()
i1b = i1[:, :, 2].flatten()
i2r = i2[:, :, 0].flatten()
i2g = i2[:, :, 1].flatten()
i2b = i2[:, :, 2].flatten()
            def _iter_fn(input_repr, ref_matrix, gstate, correct_num_new_nodes=None, correct_new_strengths=None, correct_new_node_ids=None, correct_edges=None, dropout_masks=None):
                # If necessary, update node state
                if self.nodes_mutable:
                    gstate, dropout_masks = self.node_state_updater.process(gstate, input_repr, dropout_masks)

                if len(self.word_node_mapping) > 0:
                    gstate, dropout_masks = self.direct_reference_updater.process(gstate, ref_matrix, dropout_masks)

                # If necessary, propagate node state
                if self.intermediate_propagate != 0:
                    gstate, dropout_masks = self.intermediate_propagator.process_multiple(gstate, self.intermediate_propagate, dropout_masks)

                node_loss = None
                node_accuracy = None
                # Propose and vote on new nodes
                if self.dynamic_nodes:
                    new_strengths, new_ids, dropout_masks = self.new_node_adder.get_candidates(gstate, input_repr, self.new_nodes_per_iter, dropout_masks)
                    # new_strengths and correct_new_strengths are of shape (n_batch, new_nodes_per_iter)
                    # new_ids and correct_new_node_ids are of shape (n_batch, new_nodes_per_iter, num_node_ids)
                    if with_correct_graph:
                        perm_idxs = np.array(list(itertools.permutations(range(self.new_nodes_per_iter))))
                        permuted_correct_str = correct_new_strengths[:,perm_idxs]
                        permuted_correct_ids = correct_new_node_ids[:,perm_idxs]
                        # due to advanced indexing, we should have shape (n_batch, permutation, new_nodes_per_iter, num_node_ids)
                        ext_new_str = T.shape_padaxis(new_strengths,1)
                        ext_new_ids = T.shape_padaxis(new_ids,1)
                        strength_ll = permuted_correct_str * T.log(ext_new_str + util.EPSILON) + (1-permuted_correct_str) * T.log(1-ext_new_str + util.EPSILON)
                        ids_ll = permuted_correct_ids * T.log(ext_new_ids  + util.EPSILON)
                        reduced_perm_lls = T.sum(strength_ll, axis=2) + T.sum(ids_ll, axis=[2,3])
                        if self.best_node_match_only:
                            node_loss = -T.max(reduced_perm_lls, 1)
                        else:
                            full_ll = util.reduce_log_sum(reduced_perm_lls, 1)
                            # Note that some of these permutations are identical, since we likely did not add the maximum
                            # amount of nodes. Thus we will have added repeated elements here.
                            # We have log(x+x+...+x) = log(kx), where k is the repetition factor and x is the probability we want
                            # log(kx) = log(k) + log(x)
                            # Our repetition factor k is given by (new_nodes_per_iter - correct_num_new_nodes)!
                            # Recall that n! = gamma(n+1)
                            # so log(x) = log(kx) - log(gamma(k+1))
                            log_rep_factor = T.gammaln(T.cast(self.new_nodes_per_iter - correct_num_new_nodes + 1, 'floatX'))
                            scaled_ll = full_ll - log_rep_factor
                            node_loss = -scaled_ll
                        if evaluate_accuracy:
                            best_match_idx = T.argmax(reduced_perm_lls, 1)
                            # should be of shape (n_batch), indexing the best permutation
                            best_correct_str = permuted_correct_str[T.arange(n_batch), best_match_idx]
                            best_correct_ids = permuted_correct_ids[T.arange(n_batch), best_match_idx]
                            snapped_strengths = util.independent_best(new_strengths)
                            snapped_ids = util.categorical_best(new_ids) * T.shape_padright(snapped_strengths)
                            close_strengths = T.all(T.isclose(best_correct_str, snapped_strengths), (1))
                            close_ids = T.all(T.isclose(best_correct_ids, snapped_ids), (1,2))
                            node_accuracy = T.and_(close_strengths, close_ids)
                        # now substitute in the correct nodes
                        gstate = gstate.with_additional_nodes(correct_new_strengths, correct_new_node_ids)
                    elif snap_to_best:
                        snapped_strengths = util.independent_best(new_strengths)
                        snapped_ids = util.categorical_best(new_ids)
                        gstate = gstate.with_additional_nodes(snapped_strengths, snapped_ids)
                    else:
                        gstate = gstate.with_additional_nodes(new_strengths, new_ids)


                # Update edge state
                gstate, dropout_masks = self.edge_state_updater.process(gstate, input_repr, dropout_masks)
                if with_correct_graph:
                    cropped_correct_edges = correct_edges[:,:gstate.n_nodes,:gstate.n_nodes,:]
                    edge_lls = cropped_correct_edges * T.log(gstate.edge_strengths + util.EPSILON) + (1-cropped_correct_edges) * T.log(1-gstate.edge_strengths + util.EPSILON)
                    # edge_lls currently penalizes for edges connected to nodes that do not exist
                    # we do not want it to do this, so we mask it with node strengths
                    mask_src = util.shape_padaxes(gstate.node_strengths,[2,3])
                    mask_dest = util.shape_padaxes(gstate.node_strengths,[1,3])
                    masked_edge_lls = edge_lls * mask_src * mask_dest
                    edge_loss = -T.sum(masked_edge_lls, axis=[1,2,3])
                    if evaluate_accuracy:
                        snapped_edges = util.independent_best(gstate.edge_strengths)
                        close_edges = T.isclose(cropped_correct_edges, snapped_edges)
                        ok_mask = T.invert(T.cast(mask_src * mask_dest,'bool')) # its OK for things not to match if node strengths are NOT both 1
                        edge_accuracy = T.all(T.or_(close_edges, ok_mask), (1,2,3))
                        overall_accuracy = edge_accuracy if node_accuracy is None else T.and_(node_accuracy, edge_accuracy)
                    else:
                        overall_accuracy = None
                    gstate = gstate.with_updates(edge_strengths=cropped_correct_edges)
                    return gstate, node_loss, edge_loss, overall_accuracy
                elif snap_to_best:
                    snapped_edges = util.independent_best(gstate.edge_strengths)
                    gstate = gstate.with_updates(edge_strengths=snapped_edges)
                    return gstate
                else:
                    return gstate