Python theano.tensor.min() Examples

The following are 30 code examples of theano.tensor.min(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module theano.tensor , or try the search function .
Example #1
Source File: nanguardmode.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def contains_nan(arr):
    """
    Test whether a numpy.ndarray contains any `np.nan` values.

    Parameters
    ----------
    arr : np.ndarray

    Returns
    -------
    contains_nan : bool
        `True` if the array contains any `np.nan` values, `False` otherwise.

    Notes
    -----
    Tests for the presence of `np.nan`'s using `np.isnan(np.min(ndarray))`.
    This approach is faster and more memory efficient than the obvious
    alternative, calling `np.any(np.isnan(ndarray))`, which requires the
    construction of a boolean array with the same shape as the input array.
    """
    if isinstance(arr, theano.gof.type.CDataType._cdata_type):
        return False
    elif isinstance(arr, np.random.mtrand.RandomState):
        return False
    return np.isnan(np.min(arr)) 
Example #2
Source File: nanguardmode.py    From treeano with Apache License 2.0 6 votes vote down vote up
def contains_nan(arr):
    """
    Test whether a numpy.ndarray contains any `np.nan` values.

    Parameters
    ----------
    arr : np.ndarray

    Returns
    -------
    contains_nan : bool
        `True` if the array contains any `np.nan` values, `False` otherwise.

    Notes
    -----
    Tests for the presence of `np.nan`'s using `np.isnan(np.min(ndarray))`.
    This approach is faster and more memory efficient than the obvious
    alternative, calling `np.any(np.isnan(ndarray))`, which requires the
    construction of a boolean array with the same shape as the input array.
    """
    if isinstance(arr, theano.gof.type.CDataType._cdata_type):
        return False
    elif isinstance(arr, np.random.mtrand.RandomState):
        return False
    return np.isnan(np.min(arr)) 
Example #3
Source File: nanguardmode.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def contains_nan(arr):
    """
    Test whether a numpy.ndarray contains any `np.nan` values.

    Parameters
    ----------
    arr : np.ndarray

    Returns
    -------
    contains_nan : bool
        `True` if the array contains any `np.nan` values, `False` otherwise.

    Notes
    -----
    Tests for the presence of `np.nan`'s using `np.isnan(np.min(ndarray))`.
    This approach is faster and more memory efficient than the obvious
    alternative, calling `np.any(np.isnan(ndarray))`, which requires the
    construction of a boolean array with the same shape as the input array.
    """
    if isinstance(arr, theano.gof.type.CDataType._cdata_type):
        return False
    elif isinstance(arr, np.random.mtrand.RandomState):
        return False
    return np.isnan(np.min(arr)) 
Example #4
Source File: nanguardmode.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def contains_nan(arr):
    """
    Test whether a numpy.ndarray contains any `np.nan` values.

    Parameters
    ----------
    arr : np.ndarray

    Returns
    -------
    contains_nan : bool
        `True` if the array contains any `np.nan` values, `False` otherwise.

    Notes
    -----
    Tests for the presence of `np.nan`'s using `np.isnan(np.min(ndarray))`.
    This approach is faster and more memory efficient than the obvious
    alternative, calling `np.any(np.isnan(ndarray))`, which requires the
    construction of a boolean array with the same shape as the input array.
    """
    if isinstance(arr, theano.gof.type.CDataType._cdata_type):
        return False
    elif isinstance(arr, np.random.mtrand.RandomState):
        return False
    return np.isnan(np.min(arr)) 
Example #5
Source File: nanguardmode.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def contains_nan(arr):
    """
    Test whether a numpy.ndarray contains any `np.nan` values.

    Parameters
    ----------
    arr : np.ndarray

    Returns
    -------
    contains_nan : bool
        `True` if the array contains any `np.nan` values, `False` otherwise.

    Notes
    -----
    Tests for the presence of `np.nan`'s using `np.isnan(np.min(ndarray))`.
    This approach is faster and more memory efficient than the obvious
    alternative, calling `np.any(np.isnan(ndarray))`, which requires the
    construction of a boolean array with the same shape as the input array.
    """
    if isinstance(arr, theano.gof.type.CDataType._cdata_type):
        return False
    elif isinstance(arr, np.random.mtrand.RandomState):
        return False
    return np.isnan(np.min(arr)) 
Example #6
Source File: nanguardmode.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def contains_nan(arr):
    """
    Test whether a numpy.ndarray contains any `np.nan` values.

    Parameters
    ----------
    arr : np.ndarray

    Returns
    -------
    contains_nan : bool
        `True` if the array contains any `np.nan` values, `False` otherwise.

    Notes
    -----
    Tests for the presence of `np.nan`'s using `np.isnan(np.min(ndarray))`.
    This approach is faster and more memory efficient than the obvious
    alternative, calling `np.any(np.isnan(ndarray))`, which requires the
    construction of a boolean array with the same shape as the input array.
    """
    if isinstance(arr, theano.gof.type.CDataType._cdata_type):
        return False
    elif isinstance(arr, np.random.mtrand.RandomState):
        return False
    return np.isnan(np.min(arr)) 
Example #7
Source File: s3c.py    From TextDetector with GNU General Public License v3.0 6 votes vote down vote up
def learn_mini_batch(self, X):
        """
        .. todo::

            WRITEME
        """

        self.learn_func(X)

        if self.momentum_saturation_example is not None:
            alpha = float(self.monitor.get_examples_seen()) / float(self.momentum_saturation_example)
            alpha = min(alpha, 1.0)
            self.momentum.set_value(np.cast[config.floatX]( (1.-alpha) * self.init_momentum + alpha * self.final_momentum))

        if self.monitor.get_examples_seen() % self.print_interval == 0:
            self.print_status()

        if self.debug_m_step:
            if self.energy_functional_diff.get_value() < 0.0:
                warnings.warn( "m step decreased the em functional" )
                if self.debug_m_step != 'warn':
                    quit(-1) 
Example #8
Source File: theano_graph_pro.py    From gempy with GNU Lesser General Public License v3.0 6 votes vote down vote up
def select_finite_faults(self, grid):
        fault_points = T.vertical_stack(T.stack([self.ref_layer_points[0]], axis=0), self.rest_layer_points).T
        ctr = T.mean(fault_points, axis=1)
        x = fault_points - ctr.reshape((-1, 1))
        M = T.dot(x, x.T)
        U, D, V = T.nlinalg.svd(M)
        rotated_x = T.dot(T.dot(grid, U), V)
        rotated_fault_points = T.dot(T.dot(fault_points.T, U), V)
        rotated_ctr = T.mean(rotated_fault_points, axis=0)
        a_radius = (rotated_fault_points[:, 0].max() - rotated_fault_points[:, 0].min()) / 2
        b_radius = (rotated_fault_points[:, 1].max() - rotated_fault_points[:, 1].min()) / 2

        ellipse_factor = (rotated_x[:, 0] - rotated_ctr[0])**2 / a_radius**2 + \
            (rotated_x[:, 1] - rotated_ctr[1])**2 / b_radius**2

        if "select_finite_faults" in self.verbose:
            ellipse_factor = theano.printing.Print("h")(ellipse_factor)

        return ellipse_factor 
Example #9
Source File: theano_export.py    From gempy with GNU Lesser General Public License v3.0 6 votes vote down vote up
def select_finite_faults(self):
        fault_points = T.vertical_stack(T.stack([self.ref_layer_points[0]], axis=0), self.rest_layer_points).T
        ctr = T.mean(fault_points, axis=1)
        x = fault_points - ctr.reshape((-1, 1))
        M = T.dot(x, x.T)
        U = T.nlinalg.svd(M)[2]
        rotated_x = T.dot(self.x_to_interpolate(), U)
        rotated_fault_points = T.dot(fault_points.T, U)
        rotated_ctr = T.mean(rotated_fault_points, axis=0)
        a_radius = (rotated_fault_points[:, 0].max() - rotated_fault_points[:, 0].min()) / 2 + self.inf_factor[
            self.n_surface_op[0] - 1]
        b_radius = (rotated_fault_points[:, 1].max() - rotated_fault_points[:, 1].min()) / 2 + self.inf_factor[
            self.n_surface_op[0] - 1]
        sel = T.lt((rotated_x[:, 0] - rotated_ctr[0]) ** 2 / a_radius ** 2 + (
                    rotated_x[:, 1] - rotated_ctr[1]) ** 2 / b_radius ** 2,
                   1)

        if "select_finite_faults" in self.verbose:
            sel = theano.printing.Print("scalar_field_iter")(sel)

        return sel 
Example #10
Source File: basic.py    From TextDetector with GNU General Public License v3.0 5 votes vote down vote up
def full_min(var):
    """
    .. todo::

        WRITEME properly

    returns a symbolic expression for the value of the minimal
    element of symbolic tensor. T.min does something else as of
    the time of this writing.
    """
    return var.min(axis=range(0,len(var.type.broadcastable))) 
Example #11
Source File: theano_backend.py    From keras-lambda with MIT License 5 votes vote down vote up
def min(x, axis=None, keepdims=False):
    return T.min(x, axis=axis, keepdims=keepdims) 
Example #12
Source File: monitor_update_ratio.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def statistic_to_fn(statistic):
        return {
            "2-norm": lambda x: x.norm(2),
            "max": T.max,
            "min": T.min,
        }[statistic] 
Example #13
Source File: layers.py    From kaggle-heart with MIT License 5 votes vote down vote up
def __init__(self, incoming, mode='max', **kwargs):
        super(ArgmaxAndMaxLayer, self).__init__(incoming, **kwargs)
        if not mode in ('max', 'min', 'mean'):
            raise ValueError('invalid mode')
        self.mode = mode 
Example #14
Source File: layers.py    From kaggle-heart with MIT License 5 votes vote down vote up
def get_output_for(self, slicelocations, **kwargs):
        x = slicelocations - T.min(slicelocations, axis=1).dimshuffle(0, 'x')
        return abs(x * 2.0 / T.max(x, axis=1).dimshuffle(0, 'x') - 1.0) 
Example #15
Source File: s3c.py    From TextDetector with GNU General Public License v3.0 5 votes vote down vote up
def entropy_h(self, H_hat):
        """
        .. todo::

            WRITEME
        """

        for H_hat_v in get_debug_values(H_hat):
            assert H_hat_v.min() >= 0.0
            assert H_hat_v.max() <= 1.0

        return entropy_binary_vector(H_hat) 
Example #16
Source File: layers.py    From kaggle-heart with MIT License 5 votes vote down vote up
def get_output_for(self, input, **kwargs):
        # take the minimal working slice size, and use that one.
        if self.allow_negative:
            inp_low_zero = input - T.min(input, axis=1).dimshuffle(0, 'x')
        else:
            inp_low_zero = input
        return inp_low_zero / T.sum(inp_low_zero, axis=1).dimshuffle(0, 'x') * self.norm_sum 
Example #17
Source File: s3c.py    From TextDetector with GNU General Public License v3.0 5 votes vote down vote up
def entropy_hs(self, H_hat, var_s0_hat, var_s1_hat):
        """
        .. todo::

            WRITEME
        """

        half = as_floatX(.5)

        one = as_floatX(1.)

        two = as_floatX(2.)

        pi = as_floatX(np.pi)

        for H_hat_v in get_debug_values(H_hat):
            assert H_hat_v.min() >= 0.0
            assert H_hat_v.max() <= 1.0

        term1_plus_term2 = self.entropy_h(H_hat)
        assert len(term1_plus_term2.type.broadcastable) == 1

        term3 = T.sum( H_hat * ( half * (T.log(var_s1_hat) +  T.log(two*pi) + one )  ) , axis= 1)
        assert len(term3.type.broadcastable) == 1

        term4 = T.dot( 1.-H_hat, half * (T.log(var_s0_hat) +  T.log(two*pi) + one ))
        assert len(term4.type.broadcastable) == 1


        for t12, t3, t4 in get_debug_values(term1_plus_term2, term3, term4):
            debug_assert(not contains_nan(t12))
            debug_assert(not contains_nan(t3))
            debug_assert(not contains_nan(t4))

        rval = term1_plus_term2 + term3 + term4

        assert len(rval.type.broadcastable) == 1

        return rval 
Example #18
Source File: s3c.py    From TextDetector with GNU General Public License v3.0 5 votes vote down vote up
def print_status(self):
        """
        .. todo::

            WRITEME
        """
        b = self.bias_hid.get_value(borrow=True)
        assert not contains_nan(b)
        p = 1./(1.+np.exp(-b))
        logger.info('p: ({0}, {1}, {2})'.format(p.min(), p.mean(), p.max()))
        B = self.B_driver.get_value(borrow=True)
        assert not contains_nan(B)
        logger.info('B: ({0}, {1}, {2})'.format(B.min(), B.mean(), B.max()))
        mu = self.mu.get_value(borrow=True)
        assert not contains_nan(mu)
        logger.info('mu: ({0}, {1}, {2})'.format(mu.min(), mu.mean(),
                                                 mu.max()))
        alpha = self.alpha.get_value(borrow=True)
        assert not contains_nan(alpha)
        logger.info('alpha: ({0}, {1}, {2})'.format(alpha.min(), alpha.mean(),
                                                    alpha.max()))
        W = self.W.get_value(borrow=True)
        assert isfinite(W)
        logger.info('W: ({0}, {1}, {2})'.format(W.min(), W.mean(), W.max()))
        norms = numpy_norms(W)
        logger.info('W norms: '
                    '({0}, {1}, {2})'.format(norms.min(), norms.mean(),
                                             norms.max())) 
Example #19
Source File: nn_heart.py    From kaggle-heart with MIT License 5 votes vote down vote up
def get_output_for(self, input, **kwargs):
        # take the minimal working slice size, and use that one.
        if self.allow_negative:
            inp_low_zero = input - T.min(input, axis=1).dimshuffle(0, 'x')
        else:
            inp_low_zero = input
        return inp_low_zero / T.sum(inp_low_zero, axis=1).dimshuffle(0, 'x') * self.norm_sum 
Example #20
Source File: s3c.py    From TextDetector with GNU General Public License v3.0 5 votes vote down vote up
def truncated_KL(self, V, Y = None, obs = None):
        """
        KL divergence between variation and true posterior, dropping terms that
        don't depend on the variational parameters

        Parameters
        ----------
        V : WRITEME
        Y : WRITEME
        obs : WRITEME

        Returns
        -------
        WRITEME
        """

        assert Y is None
        assert obs is not None

        H_hat = obs['H_hat']
        var_s0_hat = obs['var_s0_hat']
        var_s1_hat = obs['var_s1_hat']
        S_hat = obs['S_hat']
        model = self.model

        for H_hat_v in get_debug_values(H_hat):
            assert H_hat_v.min() >= 0.0
            assert H_hat_v.max() <= 1.0

        entropy_term = - model.entropy_hs(H_hat = H_hat, var_s0_hat = var_s0_hat, var_s1_hat = var_s1_hat)
        energy_term = model.expected_energy_vhs(V, H_hat = H_hat, S_hat = S_hat,
                                        var_s0_hat = var_s0_hat, var_s1_hat = var_s1_hat)


        for entropy, energy in get_debug_values(entropy_term, energy_term):
            debug_assert(not contains_nan(entropy))
            debug_assert(not contains_nan(energy))

        KL = entropy_term + energy_term

        return KL 
Example #21
Source File: theano_backend.py    From deepQuest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def min(x, axis=None, keepdims=False):
    return T.min(x, axis=axis, keepdims=keepdims) 
Example #22
Source File: monitor_update_ratio.py    From treeano with Apache License 2.0 5 votes vote down vote up
def statistic_to_fn(statistic):
        return {
            "2-norm": lambda x: x.norm(2),
            "max": T.max,
            "min": T.min,
        }[statistic] 
Example #23
Source File: monitor_update_ratio.py    From treeano with Apache License 2.0 5 votes vote down vote up
def mutate_update_deltas(self, network, update_deltas):
        if not network.find_hyperparameter(["monitor"]):
            return
        if not network.find_hyperparameter(["monitor_updates"], True):
            # don't do anything if manually asking to ignore
            # ---
            # rationale: want the ability for a validation network to turn
            # this off
            return
        # these need to be strings so that we can print their names
        # ---
        # by default, only show 2-norm
        # because max and min don't always have the same sign and are harder
        # to compare
        statistics = network.find_hyperparameter(["statistics"],
                                                 ["2-norm"])
        # TODO parameterize search tags (to affect not only "parameters"s)
        vws = network.find_vws_in_subtree(tags={"parameter"},
                                          is_shared=True)
        for vw in vws:
            if vw.variable not in update_deltas:
                continue
            delta = update_deltas[vw.variable]
            for stat in statistics:
                assert isinstance(stat, str)
                name = "%s_%s" % (vw.name, stat)
                stat_fn = self.statistic_to_fn(stat)
                # computing the value of the stat after the update instead of
                # before
                # ---
                # rationale: avoiding 0-division errors for 0-initialized
                # shared variables
                shared_stat = stat_fn(vw.variable + delta)
                delta_stat = stat_fn(delta)
                ratio = delta_stat / shared_stat
                network.create_vw(
                    name,
                    variable=ratio,
                    shape=(),
                    tags={"monitor"}
                ) 
Example #24
Source File: monitor_update_ratio.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def mutate_update_deltas(self, network, update_deltas):
        if not network.find_hyperparameter(["monitor"]):
            return
        if not network.find_hyperparameter(["monitor_updates"], True):
            # don't do anything if manually asking to ignore
            # ---
            # rationale: want the ability for a validation network to turn
            # this off
            return
        # these need to be strings so that we can print their names
        # ---
        # by default, only show 2-norm
        # because max and min don't always have the same sign and are harder
        # to compare
        statistics = network.find_hyperparameter(["statistics"],
                                                 ["2-norm"])
        # TODO parameterize search tags (to affect not only "parameters"s)
        vws = network.find_vws_in_subtree(tags={"parameter"},
                                          is_shared=True)
        for vw in vws:
            if vw.variable not in update_deltas:
                continue
            delta = update_deltas[vw.variable]
            for stat in statistics:
                assert isinstance(stat, str)
                name = "%s_%s" % (vw.name, stat)
                stat_fn = self.statistic_to_fn(stat)
                # computing the value of the stat after the update instead of
                # before
                # ---
                # rationale: avoiding 0-division errors for 0-initialized
                # shared variables
                shared_stat = stat_fn(vw.variable + delta)
                delta_stat = stat_fn(delta)
                ratio = delta_stat / shared_stat
                network.create_vw(
                    name,
                    variable=ratio,
                    shape=(),
                    tags={"monitor"}
                ) 
Example #25
Source File: rationale.py    From rcnn with Apache License 2.0 5 votes vote down vote up
def dump_rationales(self, path, batches_x, batches_y, eval_func, sample_func):
        embedding_layer = self.embedding_layer
        padding_id = self.embedding_layer.vocab_map["<padding>"]
        lst = [ ]
        for bx, by in zip(batches_x, batches_y):
            bz = np.ones(bx.shape, dtype="int8")
            loss_vec_t, preds_t = eval_func(bx, bz, by)
            bz = sample_func(bx)
            loss_vec_r, preds_r = eval_func(bx, bz, by)
            assert len(loss_vec_r) == bx.shape[1]
            for loss_t, p_t, loss_r, p_r, x,y,z in zip(loss_vec_t, preds_t, \
                                loss_vec_r, preds_r, bx.T, by, bz.T):
                loss_t, loss_r = float(loss_t), float(loss_r)
                p_t, p_r, x, y, z = p_t.tolist(), p_r.tolist(), x.tolist(), y.tolist(), z.tolist()
                w = embedding_layer.map_to_words(x)
                r = [ u if v == 1 else "__" for u,v in zip(w,z) ]
                diff = max(y)-min(y)
                lst.append((diff, loss_t, loss_r, r, w, x, y, z, p_t, p_r))

        #lst = sorted(lst, key=lambda x: (len(x[3]), x[2]))
        with open(path,"w") as fout:
            for diff, loss_t, loss_r, r, w, x, y, z, p_t, p_r in lst:
                fout.write( json.dumps( { "diff": diff,
                                          "loss_t": loss_t,
                                          "loss_r": loss_r,
                                          "rationale": " ".join(r),
                                          "text": " ".join(w),
                                          "x": x,
                                          "z": z,
                                          "y": y,
                                          "p_t": p_t,
                                          "p_r": p_r } ) + "\n" ) 
Example #26
Source File: rationale_dependent.py    From rcnn with Apache License 2.0 5 votes vote down vote up
def dump_rationales(self, path, batches_x, batches_y, eval_func, sample_func):
        embedding_layer = self.embedding_layer
        padding_id = self.embedding_layer.vocab_map["<padding>"]
        lst = [ ]
        for bx, by in zip(batches_x, batches_y):
            loss_vec_r, preds_r, bz = eval_func(bx, by)
            assert len(loss_vec_r) == bx.shape[1]
            for loss_r, p_r, x,y,z in zip(loss_vec_r, preds_r, bx.T, by, bz.T):
                loss_r = float(loss_r)
                p_r, x, y, z = p_r.tolist(), x.tolist(), y.tolist(), z.tolist()
                w = embedding_layer.map_to_words(x)
                r = [ u if v == 1 else "__" for u,v in zip(w,z) ]
                diff = max(y)-min(y)
                lst.append((diff, loss_r, r, w, x, y, z, p_r))

        #lst = sorted(lst, key=lambda x: (len(x[3]), x[2]))
        with open(path,"w") as fout:
            for diff, loss_r, r, w, x, y, z, p_r in lst:
                fout.write( json.dumps( { "diff": diff,
                                          "loss_r": loss_r,
                                          "rationale": " ".join(r),
                                          "text": " ".join(w),
                                          "x": x,
                                          "z": z,
                                          "y": y,
                                          "p_r": p_r } ) + "\n" ) 
Example #27
Source File: models.py    From traversing_knowledge_graphs with MIT License 5 votes vote down vote up
def bilinear_diag():
    # W should be a matrix with the diagonal matrix for the i-th relation
    # stored as the i-th column of W
    X_s = T.matrix('X_s')
    W = T.matrix('W')
    X_t = T.matrix('X_t')

    diags = W[:, :, None].transpose(1, 0, 2)

    results, updates = theano.scan(fn=lambda diag, v: diag * v,
                                   outputs_info=X_s, sequences=[diags])

    # # score is always a column vector
    score = X_t.T.dot(results[-1]).reshape((-1, 1))

    # assumes the first entry is the correct score
    margin = score[0] - score[1:]
    cost = T.min(T.concatenate((T.ones_like(margin), margin), axis=1), axis=1).mean()

    gX_s, gW, gX_t = T.grad(cost, [X_s, W, X_t])

    print 'compiling bilinear diag fprop'
    fprop = theano.function([X_s, W, X_t], cost, name='fprop', mode='FAST_RUN')
    fprop.trust_input = True

    print 'compiling bilinear diag bprop'
    bprop = theano.function([X_s, W, X_t], [gX_s, gW, gX_t],
                            name='bprop', mode='FAST_COMPILE')
    bprop.trust_input = True

    print 'compiling bilinear diag score'
    score = theano.function(inputs=[X_s, W, X_t], outputs=score,
                            name='score', mode='FAST_RUN')
    score.trust_input = True

    return {'score': score, 'fprop': fprop, 'bprop': bprop} 
Example #28
Source File: models.py    From traversing_knowledge_graphs with MIT License 5 votes vote down vote up
def neural_tensor_network():
    # tensor params
    subj = T.col('e_1')
    targets = T.matrix('e_2')
    W = T.tensor3('W')

    # neural net params
    u = T.col('u')
    V = T.matrix('V')
    b = T.col('b')

    # tensor
    h = subj.T.dot(W).dot(targets)

    # neural net
    d = subj.shape[0]
    V_subj = V[:, :d].dot(subj)
    V_targ = V[:, d:].dot(targets)

    activations = T.tanh(h + V_subj + V_targ + b)
    score = u.T.dot(activations).reshape((-1, 1))

    margins = score[0] - score[1:]
    cost = T.min(T.concatenate((T.ones_like(margins), margins), axis=1), axis=1).mean()

    gsubj, gtargets, gW, gu, gV, gb = T.grad(cost, [subj, targets, W, u, V, b])

    print 'Compiling NTN score'
    score = theano.function([subj, W, targets, u, V, b], score, name='NTN Score',
                            mode='FAST_RUN')

    print 'Compiling NTN fprop'
    fprop = theano.function([subj, W, targets, u, V, b], cost, name='NTN fprop',
                            mode='FAST_RUN')

    print 'Compiling NTN bprop'
    bprop = theano.function([subj, W, targets, u, V, b],
                            outputs=[gsubj, gW, gtargets, gu, gV, gb],
                            name='NTN bprop', mode='FAST_RUN')

    return {'score': score, 'fprop': fprop, 'bprop': bprop} 
Example #29
Source File: models.py    From traversing_knowledge_graphs with MIT License 5 votes vote down vote up
def bilinear_model(objective):
    # construct theano expression graph
    X_s = T.matrix('X_s')
    W = T.tensor3('W')
    X_t = T.matrix('X_t')

    results, updates = theano.scan(fn=lambda W, v: W.dot(v),
                                   outputs_info=X_s, sequences=[W])

    # score is always a column vector
    score = X_t.T.dot(results[-1]).reshape((-1, 1))

    # # SUM OF MARGINS #####
    # # assumes the first entry is the correct score
    # margin = score[0] - score[1:]
    # cost = T.min(T.concatenate((T.ones_like(margin), margin), axis=1), axis=1).mean()
    # ######################

    ## MAX OF MARGINS ####
    # assumes the first entry is the correct score
    max_margin = score[0] - T.max(score[1:])
    cost = T.min(T.concatenate((T.ones_like(max_margin), max_margin)))
    ######################

    gX_s, gW, gX_t = T.grad(cost, [X_s, W, X_t])

    print 'compiling bilinear fprop'
    fprop = theano.function([X_s, W, X_t], cost, name='fprop', mode='FAST_RUN')
    fprop.trust_input = True

    print 'compiling bilinear bprop'
    bprop = theano.function([X_s, W, X_t], [gX_s, gW, gX_t],
                            name='bprop', mode='FAST_RUN')
    bprop.trust_input = True

    print 'compiling bilinear score'
    score = theano.function(inputs=[X_s, W, X_t], outputs=score,
                            name='score', mode='FAST_RUN')
    score.trust_input = True

    return {'score': score, 'fprop': fprop, 'bprop': bprop} 
Example #30
Source File: mask_generator.py    From Neural-Photo-Editor with MIT License 5 votes vote down vote up
def _get_hidden_layer_connectivity(self, layerIdx):
        layer_size = self._hidden_sizes[layerIdx]
        if layerIdx == 0:
            p_vals = self._get_p(T.min(self.layers_connectivity[layerIdx]))
        else:
            p_vals = self._get_p(T.min(self.layers_connectivity_updates[layerIdx-1]))

        # #Implementations of np.choose in theano GPU
        # return T.nonzero(self._mrng.multinomial(pvals=[self._p_vals] * layer_size, dtype=theano.config.floatX))[1].astype(dtype=theano.config.floatX)
        # return T.argmax(self._mrng.multinomial(pvals=[self._p_vals] * layer_size, dtype=theano.config.floatX), axis=1)
        return T.sum(T.cumsum(self._mrng.multinomial(pvals=T.tile(p_vals[::-1][None, :], (layer_size, 1)), dtype=theano.config.floatX), axis=1), axis=1)