提交 b8a24137 authored 作者: James Bergstra's avatar James Bergstra

updated autoencoder benchmark

上级 3679d8e6
aa.x : aa.cc
g++ -O3 -ffast-math aa.cc -o aa.x -L${PUB_PREFIX}/lib -lgsl -lcblas -lgoto -lgfortran -lm
clean :
rm aa.x
/*
*
* g++ -O2 -ffast-math -I$PUB_PREFIX/include aa.cc -o aa.x -lgsl -lgslcblas
*
* g++ -O2 -ffast-math -I$PUB_PREFIX/include aa.cc -o aa.x -L$PUB_PREFIX/lib -lgsl -lcblas -lgoto -lgfortran
*
* ./aa.x 10 5 7 1000
*
* */
#include <cassert> #include <cassert>
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
...@@ -5,15 +14,22 @@ ...@@ -5,15 +14,22 @@
#include <gsl/gsl_rng.h> #include <gsl/gsl_rng.h>
#include <gsl/gsl_blas.h> #include <gsl/gsl_blas.h>
typedef float real; #include <time.h>
#include <sys/time.h>
double pytime(const struct timeval * tv)
{
return (double) tv->tv_sec + (double) tv->tv_usec / 1000000.0;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
assert(argc == 4); assert(argc == 5);
int neg = strtol(argv[1], 0, 0); int neg = strtol(argv[1], 0, 0);
int nout = strtol(argv[2], 0, 0); int nout = strtol(argv[2], 0, 0);
int nhid = strtol(argv[3], 0, 0); int nhid = strtol(argv[3], 0, 0);
int niter = strtol(argv[4], 0, 0);
double lr = 0.01; double lr = 0.01;
gsl_rng * rng = gsl_rng_alloc (gsl_rng_taus); gsl_rng * rng = gsl_rng_alloc (gsl_rng_taus);
gsl_rng_set(rng, 234); gsl_rng_set(rng, 234);
...@@ -41,8 +57,11 @@ int main(int argc, char **argv) ...@@ -41,8 +57,11 @@ int main(int argc, char **argv)
// //
// //
struct timeval tv0, tv1;
gettimeofday(&tv0, 0);
double err = 0.0; double err = 0.0;
for (int iter = 0; iter < 1000; ++iter) for (int iter = 0; iter < niter; ++iter)
{ {
gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, x, w, 0.0, xw); gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, x, w, 0.0, xw);
...@@ -87,8 +106,9 @@ int main(int argc, char **argv) ...@@ -87,8 +106,9 @@ int main(int argc, char **argv)
} }
} }
gettimeofday(&tv1, 0);
fprintf(stdout, "%lf\n", 0.5 * err); fprintf(stdout, "took = %lfs to get err %lf\n", pytime(&tv1) - pytime(&tv0), 0.5 * err);
//skip freeing //skip freeing
return 0; return 0;
} }
......
#!/usr/bin/env python2.5
from __future__ import absolute_import
import numpy
import sys
import time
import theano
import theano.tensor as T
import theano.sandbox
import theano.sandbox.wraplinker
from theano.compile import module
if 0:
class Opt(object):
merge = theano.gof.MergeOptimizer()
gemm_opt_1 = theano.gof.TopoOptimizer(theano.tensor_opt.gemm_pattern_1)
gemm_opt_2 = theano.gof.TopoOptimizer( # d -= a * (dot()+transpose(dot))
theano.gof.PatternSub(
(
T.sub_inplace,
'd',
(
T.mul,
dict(pattern = (T.DimShuffle((), ['x', 'x'], inplace = True), 'a'),
allow_multiple_clients = True),
(
T.add,
(T.dot, 'b', 'c'),
(T.transpose_inplace, (T.dot, 'f', 'g'))
)
)
),
(
T.gemm,
(
T.gemm,
'd',
(T.neg, 'a'),
(T.transpose_inplace, 'g'),
(T.transpose_inplace, 'f'),
T.constant(1.0)
),
(T.neg, 'a'),
'b',
'c',
T.constant(1.0)
),
allow_multiple_clients = False))
sqr = []
sqr.append( theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.mul,'x', 'x'),
(T.sqr, 'x'), allow_multiple_clients=True)))
sqr.append(theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.pow, 'x', (T.DimShuffle((), ['x', 'x'], inplace=True), T.constant(2))),
(T.sqr, 'x'), allow_multiple_clients=True)))
ident_opt_list = []
ident_opt_list.append( # remove explicit copies
theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.tensor_copy, 'x'),
'x',
allow_multiple_clients=True)))
ident_opt_list.append( # remove double-transpose
theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.transpose_inplace, (T.transpose_inplace, 'x')),
'x',
allow_multiple_clients=True)))
ident_opt_list.append(
theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.sqr, (T.sqrt,'x')),
'x',
allow_multiple_clients=True)))
ident_opt_list.append(
theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.sqrt, (T.sqr,'x')),
'x',
allow_multiple_clients=True)))
ident_opt_list.append(
theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.mul, 'x', (T.div,'y', 'x')),
'y',
allow_multiple_clients=True)))
ident_opt_list.append(
theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.mul, (T.div,'y', 'x'), 'x'),
'y',
allow_multiple_clients=True)))
ident_opt_list.append(
theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.div, (T.mul,'y', 'x'), 'x'),
'y',
allow_multiple_clients=True)))
ident_opt_list.append(
theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.div, (T.mul,'y', 'x'), 'y'),
'x',
allow_multiple_clients=True)))
def __call__(self, env):
self.merge(env)
#eliminate identities
if 0:
print 'SKIPPING optimizations'
else:
for opt in self.ident_opt_list:
opt(env)
for opt in self.sqr:
opt(env)
self.gemm_opt_1(env)
self.gemm_opt_2(env)
self.merge(env)
def linker(print_prog=False):
if 1:
print 'wtf?'
#return theano.gof.OpWiseCLinker()
imap = {None:'-'}
def blah(i, node, thunk):
imap[node] = str(i)
if print_prog:# and node.op.__class__ is T.DimShuffle:
if False and node.op == T.DimShuffle((), ['x', 'x'], inplace = True):
print node.op == T.DimShuffle((), ['x', 'x'], inplace = True),
print node.inputs[0], type(node.inputs[0]),
print node.inputs[0].equals(T.constant(2)),
outputs = node.outputs
inputs = theano.gof.graph.inputs(outputs)
print 'node ', i, node,
print ':'.join([imap[inp.owner] for inp in node.inputs])
#print theano.sandbox.pprint.pp.process_graph(inputs, outputs)
return theano.sandbox.wraplinker.WrapLinkerMany(
[theano.gof.OpWiseCLinker()],
[theano.sandbox.wraplinker.run_all
,blah
#,theano.sandbox.wraplinker.numpy_notall_isfinite
])
else:
return theano.gof.OpWiseCLinker()
class M(module.Module):
def __init__(self):
super(M, self).__init__()
x = T.matrix('x') # input, target
self.w = module.Member(T.matrix('w')) # weights
self.a = module.Member(T.vector('a')) # hid bias
self.b = module.Member(T.vector('b')) # output bias
hid = T.tanh(T.dot(x, self.w) + self.a)
out = T.tanh(T.dot(hid, self.w.T) + self.b)
err = 0.5 * T.sum((out - x)**2)
params = [self.w, self.a, self.b]
gparams = T.grad(err, params)
updates = [(p, p - 0.01 * gp) for p, gp in zip(params, gparams)]
self.step = module.Method([x], err, updates=dict(updates))
mod = M()
m = mod.make(mode='FAST_RUN')
neg, nout, nhid, niter = [int(a) for a in sys.argv[1:]]
rng = numpy.random.RandomState(342)
m.w = rng.rand(nout, nhid)
m.a = rng.randn(nhid) * 0.0
m.b = rng.randn(nout) * 0.0
x = (rng.rand(neg, nout)-0.5) * 1.5
t = time.time()
for i in xrange(niter):
err = m.step(x)
print 'time: ',time.time() - t, 'err: ', err
from __future__ import absolute_import
import numpy
import sys
import time
import theano
import theano.tensor as T
class Opt(object):
merge = theano.gof.MergeOptimizer()
gemm_opt_1 = theano.gof.TopoOptimizer(theano.tensor_opt.gemm_pattern_1)
sqr_opt_0 = theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.mul,'x', 'x'),
(T.sqr, 'x')))
ident_opt_0 = theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.sqr, (T.sqrt,'x')),
'x',
allow_multiple_clients=True))
ident_opt_1 = theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.sqrt, (T.sqr,'x')),
'x',
allow_multiple_clients=True))
ident_muldiv_0 = theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.mul, 'x', (T.div,'y', 'x')),
'y',
allow_multiple_clients=True))
ident_muldiv_1 = theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.mul, (T.div,'y', 'x'), 'x'),
'y',
allow_multiple_clients=True))
ident_muldiv_2 = theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.div, (T.mul,'y', 'x'), 'x'),
'y',
allow_multiple_clients=True))
ident_muldiv_3 = theano.gof.TopoOptimizer(
theano.gof.PatternSub(
(T.div, (T.mul,'y', 'x'), 'y'),
'x',
allow_multiple_clients=True))
def __call__(self, env):
self.merge(env)
#eliminate identities
if 0:
print 'SKIPPING optimizations'
else:
self.ident_opt_0(env)
self.ident_opt_1(env)
self.ident_muldiv_0(env)
self.ident_muldiv_1(env)
self.ident_muldiv_2(env)
self.ident_muldiv_3(env)
self.gemm_opt_1(env)
self.sqr_opt_0(env)
self.merge(env)
def aa_fn(hid_fn, out_fn):
x = T.matrix() # input, target
w = T.matrix() # weights
a = T.vector() # hid bias
b = T.vector() # output bias
hid = hid_fn(T.dot(x, w) + a)
out = out_fn(T.dot(hid, w.T) + b)
err = 0.5 * T.sum((out - x)**2)
params = [w, a, b]
gparams = T.grad(err, params)
uparams = [T.sub_inplace(p, 0.01 * gp) for p, gp in zip(params, gparams)]
return theano.function([x, w, a, b], [err] + uparams
, linker = theano.gof.OpWiseCLinker()
#, linker = theano.gof.PerformLinker()
, optimizer = Opt()
)
aa_tanh_tanh = aa_fn(T.tanh, T.tanh)
neg, nout, nhid = [int(a) for a in sys.argv[1:]]
rng = numpy.random.RandomState(342)
x = (rng.rand(neg, nout)-0.5) * 1.5
w = rng.rand(nout, nhid)
a = rng.randn(nhid) * 0.0
b = rng.randn(nout) * 0.0
t = time.time()
for i in xrange(1000):
err_and_stuff = aa_tanh_tanh(x, w, a, b)
print time.time() - t, err_and_stuff[0]
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论