Example #1
0
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],
          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):
Example #2
0
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(
            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
Example #3
0
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])
f2 = function([x4],H,updates=updates2)
print f2([4,4])

W = T.dmatrix('W')
V = T.dmatrix('V')
xx = T.dvector('xx')
yy = T.dot(xx,W)
JV = T.Rop(yy,W,V)
fwv = theano_test.function([W,V,xx],JV)
print fwv([[1, 1], [1, 1]], [[2, 2], [2, 2]], [0,1])
Example #4
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)