1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
>>> import theano WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain` WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string. >>> import theano.tensor as T >>> from theano import function, pp, printing, scan, shared >>> import numpy as np >>> >>> >>> '''Define a function''' 'Define a function' >>> # 1. theano function ... x, y, a, b = T.fscalars('x', 'y', 'a', 'b') >>> a = x + y >>> b = x - y >>> f_add = theano.function(inputs=[x, y], outputs=a) >>> f_add_minus = theano.function(inputs=[x, y], outputs=[a, b]) >>> >>> x1 = 10. >>> y1 = 1. >>> a1 = f_add(x1, y1) >>> a2, b2 = f_add_minus(x1, y1) >>> print a1, a2, b2 11.0 11.0 9.0 >>> >>> # 2. python function ... def f_add(x, y): ... a = x + y ... return a ... >>> def f_add_minus(x, y): ... a = x + y ... b = x - y ... return a, b ... >>> x1 = 10. >>> y1 = 1. >>> a1 = f_add(x1, y1) >>> a2, b2 = f_add_minus(x1, y1) >>> print a1, a2, b2 11.0 11.0 9.0 >>> >>> >>> >>> ''' Derivatives ''' ' Derivatives ' >>> # Derivative ... x = T.scalar('x', dtype='float32') >>> y = x ** 2 >>> gy = T.grad(y, x) >>> f = theano.function([x], gy) >>> print f(4) 8.0 >>> >>> # partial derivative ... x = T.scalar('x', dtype='float32') >>> a = T.scalar('a', dtype='float32') >>> y = x * a >>> gy = T.grad(y, [x, a]) >>> f = theano.function([x, a], gy) >>> print f(4, 3) [array(3.0, dtype=float32), array(4.0, dtype=float32)] >>> >>> >>> ''' Debugging tricks ''' ' Debugging tricks ' >>> x = T.scalar('x',dtype='float32') >>> y = x ** 2 >>> gy = T.grad(y, x) >>> f = function([x], gy) >>> >>> # using pp ... pp(gy) # before optimization '((fill((x ** TensorConstant{2}), TensorConstant{1.0}) * TensorConstant{2}) * (x ** (TensorConstant{2} - TensorConstant{1})))' >>> pp(f.maker.fgraph.outputs[0]) # after optimization '(TensorConstant{2.0} * x)' >>> >>> # using printing.debugprinting ... printing.debugprint(gy) # before optimization Elemwise{mul} [id A] '' |Elemwise{mul} [id B] '' | |Elemwise{second,no_inplace} [id C] '' | | |Elemwise{pow,no_inplace} [id D] '' | | | |x [id E] | | | |TensorConstant{2} [id F] | | |TensorConstant{1.0} [id G] | |TensorConstant{2} [id F] |Elemwise{pow} [id H] '' |x [id E] |Elemwise{sub} [id I] '' |TensorConstant{2} [id F] |InplaceDimShuffle{} [id J] '' |TensorConstant{1} [id K] >>> printing.debugprint(f.maker.fgraph.outputs[0]) # after optimization Elemwise{mul,no_inplace} [id A] '' |TensorConstant{2.0} [id B] |x [id C] >>> >>> # printing.Print ... # recall that functions defined before do not print internal variables ... x = T.scalar('x',dtype='float32') >>> xp2 = x + 2 >>> xp2_printed = printing.Print('this is xp2:')(xp2) >>> xp2m2 = xp2_printed * 2 >>> f = function([x], xp2m2) >>> f(20) this is xp2: __str__ = 22.0 array(44.0, dtype=float32) >>> >>> # try to make this work ... x = T.scalar('x',dtype='float32') >>> y = x ** 2 >>> yprint = printing.Print('this is y:')(y) >>> gy = T.grad(yprint, x) >>> f = function([x], gy) >>> # alternatively: ... # p = printing.Print('y') ... # yprint = p(y) ... >>> >>> ''' shared variable ''' ' shared variable ' >>> state = shared(0.) >>> inc = T.iscalar('inc') >>> accumulator = function([inc], state, updates=[(state, state+inc)]) >>> print state.get_value() 0.0 >>> z = accumulator(10) >>> print z 0.0 >>> print state.get_value() 10.0 >>> >>> >>> ''' Loop ''' ' Loop ' >>> # python for loop ... import numpy >>> A = numpy.array([1, 2], dtype='float32') >>> k = 5 >>> result = [numpy.array([1,1])] >>> def mul(a, b): return a*b ... >>> for i in range(k): ... result.append(mul(result[-1], A)) ... >>> print result[-1] [ 1. 32.] >>> >>> # theano scan ... k = T.scalar("k", dtype='int32') >>> A = T.vector("A", dtype='float32') >>> def mul(a, b): return a*b ... >>> result, updates = theano.scan( ... fn=mul, ... outputs_info=T.ones_like(A), ... non_sequences=A, ... n_steps=k) >>> >>> power = theano.function( ... inputs=[A,k], ... outputs=result[-1], ... updates=updates) >>> >>> A_val = numpy.array([1, 2], dtype='float32') >>> k_val = 5 >>> r = power(A_val, 5) >>> print r [ 1. 32.] >>> >>> >>> # calculate polynomial ... # that is, a0 * x^0 + a1 * x^1 + a2 * x^2 + ... + an * x^n ... coefficients = theano.tensor.vector("coefficients", dtype='float32') >>> x = T.scalar("x", dtype='float32') >>> >>> max_coefficients_supported = 10000 >>> >>> def cumulative_poly(coeff, power, prior_sum, x): ... return prior_sum + coeff * (x ** power) ... >>> # Generate the components of the polynomial ... zero = np.asarray(0., dtype='float32') >>> full_range=theano.tensor.arange(max_coefficients_supported, dtype='float32') >>> results, updates = theano.scan(fn=cumulative_poly, ... outputs_info=T.as_tensor_variable(zero), ... sequences=[coefficients, full_range], ... non_sequences=x) >>> >>> polynomial = results[-1] >>> calculate_polynomial = theano.function(inputs=[coefficients, x], ... outputs=polynomial) >>> >>> test_coeff = np.asarray([1, 0, 2], dtype=np.float32) >>> print(calculate_polynomial(test_coeff, 3)) 19.0
|