Exemplo n.º 1
0
# Declare Theano symbolic variables
x = T.matrix("x")
y = T.vector('y')
w = theano_test.shared(rng.randn(feats),name='w')
b = theano_test.shared(0.,name='b')
print 'Initial model:'
print w.get_value(),b.get_value()

# Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b))   # Probability that target = 1
prediction = p_1 > 0.5                    # The prediction thresholded
xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function
cost = xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize
gw, gb = T.grad(cost, [w, b])             # Compute the gradient of the cost
                                          # (we shall return to this in a
                                          # following section of this tutorial)
# Compile
train = theano_test.function(
          inputs=[x,y],
          outputs=[prediction, xent],
          updates=((w, w - 0.1 * gw), (b, b - 0.1 * gb)))
predict = theano_test.function(inputs=[x], outputs=prediction)

# Train
for i in range(training_steps):
    pred, err = train(D[0], D[1])

print "Final model:"
print w.get_value(), b.get_value()
print "target values for D:", D[1]
print "prediction on D:", predict(D[0])
Exemplo n.º 2
0
#encoding:UTF-8
__author__ = 'auroua'
import theano_test
from theano_test import pp
from theano_test import function
import theano_test.tensor as T
x = T.dscalar('x')
y = x**2
gy = T.grad(y,x)
f = function([x],y)

print f(4)


x2 = T.dmatrix('x2')
s = T.sum(1/(1+T.exp(-x2)))
gs = T.grad(s,x2)
dlogistic = function([x2],gs)
print dlogistic([[0,1],[-1,-2]])

x3 = T.dvector('x3')
y3 = x3**2
J,updates = theano_test.scan(lambda i,y,x:T.grad(y[i],x),sequences=T.arange(y3.shape[0]),non_sequences=[y3,x3])
f = function([x3],J,updates=updates)
print f([4,4])

x4 = T.dvector('x4')
y4 = x4**2
cost = y4.sum()
gy4 = T.grad(cost,x4)
H,updates2 = theano_test.scan(lambda i,gy,x4:T.grad(gy[i],x4),sequences=T.arange(gy4.shape[0]),non_sequences=[gy4,x4])
Exemplo n.º 3
0
__author__ = 'auroua'
from theano_test import function, config, shared, sandbox
import theano_test.sandbox.cuda.basic_ops
import theano_test.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], sandbox.cuda.basic_ops.gpu_from_host(T.exp(x)))
print f.maker.fgraph.toposort()
t0 = time.time()
for i in xrange(iters):
    r = f()
t1 = time.time()
print 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
print 'Numpy result is', numpy.asarray(r)
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print 'Used the cpu'
else:
    print 'Used the gpu'
Exemplo n.º 4
0
__author__ = 'auroua'

import theano_test.tensor as T
from theano_test import function
import theano_test
import pydot

print pydot.find_graphviz()

x = T.dmatrix('x')
y = x*2

print type(y.owner)
print y.owner.op.name
print len(y.owner.inputs)
print type(y.owner.inputs[1].owner)

#apply nodes are those that define which computations the graph does
# When compiling a Theano function, what you give to the theano.function is actually a graph
# (starting from the output variables you can traverse the graph up to the input variables).
# While this graph structure shows how to compute the output from the input,
# it also offers the possibility to improve the way this computation is carried out.

a = T.vector('a')
b = a+a**10
fab = function([a],b)
print fab([0,1,2])

theano_test.printing.pydotprint(b, outfile="/home/auroua/symbolic_graph_unopt.png", var_with_name_simple=True)
theano_test.printing.pydotprint(fab, outfile="/home/auroua/symbolic_graph_opt.png", var_with_name_simple=True)
Exemplo n.º 5
0
x.tag.test_value = D[0]
y.tag.test_value = D[1]
#print "Initial model:"
#print w.get_value(), b.get_value()

# Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w)-b)) # Probability of having a one
prediction = p_1 > 0.5 # The prediction that is done: 0 or 1
xent = -y*T.log(p_1) - (1-y)*T.log(1-p_1) # Cross-entropy
cost = xent.mean() + 0.01*(w**2).sum() # The cost to optimize
gw,gb = T.grad(cost, [w,b])

# Compile expressions to functions
train = theano_test.function(
            inputs=[x,y],
            outputs=[prediction, xent],
            updates={w:w-0.01*gw, b:b-0.01*gb},
            name = "train")
predict = theano_test.function(inputs=[x], outputs=prediction,
            name = "predict")

if any([x.op.__class__.__name__ in ['Gemv', 'CGemv', 'Gemm', 'CGemm'] for x in
        train.maker.fgraph.toposort()]):
    print 'Used the cpu'
elif any([x.op.__class__.__name__ in ['GpuGemm', 'GpuGemv'] for x in
          train.maker.fgraph.toposort()]):
    print 'Used the gpu'
else:
    print 'ERROR, not able to tell if theano used the cpu or the gpu'
    print train.maker.fgraph.toposort()
Exemplo n.º 6
0
import numpy
import theano_test
import theano_test.tensor as T

rng = numpy.random
N = 400
feats = 784
D = (rng.randn(N,feats).astype(theano_test.config.floatX),rng.randint(size=N,low=0,high=2).astype(theano_test.config.floatX))
traing_steps = 10000

x = T.dmatrix('x')
y = T.vector('y')
w = theano_test.shared(rng.rand(feats).astype(theano_test.config.floatX),name='w')
b = theano_test.shared(numpy.asarray(0.,dtype=theano_test.config.floatX),name='b')
x.tag.test_value = D[0]
y.tag.test_value = D[1]

p_1 = 1/(1+T.exp(-T.dot(x,w)-b))
prediction = p_1>0.5

xent = -y*T.log(p_1) - (1-y)*T.log(1-p_1)
cost = xent.mean() + 0.01*(w**2).sum()

gw,gb = T.grad(cost, [w,b])

train = theano_test.function(inputs=[x,y], outputs=[prediction, xent], updates=[[w, w-0.01*gw], [b, b-0.01*gb]], name = "train")
predict = theano_test.function(inputs=[x], outputs=prediction, name = "predict")

print theano_test.printing.pprint(prediction)
print theano_test.printing.debugprint(prediction)
print theano_test.printing.debugprint(predict)
Exemplo n.º 7
0
__author__ = 'auroua'
from theano_test import function, config, shared, sandbox
import theano_test.tensor as T
import numpy
import time
import theano_test

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print f.maker.fgraph.toposort()
t0 = time.time()
for i in xrange(iters):
    r = f()
t1 = time.time()
print 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print 'Used the cpu'
else:
    print 'Used the gpu'


#print theano.config
Exemplo n.º 8
0
instances = 10
draws = np.random.randint(0,2,size=(instances,step))
walk = np.where(draws>0,1,-1)
#print walk
walks = walk.cumsum(1)
print walks

hist3 = (np.abs(walks)>=3).any(1)
print hist3

crossing_time = np.abs(walks[hist3]).argmax(1)
crossing_time2 = (np.abs(walks[hist3])>=3).argmax(1)
print crossing_time
print crossing_time2

print  crossing_time.mean()
print  crossing_time2.mean()

import theano_test
from theano_test import tensor
print theano_test.__version__

a = tensor.dscalar()
b = tensor.dscalar()

c = a + b

f = theano_test.function([a,b],c)

print f
assert 4.0 == f(1.5,2.5)
Exemplo n.º 9
0
__author__ = "auroua"
import theano_test.tensor as T
from theano_test import function
from theano_test import pp

x = T.dscalar("x")
y = T.dscalar("y")
z = x + y
f = function([x, y], z)
f(2, 3)
z.eval({x: 16.3, y: 14.3})
print z
print pp(z)

xm = T.dmatrix("xm")
ym = T.dmatrix("ym")
zm = xm + ym
f2 = function((xm, ym), zm)

f2(np.array([[1, 2], [2, 3]]), np.array([[3, 4], [4, 5]]))

xv = T.dvector("xv")
yv = T.dvector("yv")
zv = xv ** 2 + yv ** 2 + 2 * xv * yv
fv = function((xv, yv), zv)
print pp(zv)
print fv([1, 2], [3, 4])
Exemplo n.º 10
0
#encoding:UTF-8
__author__ = 'auroua'
from theano_test import pp
import theano_test.tensor as T
from theano_test import function

#简单标量函数的求导
x = T.dscalar('x')
y = x ** 2
gy = T.grad(y,x)
print pp(gy)
f = function([x],gy)
print f(4)
print f(94.2)
print pp(f.maker.fgraph.outputs[0])

#sigmodi的求导
xs = T.dmatrix('x')
y = T.sum(1/(1+T.exp(-xs)))
gs = T.grad(y,xs)
dlogistic = function([xs],gs)
print dlogistic([[0, 1], [-1, -2]])