提交 b483da5e authored 作者: Ian Goodfellow's avatar Ian Goodfellow

pep8 test_mlp.py

上级 3f734be4
......@@ -172,26 +172,27 @@ def run_conv_nnet1(use_gpu):
if config.mode == 'DEBUG_MODE':
n_train = 1
logical_hid_shape = tcn.blas.GpuConv.logical_output_shape_2d(shape_img[2:],shape_kern[2:], 'valid')
logical_hid_shape = tcn.blas.GpuConv.logical_output_shape_2d(
shape_img[2:], shape_kern[2:], 'valid')
n_hid = n_kern * logical_hid_shape[0] * logical_hid_shape[1]
n_out = 10
w = shared_fn(0.01*(my_rand(*shape_kern)-0.5), 'w')
w = shared_fn(0.01 * (my_rand(*shape_kern) - 0.5), 'w')
b = shared_fn(my_zeros((n_kern,)), 'b')
v = shared_fn(my_zeros((n_hid, n_out)), 'c')
c = shared_fn(my_zeros(n_out), 'c')
x = tensor.Tensor(dtype='float32', broadcastable=(0,1,0,0))('x')
x = tensor.Tensor(dtype='float32', broadcastable=(0, 1, 0, 0))('x')
y = tensor.fmatrix('y')
lr = tensor.fscalar('lr')
conv_op = conv.ConvOp(shape_img[2:], shape_kern[2:], n_kern, n_batch, 1, 1)
conv_op.set_flops()
hid = tensor.tanh(conv_op(x, w)+b.dimshuffle((0,'x','x')))
hid = tensor.tanh(conv_op(x, w) + b.dimshuffle((0, 'x', 'x')))
hid_flat = hid.reshape((n_batch, n_hid))
out = tensor.tanh(tensor.dot(hid_flat, v)+c)
loss = tensor.sum(0.5 * (out-y)**2 * lr)
out = tensor.tanh(tensor.dot(hid_flat, v) + c)
loss = tensor.sum(0.5 * (out - y) ** 2 * lr)
#print 'loss type', loss.type
params = [w, b, v, c]
......@@ -200,7 +201,8 @@ def run_conv_nnet1(use_gpu):
mode = get_mode(use_gpu)
#print 'building pfunc ...'
train = pfunc([x,y,lr], [loss], mode=mode, updates=[(p, p-g) for p,g in zip(params, gparams)])
train = pfunc([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()):
# print i, n
......@@ -221,10 +223,10 @@ def test_conv_nnet1():
rval_cpu = run_conv_nnet1(False)
utt.seed_rng()
rval_gpu = run_conv_nnet1(True)
assert numpy.allclose(rval_cpu, rval_gpu,rtol=1e-4,atol=1e-6)
assert numpy.allclose(rval_cpu, rval_gpu, rtol=1e-4, atol=1e-6)
def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
if use_gpu:
shared_fn = tcn.shared_constructor
else:
......@@ -239,10 +241,8 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
#n_train=10, n_batch=60, n_kern=10, n_kern1=10, error see of -5.26905e-05
#n_train=30, n_batch=60, n_kern=10, n_kern1=10, error see of -3.8147e-06
#n_train=30, n_batch=60, n_kern=20, n_kern1=10, error see of 6.82771e-05
#n_train=30, n_batch=60, n_kern=20, n_kern1=30, error see of 0.000231534
n_batch = 60
shape_img = (n_batch, 1, 32, 32)
......@@ -252,35 +252,40 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
n_kern1 = 10
shape_kern1 = (n_kern1, n_kern, 5, 5)
n_train=30
if config.mode=='DEBUG_MODE': n_train=1
n_train = 30
if config.mode == 'DEBUG_MODE':
n_train = 1
logical_hid_shape = tcn.blas.GpuConv.logical_output_shape_2d(tuple(shape_img[2:]),tuple(shape_kern[2:]), 'valid')
logical_hid_shape1 = tcn.blas.GpuConv.logical_output_shape_2d((logical_hid_shape[0]/2, logical_hid_shape[1]/2), tuple(shape_kern1[2:]), 'valid')
logical_hid_shape = tcn.blas.GpuConv.logical_output_shape_2d(tuple(
shape_img[2:]), tuple(shape_kern[2:]), 'valid')
logical_hid_shape1 = tcn.blas.GpuConv.logical_output_shape_2d((
logical_hid_shape[0]/2, logical_hid_shape[1]/2), tuple(shape_kern1[2:]), 'valid')
n_hid = n_kern1 * logical_hid_shape1[0] * logical_hid_shape1[1]
n_out = 10
w0 = shared_fn(0.01*(my_rand(*shape_kern)-0.5), 'w0')
w0 = shared_fn(0.01 * (my_rand(*shape_kern) - 0.5), 'w0')
b0 = shared_fn(my_zeros((n_kern,)), 'b0')
w1 = shared_fn(0.01*(my_rand(*shape_kern1)-0.5), 'w1')
w1 = shared_fn(0.01 * (my_rand(*shape_kern1) - 0.5), 'w1')
b1 = shared_fn(my_zeros((n_kern1,)), 'b1')
v = shared_fn(my_zeros((n_hid, n_out)), 'c')
c = shared_fn(my_zeros(n_out), 'c')
x = tensor.Tensor(dtype='float32', broadcastable=(0,1,0,0))('x')
x = tensor.Tensor(dtype='float32', broadcastable=(0, 1, 0, 0))('x')
y = tensor.fmatrix('y')
lr = tensor.fscalar('lr')
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, logical_hid_shape[1]/2), shape_kern1[2:], n_kern1, n_batch, 1, 1)
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)
conv_op.set_flops()
conv_op1.set_flops()
hid = tensor.tanh(conv_op(x, w0)+b0.dimshuffle((0,'x','x')))
hid1 = tensor.tanh(conv_op1(hid[:,:,::2,::2], w1) + b1.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((
0, 'x', 'x')))
hid_flat = hid1.reshape((n_batch, n_hid))
out = tensor.tanh(tensor.dot(hid_flat, v)+c)
loss = tensor.sum(0.5 * (out-y)**2 * lr)
out = tensor.tanh(tensor.dot(hid_flat, v) + c)
loss = tensor.sum(0.5 * (out - y) ** 2 * lr)
#print 'loss type', loss.type
params = [w0, b0, w1, b1, v, c]
......@@ -289,13 +294,14 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
mode = get_mode(use_gpu)
#print 'building pfunc ...'
train = pfunc([x,y,lr], [loss], mode=mode, updates=[(p, p-g) for p,g in zip(params, gparams)])
train = pfunc([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()):
# print i, n
xval = my_rand(*shape_img)
yval = my_rand(n_batch,n_out)#int32 make all 0...
yval = my_rand(n_batch, n_out) # int32 make all 0...
lr = theano._asarray(0.01, dtype='float32')
for i in xrange(n_train):
rval = train(xval, yval, lr)
......@@ -311,7 +317,7 @@ def test_conv_nnet2():
utt.seed_rng()
rval_cpu = run_conv_nnet2(False)
#print rval_cpu[0], rval_gpu[0],rval_cpu[0]-rval_gpu[0]
assert numpy.allclose(rval_cpu, rval_gpu,rtol=1e-4,atol=1e-4)
assert numpy.allclose(rval_cpu, rval_gpu, rtol=1e-4, atol=1e-4)
def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
......@@ -322,59 +328,61 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
else:
shared_fn = shared
isize1=isize
isize2=isize
if isinstance(isize,(tuple,)):
isize1=isize[0]
isize2=isize[1]
isize1 = isize
isize2 = isize
if isinstance(isize, (tuple, )):
isize1 = isize[0]
isize2 = isize[1]
shape_img = (n_batch, 1, isize1, isize2)
n_kern = 20 # 6 were used in LeNet5
shape_kern = (n_kern, 1, ksize, ksize)
n_kern1 = 30 # 16 were used in LeNet5
n_kern1 = 30 # 16 were used in LeNet5
shape_kern1 = (n_kern1, n_kern, ksize, ksize)
logical_hid_shape = tcn.blas.GpuConv.logical_output_shape_2d((isize1, isize2), (ksize, ksize), 'valid')
logical_hid_shape = tcn.blas.GpuConv.logical_output_shape_2d((
isize1, isize2), (ksize, ksize), 'valid')
logical_hid_shape1 = tcn.blas.GpuConv.logical_output_shape_2d((logical_hid_shape[0]/2,
logical_hid_shape[1]/2), (ksize, ksize), 'valid')
n_hid = n_kern1 * logical_hid_shape1[0] * logical_hid_shape1[1]
n_out = 10
w0 = shared_fn(0.01*(my_rand(*shape_kern)-0.5), 'w0')
w0 = shared_fn(0.01 * (my_rand(*shape_kern) - 0.5), 'w0')
b0 = shared_fn(my_zeros((n_kern,)), 'b0')
w1 = shared_fn(0.01*(my_rand(*shape_kern1)-0.5), 'w1')
w1 = shared_fn(0.01 * (my_rand(*shape_kern1) - 0.5), 'w1')
b1 = shared_fn(my_zeros((n_kern1,)), 'b1')
v = shared_fn(0.01*my_randn(n_hid, n_out), 'v')
v = shared_fn(0.01 * my_randn(n_hid, n_out), 'v')
c = shared_fn(my_zeros(n_out), 'c')
#print 'ALLOCATING ARCH: w0 shape', w0.get_value(borrow=True).shape
#print 'ALLOCATING ARCH: w1 shape', w1.get_value(borrow=True).shape
#print 'ALLOCATING ARCH: v shape', v.get_value(borrow=True).shape
x = tensor.Tensor(dtype='float32', broadcastable=(0,1,0,0))('x')
x = tensor.Tensor(dtype='float32', broadcastable=(0, 1, 0, 0))('x')
y = tensor.fmatrix('y')
lr = tensor.fscalar('lr')
conv_op = conv.ConvOp(shape_img[2:], shape_kern[2:], n_kern,
n_batch, 1, 1, verbose=verbose, version=version)
conv_op1 = conv.ConvOp(
(n_kern,logical_hid_shape[0]/2, logical_hid_shape[1]/2),
(n_kern, logical_hid_shape[0] / 2, logical_hid_shape[1] / 2),
shape_kern1[2:], n_kern1, n_batch, 1, 1,verbose=verbose, version=version)
conv_op.set_flops()
conv_op1.set_flops()
ds_op = downsample.DownsampleFactorMax((2,2), ignore_border=False)
ds_op = downsample.DownsampleFactorMax((2, 2), ignore_border=False)
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:
hid = tensor.tanh((conv_op(x, w0)+b0.dimshuffle((0,'x','x')))[:,:,::2,::2])
hid1 = tensor.tanh(conv_op1(hid, w1) + b1.dimshuffle((0,'x','x')))
hid = tensor.tanh((conv_op(x, w0) + b0.dimshuffle((0, 'x', 'x')
))[:, :, ::2, ::2])
hid1 = tensor.tanh(conv_op1(hid, w1) + b1.dimshuffle((0, 'x', 'x')))
hid_flat = hid1.reshape((n_batch, n_hid))
out = tensor.nnet.softmax(tensor.dot(hid_flat, v)+c)
loss = tensor.sum(tensor.nnet.crossentropy_categorical_1hot(out, tensor.argmax(y, axis=1)) * lr)
out = tensor.nnet.softmax(tensor.dot(hid_flat, v) + c)
loss = tensor.sum(tensor.nnet.crossentropy_categorical_1hot(out,
tensor.argmax(y, axis=1)) * lr)
#print 'loss type', loss.type
params = [w0, b0, w1, b1, v, c]
......@@ -383,7 +391,8 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
mode = get_mode(use_gpu, check_isfinite)
#print 'building pfunc ...'
train = pfunc([x,y,lr], [loss], mode=mode, updates=[(p, p-g) for p,g in zip(params, gparams)])
train = pfunc([x, y, lr], [loss], mode=mode, updates=[(p, p - g) for p,
g in zip(params, gparams)])
if verbose:
theano.printing.debugprint(train)
......@@ -392,7 +401,7 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
topo = train.maker.fgraph.toposort()
assert len([n for n in topo if isinstance(n.op, tcn.blas.GpuConv)]) > 0
shape_target = (n_batch,n_out)
shape_target = (n_batch, n_out)
return train, params, shape_img, shape_target, mode
......@@ -405,7 +414,7 @@ def run_conv_nnet2_classif(use_gpu, seed, isize, ksize, bsize,
"""Run the train function returned by build_conv_nnet2_classif on one device.
"""
utt.seed_rng(seed) # Seeds numpy.random with seed
utt.seed_rng(seed) # Seeds numpy.random with seed
train, params, x_shape, y_shape, mode = build_conv_nnet2_classif(
use_gpu=use_gpu,
isize=isize,
......@@ -488,7 +497,7 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize,
verbose=verbose,
version=version)
utt.seed_rng(seed) # Seeds numpy.random with seed
utt.seed_rng(seed) # Seeds numpy.random with seed
train_cpu, params_cpu, x_shape, y_shape, mode_cpu = \
build_conv_nnet2_classif(
use_gpu=False,
......@@ -499,7 +508,7 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize,
version=version,
check_isfinite=check_isfinite)
utt.seed_rng(seed) # Seeds numpy.random with seed
utt.seed_rng(seed) # Seeds numpy.random with seed
train_gpu, params_gpu, x_shape_gpu, y_shape_gpu, mode_gpu = \
build_conv_nnet2_classif(
use_gpu=True,
......@@ -525,28 +534,30 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize,
t0 = time.time()
rval_cpu = train_cpu(xval, yval, lr)[0]
t1 = time.time()
time_cpu += (t1-t0)
time_cpu += (t1 - t0)
# Train one batch on GPU
t0 = time.time()
rval_gpu = train_gpu(xval, yval, lr)[0]
t1 = time.time()
time_gpu += (t1-t0)
time_gpu += (t1 - t0)
# Compare results
if (verbose or not
numpy.allclose(rval_cpu, rval_gpu, rtol=1e-5, atol=float_atol)):
print "At batch:", i+1
print "At batch:", i + 1
print "CPU:", rval_cpu
print "GPU:", rval_gpu
print "abs diff:", numpy.absolute(rval_gpu-rval_cpu)
print "rel diff:", numpy.absolute((rval_gpu-rval_cpu)/rval_gpu)
print "abs diff:", numpy.absolute(rval_gpu - rval_cpu)
print "rel diff:", numpy.absolute((
rval_gpu - rval_cpu) / rval_gpu)
if not ignore_error:
assert numpy.allclose(rval_cpu, rval_gpu, rtol=1e-5, atol=float_atol)
assert numpy.allclose(rval_cpu, rval_gpu,
rtol=1e-5, atol=float_atol)
# Synchronize parameters to start from the same point next time
if i < n_train-1:
if i < n_train - 1:
for cpu_p, gpu_p in zip(params_cpu, params_gpu):
cpu_p.set_value(gpu_p.get_value(borrow=False), borrow=True)
......@@ -574,27 +585,27 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize,
# Default parameters for all subsequent tests
gpu_only=False
cpu_only=False
ignore_error=False
verbose=0
version=-1
gpu_only = False
cpu_only = False
ignore_error = False
verbose = 0
version = -1
seed = utt.fetch_seed()
def test_lenet_28(): #MNIST
def test_lenet_28(): # MNIST
cmp_run_conv_nnet2_classif(seed, 28, 5, 60, n_train=10,
ignore_error=ignore_error, gpu_only=gpu_only,
cpu_only=cpu_only, verbose=verbose, version=version)
def test_lenet_32(): #CIFAR10 / Shapeset
def test_lenet_32(): # CIFAR10 / Shapeset
cmp_run_conv_nnet2_classif(seed, 32, 5, 60, n_train=8,
ignore_error=ignore_error, gpu_only=gpu_only,
verbose=verbose, version=version)
def test_lenet_32_long(): #CIFAR10 / Shapeset
def test_lenet_32_long(): # CIFAR10 / Shapeset
# this tests the gradient of downsample on the GPU,
# which does not recieve specific testing
cmp_run_conv_nnet2_classif(seed, 32, 5, 30, n_train=50,
......@@ -602,7 +613,7 @@ def test_lenet_32_long(): #CIFAR10 / Shapeset
cpu_only=cpu_only, verbose=verbose, version=version)
def test_lenet_64(): # ???
def test_lenet_64(): # ???
#float_atol need to pass in debug mode
#needed as cpu use extended precision and gpu don't
cmp_run_conv_nnet2_classif(seed, 64, 7, 10, n_train=10,
......@@ -611,14 +622,14 @@ def test_lenet_64(): # ???
check_isfinite=True, version=version)
def test_lenet_108(): # NORB
def test_lenet_108(): # NORB
cmp_run_conv_nnet2_classif(seed, 108, 7, 5, n_train=4,
ignore_error=ignore_error, gpu_only=gpu_only,
cpu_only=cpu_only, verbose=verbose,
check_isfinite=True, version=version)
def test_lenet_256(): # ImageNet
def test_lenet_256(): # ImageNet
cmp_run_conv_nnet2_classif(seed, 256, 9, 2, n_train=5,
ignore_error=ignore_error, gpu_only=gpu_only,
cpu_only=cpu_only, verbose=verbose,
......@@ -626,16 +637,16 @@ def test_lenet_256(): # ImageNet
#I did a wanted error in the name as we don't want it to execute automatically for now as it don't work
def tes_lenet_hd(): #HD 720p: 1280(wid)x720(len)
cmp_run_conv_nnet2_classif(seed, (720,1280), 9, 2, n_train=3,
def tes_lenet_hd(): # HD 720p: 1280(wid)x720(len)
cmp_run_conv_nnet2_classif(seed, (720, 1280), 9, 2, n_train=3,
ignore_error=ignore_error, gpu_only=gpu_only,
cpu_only=cpu_only, verbose=verbose,
check_isfinite=True, version=version)
#I did a wanted error in the name as we don't want it to execute automatically for now as it don't work
def tes_lenet_full_hd(): #HD 1080p: 1920(wid)x1080(len)
cmp_run_conv_nnet2_classif(seed, (1080,1920), 9, 2, n_train=3,
def tes_lenet_full_hd(): # HD 1080p: 1920(wid)x1080(len)
cmp_run_conv_nnet2_classif(seed, (1080, 1920), 9, 2, n_train=3,
ignore_error=ignore_error, gpu_only=gpu_only,
cpu_only=cpu_only, verbose=verbose,
check_isfinite=True, version=version)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论