Theano Study Notes (1) -- Algebra

Source: Internet
Author: User
Tags theano

Scalar Addition

import theano.tensor as Tfrom theano import functionx = T.dscalar('x')y = T.dscalar('y')z = x + yf = function([x, y], z)

The input defines two symbol variables to replace the value. The output is a zero-dimensional numpy. ndarray array.

 

Matrix Addition

Change the input type. If the dimension of the matrix is different, the numpy broadcast rule is followed.

import theano.tensor as Tfrom theano import functionx = T.dmatrix('x')y = T.dmatrix('y')z = x + yf = function([x, y], z)

 

Define a formula such as a ** 2 + B ** 2 + 2 * a * B

Each variable must be declared separately.

import theanoa = theano.tensor.vector()b = theano.tensor.vector()out = a ** 2 + b ** 2 + 2 * a * bf = theano.function([a,b],out)print f([0, 1],[1,2])>>> [ 1. 9.]

 

Support for multiple outputs

import theano.tensor as Tfrom theano import functiona, b = T.dmatrices('a', 'b')diff = a - babs_diff = abs(diff)diff_squared = diff**2f = function([a, b], [diff, abs_diff,diff_squared])print f([[1, 1], [1, 1]], [[0, 1], [2,3]])>>> [array([[ 1.,  0.],      [-1., -2.]]), array([[ 1.,  0.],      [ 1.,  2.]]), array([[ 1.,  0.],      [ 1.,  4.]])]

 

Set default parameters

Like standard Python, the default parameter must be non-default, and the default variable name can also be defined.

import theano.tensor as Tfrom theano import functionfrom theano import Paramx, y = T.dscalars('x', 'y')z = x + yf = function([x, Param(y, default=1,name='by_name')],z)print f(33)print f(33, 2)print f(33,by_name=3)>>> 34.035.036.0

 

Shared variable

To provide better performance on the GPU, the sharing variable is introduced. The example uses the accumulators.

import theano.tensor as Tfrom theano import functionfrom theano import sharedstate = shared(0)inc = T.iscalar('inc')accumulator = function([inc], state,updates=[(state, state+inc)])print state.get_value()accumulator(1)print state.get_value()accumulator(300)print state.get_value()state.set_value(-1)print accumulator(3)print state.get_value()>>> 01301-12

The state value is refreshed only after the function is called. In addition, multiple functions can be defined to share the same shared variable, such as the subtraction operator.

decrementor = function([inc], state,updates=[(state, state-inc)])print decrementor(2)print state.get_value()>>> 20

If a function shares the shared variable but does not want to change its value, you can use the given parameter to replace the variable. The old state does not change.

fn_of_state = state * 2 + incfoo = T.scalar(dtype=state.dtype)skip_shared = function([inc, foo],fn_of_state,                           givens=[(state,foo)])print skip_shared(1, 3)print state.get_value()>>> 70

 

Generate random number

Like srand () in C, they are pseudo-random numbers.

From theano import functionfrom theano. tensor. shared_randomstreamsimport randomstreamssrng = randomstreams (seed = 234) # seed rv_u = SRNG. uniform (2, 2) # uniformly distributed rv_n = SRNG. normal (2, 2) # normal distribution F = function ([], rv_u) # every call, G = function ([], rv_n, no_default_updates = true) will be updated each time) # If this set of random numbers is used all the time, nearly_zeros = function ([], rv_u + rv_u-2 * rv_u) print nearly_zeros () # The function obtains only one random number at a time, even if the expression contains three random numbers

Seed stream: These two random variables can be set globally for the same seed or separately.

# Set separately and use. RNG. set_value () function rng_val = rv_u.rng.get_value (borrow = true) # Get the RNG for rv_urng_val.seed (89234) # seeds partition (rng_val, borrow = true) # global settings, use. seed () function SRNG. seed (902340)

Shared stream between functions

State_after_v0 = rv_u.rng.get_value (). get_state () # Save the statenearly_zeros () # This affects rv_u's generatorv1 = f () # The first call before the call. Then the state changes to RNG = rv_u.rng.get_value (borrow = true) RNG. set_state (state_after_v0) # restore rv_u.rng.set_value (RNG, borrow = true) for its State v2 = f () # V2! = Random number of state corresponding to V1 output update V3 = f () # V3 = V1 updated again and restored to the original state

 

Copy status between two theano Images

Import theanoimport numpyimport theano. tensor as tfrom theano. sandbox. rng_mrg importmrg_randomstreamsfrom theano. tensor. shared_randomstreamsimport randomstreams class graph (): def _ init _ (self, seed = 123): Self. RNG = randomstreams (SEED) self. y = self. RNG. uniform (size = (1,) G1 = graph (seed = 123) F1 = theano. function ([], g1.y) G2 = graph (seed = 987) F2 = theano. function ([], g2.y) print 'by default, the TW O functionsare out of sync. 'print 'f1 () returns ', F1 () print 'f2 () returns', F2 () # output different random values def copy_random_state (G1, G2 ): if isinstance (g1.rng, mrg_randomstreams): # type judgment: the first parameter is an object, and the second is a list of type names or type names. The return value is boolean. G2.rng. rstate = g1.rng. rstate for (su1, su2) in zip (g1.rng. state_updates, g2.rng. state_updates): # package su2 [0]. set_value (su1 [0]. get_value () # assign the value 'we now copy the state of thetheano random number generators. 'Copy _ random_state (G1, G2) print 'f1 () returns ', F1 () print 'f2 () returns', F2 () # output the same random value >>> by default, the two functions are outof sync. f1 () Returns [0.72803009] F2 () Returns [0.55056769] We now copy the state of the theanorandom number generators. f1 () Returns [1, 0.59044123] F2 () Returns [2, 0.59044123]


Welcome to the discussion and follow up on this blog, Weibo, and zhihu personal homepage for further updates ~

Reprinted, please respect the work of the author and keep the above text and link of the article completely. Thank you for your support!

Theano Study Notes (1) -- Algebra

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.