"""
Basic usage of theano constructs.
"""
import numpy
import theano
from theano import tensor
from theano import function
# let's defines two symbols (or variables):
x = tensor.dscalar('x')
y = tensor.dscalar('y')
# a dscalar is a 0 dimensional tensor
# the argument to dscalar is the name of the variable,
# which you can leave out.
# there are several types of scalars:
# dscalar float64
# fscalar float32
# iscalar int32
# the above is a shortcut fore:
x = tensor.scalar('x', dtype = 'float64')
# to see what type of object we created:
type(x)
x.type
z = x + y
from theano import pp
pp(z)
# let's create a function
f = function([x, y], z)
# this compiles the expression into someting
# that can be executed:
f(2, 3)
# we like dot products:
x = tensor.dvector('x')
y = tensor.dvector('y')
dot_symbolic = tensor.dot(x, y)
dot = function([x,y], dot_symbolic)
dot([1,1,-1], [1,1,1])
# the discriminant function for classifier:
# shared variables are useful for storing the weights of a neural network
# theano will automatically try to put such variables on a GPU if one is available.
w = theano.shared(value=numpy.array([1.0, 1.0]), name='w')
b = theano.shared(value=2.0, name='b')
x = tensor.dvector('x')
discriminant_symbolic = tensor.dot(w, x) + b
discriminant = function([x], discriminant_symbolic)
discriminant([1, 1])