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

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

项目:reinforcement-learning-policy-gradients    作者:DarkElement75    | 项目源码 | 文件源码
def get_action(observation):

    #test_x, test_y = test_data

    i = T.lscalar() # mini-batch index

    self.test_mb_predictions = theano.function(
        [i], self.layers[-1].y_out,
        givens={
            self.x: observation
        }, on_unused_input='warn')

    return self.test_mb_predictions(0)



#Initialize network
项目:reinforcement-learning-policy-gradients    作者:DarkElement75    | 项目源码 | 文件源码
def get_action(observation):

    #test_x, test_y = test_data

    i = T.lscalar() # mini-batch index

    self.test_mb_predictions = theano.function(
        [i], self.layers[-1].y_out,
        givens={
            self.x: observation
        }, on_unused_input='warn')

    return self.test_mb_predictions(0)



#Initialize network
项目:ObjRecPoseEst    作者:paroj    | 项目源码 | 文件源码
def setupVariables(self):
        floatX = theano.config.floatX  # @UndefinedVariable


        # params
        self.learning_rate = T.scalar('learning_rate',dtype=floatX) 
        self.momentum = T.scalar('momentum',dtype=floatX)

        # input
        self.tvIndex = T.lscalar()  # index to a [mini]batch
        #self.tvIndex.tag.test_value = 10
        self.tvX = self.descrNet.inputVar

        # targets
        self.tvY = T.ivector('y')
        self.tvYr = T.tensor4('yr')
        self.tvPairIdx = T.imatrix('pairIdx')
        self.tvPairLabels = T.ivector('pairLabels')
        self.tvTripletIdx = T.imatrix('tripletIdx')
        self.tvTripletThresh = T.scalar('tripletThresh')
        self.tvTripletPoolIdx = T.imatrix('tripletPoolIdx')
        self.tvTripletPoolThresh = T.scalar('tripletPoolThresh')
        self.tvPosTripletPoolSize = T.iscalar('posTripletPoolSize')
        self.tvNegTripletPoolSize = T.iscalar('negTripletPoolSize')
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_op(self):
        n = tensor.lscalar()
        f = theano.function([self.p, n], multinomial(n, self.p))

        _n = 5
        tested = f(self._p, _n)
        assert tested.shape == self._p.shape
        assert numpy.allclose(numpy.floor(tested.todense()), tested.todense())
        assert tested[2, 1] == _n

        n = tensor.lvector()
        f = theano.function([self.p, n], multinomial(n, self.p))

        _n = numpy.asarray([1, 2, 3, 4], dtype='int64')
        tested = f(self._p, _n)
        assert tested.shape == self._p.shape
        assert numpy.allclose(numpy.floor(tested.todense()), tested.todense())
        assert tested[2, 1] == _n[2]
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_mixed_shape(self):
        # Test when the provided shape is a tuple of ints and scalar vars
        rng_R = random_state_type()
        shape0 = tensor.lscalar()
        shape = (shape0, 3)
        post_r, u = uniform(rng_R, size=shape, ndim=2)
        f = compile.function([rng_R, shape0], u)
        rng_state0 = numpy.random.RandomState(utt.fetch_seed())

        assert f(rng_state0, 2).shape == (2, 3)
        assert f(rng_state0, 8).shape == (8, 3)

        post_r, v = uniform(rng_R, size=shape)
        g = compile.function([rng_R, shape0], v)
        assert g(rng_state0, 2).shape == (2, 3)
        assert g(rng_state0, 8).shape == (8, 3)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_mixed_shape_bcastable(self):
        # Test when the provided shape is a tuple of ints and scalar vars
        rng_R = random_state_type()
        shape0 = tensor.lscalar()
        shape = (shape0, 1)
        post_r, u = uniform(rng_R, size=shape, ndim=2)
        assert u.broadcastable == (False, True)
        f = compile.function([rng_R, shape0], u)
        rng_state0 = numpy.random.RandomState(utt.fetch_seed())

        assert f(rng_state0, 2).shape == (2, 1)
        assert f(rng_state0, 8).shape == (8, 1)

        post_r, v = uniform(rng_R, size=shape)
        assert v.broadcastable == (False, True)
        g = compile.function([rng_R, shape0], v)
        assert g(rng_state0, 2).shape == (2, 1)
        assert g(rng_state0, 8).shape == (8, 1)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_dtype(self):
        rng_R = random_state_type()
        low = tensor.lscalar()
        high = tensor.lscalar()
        post_r, out = random_integers(rng_R, low=low, high=high, size=(20, ),
                                      dtype='int8')
        assert out.dtype == 'int8'
        f = compile.function([rng_R, low, high], [post_r, out])

        rng = numpy.random.RandomState(utt.fetch_seed())
        rng0, val0 = f(rng, 0, 9)
        assert val0.dtype == 'int8'

        rng1, val1 = f(rng0, 255, 257)
        assert val1.dtype == 'int8'
        assert numpy.all(abs(val1) <= 1)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_mixed_shape_bcastable(self):
        # Test when the provided shape is a tuple of ints and scalar vars
        random = RandomStreams(utt.fetch_seed())
        shape0 = tensor.lscalar()
        shape = (shape0, 1)
        u = random.uniform(size=shape, ndim=2)
        assert u.broadcastable == (False, True)
        f = function([shape0], u)
        assert f(2).shape == (2, 1)
        assert f(8).shape == (8, 1)

        v = random.uniform(size=shape)
        assert v.broadcastable == (False, True)
        g = function([shape0], v)
        assert g(2).shape == (2, 1)
        assert g(8).shape == (8, 1)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_join_matrixV_negative_axis(self):
        """variable join negative axis"""
        v = numpy.array([[.1, .2, .3], [.4, .5, .6]], dtype=self.floatX)
        a = self.shared(v)
        b = as_tensor_variable(v)
        ax = lscalar()
        s = join(ax, a, b)

        f = inplace_func([ax], [s], mode=self.mode)
        topo = f.maker.fgraph.toposort()
        assert [True for node in topo
                if isinstance(node.op, type(self.join_op))]

        want = numpy.array([[.1, .2, .3, .1, .2, .3],
                            [.4, .5, .6, .4, .5, .6]])

        got = f(-1)
        assert numpy.allclose(got, want)

        want = numpy.array([[.1, .2, .3], [.4, .5, .6],
                            [.1, .2, .3], [.4, .5, .6]])
        got = f(-2)
        assert numpy.allclose(got, want)

        self.assertRaises(IndexError, f, -3)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_impls(self):
        i = iscalar()
        ii = lscalar()
        d = dscalar()
        f = fscalar()
        c = cscalar()

        assert numpy.allclose(function([i, d], i / d)(5, 7.0), (5.0 / 7.0))
        assert numpy.allclose(function([i, d], d / i)(5, 7.0), (7.0 / 5.0))
        assert numpy.allclose(function([i, f], i / f)(5, 11.0), (5.0 / 11.0))
        assert numpy.allclose(function([i, f], f / i)(5, 11.0), (11.0 / 5.0))
        assert numpy.allclose(function([i, ii], i // ii)(5, 3), (5 // 3))
        assert numpy.allclose(function([i, ii], ii // i)(5, 3), (3 // 5))
        assert numpy.allclose(function([i, ii], true_div(i, ii))(5, 3),
                (5. / 3.))
        assert numpy.allclose(function([i, ii], true_div(ii, i))(5, 3),
                (3. / 5.))
        assert numpy.allclose(function([i, c], i / c)(5, numpy.complex(5, 3)),
                (5. / (5 + 3j)))
        assert numpy.allclose(function([i, c], c / i)(5, numpy.complex(5, 3)),
                ((5 + 3j) / 5.))
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test0(self):
        tt = constant(56)  # scal.constant(56)
        ss = scalar_from_tensor(tt)
        self.assertTrue(ss.owner.op is scalar_from_tensor)
        self.assertTrue(ss.type.dtype == tt.type.dtype)

        v = eval_outputs([ss])

        self.assertTrue(v == 56, v)
        if config.cast_policy == 'custom':
            self.assertTrue(isinstance(v, numpy.int16))
        elif config.cast_policy in ('numpy', 'numpy+floatX'):
            self.assertTrue(isinstance(
                v, getattr(numpy, str(numpy.asarray(56).dtype))))
        else:
            raise NotImplementedError(config.cast_policy)
        self.assertTrue(v.shape == (), v.shape)
        tt = lscalar()
        ss = scalar_from_tensor(tt)
        g = ss.owner.op.grad([tt], [ss])
        fff = function([tt], ss)
        v = fff(numpy.asarray(5))
        self.assertTrue(v == 5, v)
        self.assertTrue(isinstance(v, numpy.int64))
        self.assertTrue(v.shape == (), v.shape)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_doc(self):
        """Ensure the code given in pfunc.txt works as expected"""

        # Example #1.
        a = lscalar()
        b = shared(1)
        f1 = pfunc([a], (a + b))
        f2 = pfunc([In(a, value=44)], a + b, updates={b: b + 1})
        self.assertTrue(b.get_value() == 1)
        self.assertTrue(f1(3) == 4)
        self.assertTrue(f2(3) == 4)
        self.assertTrue(b.get_value() == 2)
        self.assertTrue(f1(3) == 5)
        b.set_value(0)
        self.assertTrue(f1(3) == 3)

        # Example #2.
        a = tensor.lscalar()
        b = shared(7)
        f1 = pfunc([a], a + b)
        f2 = pfunc([a], a * b)
        self.assertTrue(f1(5) == 12)
        b.set_value(8)
        self.assertTrue(f1(5) == 13)
        self.assertTrue(f2(4) == 32)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_default_updates_expressions(self):
        x = shared(0)
        y = shared(1)
        a = lscalar('a')

        z = a * x
        x.default_update = x + y

        f1 = pfunc([a], z)
        f1(12)
        assert x.get_value() == 1

        f2 = pfunc([a], z, no_default_updates=True)
        assert f2(7) == 7
        assert x.get_value() == 1

        f3 = pfunc([a], z, no_default_updates=[x])
        assert f3(9) == 9
        assert x.get_value() == 1
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_partial_function_with_updates():

    def check_updates(linker_name):
        x = tensor.lscalar('input')
        y = theano.shared(numpy.asarray(1, 'int64'), name='global')
        f = theano.function([x], [x, x + 34], updates=[(y, x + 1)], mode=Mode(
            optimizer=None, linker=linker_name))
        g = theano.function([x], [x - 6], updates=[(y, y + 3)], mode=Mode(
            optimizer=None, linker=linker_name))

        assert f(3, output_subset=[]) == []
        assert y.get_value() == 4
        assert g(30, output_subset=[0]) == [24]
        assert g(40, output_subset=[]) == []
        assert y.get_value() == 10

    check_updates(vm.VM_Linker(allow_partial_eval=True, use_cloop=False))
    check_updates('cvm')
项目:keras-textgen    作者:kenoma    | 项目源码 | 文件源码
def sample(self):
        def recurrent_gn(hs, idx):
            h_t, ps = self.recurrent_fn(idx, hs)

            y_i = T.argmax(ps) #using a greedy strategy

            return h_t, y_i

        h_0 = theano.shared(value=np.zeros((1, self.sdim),dtype='float32'), name='h0')

        sod = T.lscalar('sod')
        n = T.lscalar('n')
        [h, y_idx], _ = theano.scan(recurrent_gn,
                                               outputs_info = [h_0, sod],
                                               n_steps = n)

        sample_model = theano.function(inputs=[sod, n], outputs=[y_idx], on_unused_input='ignore')

        return sample_model
项目:structured-output-ae    作者:sbelharbi    | 项目源码 | 文件源码
def fit(self,
            data=None,
            learning_rate=0.1,
            batch_size=100,
            n_epochs=20,
            lr_scalar=0.998,
            weights_file="out/ae_weights_mnist.npy"):
        """
        Fit the data to the autoencoder (training).
        """
        if data is None:
            raise Exception("Data can't be empty.")

        index = T.lscalar("index")
        data_shared = theano.shared(
            np.asarray(data, dtype=theano.config.floatX))
        n_batches = data.shape[0] / batch_size
        (cost, updates) = self.get_sgd_updates(
            learning_rate, lr_scalar, batch_size)
        train_ae = theano.function(
            [index], cost, updates=updates,
            givens={
                self.x: data_shared[index*batch_size: (index+1)*batch_size]})

        print "Start training the ae."
        ae_costs = []

        for epoch in xrange(n_epochs):
            print "Training at epoch %d" % epoch
            cost_one_epoch = []
            for batch_index in xrange(n_batches):
                cost_one_epoch.append(train_ae(batch_index))
            print "Training at epoch %d, %f" % (epoch, np.mean(cost_one_epoch))
            ae_costs.append(np.mean(cost_one_epoch))

        print "Saving files ..."
        self.save_params(weights_file)
        return ae_costs
项目:structured-output-ae    作者:sbelharbi    | 项目源码 | 文件源码
def fit(self,
            data=None,
            learning_rate=0.08,
            batch_size=100,
            n_epochs=22,
            sparsity_penality=0.001,
            sparsity_level=0.05,
            weights_file="out/sa_weights_mnists.npy"):
        if data is None:
            raise Exception("Data can not be empty.")

        index = T.lscalar("index")
        data_shared = theano.shared(np.asarray(data.tolist(),
                                               dtype=theano.config.floatX))

        n_batches = data.shape[0] / batch_size
        (cost, updates) = self.get_sa_sgd_updates(
            learning_rate, sparsity_level, sparsity_penality, batch_size)

        train_ae = theano.function(
            [index], updates, givens={
                self.x: data_shared[index*batch_size: (index+1)*batch_size]})

        print "Started training SA."
        ae_costs = []
        for epoch in xrange(n_epochs):
            print "Training at epoch %d" % epoch
            cost_one_epoch = []
            for batch_index in xrange(n_batches):
                cost_one_epoch.append(train_ae(batch_index))
            print "Training at epoch %d, %f" % (epoch, np.mean(cost_one_epoch))
            ae_costs.append(np.mean(cost_one_epoch))

        print "Saving files ..."
        np.save(weights_file, self.params[0].get_value())
        return ae_costs
项目:deep-learning-theano    作者:aidiary    | 项目源码 | 文件源码
def pretraining_functions(self, train_set_x, batch_size):
        """???????????pre-training??????????
        ?????????x?????"""
        # ?????????????????
        index = T.lscalar('index')

        # ??????????????????????????????
        corruption_level = T.scalar('corruption')
        learning_rate = T.scalar('lr')

        batch_begin = index * batch_size
        batch_end = batch_begin + batch_size

        # ????????????????
        # ????????????????
        pretrain_functions = []
        for autoencoder in self.autoencoder_layers:
            # ??????????????????
            cost, updates = autoencoder.get_cost_updates(corruption_level, learning_rate)
            fn = theano.function(
                inputs=[
                    index,
                    # Param????????????????????Python????????
                    # Tensor???????corruption, lr???????
                    theano.Param(corruption_level, default=0.2),
                    theano.Param(learning_rate, default=0.1)
                ],
                outputs=cost,
                updates=updates,
                givens={
                    self.x: train_set_x[batch_begin:batch_end]
                }
            )
            pretrain_functions.append(fn)

        return pretrain_functions
项目:DL-Benchmarks    作者:DL-Benchmarks    | 项目源码 | 文件源码
def build_finetune_functions(self, batch_size):

        index = T.lscalar('index')  # index to a [mini]batch
        # beginning of a batch, given `index`
        batch_begin = index * batch_size
        # ending of a batch given `index`
        batch_end = batch_begin + batch_size

        cost = self.finetune_cost
        shared_cost = theano.shared(np.float32(0.0))
        forward_mlp = theano.function(
            [index], [],
            updates=[(shared_cost, cost)],
            givens={
                    self.x: self.train_set_x[batch_begin: batch_end],
                    self.y: self.train_set_y[batch_begin: batch_end],
                    })

        grads_temp = T.grad(cost, self.params)

        # This is both forward and backward
        forward_backward_mlp = theano.function(
            [index], grads_temp,
            givens={
                    self.x: self.train_set_x[batch_begin: batch_end],
                    self.y: self.train_set_y[batch_begin: batch_end],
                    })

        return forward_mlp, forward_backward_mlp
项目:neural-networks-and-deep-learning    作者:skylook    | 项目源码 | 文件源码
def ensemble(nets): 
    """Takes as input a list of nets, and then computes the accuracy on
    the test data when classifications are computed by taking a vote
    amongst the nets.  Returns a tuple containing a list of indices
    for test data which is erroneously classified, and a list of the
    corresponding erroneous predictions.

    Note that this is a quick-and-dirty kluge: it'd be more reusable
    (and faster) to define a Theano function taking the vote.  But
    this works.

    """

    test_x, test_y = test_data
    for net in nets:
        i = T.lscalar() # mini-batch index
        net.test_mb_predictions = theano.function(
            [i], net.layers[-1].y_out,
            givens={
                net.x: 
                test_x[i*net.mini_batch_size: (i+1)*net.mini_batch_size]
            })
        net.test_predictions = list(np.concatenate(
            [net.test_mb_predictions(i) for i in xrange(1000)]))
    all_test_predictions = zip(*[net.test_predictions for net in nets])
    def plurality(p): return Counter(p).most_common(1)[0][0]
    plurality_test_predictions = [plurality(p) 
                                  for p in all_test_predictions]
    test_y_eval = test_y.eval()
    error_locations = [j for j in xrange(10000) 
                       if plurality_test_predictions[j] != test_y_eval[j]]
    erroneous_predictions = [plurality(all_test_predictions[j])
                             for j in error_locations]
    print "Accuracy is {:.2%}".format((1-len(error_locations)/10000.0))
    return error_locations, erroneous_predictions
项目:ObjRecPoseEst    作者:paroj    | 项目源码 | 文件源码
def setupDescriptorComputation(self,dataManager,batch_size=None):

        if batch_size == None:
            batch_size = self.cfgParams.batch_size

        if (self.tfComputeDescr is not None) and (self.descrCompDataManager == dataManager) and (self.descrCompBatchSize == batch_size):
            # no need to recompile
            return

        self.descrCompDataManager = dataManager
        self.descrCompBatchSize = batch_size

        self.tvIndex = T.lscalar()
        print("compiling compute_descr() ...",end="")
        if isinstance(self.inputVar,list):
            tvsData_x = dataManager.tvsData_x
            if not isinstance(tvsData_x,list):
                tvsData_x = [tvsData_x]

            givens_comp_descr = { iv: data[self.tvIndex * batch_size:(self.tvIndex + 1) * batch_size] for (iv,data) in zip(self.inputVar,tvsData_x) }
        else:
            givens_comp_descr = { self.inputVar: dataManager.tvsData_x[self.tvIndex * batch_size:(self.tvIndex + 1) * batch_size] }
        self.tfComputeDescr = theano.function(inputs = [self.tvIndex],
                                              outputs = self.output,
                                              givens = givens_comp_descr )

        print("done")
项目:learning-class-invariant-features    作者:sbelharbi    | 项目源码 | 文件源码
def fit(self,
            data=None,
            learning_rate=0.1,
            batch_size=100,
            n_epochs=20,
            lr_scalar=0.998,
            weights_file="out/ae_weights_mnist.npy"):
        """
        Fit the data to the autoencoder (training).
        """
        if data is None:
            raise Exception("Data can't be empty.")

        index = T.lscalar("index")
        data_shared = theano.shared(
            np.asarray(data, dtype=theano.config.floatX))
        n_batches = data.shape[0] / batch_size
        (cost, updates) = self.get_sgd_updates(
            learning_rate, lr_scalar, batch_size)
        train_ae = theano.function(
            [index], cost, updates=updates,
            givens={
                self.x: data_shared[index*batch_size: (index+1)*batch_size]})

        print "Start training the ae."
        ae_costs = []

        for epoch in xrange(n_epochs):
            print "Training at epoch %d" % epoch
            cost_one_epoch = []
            for batch_index in xrange(n_batches):
                cost_one_epoch.append(train_ae(batch_index))
            print "Training at epoch %d, %f" % (epoch, np.mean(cost_one_epoch))
            ae_costs.append(np.mean(cost_one_epoch))

        print "Saving files ..."
        self.save_params(weights_file)
        return ae_costs
项目:image-denoising    作者:utkarshojha    | 项目源码 | 文件源码
def pretraining_functions(self, train_set_x, batch_size):

        # index to a [mini]batch
        index = T.lscalar('index')  # index to a minibatch
        corruption_level = T.scalar('corruption')  # % of corruption to use
        learning_rate = T.scalar('lr')  # learning rate to use
        # begining of a batch, given `index`
        batch_begin = index * batch_size
        # ending of a batch given `index`
        batch_end = batch_begin + batch_size

        pretrain_fns = []
        for dA in self.dA_layers:
            # get the cost and the updates list
            cost, updates = dA.get_cost_updates(corruption_level,
                                                learning_rate)
            # compile the theano function
            fn = theano.function(
                inputs=[
                    index,
                    theano.In(corruption_level, value=0.2),
                    theano.In(learning_rate, value=0.1)
                ],
                outputs=cost,
                updates=updates,
                givens={
                    self.x: train_set_x[batch_begin: batch_end]
                }
            )
            # append `fn` to the list of functions
            pretrain_fns.append(fn)

        return pretrain_fns
项目:deep-motion-analysis    作者:Brimborough    | 项目源码 | 文件源码
def train(self, network, input_data, output_data, filename=None):

        input = input_data.type()
        output = output_data.type()
        index = T.lscalar()

        self.params = network.params
        self.m0params = [theano.shared(np.zeros(p.shape.eval(), dtype=theano.config.floatX), borrow=True) for p in self.params]
        self.m1params = [theano.shared(np.zeros(p.shape.eval(), dtype=theano.config.floatX), borrow=True) for p in self.params]
        self.t = theano.shared(np.array([1], dtype=theano.config.floatX))

        cost, updates = self.get_cost_updates(network, input, output)
        train_func = theano.function([index], cost, updates=updates, givens={
            input:input_data[index*self.batchsize:(index+1)*self.batchsize],
            output:output_data[index*self.batchsize:(index+1)*self.batchsize],
        }, allow_input_downcast=True)

        last_mean = 0
        for epoch in range(self.epochs):

            batchinds = np.arange(input_data.shape.eval()[0] // self.batchsize)
            self.rng.shuffle(batchinds)

            sys.stdout.write('\n')

            c = []
            for bii, bi in enumerate(batchinds):
                c.append(train_func(bi))
                if np.isnan(c[-1]): return
                if bii % (int(len(batchinds) / 1000) + 1) == 0:
                    sys.stdout.write('\r[Epoch %i]  %0.1f%% mean %.5f' % (epoch, 100 * float(bii)/len(batchinds), np.mean(c)))
                    sys.stdout.flush()

            curr_mean = np.mean(c)
            diff_mean, last_mean = curr_mean-last_mean, curr_mean
            sys.stdout.write('\r[Epoch %i] 100.0%% mean %.5f diff %.5f %s' % 
                (epoch, curr_mean, diff_mean, str(datetime.now())[11:19]))
            sys.stdout.flush()

            network.save(filename)
项目:deep-motion-analysis    作者:Brimborough    | 项目源码 | 文件源码
def train(self, lstm_network, decoder_network, input_data, output_data, filename=None):

        input = input_data.type()
        output = output_data.type()
        index = T.lscalar()

        self.params = lstm_network.params
        self.m0params = [theano.shared(np.zeros(p.shape.eval(), dtype=theano.config.floatX), borrow=True) for p in self.params]
        self.m1params = [theano.shared(np.zeros(p.shape.eval(), dtype=theano.config.floatX), borrow=True) for p in self.params]
        self.t = theano.shared(np.array([1], dtype=theano.config.floatX))

        cost, updates = self.get_cost_updates(lstm_network, decoder_network, input, output)
        train_func = theano.function([index], cost, updates=updates, givens={
            input:input_data[index*self.batchsize:(index+1)*self.batchsize],
            output:output_data[index*self.batchsize:(index+1)*self.batchsize],
        }, allow_input_downcast=True)

        last_mean = 0
        for epoch in range(self.epochs):

            batchinds = np.arange(input_data.shape.eval()[0] // self.batchsize)
            self.rng.shuffle(batchinds)

            sys.stdout.write('\n')

            c = []
            for bii, bi in enumerate(batchinds):
                c.append(train_func(bi))
                if np.isnan(c[-1]): return
                if bii % (int(len(batchinds) / 1000) + 1) == 0:
                    sys.stdout.write('\r[Epoch %i]  %0.1f%% mean %.5f' % (epoch, 100 * float(bii)/len(batchinds), np.mean(c)))
                    sys.stdout.flush()

            curr_mean = np.mean(c)
            diff_mean, last_mean = curr_mean-last_mean, curr_mean
            sys.stdout.write('\r[Epoch %i] 100.0%% mean %.5f diff %.5f %s' % 
                (epoch, curr_mean, diff_mean, str(datetime.now())[11:19]))
            sys.stdout.flush()

            lstm_network.save(filename)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_add_canonizer_problem0():
    n_segments = 10
    label = lscalar('label')
    segment_labels = label + theano._asarray([0] * n_segments, dtype='int64')

    r = segment_labels * 5
    f = function([label], r)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_prod_upcast(self):
        s = theano.tensor.lscalar()
        a = theano.tensor.alloc(numpy.asarray(5, dtype='float32'), s, s)
        orig = theano.config.warn_float64
        theano.config.warn_float64 = "raise"
        try:
            f = theano.function([s], a.prod())
            f(5)
        finally:
            theano.config.warn_float64 = orig
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_local_div_to_inv():
    num_len_s = tensor.lscalar('num_len')
    denom_s = tensor.scalar('denom')

    num_v = tensor.alloc(1, num_len_s)
    denom_m = denom_s.dimshuffle('x', 'x')

    out = num_v / denom_m
    assert numpy.all(out.broadcastable == (True, False))

    f = theano.function([num_len_s, denom_s], out)
    out_val = f(3, 2.)
    assert out_val.shape == (1, 3)
    utt.assert_allclose(out_val, 0.5)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_correct_solution(self):
        x = tensor.lmatrix()
        y = tensor.lmatrix()
        z = tensor.lscalar()
        b = theano.tensor.nlinalg.lstsq()(x, y, z)
        f = function([x, y, z], b)
        TestMatrix1 = numpy.asarray([[2, 1], [3, 4]])
        TestMatrix2 = numpy.asarray([[17, 20], [43, 50]])
        TestScalar = numpy.asarray(1)
        f = function([x, y, z], b)
        m = f(TestMatrix1, TestMatrix2, TestScalar)
        self.assertTrue(numpy.allclose(TestMatrix2, numpy.dot(TestMatrix1, m[0])))
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_perform(self):
        x = tensor.lscalar()
        f = function([x], self.op(x))
        M = numpy.random.randint(3, 51, size=())
        assert numpy.allclose(f(M), numpy.bartlett(M))
        assert numpy.allclose(f(0), numpy.bartlett(0))
        assert numpy.allclose(f(-1), numpy.bartlett(-1))
        b = numpy.array([17], dtype='uint8')
        assert numpy.allclose(f(b[0]), numpy.bartlett(b[0]))
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_mixed_shape(self):
        # Test when the provided shape is a tuple of ints and scalar vars
        random = RandomStreams(utt.fetch_seed())
        shape0 = tensor.lscalar()
        shape = (shape0, 3)
        f = function([shape0], random.uniform(size=shape, ndim=2))
        assert f(2).shape == (2, 3)
        assert f(8).shape == (8, 3)

        g = function([shape0], random.uniform(size=shape))
        assert g(2).shape == (2, 3)
        assert g(8).shape == (8, 3)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_dtype(self):
        random = RandomStreams(utt.fetch_seed())
        low = tensor.lscalar()
        high = tensor.lscalar()
        out = random.random_integers(low=low, high=high, size=(20,), dtype='int8')
        assert out.dtype == 'int8'
        f = function([low, high], out)

        val0 = f(0, 9)
        assert val0.dtype == 'int8'

        val1 = f(255, 257)
        assert val1.dtype == 'int8'
        assert numpy.all(abs(val1) <= 1)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_stack_scalar_make_vector_dtype(self):
        '''Test that calling stack() on scalars instantiates MakeVector,
        event when the scalar don't have the same dtype.'''
        a = tensor.iscalar('a')
        b = tensor.lscalar('b')
        s = stack([a, b, a, b])
        f = function([a, b], s, mode=self.mode)
        val = f(1, 2)
        self.assertTrue(numpy.all(val == [1, 2, 1, 2]))
        topo = f.maker.fgraph.toposort()
        assert len([n for n in topo if isinstance(n.op, opt.MakeVector)]) > 0
        assert len([n for n in topo if isinstance(n, type(self.join_op))]) == 0
        assert f.maker.fgraph.outputs[0].dtype == 'int64'
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_stack_scalar_make_vector_constant(self):
        '''Test that calling stack() on scalars instantiates MakeVector,
        event when the scalar are simple int type.'''
        a = tensor.iscalar('a')
        b = tensor.lscalar('b')
        # test when the constant is the first element.
        # The first element is used in a special way
        s = stack([10, a, b, numpy.int8(3)])
        f = function([a, b], s, mode=self.mode)
        val = f(1, 2)
        self.assertTrue(numpy.all(val == [10, 1, 2, 3]))
        topo = f.maker.fgraph.toposort()
        assert len([n for n in topo if isinstance(n.op, opt.MakeVector)]) > 0
        assert len([n for n in topo if isinstance(n, type(self.join_op))]) == 0
        assert f.maker.fgraph.outputs[0].dtype == 'int64'
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_vector_len(self):
        x = lscalar('x')
        y = dscalar('y')

        triple = as_tensor_variable((x, y, 9.0))
        assert 3 == get_vector_length(triple)

        a, b, c = triple
        f = function([x, y], [b, c, a], mode=self.mode)
        topo = f.maker.fgraph.toposort()
        assert [True for node in topo if isinstance(node.op, opt.MakeVector)]

        assert numpy.allclose(f(4, 5), [5, 9, 4])
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_simple_2d(self):
        """Increments or sets part of a tensor by a scalar using full slice and
        a partial slice depending on a scalar.
        """
        a = tt.dmatrix()
        increment = tt.dscalar()
        sl1 = slice(None)
        sl2_end = tt.lscalar()
        sl2 = slice(sl2_end)

        for do_set in [False, True]:

            if do_set:
                resut = tt.set_subtensor(a[sl1, sl2], increment)
            else:
                resut = tt.inc_subtensor(a[sl1, sl2], increment)

            f = theano.function([a, increment, sl2_end], resut)

            val_a = numpy.ones((5, 5))
            val_inc = 2.3
            val_sl2_end = 2

            result = f(val_a, val_inc, val_sl2_end)

            expected_result = numpy.copy(val_a)
            if do_set:
                expected_result[:, :val_sl2_end] = val_inc
            else:
                expected_result[:, :val_sl2_end] += val_inc

            utt.assert_allclose(result, expected_result)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_default_updates_input(self):
        x = shared(0)
        y = shared(1)
        if theano.configdefaults.python_int_bitwidth() == 32:
            a = iscalar('a')
        else:
            a = lscalar('a')

        x.default_update = y
        y.default_update = y + a

        f1 = pfunc([], x, no_default_updates=True)
        f1()
        assert x.get_value() == 0
        assert y.get_value() == 1

        f2 = pfunc([], x, no_default_updates=[x])
        f2()
        assert x.get_value() == 0
        assert y.get_value() == 1

        f3 = pfunc([], x, no_default_updates=[y])
        f3()
        assert x.get_value() == 1
        assert y.get_value() == 1

        f4 = pfunc([a], x)
        f4(2)
        assert x.get_value() == 1
        assert y.get_value() == 3

        f5 = pfunc([], x, updates={y: (y - 1)})
        f5()
        assert x.get_value() == 3
        assert y.get_value() == 2

        # a is needed as input if y.default_update is used
        self.assertRaises(theano.gof.MissingInputError, pfunc, [], x)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_duplicate_inputs(self):
        x = theano.tensor.lscalar('x')
        self.assertRaises(theano.compile.UnusedInputError,
                          theano.function, [x, x, x], x)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def make_node(self, *args):
        # HERE `args` must be THEANO VARIABLES
        return gof.Apply(op=self, inputs=args, outputs=[tensor.lscalar()])
项目:DeepLearningTutorialForChinese    作者:zhaoyu611    | 项目源码 | 文件源码
def pretraining_function(self,train_set_x,batch_size):
        '''
        ????????????????dA???,??????????
        ?????minibatch????????minibatch???????
        train_set_x: theano.tensor.TensorType   ??dA????(????)
        batch_size: int  [mini]batch??

        '''
        #[mini]batch???
        index=T.lscalar('index')
        corruption_level=T.scalar('corruption') #corruption???
        learning_rate=T.scalar('lr') #???
        #batch??
        n_bathes=train_set_x.get_value(borrow=True).shape[0]/batch_size
        #??index?????
        # batch
        batch_begin=index*batch_size
        #??index?????batch
        batch_end=batch_begin+batch_size

        pretrain_fns=[]
        for dA in self.dA_layers: #??dA
            #???????????
            cost,updates=dA.get_cost_updates(corruption_level,
                                            learning_rate)
            #??theano??
            fn=theano.function(inputs=[index,
                            theano.Param(corruption_level,default=0.2),
                            theano.Param(learning_rate,default=0.1)],
                                outputs=cost,
                                updates=updates,
                                givens={self.x:train_set_x[batch_begin:
                                                           batch_end]})
            #?fn???????
            pretrain_fns.append(fn)

        return pretrain_fns
项目:DeepLearningTutorialForChinese    作者:zhaoyu611    | 项目源码 | 文件源码
def pretraining_functions(self,train_set_x,batch_size,k):
        """
        ??????????????????????????minibatch???
        ????RBM,?????minibatch???????

        train_set_x: theano.tensor.TensorType ??????
        batch_size: int minibatch???
        k:  int CD-k/PCD-k?Gibbs????
        """
        index=T.lscalar('index') #minibatch???
        learning_rate=T.scalar('lr') #???
        #bathes??
        n_batches=train_set_x.get_value(borrow=True).shape[0]/batch_size
        #??index????batch
        batch_begin=index*batch_size
        #??index????batch
        batch_end=batch_begin+batch_size

        pretrain_fns=[]
        for rbm in self.rbm_layers:  #??????RBM
            #??????????
            #??CD-k(??persisitent=None)?????RBM
            cost,updates=rbm.get_cost_updates(learning_rate,persistent=None,k=k)

            #??thenao??,???learning_rate???tensor??
            fn=theano.function(inputs=[index,theano.Param(learning_rate,default=0.1)],
                               outputs=cost,updates=updates,
                               givens={self.x:train_set_x[batch_begin:batch_end]})
            #?'fn'???list???
            pretrain_fns.append(fn)
        return pretrain_fns
项目:deep-prior    作者:moberweger    | 项目源码 | 文件源码
def setupFunctions(self):
        floatX = theano.config.floatX  # @UndefinedVariable

        dnParams = self.poseNet.cfgParams

        # params
        self.learning_rate = T.scalar('learning_rate', dtype=floatX)
        self.momentum = T.scalar('momentum', dtype=floatX)

        # input
        self.index = T.lscalar()  # index to a [mini]batch
        self.x = self.poseNet.inputVar

        # targets
        if self.poseNet.cfgParams.numJoints == 1 and self.poseNet.cfgParams.nDims == 1:
            y = T.vector('y',dtype=floatX) # R^D
        elif self.poseNet.cfgParams.numJoints == 1:
            y = T.matrix('y',dtype=floatX) # R^Dx3
        else:
            y = T.tensor3('y',dtype=floatX) # R^Dx16x3

        # L2 error
        if self.poseNet.cfgParams.numJoints == 1 and self.poseNet.cfgParams.nDims == 1:
            cost = T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.nDims))-y).mean(axis=1)
        elif self.poseNet.cfgParams.numJoints == 1:
            cost = T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.nDims))-y).sum(axis=1)
        else:
            cost = T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.numJoints,self.poseNet.cfgParams.nDims))-y).sum(axis=2).mean(axis=1) # error is sum of all joints

        self.cost = cost.mean() # The cost to minimize

        # weight vector length for regularization (weight decay)       
        totalWeightVectorLength = 0
        for W in self.poseNet.weights:
            totalWeightVectorLength += self.cfgParams.weightreg_factor * (W ** 2).sum()

        if not self.poseNet.hasDropout():
            self.cost += totalWeightVectorLength  # + weight vector norm

        # create a list of gradients for all model parameters
        self.params = self.poseNet.params
        self.grads = T.grad(self.cost, self.params)

        # euclidean mean errors over all joints
        if self.poseNet.cfgParams.numJoints == 1 and self.poseNet.cfgParams.nDims == 1:
            errors = T.sqrt(T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.nDims))-y)).mean(axis=1)
        elif self.poseNet.cfgParams.numJoints == 1:
            errors = T.sqrt(T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.nDims))-y).sum(axis=1))
        else:
            errors = T.sqrt(T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.numJoints,self.poseNet.cfgParams.nDims))-y).sum(axis=2)).mean(axis=1)

        # mean error over full set
        self.errors = errors.mean()

        # store stuff                    
        self.y = y
项目:deep-prior    作者:moberweger    | 项目源码 | 文件源码
def setupFunctions(self):
        floatX = theano.config.floatX  # @UndefinedVariable

        dnParams = self.poseNet.cfgParams

        # params
        self.learning_rate = T.scalar('learning_rate', dtype=floatX)
        self.momentum = T.scalar('momentum', dtype=floatX)

        # input
        self.index = T.lscalar()  # index to a [mini]batch
        self.x = []
        for i in range(self.poseNet.cfgParams.numInputs):
            self.x.append(self.poseNet.inputVar[i])

        # targets
        if self.poseNet.cfgParams.numJoints == 1 and self.poseNet.cfgParams.nDims == 1:
            y = T.vector('y', dtype=floatX) # R^D
        elif self.poseNet.cfgParams.numJoints == 1:
            y = T.matrix('y', dtype=floatX) # R^Dx3
        else:
            y = T.tensor3('y', dtype=floatX) # R^Dx16x3

        # L2 error
        if self.poseNet.cfgParams.numJoints == 1 and self.poseNet.cfgParams.nDims == 1:
            cost = T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.nDims)) - y)
        elif self.poseNet.cfgParams.numJoints == 1:
            cost = T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.nDims))-y).sum(axis=1)
        else:
            cost = T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.numJoints,self.poseNet.cfgParams.nDims))-y).sum(axis=2).mean(axis=1) # error is sum of all joints

        self.cost = cost.mean() # The cost to minimize

        # weight vector length for regularization (weight decay)       
        totalWeightVectorLength = 0
        for W in self.poseNet.weights:
            totalWeightVectorLength += self.cfgParams.weightreg_factor * (W ** 2).sum()

        if not self.poseNet.hasDropout():
            self.cost += totalWeightVectorLength  # + weight vector norm

        # create a list of gradients for all model parameters
        self.params = self.poseNet.params
        self.grads = T.grad(self.cost, self.params)

        # euclidean mean errors over all joints
        if self.poseNet.cfgParams.numJoints == 1 and self.poseNet.cfgParams.nDims == 1:
            errors = T.sqrt(T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.nDims))-y))
        elif self.poseNet.cfgParams.numJoints == 1:
            errors = T.sqrt(T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.nDims))-y).sum(axis=1))
        else:
            errors = T.sqrt(T.sqr(T.reshape(self.poseNet.output,(self.cfgParams.batch_size,self.poseNet.cfgParams.numJoints,self.poseNet.cfgParams.nDims))-y).sum(axis=2)).mean(axis=1)

        # mean error over full set
        self.errors = errors.mean()

        # store stuff                    
        self.y = y
项目:RBM-DBN-theano-DL4J    作者:lzhbrian    | 项目源码 | 文件源码
def pretraining_functions(self, train_set_x, batch_size, k):
        '''Generates a list of functions, for performing one step of
        gradient descent at a given layer. The function will require
        as input the minibatch index, and to train an RBM you just
        need to iterate, calling the corresponding function on all
        minibatch indexes.

        :type train_set_x: theano.tensor.TensorType
        :param train_set_x: Shared var. that contains all datapoints used
                            for training the RBM
        :type batch_size: int
        :param batch_size: size of a [mini]batch
        :param k: number of Gibbs steps to do in CD-k / PCD-k

        '''

        # index to a [mini]batch
        index = T.lscalar('index')  # index to a minibatch
        learning_rate = T.scalar('lr')  # learning rate to use

        # number of batches
        n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
        # begining of a batch, given `index`
        batch_begin = index * batch_size
        # ending of a batch given `index`
        batch_end = batch_begin + batch_size

        pretrain_fns = []
        for rbm in self.rbm_layers:

            # get the cost and the updates list
            # using CD-k here (persisent=None) for training each RBM.
            # TODO: change cost function to reconstruction error
            cost, updates = rbm.get_cost_updates(learning_rate,
                                                 persistent=None, k=k)

            # compile the theano function
            fn = theano.function(
                inputs=[index, theano.In(learning_rate, value=0.1)],
                outputs=cost,
                updates=updates,
                givens={
                    self.x: train_set_x[batch_begin:batch_end]
                }
            )
            # append `fn` to the list of functions
            pretrain_fns.append(fn)

        return pretrain_fns
项目:DeepMirTar_SdA    作者:Bjoux2    | 项目源码 | 文件源码
def pretraining_functions(self, pre_train_set_x):

        ''' Generates a list of functions, each of them implementing one
        step in trainnig the dA corresponding to the layer with same index.
        The function will require as input the minibatch index, and to train
        a dA you just need to iterate, calling the corresponding function on
        all minibatch indexes.

        :type train_set_x: theano.tensor.TensorType
        :param train_set_x: Shared variable that contains all datapoints used
                            for training the dA

        :type batch_size: int
        :param batch_size: size of a [mini]batch

        :type learning_rate: float
        :param learning_rate: learning rate used during training for any of
                              the dA layers
        '''
        # index to a [mini]batch
        index = T.lscalar('index')  # index to a minibatch
        corruption_level = T.scalar('corruption')  # % of corruption to use
        learning_rate = T.scalar('lr')  # learning rate to use
        # begining of a batch, given `index`
        batch_begin = index * self.pretrain_batch_size
        # ending of a batch given `index`
        batch_end = batch_begin + self.pretrain_batch_size

        pretrain_fns = []
        for dA in self.dA_layers:
            # get the cost and the updates list
            cost, updates = dA.get_cost_updates(corruption_level,
                                                learning_rate)
            # compile the theano function
            fn = theano.function(
                inputs=[
                    index,
                    theano.In(corruption_level, value=0.2),
                    theano.In(learning_rate, value=0.1)
                ],
                outputs=cost,
                updates=updates,
                givens={
                    self.x: pre_train_set_x[batch_begin: batch_end]
                }
            )
            # append `fn` to the list of functions
            pretrain_fns.append(fn)

        return pretrain_fns
项目:DeepDTIs_DBN    作者:Bjoux2    | 项目源码 | 文件源码
def pretraining_functions(self, train_set_x):
        '''Generates a list of functions, for performing one step of
        gradient descent at a given layer. The function will require
        as input the minibatch index, and to train an RBM you just
        need to iterate, calling the corresponding function on all
        minibatch indexes.

        :type train_set_x: theano.tensor.TensorType
        :param train_set_x: Shared var. that contains all datapoints used
                            for training the RBM
        :type batch_size: int
        :param batch_size: size of a [mini]batch
        :param k: number of Gibbs steps to do in CD-k / PCD-k

        '''

        # index to a [mini]batch
        index = T.lscalar('index')  # index to a minibatch
        learning_rate = T.scalar('lr')  # learning rate to use

        # begining of a batch, given `index`
        batch_begin = index * self.pretrain_batch_size
        # ending of a batch given `index`
        batch_end = batch_begin + self.pretrain_batch_size

        pretrain_fns = []
        for rbm in self.rbm_layers:

            # get the cost and the updates list
            # using CD-k here (persisent=None) for training each RBM.
            # TODO: change cost function to reconstruction error
            cost, updates = rbm.get_cost_updates(learning_rate,
                                                 persistent=None, k=self.k)

            # compile the theano function
            fn = theano.function(
                inputs=[index, theano.In(learning_rate, value=0.1)],
                outputs=cost,
                updates=updates,
                givens={
                    self.x: train_set_x[batch_begin:batch_end]
                }
            )
            # append `fn` to the list of functions
            pretrain_fns.append(fn)

        return pretrain_fns
项目:DeepDTIs_DBN    作者:Bjoux2    | 项目源码 | 文件源码
def build_finetune_functions(self, train_set_x, train_set_y,valid_set_x,valid_set_y):
        # compute number of minibatches for training, validation and testing
        n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
        n_valid_batches //= self.finetune_batch_size

        index = T.lscalar('index')  # index to a [mini]batch

        # compute the gradients with respect to the model parameters
        gparams = T.grad(self.finetune_cost, self.params)

        # compute list of fine-tuning updates
        updates = []
        for param, gparam in zip(self.params, gparams):
            updates.append((param, param - gparam * self.finetune_lr))

        train_fn = theano.function(
            inputs=[index],
            outputs=self.finetune_cost,
            updates=updates,
            givens={
                self.x: train_set_x[
                    index * self.finetune_batch_size: (index + 1) * self.finetune_batch_size
                ],
                self.y: train_set_y[
                    index * self.finetune_batch_size: (index + 1) * self.finetune_batch_size
                ]
            }
        )

        valid_score_i = theano.function(
            [index],
            self.errors,
            givens={
                self.x: valid_set_x[
                    index * self.finetune_batch_size: (index + 1) * self.finetune_batch_size
                ],
                self.y: valid_set_y[
                    index * self.finetune_batch_size: (index + 1) * self.finetune_batch_size
                ]
            }
        )

        # Create a function that scans the entire validation set
        def valid_score():
            return [valid_score_i(i) for i in range(n_valid_batches)]

        return train_fn, valid_score
项目:DeepDTIs_DBN    作者:Bjoux2    | 项目源码 | 文件源码
def pretraining_functions(self, train_set_x, batch_size, k):
        '''Generates a list of functions, for performing one step of
        gradient descent at a given layer. The function will require
        as input the minibatch index, and to train an RBM you just
        need to iterate, calling the corresponding function on all
        minibatch indexes.

        :type train_set_x: theano.tensor.TensorType
        :param train_set_x: Shared var. that contains all datapoints used
                            for training the RBM
        :type batch_size: int
        :param batch_size: size of a [mini]batch
        :param k: number of Gibbs steps to do in CD-k / PCD-k

        '''

        # index to a [mini]batch
        index = T.lscalar('index')  # index to a minibatch
        learning_rate = T.scalar('lr')  # learning rate to use

        # number of batches
        n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
        # begining of a batch, given `index`
        batch_begin = index * batch_size
        # ending of a batch given `index`
        batch_end = batch_begin + batch_size

        pretrain_fns = []
        for rbm in self.rbm_layers:

            # get the cost and the updates list
            # using CD-k here (persisent=None) for training each RBM.
            # TODO: change cost function to reconstruction error
            cost, updates = rbm.get_cost_updates(learning_rate,
                                                 persistent=None, k=k)

            # compile the theano function
            fn = theano.function(
                inputs=[index, theano.Param(learning_rate, default=0.1)],
                outputs=cost,
                updates=updates,
                givens={
                    self.x: train_set_x[batch_begin:batch_end]
                }
            )
            # append `fn` to the list of functions
            pretrain_fns.append(fn)

        return pretrain_fns
项目:Deep-Learning-with-Theano    作者:PacktPublishing    | 项目源码 | 文件源码
def get_train_model(data, inputs, loss, params, batch_size=32):

    """
        trainer: Function to define the trainer of the model on the data set that bassed as the parameters of the function


        parameters:
            contexts: List of the contexts (the input of the trainer)
            targets: List of the targets.

        return:
            Theano function represents the train model
    """



    data_contexts = data[0]
    data_targets = data[1]

    context = inputs[0]
    target = inputs[1]


    learning_rate = T.fscalar('learning_rate') # theano input: the learning rate, the value of this input
                                               # can be constant like 0.1 or
                                               #it can be come from a function like a decay learning rate function





    index = T.lscalar('index') # the index of minibatch



    g_params = T.grad(cost=loss, wrt=params)

    updates = [
            (param, param - learning_rate * gparam)
            for param, gparam in zip(params, g_params)
    ]


    train_fun = theano.function(
        [index, learning_rate],
        loss,
        updates=updates,
        givens={
            context: data_contexts[index * args.batch_size: (index + 1) * args.batch_size],
            target: data_targets[index * args.batch_size: (index + 1) * args.batch_size]
        }
    )


    return train_fun
项目:deep-learning-theano    作者:aidiary    | 项目源码 | 文件源码
def build_finetune_functions(self, datasets, batch_size, learning_rate):
        """fine-tuning???????"""
        train_set_x, train_set_y = datasets[0]
        valid_set_x, valid_set_y = datasets[1]
        test_set_x, test_set_y = datasets[2]

        n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
        n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size

        index = T.lscalar('index')

        gparams = T.grad(self.finetune_cost, self.params)

        updates = [
            (param, param - gparam * learning_rate) for param, gparam in zip(self.params, gparams)
        ]

        train_model = theano.function(
            inputs=[index],
            outputs=self.finetune_cost,
            updates=updates,
            givens={
                self.x: train_set_x[index * batch_size: (index + 1) * batch_size],
                self.y: train_set_y[index * batch_size: (index + 1) * batch_size]
            },
            name='train')

        test_score_i = theano.function(
            inputs=[index],
            outputs=self.errors,
            givens={
                self.x: test_set_x[index * batch_size: (index + 1) * batch_size],
                self.y: test_set_y[index * batch_size: (index + 1) * batch_size]
            },
            name='test')

        valid_score_i = theano.function(
            inputs=[index],
            outputs=self.errors,
            givens={
                self.x: valid_set_x[index * batch_size: (index + 1) * batch_size],
                self.y: valid_set_y[index * batch_size: (index + 1) * batch_size]
            },
            name='validate')

        def valid_score():
            """???????valid?????????"""
            return [valid_score_i(i) for i in xrange(n_valid_batches)]

        def test_score():
            """???????test?????????"""
            return [test_score_i(i) for i in xrange(n_test_batches)]

        return train_model, valid_score, test_score