In [40]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import theano
import theano.tensor as T
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams

In [2]:
x = T.scalar()
y = T.scalar()
z = x + y

In [3]:
fntSum = theano.function(inputs=[x, y], outputs=z)

In [4]:
r1 = fntSum(2, 3)
print r1


5.0

In [5]:
# data type
x = T.scalar() # default
x = T.iscalar() # int32
x = T.dscalar() # float64

x = T.vector() # 1-D
x = T.matrix() # 2-D
x = T.tensor3() # 3-D
x = T.tensor4() # 4-D

In [6]:
# default datatype
print theano.config.device
print theano.config.floatX


cpu
float64

In [7]:
x = T.matrix()
y = T.matrix()
z = T.dot(x, y)
fntSumMat = theano.function([x, y], z)

In [8]:
x = np.random.random((3,3))
y = np.random.random((3,3))

x = np.array(x, dtype=theano.config.floatX)
y = np.array(y, dtype=theano.config.floatX)

print fntSumMat(x, y)


[[ 0.89720252  0.96093883  1.19207892]
 [ 0.65795941  0.72528811  0.91326167]
 [ 1.02690925  1.07333755  1.31240306]]

In [9]:
# shared variable
# shared variable can't be input or output
t = np.array([[1,2], [3,4]], dtype=theano.config.floatX)
t = theano.shared(t)
# t = theano.shared(t, borrow=True) # default : false 
print t
print t.get_value()
t.set_value(t.get_value() + 1)
print t.get_value()


<TensorType(float64, matrix)>
[[ 1.  2.]
 [ 3.  4.]]
[[ 2.  3.]
 [ 4.  5.]]

In [11]:
# differentiation
x = T.scalar()
y = x ** 2
diff = theano.function([x], T.grad(y, [x]))
for i in range(5):
    print "input : %s, output : %s" %(i, diff(i))


input : 0, output : [array(0.0)]
input : 1, output : [array(2.0)]
input : 2, output : [array(4.0)]
input : 3, output : [array(6.0)]
input : 4, output : [array(8.0)]

In [12]:
# differentiation
x = T.scalar()
y = x ** 2
diff = theano.function([x], T.grad(y, [x])[0])
for i in range(5):
    print "input : %s, output : %s" %(i, diff(i))


input : 0, output : 0.0
input : 1, output : 2.0
input : 2, output : 4.0
input : 3, output : 6.0
input : 4, output : 8.0

In [13]:
# evaluate & update
x = T.scalar()
w = theano.shared(np.array(3, dtype=theano.config.floatX), borrow=True)
obj = (1 -x*w)**2

# 0.1 is learning rate
learn_w = (w, w-0.1*T.grad(obj, w))
learn = theano.function([x], obj, updates=[learn_w])

for i in range(10):
    print "input : %s, w : %s, output : %s" %(i, w.get_value(), learn(i))


input : 0, w : 3.0, output : 1.0
input : 1, w : 3.0, output : 4.0
input : 2, w : 2.6, output : 17.64
input : 3, w : 0.92, output : 3.0976
input : 4, w : -0.136, output : 2.383936
input : 5, w : 1.0992, output : 20.214016
input : 6, w : -3.3968, output : 457.13860864
input : 7, w : 22.26016, output : 23969.5791981
input : 8, w : -194.489408, output : 2423985.13928
input : 9, w : 2296.5750144, output : 427173463.188

In [25]:
np.random.randn(*(2,2))


Out[25]:
array([[ 0.82764089, -2.00598699],
       [ 1.01048744, -1.46717392]])

In [30]:
x = T.matrix() # 2-D
y = T.matrix() # 2-D

f_dot = theano.function([x, y], T.dot(x, y))

x = np.array([[1, 1], [1, 1]], dtype=theano.config.floatX)
y = np.array([[1, 1], [1, 1]], dtype=theano.config.floatX)

print f_dot(x, y)


[[ 2.  2.]
 [ 2.  2.]]

In [32]:
x = T.matrix() # 2-D
y = T.matrix() # 2-D

f_sigmoid = theano.function([x, y], T.nnet.sigmoid(T.dot(x, y)))

x = np.array([[1, 1], [1, 1]], dtype=theano.config.floatX)
y = np.array([[1, 1], [1, 1]], dtype=theano.config.floatX)

print f_sigmoid(x, y)


[[ 0.88079708  0.88079708]
 [ 0.88079708  0.88079708]]

In [41]:
x = T.vector()

f_softmax = theano.function([x], T.nnet.softmax(x))

x = range(10)

print f_softmax(x)


[[  7.80134161e-05   2.12062451e-04   5.76445508e-04   1.56694135e-03
    4.25938820e-03   1.15782175e-02   3.14728583e-02   8.55520989e-02
    2.32554716e-01   6.32149258e-01]]

In [42]:
srng = RandomStreams()

In [70]:
x = T.matrix()

p = 0.5
retain_prob = 1 - p

f_binomia = theano.function([x], srng.binomial(x.shape, p=retain_prob, dtype=theano.config.floatX))

In [72]:
x = np.array(np.random.random((10, 10)), dtype=theano.config.floatX)

r = f_binomia(x)
print np.unique(r, return_counts=True)


(array([ 0.,  1.]), array([ 7, 93]))

In [68]:



(array([ 0.,  1.]), array([11, 14]))