Python theano.tensor 模块,ones() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用theano.tensor.ones()

项目:crfrnn_layer    作者:HapeMask    | 项目源码 | 文件源码
def get_output_for(self, inputs, **kwargs):
        vals, ref = inputs

        def filt(V, R):
            if self.norm_type is not None:
                o = tt.ones((1, V.shape[1], V.shape[2]), np.float32)
                norm = gaussian_filter(R, o, self.kern_std, self.ref_dim)
                norm = tt.sqrt(norm) if self.norm_type == "sym" else norm
                norm += 1e-8

            V = V / norm if self.norm_type in ["pre", "sym"] else V
            F = gaussian_filter(R, V, self.kern_std)
            return F / norm if self.norm_type in ["post", "sym"] else F

        filtered = theano.scan(fn=filt, sequences=[vals, ref],
                               outputs_info=None)[0]
        return filtered
项目:NMT    作者:tuzhaopeng    | 项目源码 | 文件源码
def create_probs_computer(self, return_alignment=False):
        if not hasattr(self, 'probs_fn'):
            logger.debug("Compile probs computer")
            self.probs_fn = theano.function(
                    inputs=self.inputs,
                    outputs=[self.predictions.word_probs, self.alignment],
                    name="probs_fn")
        def probs_computer(x, y):
            x_mask = numpy.ones(x.shape[0], dtype="float32")
            y_mask = numpy.ones(y.shape[0], dtype="float32")
            probs, alignment = self.probs_fn(x[:, None], y[:, None],
                    x_mask[:, None], y_mask[:, None])
            if return_alignment:
                return probs, alignment
            else:
                return probs
        return probs_computer
项目:semi-auto-anno    作者:moberweger    | 项目源码 | 文件源码
def project3Dto2D(self, Li, idxs):
        """
        Project 3D point to 2D
        :param Li: joints in normalized 3D
        :param idxs: frames specified by subset
        :return: 2D points, in normalized 2D coordinates
        """

        if not isinstance(idxs, numpy.ndarray):
            idxs = numpy.asarray([idxs])

        # 3D -> 2D projection also shift by M to cropped window
        Li_glob3D = (numpy.reshape(Li, (len(idxs), self.numJoints, 3))*self.Di_scale[idxs][:, None, None]+self.Di_off3D[idxs][:, None, :]).reshape((len(idxs)*self.numJoints, 3))
        Li_glob3D_hom = numpy.concatenate([Li_glob3D, numpy.ones((len(idxs)*self.numJoints, 1), dtype='float32')], axis=1)
        Li_glob2D_hom = numpy.dot(Li_glob3D_hom, self.cam_proj.T)
        Li_glob2D = (Li_glob2D_hom[:, 0:3] / Li_glob2D_hom[:, 3][:, None]).reshape((len(idxs), self.numJoints, 3))
        Li_img2D_hom = numpy.einsum('ijk,ikl->ijl', Li_glob2D, self.Di_trans2D[idxs])
        Li_img2D = (Li_img2D_hom[:, :, 0:2] / Li_img2D_hom[:, :, 2][:, :, None]).reshape((len(idxs), self.numJoints*2))
        Li_img2Dcrop = (Li_img2D - (self.Di.shape[3]/2.)) / (self.Di.shape[3]/2.)
        return Li_img2Dcrop
项目:KGP-ASR    作者:KGPML    | 项目源码 | 文件源码
def _add_blanks(y, blank_symbol, y_mask=None):
    """Add blanks to a matrix and updates mask
    Input shape: output_seq_len x num_batch
    Output shape: 2*output_seq_len+1 x num_batch
    """
    # for y
    y_extended = y.T.dimshuffle(0, 1, 'x')
    blanks = tensor.zeros_like(y_extended) + blank_symbol
    concat = tensor.concatenate([y_extended, blanks], axis=2)
    res = concat.reshape((concat.shape[0],
                          concat.shape[1] * concat.shape[2])).T
    begining_blanks = tensor.zeros((1, res.shape[1])) + blank_symbol
    blanked_y = tensor.concatenate([begining_blanks, res], axis=0)
    # for y_mask
    if y_mask is not None:
        y_mask_extended = y_mask.T.dimshuffle(0, 1, 'x')
        concat = tensor.concatenate([y_mask_extended,
                                     y_mask_extended], axis=2)
        res = concat.reshape((concat.shape[0],
                              concat.shape[1] * concat.shape[2])).T
        begining_blanks = tensor.ones((1, res.shape[1]), dtype=floatX)
        blanked_y_mask = tensor.concatenate([begining_blanks, res], axis=0)
    else:
        blanked_y_mask = None
    return blanked_y.astype('int32'), blanked_y_mask
项目:SocializedWordEmbeddings    作者:HKUST-KnowComp    | 项目源码 | 文件源码
def forward_all(self, x, masks = None, h0=None, return_c=False, direction = None):
        if h0 is None:
            if x.ndim > 1:
                h0 = T.zeros((x.shape[1], self.n_out*(self.order+1)), dtype=theano.config.floatX)
            else:
                h0 = T.zeros((self.n_out*(self.order+1),), dtype=theano.config.floatX)

        if masks == None:
            masks = T.ones((x.shape[0], x.shape[1]), dtype = theano.config.floatX)
        h, _ = theano.scan(
                    fn = self.forward,
                    sequences = [x, masks],
                    outputs_info = [ h0 ]
                )
        if return_c:
            return h
        elif x.ndim > 1:
            return h[:,:,self.n_out*self.order:]
        else:
            return h[:,self.n_out*self.order:]
项目:SocializedWordEmbeddings    作者:HKUST-KnowComp    | 项目源码 | 文件源码
def initialize_params(self, n_in, n_out, activation):
        if USE_XAVIER_INIT:
            if activation == ReLU:
                scale = np.sqrt(4.0/(n_in+n_out), dtype=theano.config.floatX)
                b_vals = np.ones(n_out, dtype=theano.config.floatX) * 0.01
            elif activation == softmax:
                scale = np.float64(0.001 * scale).astype(theano.config.floatX)
                b_vals = np.zeros(n_out, dtype=theano.config.floatX)
            else:
                scale = np.sqrt(2.0/(n_in+n_out), dtype=theano.config.floatX)
                b_vals = np.zeros(n_out, dtype=theano.config.floatX)
            W_vals = random_init((n_in,n_out), rng_type="normal") * scale
        else:
            W_vals = random_init((n_in,n_out))
            if activation == softmax:
                W_vals *= (0.001 * self.scale)
            if activation == ReLU:
                b_vals = np.ones(n_out, dtype=theano.config.floatX) * 0.01
            else:
                b_vals = random_init((n_out,))
        self.W = create_shared(W_vals, name="W")
        if self.has_bias: self.b = create_shared(b_vals, name="b")
项目:deeptravel    作者:keon    | 项目源码 | 文件源码
def gen_hull(p, p_mask, f_encode, f_probi, options):
    # p: n_sizes * n_samples * data_dim
    n_sizes = p.shape[0]
    n_samples = p.shape[1] if p.ndim == 3 else 1
    hprev = f_encode(p_mask, p)  # n_sizes * n_samples * data_dim
    points = numpy.zeros((n_samples, n_sizes), dtype='int64')
    h = hprev[-1]
    c = numpy.zeros((n_samples, options['dim_proj']), dtype=config.floatX)
    xi = numpy.zeros((n_samples,), dtype='int64')
    xi_mask = numpy.ones((n_samples,), dtype=config.floatX)
    for i in range(n_sizes):
        h, c, probi = f_probi(p_mask[i], xi, h, c, hprev, p_mask, p)
        xi = probi.argmax(axis=0)
        xi *= xi_mask.astype(numpy.int64)  # Avoid compatibility problem in numpy 1.10
        xi_mask = (numpy.not_equal(xi, 0)).astype(config.floatX)
        if numpy.equal(xi_mask, 0).all():
            break
        points[:, i] = xi
    return points
项目:odin    作者:imito    | 项目源码 | 文件源码
def log_prob_bernoulli(p_true, p_approx, mask=None):
  """ Compute log probability of some binary variables with probabilities
  given by p_true, for probability estimates given by p_approx. We'll
  compute joint log probabilities over row-wise groups.
  Note
  ----
  origin implementation from:
  https://github.com/Philip-Bachman/ICML-2015/blob/master/LogPDFs.py
  Copyright (c) Philip Bachman
  """
  if mask is None:
    mask = T.ones((1, p_approx.shape[1]))
  log_prob_1 = p_true * T.log(p_approx)
  log_prob_0 = (1.0 - p_true) * T.log(1.0 - p_approx)
  log_prob_01 = log_prob_1 + log_prob_0
  row_log_probs = T.sum((log_prob_01 * mask), axis=1, keepdims=True)
  return row_log_probs

#logpxz = -0.5*np.log(2 * np.pi) - log_sigma_decoder - (0.5 * ((x - mu_decoder) / T.exp(log_sigma_decoder))**2)
项目:odin    作者:imito    | 项目源码 | 文件源码
def log_prob_gaussian(mu_true, mu_approx, les_sigmas=1.0, mask=None):
  """
  Compute log probability of some continuous variables with values given
  by mu_true, w.r.t. gaussian distributions with means given by mu_approx
  and standard deviations given by les_sigmas.
  Note
  ----
  origin implementation from:
  https://github.com/Philip-Bachman/ICML-2015/blob/master/LogPDFs.py
  Copyright (c) Philip Bachman
  """
  if mask is None:
    mask = T.ones((1, mu_approx.shape[1]))
  ind_log_probs = C - T.log(T.abs_(les_sigmas)) - \
  ((mu_true - mu_approx)**2.0 / (2.0 * les_sigmas**2.0))
  row_log_probs = T.sum((ind_log_probs * mask), axis=1, keepdims=True)
  return row_log_probs
项目:odin    作者:imito    | 项目源码 | 文件源码
def log_prob_gaussian2(mu_true, mu_approx, log_vars=1.0, mask=None):
  """
  Compute log probability of some continuous variables with values given
  by mu_true, w.r.t. gaussian distributions with means given by mu_approx
  and log variances given by les_logvars.
  Note
  ----
  origin implementation from:
  https://github.com/Philip-Bachman/ICML-2015/blob/master/LogPDFs.py
  Copyright (c) Philip Bachman
  """
  if mask is None:
    mask = T.ones((1, mu_approx.shape[1]))
  ind_log_probs = C - (0.5 * log_vars) - \
  ((mu_true - mu_approx)**2.0 / (2.0 * T.exp(log_vars)))
  row_log_probs = T.sum((ind_log_probs * mask), axis=1, keepdims=True)
  return row_log_probs
项目:lmkit    作者:jiangnanhugo    | 项目源码 | 文件源码
def build_predict(self):
        n_steps=self.x.shape[0]
        batch_size=self.x.shape[1]

        fires=T.ones(shape=(n_steps,batch_size),dtype=np.int32)*self.tree[-1][0].index

        def predict_step(current_node,input_vector):
            # left nodes
            node_res_l=T.nnet.sigmoid(T.sum(self.wp_matrix[current_node]*input_vector,axis=-1))
            node_res_r=T.nnet.sigmoid(-1.0*T.sum(self.wp_matrix[current_node]*input_vector,axis=-1))

            choice=node_res_l>node_res_r
            next_node=self.tree_matrix[current_node,choice]
            labelings=self.tree_matrix[current_node,choice+2]

            return next_node,labelings,choice

        xresult,_=theano.scan(fn=predict_step,
                              outputs_info=[fires,None,None],
                              non_sequences=self.x,
                              n_steps=self.max_route_len)
        self.labels=xresult[1][-1]*self.maskY
        self.predict_labels=theano.function(inputs=[self.x,self.maskY],
                                           outputs=self.labels)
        #self.label_tool=theano.function([self.x],xresult)
项目:lmkit    作者:jiangnanhugo    | 项目源码 | 文件源码
def build_predict(self):
        n_steps=self.x.shape[0]
        batch_size=self.x.shape[1]

        fires=T.ones(shape=(n_steps,batch_size),dtype=np.int32)*self.tree[-1][0].index

        def predict_step(current_node,input_vector):
            # left nodes
            node_res_l=T.nnet.sigmoid(T.sum(self.wp_matrix[current_node]*input_vector,axis=-1))
            node_res_r=T.nnet.sigmoid(-1.0*T.sum(self.wp_matrix[current_node]*input_vector,axis=-1))

            choice=node_res_l>node_res_r
            next_node=self.tree_matrix[current_node,choice]
            labelings=self.tree_matrix[current_node,choice+2]

            return next_node,labelings,choice

        xresult,_=theano.scan(fn=predict_step,
                              outputs_info=[fires,None,None],
                              non_sequences=self.x,
                              n_steps=self.max_route_len)
        self.labels=xresult[1][-1]*self.maskY
        self.predict_labels=theano.function(inputs=[self.x,self.maskY],
                                           outputs=self.labels)
        #self.label_tool=theano.function([self.x],xresult)
项目:nematus    作者:EdinburghNLP    | 项目源码 | 文件源码
def param_init_fflayer(options, params, prefix='ff', nin=None, nout=None,
                       ortho=True, weight_matrix=True, bias=True, followed_by_softmax=False):
    if nin is None:
        nin = options['dim_proj']
    if nout is None:
        nout = options['dim_proj']
    if weight_matrix:
        params[pp(prefix, 'W')] = norm_weight(nin, nout, scale=0.01, ortho=ortho)
    if bias:
       params[pp(prefix, 'b')] = numpy.zeros((nout,)).astype(floatX)

    if options['layer_normalisation'] and not followed_by_softmax:
        scale_add = 0.0
        scale_mul = 1.0
        params[pp(prefix,'ln_b')] = scale_add * numpy.ones((1*nout)).astype(floatX)
        params[pp(prefix,'ln_s')] = scale_mul * numpy.ones((1*nout)).astype(floatX)

    if options['weight_normalisation'] and not followed_by_softmax:
        scale_mul = 1.0
        params[pp(prefix,'W_wns')] = scale_mul * numpy.ones((1*nout)).astype(floatX)

    return params
项目:deep-motion-analysis    作者:Brimborough    | 项目源码 | 文件源码
def skip_connect(self, input, layer_index):
        if ([] == self.noisy_z):
            raise ValueError('Error: noisy_z is an empty list, noisy_fprop must be run before skip_connect')

        MU = self.compute_mu(input, self.As[layer_index])
        V  = self.compute_v(input, self.As[layer_index])

        reconstruction = (self.noisy_z[-1] - MU) * V + MU

#        # Non trainable Batchnormalisation
#        mean = reconstruction.mean(0)
#        std  = reconstruction.std(0) + 1e-10
#
#        # Only batchnormalise for a batchsize > 1
#        mean = ifelse(T.gt(input.shape[0], 1), mean, T.zeros(mean.shape, dtype=mean.dtype))
#        std  = ifelse(T.gt(input.shape[0], 1), std, T.ones(std.shape, dtype=std.dtype))

#        reconstruction = (reconstruction - mean) / std
        self.tmp = reconstruction

        # To caluclate the reconstruction error later
        self.reconstructions.append(reconstruction)
        self.noisy_z = self.noisy_z[0:-1]

        return reconstruction
项目:dl4nlp_in_theano    作者:luyaojie    | 项目源码 | 文件源码
def temporal_padding_mask(mask, kernel_size, padding_size):
    """Pad the middle dimension of a 2D matrix
    with "padding" zeros left and right.

    Apologies for the inane API, but Theano makes this
    really hard.
    Code from https://github.com/fchollet/keras/blob/master/keras/backend/theano_backend.py
    x: (batch, length)
    """
    mask_shape = mask.shape
    mask_sum = T.sum(mask, axis=1)
    output_length = mask_sum - kernel_size + 2 * padding_size + 1
    max_output_length = mask_shape[1] - kernel_size + 2 * padding_size + 1
    real_output_length = T.maximum(output_length, 1)
    range_base = T.arange(max_output_length)
    range_matrix = T.outer(T.ones((mask_shape[0],)), range_base)
    mask = (range_matrix < real_output_length[:, None]) * T.constant(1.0)
    return mask
项目:triple-gan    作者:zhenxuan00    | 项目源码 | 文件源码
def maximum_mean_discripancy(sample, data, sigma=[2. , 5., 10., 20., 40., 80.]):
    sample = sample.flatten(2)
    data = data.flatten(2)

    x = T.concatenate([sample, data], axis=0)
    xx = T.dot(x, x.T)
    x2 = T.sum(x*x, axis=1, keepdims=True)
    exponent = xx - .5*x2 - .5*x2.T
    s_samples = T.ones([sample.shape[0], 1])*1./sample.shape[0]
    s_data = -T.ones([data.shape[0], 1])*1./data.shape[0]
    s_all = T.concatenate([s_samples, s_data], axis=0)
    s_mat = T.dot(s_all, s_all.T)
    mmd_loss = 0.
    for s in sigma:
        kernel_val = T.exp((1./s) * exponent)
        mmd_loss += T.sum(s_mat*kernel_val)
    return T.sqrt(mmd_loss)
项目:neural_mt    作者:chrishokamp    | 项目源码 | 文件源码
def get_sampling_model_and_input(exp_config):
    # Create Theano variables
    encoder = BidirectionalEncoder(
        exp_config['src_vocab_size'], exp_config['enc_embed'], exp_config['enc_nhids'])

    decoder = Decoder(
        exp_config['trg_vocab_size'], exp_config['dec_embed'], exp_config['dec_nhids'],
        exp_config['enc_nhids'] * 2, loss_function='min_risk')

    # Create Theano variables
    logger.info('Creating theano variables')
    sampling_input = tensor.lmatrix('source')

    # Get beam search
    logger.info("Building sampling model")
    sampling_representation = encoder.apply(
        sampling_input, tensor.ones(sampling_input.shape))
    generated = decoder.generate(sampling_input, sampling_representation)

    # build the model that will let us get a theano function from the sampling graph
    logger.info("Creating Sampling Model...")
    sampling_model = Model(generated)

    return sampling_model, sampling_input, encoder, decoder
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_ones(self):
        for shp in [[], 1, [1], [1, 2], [1, 2, 3]]:
            ones = theano.function([], [tensor.ones(shp)], mode=self.mode)
            assert numpy.allclose(ones(), numpy.ones(shp))

        # scalar doesn't have to be provided as input
        x = scalar()
        shp = []
        ones_scalar = theano.function([], [tensor.ones(x.shape)],
                                      mode=self.mode)
        assert numpy.allclose(ones_scalar(), numpy.ones(shp))

        for (typ, shp) in [(vector, [3]), (matrix, [3, 4])]:
            x = typ()
            ones_tensor = theano.function([x], [tensor.ones(x.shape)],
                                          mode=self.mode)
            inp = numpy.zeros(shp, dtype=config.floatX)
            assert numpy.allclose(ones_tensor(inp),
                                  numpy.ones(shp))
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_reciprocal(self):
        """Matrix reciprocal by gradient descent"""
        ssd0, ssd = self.mat_reciprocal(3)

        utt.seed_rng()
        # hand-coded numpy implementation for verification
        x = rand(3, 3) + 0.1
        w = rand(3, 3)
        x = numpy.asarray(x, dtype=config.floatX)
        w = numpy.asarray(w, dtype=config.floatX)
        ones = numpy.ones((3, 3), dtype=config.floatX)

        myssd0 = numpy.sum((x * w - ones) ** 2.0)
        # we want at least a test that is not too fast. So we make one here.
        for i in xrange(100):
            gw = 2 * (x * w - ones) * x  # derivative of dMSE/dw
            myssd = numpy.sum((x * w - ones) ** 2)
            w -= 0.4 * gw
        self.assertAlmostEqual(ssd0, myssd0)
        self.assertAlmostEqual(ssd, myssd)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_only_nonseq_inputs(self):
        # Compile the Theano function
        n_steps = 2
        inp = tensor.matrix()
        broadcasted_inp, _ = theano.scan(lambda x: x,
                                         non_sequences=[inp],
                                         n_steps=n_steps)
        out = broadcasted_inp.sum()
        gr = tensor.grad(out, inp)
        fun = theano.function([inp], [broadcasted_inp, gr])

        # Execute the Theano function and compare outputs to the expected outputs
        inputs = numpy.array([[1, 2], [3, 4]], dtype=theano.config.floatX)
        expected_out1 = numpy.repeat(inputs[None], n_steps, axis=0)
        expected_out2 = numpy.ones(inputs.shape, dtype="int8") * n_steps

        out1, out2 = fun(inputs)
        utt.assert_allclose(out1, expected_out1)
        utt.assert_allclose(out2, expected_out2)

    # simple rnn, one input, one state, weights for each; input/state
    # are vectors, weights are scalars
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_infershape_nsteps_smaller_seq_length(self):
        x = tensor.vector('x')
        [o1, o2], _ = theano.scan(lambda x, y: (x + 1, y + x),
                         sequences=x,
                         outputs_info=[None, x[0]],
                         n_steps=20)

        f = theano.function([x],
                            [o1.shape[0], o2.shape[0]],
                            mode=mode_with_opt)

        vx = numpy.ones((30,), dtype=theano.config.floatX)
        o1, o2 = f(vx)
        assert o1 == 20
        assert o2 == 20
        lssc = [x for x in f.maker.fgraph.toposort()
                if isinstance(x.op, theano.scan_module.scan_op.Scan)]
        assert len(lssc) == 0
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_disconnected_gradient3(self):
        # This tests for a crash that would occur sometimes when taking the
        # gradient through a scan with a non-recurrent output which would
        # receive a disconnected gradient

        v = tensor.dvector('v')

        def step(seq):
            out1 = seq + 1
            out2 = out1 + 1
            return out1, out2

        [out1, out2], _ = theano.scan(step, sequences=v)
        gv = tensor.grad(out2.sum(), [v])
        f = theano.function([v], gv)

        # Ensure the output of the function is valid
        output = f(numpy.random.random(5))
        utt.assert_allclose(output, numpy.ones(5))
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_compute_test_value():
    # Verify that test values can be used with scan.
    backup = theano.config.compute_test_value
    theano.config.compute_test_value = 'raise'
    try:
        x = tensor.vector('x')
        xv = numpy.ones(3, dtype=theano.config.floatX)
        x.tag.test_value = xv
        y = theano.shared(numpy.arange(3, dtype=theano.config.floatX),
                          name='y')
        z, updates = theano.scan(
                fn=lambda u, v: u + v,
                sequences=[x, y])
        assert not updates
        z.name = 'z'
        # The gradient computation used to crash before 6af465e.
        g = tensor.grad(z.sum(), x)
        #f = theano.function([x], g)
        # print f(xv)
    finally:
        theano.config.compute_test_value = backup
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_compute_test_value_nonseq():
    # Verify that test values can be used for non_sequences with scan.
    backup = theano.config.compute_test_value
    theano.config.compute_test_value = 'raise'
    try:
        x = tensor.vector('x')
        xv = numpy.ones(3, dtype=theano.config.floatX)
        x.tag.test_value = xv
        y = theano.shared(
                numpy.arange(9, dtype=theano.config.floatX).reshape(3, 3),
                name='y')
        z, updates = theano.scan(
                fn=lambda u, v: u + v,
                sequences=[x],
                non_sequences=[y])
        assert not updates
        z.name = 'z'
        # The gradient computation used to crash before 6af465e.
        g = tensor.grad(z.sum(), x)
        #f = theano.function([x], g)
        # print f(xv)
    finally:
        theano.config.compute_test_value = backup
项目:crnn_tf    作者:liuhu-bigeye    | 项目源码 | 文件源码
def generate_data(T, nb, length_max, voca_size):
    # generate preds(nb, T, voca_size), tokens(no 0, -1 for null), lengths(length for preds)

    # preds = softmax_np(np.random.random((T, nb, voca_size+1)))            # (T, nb, voca_size + 1)
    # e_preds = softmax_np(np.random.random((T, nb, voca_size+1)))          # (T, nb, voca_size + 1)
    #
    # length = np.random.randint(2, length_max+1, size=(nb))                # (nb)
    # tokens = np.array([np.concatenate([np.random.randint(voca_size, size=l), -np.ones(length_max-l)]) for l in length])
    #
    # return e_preds.astype(floatX), preds.astype(floatX), tokens.astype(intX), length.astype(intX)

    e_preds = np.array([[0.1,0.1,0.8],[0.2,0.1,0.7],[0.3,0.4,0.3],[0.5,0.2,0.3]], dtype=floatX)
    preds = np.array([[0.2,0.5,0.3],[0.7,0.2,0.1],[0.1,0.3,0.6],[0.2,0.3,0.5]], dtype=floatX)
    tokens = np.array([1,2], dtype=intX)
    length = np.array(4, dtype=intX)
    return e_preds[:, None, :], preds[:, None, :], tokens[None, :], length[None]
项目:statestream    作者:VolkerFischer    | 项目源码 | 文件源码
def _meshgrid(height, width):
    # This should be equivalent to:
    #  x_t, y_t = np.meshgrid(np.linspace(-1, 1, width),
    #                         np.linspace(-1, 1, height))
    #  ones = np.ones(np.prod(x_t.shape))
    #  grid = np.vstack([x_t.flatten(), y_t.flatten(), ones])
    x_t = T.dot(T.ones((height, 1)),
                _linspace(-1.0, 1.0, width).dimshuffle('x', 0))
    y_t = T.dot(_linspace(-1.0, 1.0, height).dimshuffle(0, 'x'),
                T.ones((1, width)))

    x_t_flat = x_t.reshape((1, -1))
    y_t_flat = y_t.reshape((1, -1))
    ones = T.ones_like(x_t_flat)
    grid = T.concatenate([x_t_flat, y_t_flat, ones], axis=0)
    return grid
项目:NMT-Coverage    作者:tuzhaopeng    | 项目源码 | 文件源码
def create_probs_computer(self, return_alignment=False):
        if not hasattr(self, 'probs_fn'):
            logger.debug("Compile probs computer")
            self.probs_fn = theano.function(
                    inputs=self.inputs,
                    outputs=[self.predictions.word_probs, self.alignment],
                    name="probs_fn")
        def probs_computer(x, y):
            x_mask = numpy.ones(x.shape[0], dtype="float32")
            y_mask = numpy.ones(y.shape[0], dtype="float32")
            probs, alignment = self.probs_fn(x[:, None], y[:, None],
                    x_mask[:, None], y_mask[:, None])
            if return_alignment:
                return probs, alignment
            else:
                return probs
        return probs_computer
项目:VNMT    作者:DeepLearnXMU    | 项目源码 | 文件源码
def create_probs_computer(self, return_alignment=False):
        if not hasattr(self, 'probs_fn'):
            logger.debug("Compile probs computer")
            self.probs_fn = theano.function(
                    inputs=self.inputs,
                    outputs=[self.predictions.word_probs, self.alignment],
                    name="probs_fn")
        def probs_computer(x, y):
            x_mask = numpy.ones(x.shape[0], dtype="float32")
            y_mask = numpy.ones(y.shape[0], dtype="float32")
            probs, alignment = self.probs_fn(x[:, None], y[:, None],
                    x_mask[:, None], y_mask[:, None])
            if return_alignment:
                return probs, alignment
            else:
                return probs
        return probs_computer
项目:crikey    作者:kastnerkyle    | 项目源码 | 文件源码
def sample_binomial(coeff, n_bins, theano_rng, debug=False):
    # ? Normal approximation?
    if coeff.ndim > 2:
        raise ValueError("Unsupported dim")
    if debug:
        idx = coeff * n_bins
    else:
        shp = coeff.shape
        inc = tensor.ones((shp[0], shp[1], n_bins))
        expanded_coeff = coeff.dimshuffle(0, 1, 'x')
        expanded_coeff = expanded_coeff * inc
        # n > 1 not supported?
        # idx = theano_rng.binomial(n=n_bins, p=coeff, dtype=coeff.dtype)
        idx = theano_rng.binomial(n=1, p=expanded_coeff, dtype=coeff.dtype,
                                  size=expanded_coeff.shape)
        idx = idx.sum(axis=-1)
    return tensor.cast(idx, theano.config.floatX)
项目:crikey    作者:kastnerkyle    | 项目源码 | 文件源码
def sample_binomial(coeff, n_bins, theano_rng, debug=False):
    # ? Normal approximation?
    if coeff.ndim > 2:
        raise ValueError("Unsupported dim")
    if debug:
        idx = coeff * n_bins
    else:
        shp = coeff.shape
        inc = tensor.ones((shp[0], shp[1], n_bins))
        expanded_coeff = coeff.dimshuffle(0, 1, 'x')
        expanded_coeff = expanded_coeff * inc
        # n > 1 not supported?
        # idx = theano_rng.binomial(n=n_bins, p=coeff, dtype=coeff.dtype)
        idx = theano_rng.binomial(n=1, p=expanded_coeff, dtype=coeff.dtype,
                                  size=expanded_coeff.shape)
        idx = idx.sum(axis=-1)
    return tensor.cast(idx, theano.config.floatX)
项目:crikey    作者:kastnerkyle    | 项目源码 | 文件源码
def sample_binomial(coeff, n_bins, theano_rng, debug=False):
    # ? Normal approximation?
    if coeff.ndim > 2:
        raise ValueError("Unsupported dim")
    if debug:
        idx = coeff * n_bins
    else:
        shp = coeff.shape
        inc = tensor.ones((shp[0], shp[1], n_bins))
        expanded_coeff = coeff.dimshuffle(0, 1, 'x')
        expanded_coeff = expanded_coeff * inc
        # n > 1 not supported?
        # idx = theano_rng.binomial(n=n_bins, p=coeff, dtype=coeff.dtype)
        idx = theano_rng.binomial(n=1, p=expanded_coeff, dtype=coeff.dtype,
                                  size=expanded_coeff.shape)
        idx = idx.sum(axis=-1)
    return tensor.cast(idx, theano.config.floatX)
项目:crikey    作者:kastnerkyle    | 项目源码 | 文件源码
def sample_binomial(coeff, n_bins, theano_rng, debug=False):
    # ? Normal approximation?
    if coeff.ndim > 2:
        raise ValueError("Unsupported dim")
    if debug:
        idx = coeff * n_bins
    else:
        shp = coeff.shape
        inc = tensor.ones((shp[0], shp[1], n_bins))
        expanded_coeff = coeff.dimshuffle(0, 1, 'x')
        expanded_coeff = expanded_coeff * inc
        # n > 1 not supported?
        # idx = theano_rng.binomial(n=n_bins, p=coeff, dtype=coeff.dtype)
        idx = theano_rng.binomial(n=1, p=expanded_coeff, dtype=coeff.dtype,
                                  size=expanded_coeff.shape)
        idx = idx.sum(axis=-1)
    return tensor.cast(idx, theano.config.floatX)
项目:crikey    作者:kastnerkyle    | 项目源码 | 文件源码
def sample_binomial(coeff, n_bins, theano_rng, debug=False):
    # ? Normal approximation?
    if coeff.ndim > 2:
        raise ValueError("Unsupported dim")
    if debug:
        idx = coeff * n_bins
    else:
        shp = coeff.shape
        inc = tensor.ones((shp[0], shp[1], n_bins))
        expanded_coeff = coeff.dimshuffle(0, 1, 'x')
        expanded_coeff = expanded_coeff * inc
        # n > 1 not supported?
        # idx = theano_rng.binomial(n=n_bins, p=coeff, dtype=coeff.dtype)
        idx = theano_rng.binomial(n=1, p=expanded_coeff, dtype=coeff.dtype,
                                  size=expanded_coeff.shape)
        idx = idx.sum(axis=-1)
    return tensor.cast(idx, theano.config.floatX)
项目:crikey    作者:kastnerkyle    | 项目源码 | 文件源码
def sample_binomial(coeff, n_bins, theano_rng, debug=False):
    # ? Normal approximation?
    if coeff.ndim > 2:
        raise ValueError("Unsupported dim")
    if debug:
        idx = coeff * n_bins
    else:
        shp = coeff.shape
        inc = tensor.ones((shp[0], shp[1], n_bins))
        expanded_coeff = coeff.dimshuffle(0, 1, 'x')
        expanded_coeff = expanded_coeff * inc
        # n > 1 not supported?
        # idx = theano_rng.binomial(n=n_bins, p=coeff, dtype=coeff.dtype)
        idx = theano_rng.binomial(n=1, p=expanded_coeff, dtype=coeff.dtype,
                                  size=expanded_coeff.shape)
        idx = idx.sum(axis=-1)
    return tensor.cast(idx, theano.config.floatX)
项目:crikey    作者:kastnerkyle    | 项目源码 | 文件源码
def sample_binomial(coeff, n_bins, theano_rng, debug=False):
    # ? Normal approximation?
    if coeff.ndim > 2:
        raise ValueError("Unsupported dim")
    if debug:
        idx = coeff * n_bins
    else:
        shp = coeff.shape
        inc = tensor.ones((shp[0], shp[1], n_bins))
        expanded_coeff = coeff.dimshuffle(0, 1, 'x')
        expanded_coeff = expanded_coeff * inc
        # n > 1 not supported?
        # idx = theano_rng.binomial(n=n_bins, p=coeff, dtype=coeff.dtype)
        idx = theano_rng.binomial(n=1, p=expanded_coeff, dtype=coeff.dtype,
                                  size=expanded_coeff.shape)
        idx = idx.sum(axis=-1)
    return tensor.cast(idx, theano.config.floatX)
项目:Precise-CTC    作者:Michlong    | 项目源码 | 文件源码
def _pad_blanks(queryseq, blank_symbol, queryseq_mask=None):
        """
        Pad queryseq and corresponding queryseq_mask with blank symbol
        :param queryseq  (L, B)
        :param queryseq_mask (L, B)
        :param blank_symbol  scalar
        :return queryseq_padded, queryseq_mask_padded, both with shape (2L+1, B)
        """
        # for queryseq
        queryseq_extended = queryseq.dimshuffle(1, 0, 'x')                              # (L, B) -> (B, L, 1)
        blanks = tensor.zeros_like(queryseq_extended) + blank_symbol                    # (B, L, 1)
        concat = tensor.concatenate([queryseq_extended, blanks], axis=2)                # concat.shape = (B, L, 2)
        res = concat.reshape((concat.shape[0], concat.shape[1] * concat.shape[2])).T    # res.shape = (2L, B), the reshape will cause the last 2 dimensions interlace
        begining_blanks = tensor.zeros((1, res.shape[1])) + blank_symbol                # (1, B)
        queryseq_padded = tensor.concatenate([begining_blanks, res], axis=0)            # (1+2L, B)
        # for queryseq_mask
        if queryseq_mask is not None:
            queryseq_mask_extended = queryseq_mask.dimshuffle(1, 0, 'x')                          # (L, B) -> (B, L, 1)
            concat = tensor.concatenate([queryseq_mask_extended, queryseq_mask_extended], axis=2) # concat.shape = (B, L, 2)
            res = concat.reshape((concat.shape[0], concat.shape[1] * concat.shape[2])).T
            begining_blanks = tensor.ones((1, res.shape[1]), dtype=floatX)
            queryseq_mask_padded = tensor.concatenate([begining_blanks, res], axis=0)
        else:
            queryseq_mask_padded = None
        return queryseq_padded, queryseq_mask_padded
项目:dsb3    作者:EliasVansteenkiste    | 项目源码 | 文件源码
def get_output_for(self, input, **kwargs):
        shape_ones = [1] * input.ndim
        shape_ones.insert(self.axis, self.repeats)
        ones = T.ones(tuple(shape_ones), dtype=input.dtype)

        pattern = range(input.ndim)
        pattern.insert(self.axis, "x")
        # print shape_ones, pattern
        return ones * input.dimshuffle(*pattern)
项目:crfrnn_layer    作者:HapeMask    | 项目源码 | 文件源码
def get_output_for(self, inputs, **kwargs):
        unary, ref = inputs

        N, _, H, W = ref.shape
        yx = tt.cast(tt.stack(tt.mgrid[0:H, 0:W]), "float32")
        grid = tt.alloc(yx[np.newaxis, :, :, :], N, 2, H, W)
        stacked = tt.concatenate([grid, ref], axis=1)

        def _bilateral(V, R):
            o = tt.ones((1, V.shape[1], V.shape[2]), "float32")
            norm = tt.sqrt(gaussian_filter(R, o, self.kstd_bf,
                                           self.ref_dim)) + 1e-8
            return gaussian_filter(R, V/norm, self.kstd_bf, self.ref_dim,
                                   self.val_dim) / norm

        def _step(prev_q, U, ref, normalize=True):
            qbf = _bilateral(prev_q, ref,)
            qsf = tt.nnet.conv2d(prev_q[np.newaxis, :, :, :],
                                 self.W_spatial, border_mode="half")[0]

            q_hat = -self.compat_bf * qbf + -self.compat_spatial * qsf
            q_hat = U - q_hat

            return softmax(q_hat, axis=0) if normalize else q_hat

        def _inference(unary_i, ref_i):
            U = tt.log(tt.clip(unary_i, 1e-5, 1))
            prev_q = softmax(U, axis=0)

            # This is faster than using scan.
            for i in range(self.num_iter):
                normalize = self.normalize_final_iter or i < self.num_iter-1
                prev_q = _step(prev_q, U, ref_i, normalize)
            return prev_q

        return theano.scan(fn=_inference, sequences=[unary, stacked],
                           outputs_info=None)[0]
项目:cortex    作者:rdevon    | 项目源码 | 文件源码
def __call__(self, x, m=None, h0s=None, condition_on=None):
        '''Call function.

        For learning RNNs.

        Args:
            x (T.tensor): input sequence. window x batch x dim
            m (T.tensor): mask. window x batch. For masking in recurrent steps.
            h0s (Optional[list]): initial h0s.
            condition_on (Optional[T.tensor]): conditional for recurrent step.

        Returns:
            OrderedDict: dictionary of results: hiddens, probabilities, and
                preacts.
            theano.OrderedUpdates.

        '''
        if h0s is None:
            h0s = [T.alloc(0., x.shape[1], dim_h).astype(floatX) for dim_h in self.dim_hs]

        if m is None:
            m = T.ones((x.shape[0], x.shape[1])).astype(floatX)

        params = self.get_sample_params()

        return self.step_call(x, m, h0s, *params)
项目:SteinGAN    作者:DartML    | 项目源码 | 文件源码
def conv_cond_concat(x, y):
    """ 
    concatenate conditioning vector on feature map axis 
    """
    return T.concatenate([x, y*T.ones((x.shape[0], y.shape[1], x.shape[2], x.shape[3]))], axis=1)
项目:iGAN    作者:junyanz    | 项目源码 | 文件源码
def conv_cond_concat(x, y):
    """ 
    concatenate conditioning vector on feature map axis 
    """
    return T.concatenate([x, y*T.ones((x.shape[0], y.shape[1], x.shape[2], x.shape[3]))], axis=1)
项目:NMT    作者:tuzhaopeng    | 项目源码 | 文件源码
def build_sampler(self, n_samples, n_steps, T, c):
        states = [TT.zeros(shape=(n_samples,), dtype='int64'),
                TT.zeros(shape=(n_samples,), dtype='float32')]
        init_c = c[0, -self.state['dim']:]
        states += [ReplicateLayer(n_samples)(init(init_c).out).out for init in self.initializers]
        # added by Zhaopeng Tu, 2015-10-30
        # init_coverage
        if self.state['maintain_coverage']:
            # in sampling, init_c is two-dimension (source_length*c_dim), same for init_coverage
            if self.state['use_accumulated_coverage'] and self.state['coverage_accumulated_operation'] == 'subtractive':
                init_coverage = TT.unbroadcast(TT.ones((c.shape[0], self.state['coverage_dim']), dtype='float32'), 1)
            else:
                init_coverage = TT.unbroadcast(TT.zeros((c.shape[0], self.state['coverage_dim']), dtype='float32'), 1)
            states.append(init_coverage)

        if not self.state['search']:
            c = PadLayer(n_steps)(c).out

        # Pad with final states
        non_sequences = [c, T]

        outputs, updates = theano.scan(self.sampling_step,
                outputs_info=states,
                non_sequences=non_sequences,
                sequences=[TT.arange(n_steps, dtype="int64")],
                n_steps=n_steps,
                name="{}_sampler_scan".format(self.prefix))
        if self.state['maintain_coverage']:
            return (outputs[0], outputs[1], outputs[-1]), updates
        else:
            return (outputs[0], outputs[1]), updates
项目:NMT    作者:tuzhaopeng    | 项目源码 | 文件源码
def create_scorer(self, batch=False):
        if not hasattr(self, 'score_fn'):
            logger.debug("Compile scorer")
            self.score_fn = theano.function(
                    inputs=self.inputs,
                    outputs=[-self.predictions.cost_per_sample],
                    name="score_fn")
        if batch:
            return self.score_fn
        def scorer(x, y):
            x_mask = numpy.ones(x.shape[0], dtype="float32")
            y_mask = numpy.ones(y.shape[0], dtype="float32")
            return self.score_fn(x[:, None], y[:, None],
                    x_mask[:, None], y_mask[:, None])
        return scorer
项目:NMT    作者:tuzhaopeng    | 项目源码 | 文件源码
def build_sampler(self, n_samples, n_steps, T, c):
        states = [TT.zeros(shape=(n_samples,), dtype='int64'),
                TT.zeros(shape=(n_samples,), dtype='float32')]

        init_c = c[0, -self.state['dim']:]
        states += [ReplicateLayer(n_samples)(init(init_c).out).out for init in self.initializers]
        # added by Zhaopeng Tu, 2015-10-30
        # init_coverage
        if self.state['maintain_coverage']:
            # in sampling, init_c is two-dimension (source_length*c_dim), same for init_coverage
            # modified by Zhaopeng Tu, 2015-12-18, big mistake here!!!
            # coverage should be always 3D, the first two dimensions are consistent with alignment probs
            # while the last one is the coverage dim
            if self.state['use_linguistic_coverage'] and self.state['coverage_accumulated_operation'] == 'subtractive':
                init_coverage = TT.unbroadcast(TT.ones((c.shape[0], n_samples, self.state['coverage_dim']), dtype='float32'), 2)
            else:
                init_coverage = TT.unbroadcast(TT.zeros((c.shape[0], n_samples, self.state['coverage_dim']), dtype='float32'), 2)
            states.append(init_coverage)

        if not self.state['search']:
            c = PadLayer(n_steps)(c).out

        # Pad with final states
        non_sequences = [c, T]
        if self.state['maintain_coverage'] and self.state['use_linguistic_coverage'] and self.state['use_fertility_model']:
            fertility = self.state['max_fertility'] * self.fertility_inputer(c).out
            non_sequences.append(fertility)

        outputs, updates = theano.scan(self.sampling_step,
                outputs_info=states,
                non_sequences=non_sequences,
                sequences=[TT.arange(n_steps, dtype="int64")],
                n_steps=n_steps,
                name="{}_sampler_scan".format(self.prefix))
        if self.state['maintain_coverage']:
            if self.state['use_fertility_model'] and self.state['use_linguistic_coverage']:
                return (outputs[0], outputs[1], outputs[-1], fertility), updates
            else:
                return (outputs[0], outputs[1], outputs[-1]), updates
        else:
            return (outputs[0], outputs[1]), updates
项目:NMT    作者:tuzhaopeng    | 项目源码 | 文件源码
def create_scorer(self, batch=False):
        if not hasattr(self, 'score_fn'):
            logger.debug("Compile scorer")
            self.score_fn = theano.function(
                    inputs=self.inputs,
                    outputs=[-self.predictions.cost_per_sample],
                    name="score_fn")
        if batch:
            return self.score_fn
        def scorer(x, y):
            x_mask = numpy.ones(x.shape[0], dtype="float32")
            y_mask = numpy.ones(y.shape[0], dtype="float32")
            return self.score_fn(x[:, None], y[:, None],
                    x_mask[:, None], y_mask[:, None])
        return scorer
项目:icml17_knn    作者:taolei87    | 项目源码 | 文件源码
def forward_all(self, x, hc0=None, return_c=False):
        assert x.ndim == 3 # size (len, batch, d)
        if hc0 is None:
            c0 = h0 = T.zeros((x.shape[1], self.n_out), dtype=theano.config.floatX)
        else:
            assert len(hc0) == 2
            c0, h0 = hc0

        if self.dropout is None:
            mask_h = T.ones((x.shape[1], self.n_out), dtype=theano.config.floatX)
        else:
            mask_h = get_dropout_mask((x.shape[1], self.n_out), self.dropout)
            mask_x = get_dropout_mask((x.shape[1], x.shape[2]), self.dropout)
            mask_x = mask_x.dimshuffle(('x',0,1))
            x = x*mask_x

        c, h = theano.scan(
                fn = self.forward,
                sequences = x,
                outputs_info = [ c0, h0 ],
                non_sequences = mask_h
            )[0]
        if return_c:
            return [c, h]
        else:
            return h
项目:icml17_knn    作者:taolei87    | 项目源码 | 文件源码
def forward_all(self, x, hc0=None, return_c=False):
        assert x.ndim == 3 # size (len, batch, d)
        if hc0 is None:
            c0 = h0 = T.zeros((x.shape[1], self.n_out), dtype=theano.config.floatX)
        else:
            assert len(hc0) == 2
            c0, h0 = hc0

        if self.dropout is None:
            mask_h = T.ones((x.shape[1], self.n_out), dtype=theano.config.floatX)
        else:
            mask_h = get_dropout_mask((x.shape[1], self.n_out), self.dropout)
            mask_x = get_dropout_mask((x.shape[1], x.shape[2]), self.dropout)
            mask_x = mask_x.dimshuffle(('x',0,1))
            x = x*mask_x

        c, h = theano.scan(
                fn = self.forward,
                sequences = x,
                outputs_info = [ c0, h0 ],
                non_sequences = mask_h
            )[0]
        if return_c:
            return [c, h]
        else:
            return h
项目:icml17_knn    作者:taolei87    | 项目源码 | 文件源码
def forward_all(self, x, hc0=None, return_c=False):
        assert x.ndim == 3 # size (len, batch, d)
        if hc0 is None:
            ci0 = cii0 = h0 = T.zeros((x.shape[1], self.n_out), dtype=theano.config.floatX)
        else:
            assert len(hc0) == 3
            ci0, cii0, h0 = hc0

        if self.dropout is None:
            mask_h = T.ones((x.shape[1], self.n_out), dtype=theano.config.floatX)
        else:
            mask_h = get_dropout_mask((x.shape[1], self.n_out), self.dropout)
            mask_x = get_dropout_mask((x.shape[1], x.shape[2]), self.dropout)
            mask_x = mask_x.dimshuffle(('x',0,1))
            x = x*mask_x

        ci, cii, h = theano.scan(
                fn = self.forward,
                sequences = x,
                outputs_info = [ ci0, cii0, h0 ],
                non_sequences = mask_h
            )[0]
        if return_c:
            return [ci, cii, h]
        else:
            return h
项目:reseg    作者:fvisin    | 项目源码 | 文件源码
def get_equivalent_input_padding(layer, layers_args=[]):
    """Compute the equivalent padding in the input layer

    A function to compute the equivalent padding of a sequence of
    convolutional and pooling layers. It memorizes the padding
    of all the Layers up to the first InputLayer.
    It then computes what would be the equivalent padding in the Layer
    immediately before the chain of Layers that is being taken into account.
    """
    # Initialize the DynamicPadding layers
    lasagne.layers.get_output(layer)
    # Loop through conv and pool to collect data
    all_layers = get_all_layers(layer)
    # while(not isinstance(layer, (InputLayer))):
    for layer in all_layers:
        # Note: stride is numerical, but pad *could* be symbolic
        try:
            pad, stride = (layer.pad, layer.stride)
            if isinstance(pad, int):
                pad = pad, pad
            if isinstance(stride, int):
                stride = stride, stride
            layers_args.append((pad, stride))
        except(AttributeError):
            pass

    # Loop backward to compute the equivalent padding in the input
    # layer
    tot_pad = T.zeros(2)
    pad_factor = T.ones(2)
    while(layers_args):
        pad, stride = layers_args.pop()
        tot_pad += pad * pad_factor
        pad_factor *= stride

    return tot_pad
项目:Deep-Learning-with-Theano    作者:PacktPublishing    | 项目源码 | 文件源码
def conv_cond_concat(x, y):
    return T.concatenate([x, y*T.ones((x.shape[0], y.shape[1], x.shape[2], x.shape[3]))], axis=1)