当前位置: 首页>>代码示例>>Python>>正文


Python compile.shared函数代码示例

本文整理汇总了Python中theano.compile.shared函数的典型用法代码示例。如果您正苦于以下问题:Python shared函数的具体用法?Python shared怎么用?Python shared使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了shared函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_doc

    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)
开发者ID:12190143,项目名称:Theano,代码行数:25,代码来源:test_pfunc.py

示例2: test_default_updates_multiple

    def test_default_updates_multiple(self):
        x = shared(0)
        y = shared(1)

        x.default_update = x - 1
        y.default_update = y + 1

        f1 = pfunc([], [x, y])
        f1()
        assert x.get_value() == -1
        assert y.get_value() == 2

        f2 = pfunc([], [x, y], updates=[(x, (x - 2))], no_default_updates=[y])
        f2()
        assert x.get_value() == -3
        assert y.get_value() == 2

        f3 = pfunc([], [x, y], updates=[(x, (x - 2))], no_default_updates=True)
        f3()
        assert x.get_value() == -5
        assert y.get_value() == 2

        f4 = pfunc([], [x, y], updates=[(y, (y - 2))])
        f4()
        assert x.get_value() == -6
        assert y.get_value() == 0
开发者ID:12190143,项目名称:Theano,代码行数:26,代码来源:test_pfunc.py

示例3: test_no_default_updates

    def test_no_default_updates(self):
        x = shared(0)
        y = shared(1)
        x.default_update = x + 2

        # Test that the default update is taken into account in the right cases
        f1 = pfunc([], [x], no_default_updates=True)
        f1()
        assert x.get_value() == 0

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

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

        f4 = pfunc([], [x], no_default_updates=[y])
        f4()
        assert x.get_value() == 2

        f5 = pfunc([], [x], no_default_updates=[])
        f5()
        assert x.get_value() == 4

        f5 = pfunc([], [x], no_default_updates=False)
        f5()
        assert x.get_value() == 6

        self.assertRaises(TypeError, pfunc, [], [x], no_default_updates=(x))
        self.assertRaises(TypeError, pfunc, [], [x], no_default_updates=x)
        self.assertRaises(TypeError, pfunc, [], [x],
                          no_default_updates='canard')

        # Mix explicit updates and no_default_updates
        g1 = pfunc([], [x], updates=[(x, (x - 1))], no_default_updates=True)
        g1()
        assert x.get_value() == 5

        g2 = pfunc([], [x], updates=[(x, (x - 1))], no_default_updates=[x])
        g2()
        assert x.get_value() == 4

        g3 = pfunc([], [x], updates=[(x, (x - 1))], no_default_updates=[x, y])
        g3()
        assert x.get_value() == 3

        g4 = pfunc([], [x], updates=[(x, (x - 1))], no_default_updates=[y])
        g4()
        assert x.get_value() == 2

        g5 = pfunc([], [x], updates=[(x, (x - 1))], no_default_updates=[])
        g5()
        assert x.get_value() == 1

        g5 = pfunc([], [x], updates=[(x, (x - 1))], no_default_updates=False)
        g5()
        assert x.get_value() == 0
开发者ID:12190143,项目名称:Theano,代码行数:59,代码来源:test_pfunc.py

示例4: new

 def new(cls, input, n_in, n_out, dtype=None, name=None):
     if dtype is None:
         dtype = input.dtype
     if name is None:
         name = cls.__name__
     w = shared(np.zeros((n_in, n_out), dtype=dtype), name='%s.w'%name)
     b = shared(np.zeros((n_out,), dtype=dtype), name='%s.b'%name)
     return cls(input, w, b, params=[w,b])
开发者ID:yamins81,项目名称:simffa,代码行数:8,代码来源:theano_sgd.py

示例5: test_clone0

    def test_clone0(self):
        x = shared(numpy.asarray([4, 4, 4]))
        y = shared(numpy.asarray([4, 4, 4]))
        z = shared(numpy.asarray([2, 2, 2]))
        up = pfunc([], [], updates={
            x: (x * 5),
            y: ((x * 5) + y),
            z: (((x * 5) + y) ** z)})

        up()
        assert numpy.all(x.get_value() == 20)
        assert numpy.all(y.get_value() == 24)
        assert numpy.all(z.get_value() == (24 ** 2))
开发者ID:12190143,项目名称:Theano,代码行数:13,代码来源:test_pfunc.py

示例6: test_shared

    def test_shared(self):

        # CHECK: two functions (f1 and f2) can share w
        w = shared(numpy.random.rand(2, 2), 'w')
        wval = w.get_value(borrow=False)

        x = dmatrix()
        out1 = w + x
        out2 = w * x
        f1 = pfunc([x], [out1])
        f2 = pfunc([x], [out2])
        xval = numpy.random.rand(2, 2)
        assert numpy.all(f1(xval) == xval + wval)
        assert numpy.all(f2(xval) == xval * wval)

        # CHECK: updating a shared value
        f3 = pfunc([x], out1, updates=[(w, (w - 1))])
        # f3 changes the value of w
        assert numpy.all(f3(xval) == xval + wval)
        # this same value is read by f1
        assert numpy.all(f1(xval) == xval + (wval - 1))

        w.set_value(w.get_value(borrow=True) * 10, borrow=True)
        # this same value is read by f1
        assert numpy.all(f1(xval) == xval + w.get_value(borrow=True))
开发者ID:12190143,项目名称:Theano,代码行数:25,代码来源:test_pfunc.py

示例7: test_shared_mutable

    def test_shared_mutable(self):
        bval = numpy.arange(5)
        b = shared(bval)
        b_out = b * 2

        # shared vars copy args.
        assert b.get_value(borrow=True) is not bval
        # so we do this to get at the underlying data
        bval = data_of(b)

        # by default, shared are not mutable unless doing an explicit update
        f = pfunc([], [b_out], mode='FAST_RUN')
        assert (f() == numpy.arange(5) * 2).all()
        assert numpy.all(b.get_value(borrow=True) == numpy.arange(5))

        # using updates, b is now a mutable parameter
        f = pfunc([], [b_out], updates=[(b, b_out)], mode='FAST_RUN')
        assert (f() == (numpy.arange(5) * 2)).all()
        # because of the update
        assert (b.get_value(borrow=True) == (numpy.arange(5) * 2)).all()
        assert (bval == (numpy.arange(5) * 2)).all()  # because of mutable=True

        # do not depend on updates being in-place though!
        bval = numpy.arange(5)
        b.set_value(bval, borrow=True)
        bval = data_of(b)
        f = pfunc([], [b_out], updates=[(b, (b_out + 3))], mode='FAST_RUN')
        assert (f() == (numpy.arange(5) * 2)).all()
        # because of the update
        assert (b.get_value(borrow=True) == ((numpy.arange(5) * 2) + 3)).all()
        # bval got modified to something...
        assert not (bval == numpy.arange(5)).all()
        # ... but not to b.value !
        assert not (bval == b.get_value(borrow=True)).all()
开发者ID:12190143,项目名称:Theano,代码行数:34,代码来源:test_pfunc.py

示例8: test_givens_replaces_shared_variable2

    def test_givens_replaces_shared_variable2(self):
        a = shared(1., 'a')
        a.default_update = a + 3
        c = a + 10
        f = pfunc([], c, givens={a: (a + 10)})

        assert f() == 21
        assert f() == 34
开发者ID:12190143,项目名称:Theano,代码行数:8,代码来源:test_pfunc.py

示例9: test_update_equiv

    def test_update_equiv(self):
        # Like test_update_same, but the update expression is simplified until
        # it is found to be equal to the original variable
        a = shared(1., 'a')
        b = shared(numpy.ones((2, 3)), 'b')

        # See comment in test_update_same about why we try both
        # shared variables.
        f = theano.function([], [], updates=[(a, a), (b, (2 * b - b))])
        g = theano.function([], [], updates=[(a, (a * 2 - a)), (b, b)])

        f()
        assert a.get_value(borrow=True).shape == (), a.get_value()
        assert b.get_value(borrow=True).shape == (2, 3), b.get_value()
        g()
        assert a.get_value(borrow=True).shape == (), a.get_value()
        assert b.get_value(borrow=True).shape == (2, 3), b.get_value()
开发者ID:12190143,项目名称:Theano,代码行数:17,代码来源:test_pfunc.py

示例10: test_default_scalar_container

 def test_default_scalar_container(self):
     # Similar in spirit to test_default_container, but updating a scalar
     # variable. This is a sanity check for non mutable types.
     x = shared(0.0, 'x')
     f = pfunc([], x)
     assert f() == 0
     x.set_value(x.get_value(borrow=True) + 1, borrow=True)
     assert f() == 1
开发者ID:12190143,项目名称:Theano,代码行数:8,代码来源:test_pfunc.py

示例11: test_default_updates_partial_graph

 def test_default_updates_partial_graph(self):
     a = shared(0)
     a.default_update = a + 1  # Increment a each time it is used
     b = 2 * a
     # Use only the tip of the graph, a is not used
     f = pfunc([b], b)
     assert a.get_value() == 0
     f(21)
     assert a.get_value() == 0
开发者ID:12190143,项目名称:Theano,代码行数:9,代码来源:test_pfunc.py

示例12: test_givens_replaces_shared_variable

    def test_givens_replaces_shared_variable(self):
        a = shared(1., 'a')
        a.default_update = a + 3.
        b = tensor.dscalar('b')
        c = a + 10
        f = pfunc([b], c, givens={a: b})

        assert len(f.maker.fgraph.inputs) == 1
        assert len(f.maker.fgraph.outputs) == 1
开发者ID:12190143,项目名称:Theano,代码行数:9,代码来源:test_pfunc.py

示例13: test_update_err_broadcast

    def test_update_err_broadcast(self):
        # Test that broadcastable dimensions raise error
        data = numpy.random.rand(10, 10).astype('float32')
        output_var = shared(name="output", value=data)

        # the update_var has type matrix, and the update expression
        # is a broadcasted scalar, and that should be allowed.
        self.assertRaises(TypeError, theano.function, inputs=[], outputs=[],
                          updates={output_var: output_var.sum().dimshuffle('x', 'x')})
开发者ID:12190143,项目名称:Theano,代码行数:9,代码来源:test_pfunc.py

示例14: test_default_updates_expressions

    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
开发者ID:12190143,项目名称:Theano,代码行数:19,代码来源:test_pfunc.py

示例15: test_no_shared_as_input

 def test_no_shared_as_input(self):
     """Test that shared variables cannot be used as function inputs."""
     w_init = numpy.random.rand(2, 2)
     w = shared(w_init.copy(), 'w')
     try:
         pfunc([w], theano.tensor.sum(w * w))
         assert False
     except TypeError as e:
         msg = 'Cannot use a shared variable (w) as explicit input'
         if str(e).find(msg) < 0:
             raise
开发者ID:12190143,项目名称:Theano,代码行数:11,代码来源:test_pfunc.py


注:本文中的theano.compile.shared函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。