提交 e26858a3 authored 作者: Chiheb Trabelsi's avatar Chiheb Trabelsi

test_mlp.py has been modified in order to respect the flake8 style.

上级 7aedb911
...@@ -24,7 +24,7 @@ if theano.config.mode not in ['FAST_RUN', 'Mode', 'ProfileMode']: ...@@ -24,7 +24,7 @@ if theano.config.mode not in ['FAST_RUN', 'Mode', 'ProfileMode']:
'otherwise it is too slow!') 'otherwise it is too slow!')
# Skip test if cuda_ndarray is not available. # Skip test if cuda_ndarray is not available.
if tcn.cuda_available == False: if tcn.cuda_available is False:
raise SkipTest('Optional package cuda disabled') raise SkipTest('Optional package cuda disabled')
...@@ -147,19 +147,20 @@ def test_run_nnet(): ...@@ -147,19 +147,20 @@ def test_run_nnet():
rtol = 1e-4 rtol = 1e-4
if n_in * n_hid >= 2048 * 4096: if n_in * n_hid >= 2048 * 4096:
rtol = 7e-4 rtol = 7e-4
assert numpy.allclose(rval_cpu, rval_gpu, rtol=rtol, atol=1e-6), \ assert numpy.allclose(
rval_cpu, rval_gpu, rtol=rtol, atol=1e-6), \
("max_abs_diff, max_rel_diff, n_in, n_hid", max_abs_diff, ("max_abs_diff, max_rel_diff, n_in, n_hid", max_abs_diff,
rel_diff.max(), n_in, n_hid) rel_diff.max(), n_in, n_hid)
def test_run_nnet_med(): def test_run_nnet_med():
utt.seed_rng() utt.seed_rng()
rval_cpu = run_nnet(False, 10, 128, 50, 4, n_train=10000) run_nnet(False, 10, 128, 50, 4, n_train=10000)
def test_run_nnet_small(): def test_run_nnet_small():
utt.seed_rng() utt.seed_rng()
rval_cpu = run_nnet(False, 10, 10, 4, 4, n_train=100000) run_nnet(False, 10, 10, 4, 4, n_train=100000)
def run_conv_nnet1(use_gpu): def run_conv_nnet1(use_gpu):
...@@ -203,8 +204,11 @@ def run_conv_nnet1(use_gpu): ...@@ -203,8 +204,11 @@ def run_conv_nnet1(use_gpu):
mode = get_mode(use_gpu) mode = get_mode(use_gpu)
# print 'building pfunc ...' # print 'building pfunc ...'
train = pfunc([x, y, lr], [loss], mode=mode, updates=[(p, p - g) for p, train = pfunc(
g in zip(params, gparams)]) [x, y, lr],
[loss],
mode=mode,
updates=[(p, p - g) for p, g in zip(params, gparams)])
# for i, n in enumerate(train.maker.fgraph.toposort()): # for i, n in enumerate(train.maker.fgraph.toposort()):
# print i, n # print i, n
...@@ -279,7 +283,9 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST ...@@ -279,7 +283,9 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
conv_op = conv.ConvOp(shape_img[2:], shape_kern[2:], n_kern, n_batch, 1, 1) conv_op = conv.ConvOp(shape_img[2:], shape_kern[2:], n_kern, n_batch, 1, 1)
conv_op1 = conv.ConvOp((n_kern, logical_hid_shape[0] // 2, conv_op1 = conv.ConvOp((n_kern, logical_hid_shape[0] // 2,
logical_hid_shape[1] // 2), shape_kern1[2:], n_kern1, n_batch, 1, 1) logical_hid_shape[1] // 2),
shape_kern1[2:],
n_kern1, n_batch, 1, 1)
hid = tensor.tanh(conv_op(x, w0) + b0.dimshuffle((0, 'x', 'x'))) hid = tensor.tanh(conv_op(x, w0) + b0.dimshuffle((0, 'x', 'x')))
hid1 = tensor.tanh(conv_op1(hid[:, :, ::2, ::2], w1) + b1.dimshuffle(( hid1 = tensor.tanh(conv_op1(hid[:, :, ::2, ::2], w1) + b1.dimshuffle((
...@@ -295,8 +301,11 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST ...@@ -295,8 +301,11 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
mode = get_mode(use_gpu) mode = get_mode(use_gpu)
# print 'building pfunc ...' # print 'building pfunc ...'
train = pfunc([x, y, lr], [loss], mode=mode, updates=[(p, p - g) for p, train = pfunc(
g in zip(params, gparams)]) [x, y, lr],
[loss],
mode=mode,
updates=[(p, p - g) for p, g in zip(params, gparams)])
# for i, n in enumerate(train.maker.fgraph.toposort()): # for i, n in enumerate(train.maker.fgraph.toposort()):
# print i, n # print i, n
...@@ -376,13 +385,14 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch, ...@@ -376,13 +385,14 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
if downsample_ops: if downsample_ops:
hid = tensor.tanh(ds_op(conv_op(x, w0) + b0.dimshuffle((0, 'x', 'x')))) hid = tensor.tanh(ds_op(conv_op(x, w0) + b0.dimshuffle((0, 'x', 'x'))))
else: else:
hid = tensor.tanh((conv_op(x, w0) + b0.dimshuffle((0, 'x', 'x') hid = tensor.tanh(
))[:, :, ::2, ::2]) (conv_op(x, w0) + b0.dimshuffle(
(0, 'x', 'x')))[:, :, ::2, ::2])
hid1 = tensor.tanh(conv_op1(hid, w1) + b1.dimshuffle((0, 'x', 'x'))) hid1 = tensor.tanh(conv_op1(hid, w1) + b1.dimshuffle((0, 'x', 'x')))
hid_flat = hid1.reshape((n_batch, n_hid)) hid_flat = hid1.reshape((n_batch, n_hid))
out = tensor.nnet.softmax(tensor.dot(hid_flat, v) + c) out = tensor.nnet.softmax(tensor.dot(hid_flat, v) + c)
loss = tensor.sum(tensor.nnet.crossentropy_categorical_1hot(out, loss = tensor.sum(tensor.nnet.crossentropy_categorical_1hot(
tensor.argmax(y, axis=1)) * lr) out, tensor.argmax(y, axis=1)) * lr)
# print 'loss type', loss.type # print 'loss type', loss.type
params = [w0, b0, w1, b1, v, c] params = [w0, b0, w1, b1, v, c]
...@@ -391,8 +401,11 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch, ...@@ -391,8 +401,11 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
mode = get_mode(use_gpu, check_isfinite) mode = get_mode(use_gpu, check_isfinite)
# print 'building pfunc ...' # print 'building pfunc ...'
train = pfunc([x, y, lr], [loss], mode=mode, updates=[(p, p - g) for p, train = pfunc(
g in zip(params, gparams)]) [x, y, lr],
[loss],
mode=mode,
updates=[(p, p - g) for p, g in zip(params, gparams)])
if verbose: if verbose:
theano.printing.debugprint(train) theano.printing.debugprint(train)
...@@ -440,10 +453,8 @@ def run_conv_nnet2_classif(use_gpu, seed, isize, ksize, bsize, ...@@ -440,10 +453,8 @@ def run_conv_nnet2_classif(use_gpu, seed, isize, ksize, bsize,
lr = theano._asarray(0.01, dtype='float32') lr = theano._asarray(0.01, dtype='float32')
rvals = my_zeros(n_train) rvals = my_zeros(n_train)
t0 = time.time()
for i in xrange(n_train): for i in xrange(n_train):
rvals[i] = train(xval, yval, lr)[0] rvals[i] = train(xval, yval, lr)[0]
t1 = time.time()
print_mode(mode) print_mode(mode)
if pickle and isinstance(mode, theano.compile.ProfileMode): if pickle and isinstance(mode, theano.compile.ProfileMode):
...@@ -495,7 +506,8 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize, ...@@ -495,7 +506,8 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize,
compare = True compare = True
if not compare: if not compare:
return run_conv_nnet2_classif(use_gpu=use_gpu, return run_conv_nnet2_classif(
use_gpu=use_gpu,
seed=seed, isize=isize, ksize=ksize, bsize=bsize, seed=seed, isize=isize, ksize=ksize, bsize=bsize,
n_train=n_train, n_train=n_train,
check_isfinite=check_isfinite, check_isfinite=check_isfinite,
...@@ -570,18 +582,6 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize, ...@@ -570,18 +582,6 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize,
finally: finally:
theano.tensor.basic.float32_atol = orig_float32_atol theano.tensor.basic.float32_atol = orig_float32_atol
if pickle:
if isinstance(cpu_mode, theano.compile.ProfileMode):
import pickle
print("BEGIN CPU profile mode dump")
print(pickle.dumps(cpu_mode))
print("END CPU profile mode dump")
if isinstance(gpu_mode, theano.compile.ProfileMode):
import pickle
print("BEGIN GPU profile mode dump")
print(pickle.dumps(gpu_mode))
print("END GPU profile mode dump")
# print "CPU time: %.3f, GPU time: %.3f, speed up %f" % ( # print "CPU time: %.3f, GPU time: %.3f, speed up %f" % (
# (time_cpu, time_gpu, time_cpu/time_gpu)) # (time_cpu, time_gpu, time_cpu/time_gpu))
# print "Estimated time for one pass through MNIST with CPU: %f" % ( # print "Estimated time for one pass through MNIST with CPU: %f" % (
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论