import theano
import theano.tensor as T
gradient = T.grad(cost, parameter)
x = T.scalar('x' )
x = T.scalar('x' )
y = T.scalar('y' )
x = T.scalar('x' )
y = T.scalar('y' )
z = x + y
x = T.scalar('x' )
y = T.scalar('y' )
z = x + y
x = T.scalar()
y = T.scalar()
z = x + y
f = theano.function([x,y],z)
f(2,8) # 10
x = T.vector('x' )
W = T.matrix('weights' )
b = T.vector('bias' )
z = T.nnet.softmax(T.dot(x,W) + b)
f = theano.function([x,W,b],z)
a = T.vector()
b = f(a)
c = g(b)
d = h(c)
full_fun = theano.function([a],d) # h(g(f(a)))
part_fun = theano.function([c],d) # h(c)
x = T.scalar()
y = x ** 2
x = T.scalar()
y = x ** 2
g = T.grad(y, x) # 2*x
initial_W = uniform(-k,k,(n_in, n_out))
W = theano.shared(value=initial_W , name="W" )
prediction = T.dot(x,W ) + b
cost = T.sum((prediction - target)**2)
gradient = T.grad (cost, W )
gradient = T.grad(cost, W )
update_list = [(W , W - lr * gradient )]
f = theano.function(
[x,y,lr],[cost],
updates=update_list )
# this updates W
f (minibatch_x, minibatch_y, learning_rate)
index = T.iscalar()
X = theano.shared(data['X' ])
Y = theano.shared(data['Y' ])
f = theano.function(
[index,lr],[cost],
updates=update_list,
givens ={x:X [index], y:Y [index]})
X[idx :idx+n ]
# activate the testing
theano.config.compute_test_value = 'raise'
x = T.matrix()
x.tag.test_value = numpy.ones((mbs, n_in))
y = T.vector()
y.tag.test_value = numpy.ones((mbs,))
from theano.printing import Print
a = T.nnet.sigmoid(h)
# this prints "a:", a.__str__ and a.shape
a = Print ("a" ,["__str__" ,"shape" ])(a)
b = something(a)
theano.function([...],
[..., some_node ])
b = a.reshape((n,m,p))
# b[i,k,j] == a[i,j,k]
b = a.dimshuffle(0,2,1)
# a.shape == (n,m)
b = a.dimshuffle(0,'x' ,1)
# or
b = a.reshape([n,1,m])
f = theano.function(..., profile=True )
Class
---
<% time> < sum %>< apply time>< time per call>< type><#call> <#apply> < Class name>
30.4% 30.4% 10.202s 5.03e-05s C 202712 4 theano.sandbox.cuda.basic_ops.GpuFromHost
23.8% 54.2% 7.975s 1.31e-05s C 608136 12 theano.sandbox.cuda.basic_ops.GpuElemwise
18.3% 72.5% 6.121s 3.02e-05s C 202712 4 theano.sandbox.cuda.blas.GpuGemv
6.0% 78.5% 2.021s 1.99e-05s C 101356 2 theano.sandbox.cuda.blas.GpuGer
4.1% 82.6% 1.368s 2.70e-05s Py 50678 1 theano.tensor.raw_random.RandomFunction
3.5% 86.1% 1.172s 1.16e-05s C 101356 2 theano.sandbox.cuda.basic_ops.HostFromGpu
3.1% 89.1% 1.027s 2.03e-05s C 50678 1 theano.sandbox.cuda.dnn.GpuDnnSoftmaxGrad
3.0% 92.2% 1.019s 2.01e-05s C 50678 1 theano.sandbox.cuda.nnet.GpuSoftmaxWithBias
2.8% 94.9% 0.938s 1.85e-05s C 50678 1 theano.sandbox.cuda.basic_ops.GpuCAReduce
2.4% 97.4% 0.810s 7.99e-06s C 101356 2 theano.sandbox.cuda.basic_ops.GpuAllocEmpty
0.8% 98.1% 0.256s 4.21e-07s C 608136 12 theano.sandbox.cuda.basic_ops.GpuDimShuffle
0.5% 98.6% 0.161s 3.18e-06s Py 50678 1 theano.sandbox.cuda.basic_ops.GpuFlatten
0.5% 99.1% 0.156s 1.03e-06s C 152034 3 theano.sandbox.cuda.basic_ops.GpuReshape
0.2% 99.3% 0.075s 4.94e-07s C 152034 3 theano.tensor.elemwise.Elemwise
0.2% 99.5% 0.073s 4.83e-07s C 152034 3 theano.compile.ops.Shape_i
0.2% 99.7% 0.070s 6.87e-07s C 101356 2 theano.tensor.opt.MakeVector
0.1% 99.9% 0.048s 4.72e-07s C 101356 2 theano.sandbox.cuda.basic_ops.GpuSubtensor
0.1% 100.0% 0.029s 5.80e-07s C 50678 1 theano.tensor.basic.Reshape
0.0% 100.0% 0.015s 1.47e-07s C 101356 2 theano.sandbox.cuda.basic_ops.GpuContiguous
... (remaining 0 Classes account for 0.00%(0.00s) of the runtime)
24.1% 24.1% 4.537s 1.59e-04s 28611 2 GpuFromHost(x)
x = T.vector('x' )
n = T.scalar('n' )
def inside_loop (x_t, acc, n):
return acc + x_t * n
values, _ = theano.scan(
fn = inside_loop ,
sequences=[x],
outputs_info=[T.zeros(1)],
non_sequences=[n],
n_steps=x.shape[0])
sum_of_n_times_x = values[-1 ]
def inside_loop (x_t, acc, n):
return acc + x_t * n
f(seq_0[t], seq_1[t], .., out_0[t-1], out_1[t-1], .., other_0, other_1, ..):
values, _ = theano.scan(
# ...
sum_of_n_times_x = values[-1 ]
values = [ [out_0[1], out_0[2], ...],
[out_1[1], out_1[2], ...],
...]
fn = inside_loop ,
sequences=[x],
outputs_info=[T.zeros(1)],
outputs_info=[None, out_1[0], out_2[0], ...],
non_sequences=[n],
n_steps=x.shape[0])
x = T.vector('x' )
n = T.scalar('n' )
def inside_loop (x_t, acc, n):
return acc + x_t * n
values, _ = theano.scan(
fn = inside_loop ,
sequences=[x],
outputs_info=[T.zeros(1)],
non_sequences=[n],
n_steps=x.shape[0])
sum_of_n_times_x = values[-1 ]
def loop (x_t , h_tm1 , W_x, W_h, b_h ):
return T.tanh(T.dot(x_t,W_x) +
T.dot(h_tm1, W_h) +
b_h)
values,_ = theano.scan(loop ,
[x ], [T.zeros(n_hidden) ], parameters )
y_hat = T.nnet.softmax(values[-1])
# shape: (batch size, sequence length, k)
x = T.tensor3('x' )
# define loop ...
v,u = theano.scan(loop,
[x.dimshuffle(1,0,2)],
...)
# x.shape: (batch size, n channels, height, width)
# W.shape: (n output channels, n input channels,
# filter height, filter width)
output = T.nnet.conv.conv2d(x, W)
# x.shape: (batch size, n channels, height, width)
x = x.reshape((mbsize, 32, 32, 3))
x = x.dimshuffle(0,3,1,2)
# W.shape: (n output channels, n input channels,
# filter height, filter width)
W = theano.shared(randoms((16,3,5,5)),
name='W-conv' )
output_1 = T.nnet.conv.conv2d(x, W)
W = theano.shared(randoms((32,16,5,5)),
name='W-conv-2' )
output_2 = T.nnet.conv.conv2d(output_1, W)
# output_2.shape: (batch size, 32, 24, 24)
from theano.tensor.downsample import max_pool_2d
# output_2.shape: (batch size, 32, 24, 24)
pooled = max_pool_2d(output_2, (2,2))
# pooled.shape: (batch size, 32, 12, 12)
flattened = conv_output_n.flatten(ndim=2)
# then feed `flattened` to a normal hidden layer
class HiddenLayer :
def __init__ (self, x, n_in, n_hidden):
self.W = shared(...)
self.b = shared(...)
self.output = activation(T.dot(x,W)+b)
class HiddenLayer :
def __init__ (self, x, n_in, n_hidden):
# ...
self.params = [self.W, self.b]
def save_params (self):
return [i.get_value() for i in self.params]
def load_params (self, values):
for p, value in zip (self.params, values):
p.set_value(value)
import cPickle as pickle
# save
pickle.dump(model.save_params(),
file('model_params.pkl' , 'w' )
# load
model.load_params(
pickle.load(
file('model_params.pkl' ,'r' )))
ValueError: GpuElemwise. Input dimension mis-match. Input 1 (indices
start at 0) has shape[1] == 256, but the output's size on that axis is 128.
Apply node that caused the error: GpuElemwise{add,no_inplace}
(<CudaNdarrayType(float32, matrix)>,
<CudaNdarrayType(float32, matrix)>)
Inputs types: [CudaNdarrayType(float32, matrix),
CudaNdarrayType(float32, matrix)]
TensorType(float32, matrix) cannot store a value of dtype float64 without risking loss of precision. If you do not mind this loss, you can: 1) explicitly cast your data to float32, or 2) set "allow_input_downcast=True" when calling "function".