提交 e456ce12 authored 作者: f0k's avatar f0k

added tests for GpuCorrMM_gradInputs and GpuCorrMM_gradWeights

上级 39c92e49
...@@ -844,12 +844,8 @@ class TestConv2DGPU(unittest.TestCase): ...@@ -844,12 +844,8 @@ class TestConv2DGPU(unittest.TestCase):
def test_gemm_directly(): def test_gemm_directly():
""" for direction in ['fprop', 'bprop img', 'bprop kern']:
input: (batch size, channels, rows, columns) print 'Testing direction: ' + direction
filters: (number of filters, channels, rows, columns)
"""
for mode in ['valid']: # 'full' currently disabled; doesn't allow subsampling
print 'Testing mode: ' + mode
for bs in range(1, 5): for bs in range(1, 5):
for ch in range(1,4): for ch in range(1,4):
for nf in range(1,4): for nf in range(1,4):
...@@ -857,35 +853,45 @@ def test_gemm_directly(): ...@@ -857,35 +853,45 @@ def test_gemm_directly():
for rImg2 in range(5, 9): for rImg2 in range(5, 9):
for rFlt1 in range(2, 4): for rFlt1 in range(2, 4):
for rFlt2 in range(2, 4): for rFlt2 in range(2, 4):
for subsx in range(1, 3): for subsx in range(1, 3) if direction == 'fprop' else [1]:
for subsy in range(1, 3): for subsy in range(1, 3) if direction == 'fprop' else [1]:
ishape = (bs, ch, rImg1, rImg2) ishape = (bs, ch, rImg1, rImg2)
kshape = (nf, ch, rFlt1, rFlt2) kshape = (nf, ch, rFlt1, rFlt2)
print "ishape: ", ishape
print "kshape: ", kshape
subsample = (subsx, subsy) subsample = (subsx, subsy)
print "subsample: ", subsample
npy_img = theano._asarray(numpy.random.rand(*ishape), dtype='float32') npy_img = theano._asarray(numpy.random.rand(*ishape), dtype='float32')
npy_kern = theano._asarray(numpy.random.rand(*kshape), dtype='float32') npy_kern = theano._asarray(numpy.random.rand(*kshape), dtype='float32')
i = cuda_tensor4() i = cuda_tensor4()
k = cuda_tensor4() k = cuda_tensor4()
cpuval = py_conv(npy_img, npy_kern, mode, subsample) if direction == 'fprop':
cpuval = py_conv(npy_img, npy_kern, 'valid', subsample)
op = theano.sandbox.cuda.blas.GpuCorrMM(border_mode=mode, \ op = theano.sandbox.cuda.blas.GpuCorrMM(border_mode='valid',
subsample=subsample)(i, k) subsample=subsample)(i, k)
f = theano.function([i, k], op, mode=theano_mode) f = theano.function([i, k], op, mode=theano_mode)
gpuval = f(npy_img, npy_kern[:,:,::-1,::-1])
npy_kern = npy_kern[:,:,::-1,::-1] elif direction == 'bprop img':
cpuval = py_conv(npy_img, npy_kern, 'full', subsample)
gpuval = f(npy_img, npy_kern) op = theano.sandbox.cuda.blas.GpuCorrMM_gradInputs(border_mode='valid',
subsample=subsample)(i, k)
gpuval = numpy.asarray(gpuval) f = theano.function([i, k], op, mode=theano_mode)
rval = numpy.allclose(cpuval, gpuval, rtol=1e-4) gpuval = f(npy_kern.transpose(1, 0, 2, 3), npy_img)
assert (rval == True) elif direction == 'bprop kern':
print 'Test Passed' cpuval = py_conv(npy_img, npy_kern, 'valid', subsample)
op = theano.sandbox.cuda.blas.GpuCorrMM_gradWeights(border_mode='valid',
subsample=subsample)(i, k)
f = theano.function([i, k], op, mode=theano_mode)
gpuval = numpy.array(f(npy_img.transpose(1, 0, 2, 3),
npy_kern.transpose(1, 0, 2, 3)[:,:,::-1,::-1])).transpose(1, 0, 2, 3)
if not numpy.allclose(cpuval, gpuval, rtol=1e-4):
print "Test failed for"
print "direction: ", direction
print "ishape: ", ishape
print "kshape: ", kshape
print "subsample: ", subsample
assert False
def benchmark(): def benchmark():
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论