Exemplo n.º 1
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.º 2
0
#encoding:UTF-8
__author__ = 'auroua'
import numpy as np
import theano_test
import theano_test.tensor as T
rng = np.random

N = 400
feats = 784
D = (rng.randn(N,feats),rng.randint(size=N,low=0,high=2))
training_steps = 10000

# 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],
Exemplo n.º 3
0
__author__ = 'auroua'
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))
training_steps = 10000

# Declare Theano symbolic variables
x = T.matrix("x")
y = T.vector("y")
w = theano_test.shared(rng.randn(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]
#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(