提交 84cd72e6 authored 作者: Benjamin Scellier's avatar Benjamin Scellier

file theano/gpuarray/tests/test_dnn.py

上级 f07161ed
...@@ -3,7 +3,7 @@ import logging ...@@ -3,7 +3,7 @@ import logging
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from nose_parameterized import parameterized from nose_parameterized import parameterized
import numpy import numpy as np
from itertools import product, chain from itertools import product, chain
import theano import theano
...@@ -38,7 +38,7 @@ def test_dnn_conv_desc_merge(): ...@@ -38,7 +38,7 @@ def test_dnn_conv_desc_merge():
if not dnn.dnn_available(test_ctx_name): if not dnn.dnn_available(test_ctx_name):
raise SkipTest(dnn.dnn_available.msg) raise SkipTest(dnn.dnn_available.msg)
kern_shp = T.as_tensor_variable( kern_shp = T.as_tensor_variable(
numpy.asarray([3, 1, 2, 2]).astype('int64')) np.asarray([3, 1, 2, 2]).astype('int64'))
desc1 = dnn.GpuDnnConvDesc(border_mode='valid', subsample=(2, 2), desc1 = dnn.GpuDnnConvDesc(border_mode='valid', subsample=(2, 2),
conv_mode='conv')(kern_shp) conv_mode='conv')(kern_shp)
desc2 = dnn.GpuDnnConvDesc(border_mode='full', subsample=(1, 1), desc2 = dnn.GpuDnnConvDesc(border_mode='full', subsample=(1, 1),
...@@ -69,8 +69,8 @@ def test_dnn_conv_merge(): ...@@ -69,8 +69,8 @@ def test_dnn_conv_merge():
o1 = dnn.dnn_conv(img, kern) o1 = dnn.dnn_conv(img, kern)
o2 = dnn.dnn_conv(img, kern) o2 = dnn.dnn_conv(img, kern)
f = theano.function([img, kern], [o1, o2], mode=mode_with_gpu) f = theano.function([img, kern], [o1, o2], mode=mode_with_gpu)
d1, d2 = f(numpy.random.rand(*img_shp).astype(theano.config.floatX), d1, d2 = f(np.random.rand(*img_shp).astype(theano.config.floatX),
numpy.random.rand(*kern_shp).astype(theano.config.floatX)) np.random.rand(*kern_shp).astype(theano.config.floatX))
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert len([n for n in topo if isinstance(n.op, dnn.GpuDnnConv)]) == 1 assert len([n for n in topo if isinstance(n.op, dnn.GpuDnnConv)]) == 1
...@@ -110,8 +110,8 @@ def test_dnn_conv_inplace(): ...@@ -110,8 +110,8 @@ def test_dnn_conv_inplace():
o1 = dnn.dnn_conv(img, kern, conv_mode='conv') o1 = dnn.dnn_conv(img, kern, conv_mode='conv')
o2 = dnn.dnn_conv(img, kern, conv_mode='cross') o2 = dnn.dnn_conv(img, kern, conv_mode='cross')
f = theano.function([img, kern], [o1, o2], mode=mode_with_gpu) f = theano.function([img, kern], [o1, o2], mode=mode_with_gpu)
d1, d2 = f(numpy.random.rand(*img_shp).astype(theano.config.floatX), d1, d2 = f(np.random.rand(*img_shp).astype(theano.config.floatX),
numpy.random.rand(*kern_shp).astype(theano.config.floatX)) np.random.rand(*kern_shp).astype(theano.config.floatX))
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
convs = [n for n in topo if isinstance(n.op, dnn.GpuDnnConv)] convs = [n for n in topo if isinstance(n.op, dnn.GpuDnnConv)]
assert len(convs) == 2 assert len(convs) == 2
...@@ -189,7 +189,7 @@ def test_pooling(): ...@@ -189,7 +189,7 @@ def test_pooling():
(1, 3, 99, 99), (1, 3, 99, 99),
(32, 1, 147, 197), (32, 1, 147, 197),
]: ]:
data = numpy.random.normal(0, 1, shp).astype(theano.config.floatX) data = np.random.normal(0, 1, shp).astype(theano.config.floatX)
a = f_cpu(data).__array__() a = f_cpu(data).__array__()
b = f_gpu(data).__array__() b = f_gpu(data).__array__()
utt.assert_allclose(a, b) utt.assert_allclose(a, b)
...@@ -197,7 +197,7 @@ def test_pooling(): ...@@ -197,7 +197,7 @@ def test_pooling():
# Test the grad # Test the grad
for shp in [(1, 1, 2, 2), for shp in [(1, 1, 2, 2),
(1, 1, 3, 3)]: (1, 1, 3, 3)]:
data = numpy.random.normal(0, 1, shp).astype(theano.config.floatX) * 10 data = np.random.normal(0, 1, shp).astype(theano.config.floatX) * 10
ws = 2 ws = 2
stride = 2 stride = 2
...@@ -236,9 +236,9 @@ def test_pooling_with_tensor_vars(): ...@@ -236,9 +236,9 @@ def test_pooling_with_tensor_vars():
if not dnn.dnn_available(test_ctx_name): if not dnn.dnn_available(test_ctx_name):
raise SkipTest(dnn.dnn_available.msg) raise SkipTest(dnn.dnn_available.msg)
x = T.tensor4() x = T.tensor4()
ws = theano.shared(numpy.array([2, 2], dtype='int32')) ws = theano.shared(np.array([2, 2], dtype='int32'))
stride = theano.shared(numpy.array([1, 1], dtype='int32')) stride = theano.shared(np.array([1, 1], dtype='int32'))
pad = theano.shared(numpy.array([0, 0], dtype='int32')) pad = theano.shared(np.array([0, 0], dtype='int32'))
mode = 'max' mode = 'max'
def fn(x): def fn(x):
...@@ -251,7 +251,7 @@ def test_pooling_with_tensor_vars(): ...@@ -251,7 +251,7 @@ def test_pooling_with_tensor_vars():
for shp in [(1, 1, 2, 2), for shp in [(1, 1, 2, 2),
(1, 1, 3, 3)]: (1, 1, 3, 3)]:
data = numpy.random.normal(0, 1, shp).astype(theano.config.floatX) * 10 data = np.random.normal(0, 1, shp).astype(theano.config.floatX) * 10
theano.tests.unittest_tools.verify_grad( theano.tests.unittest_tools.verify_grad(
fn, [data], mode=mode_with_gpu) fn, [data], mode=mode_with_gpu)
...@@ -275,10 +275,10 @@ def test_pooling_with_tensor_vars(): ...@@ -275,10 +275,10 @@ def test_pooling_with_tensor_vars():
for shp in [(1, 10, 100, 100), for shp in [(1, 10, 100, 100),
(1, 3, 99, 99), (1, 3, 99, 99),
(32, 1, 147, 197)]: (32, 1, 147, 197)]:
data = numpy.random.normal(0, 1, shp).astype(theano.config.floatX) data = np.random.normal(0, 1, shp).astype(theano.config.floatX)
# Change the window size dynamically # Change the window size dynamically
ws.set_value(numpy.array([i, i]).astype('int32')) ws.set_value(np.array([i, i]).astype('int32'))
a = f_gpu(data).__array__() a = f_gpu(data).__array__()
b = f_cpu(data).__array__() b = f_cpu(data).__array__()
utt.assert_allclose(a, b) utt.assert_allclose(a, b)
...@@ -336,11 +336,11 @@ def test_pooling3d(): ...@@ -336,11 +336,11 @@ def test_pooling3d():
(1, 3, 99, 99, 29), (1, 3, 99, 99, 29),
(2, 1, 147, 97, 37), (2, 1, 147, 97, 37),
]: ]:
data = numpy.random.normal(0, 1, shp).astype(theano.config.floatX) data = np.random.normal(0, 1, shp).astype(theano.config.floatX)
a = f_cpu(data).__array__() a = f_cpu(data).__array__()
b = f_gpu(data).__array__() b = f_gpu(data).__array__()
utt.assert_allclose(a, b, utt.assert_allclose(a, b,
atol=numpy.finfo(theano.config.floatX).eps) atol=np.finfo(theano.config.floatX).eps)
# Test the grad # Test the grad
for shp in [(1, 1, 2, 2, 2), for shp in [(1, 1, 2, 2, 2),
...@@ -350,7 +350,7 @@ def test_pooling3d(): ...@@ -350,7 +350,7 @@ def test_pooling3d():
(1, 1, 4, 3, 3), (1, 1, 4, 3, 3),
(1, 1, 4, 4, 4), (1, 1, 4, 4, 4),
(1, 1, 5, 5, 5)]: (1, 1, 5, 5, 5)]:
data = numpy.random.normal(0, 1, shp).astype(theano.config.floatX) * 10 data = np.random.normal(0, 1, shp).astype(theano.config.floatX) * 10
ws = 2 ws = 2
stride = 2 stride = 2
...@@ -390,7 +390,7 @@ def test_pooling_opt(): ...@@ -390,7 +390,7 @@ def test_pooling_opt():
assert any([isinstance(n.op, dnn.GpuDnnPool) assert any([isinstance(n.op, dnn.GpuDnnPool)
for n in f.maker.fgraph.toposort()]) for n in f.maker.fgraph.toposort()])
f(numpy.zeros((10, 10), dtype=theano.config.floatX)) f(np.zeros((10, 10), dtype=theano.config.floatX))
# gradient of 2D pooling # gradient of 2D pooling
f = theano.function( f = theano.function(
...@@ -403,7 +403,7 @@ def test_pooling_opt(): ...@@ -403,7 +403,7 @@ def test_pooling_opt():
assert any([isinstance(n.op, dnn.GpuDnnPoolGrad) assert any([isinstance(n.op, dnn.GpuDnnPoolGrad)
for n in f.maker.fgraph.toposort()]) for n in f.maker.fgraph.toposort()])
f(numpy.zeros((10, 10), dtype=theano.config.floatX)) f(np.zeros((10, 10), dtype=theano.config.floatX))
# Test sum pooling # Test sum pooling
f = theano.function( f = theano.function(
...@@ -414,7 +414,7 @@ def test_pooling_opt(): ...@@ -414,7 +414,7 @@ def test_pooling_opt():
assert any([isinstance(n.op, dnn.GpuDnnPool) assert any([isinstance(n.op, dnn.GpuDnnPool)
for n in f.maker.fgraph.toposort()]) for n in f.maker.fgraph.toposort()])
data = numpy.random.rand(10, 10).astype(theano.config.floatX) data = np.random.rand(10, 10).astype(theano.config.floatX)
f(data) f(data)
# 3D pooling # 3D pooling
...@@ -429,7 +429,7 @@ def test_pooling_opt(): ...@@ -429,7 +429,7 @@ def test_pooling_opt():
assert any([isinstance(n.op, dnn.GpuDnnPool) assert any([isinstance(n.op, dnn.GpuDnnPool)
for n in f.maker.fgraph.toposort()]) for n in f.maker.fgraph.toposort()])
f(numpy.zeros((10, 10, 10), dtype=theano.config.floatX)) f(np.zeros((10, 10, 10), dtype=theano.config.floatX))
# gradient of 3D pooling # gradient of 3D pooling
f = theano.function( f = theano.function(
...@@ -442,7 +442,7 @@ def test_pooling_opt(): ...@@ -442,7 +442,7 @@ def test_pooling_opt():
assert any([isinstance(n.op, dnn.GpuDnnPoolGrad) assert any([isinstance(n.op, dnn.GpuDnnPoolGrad)
for n in f.maker.fgraph.toposort()]) for n in f.maker.fgraph.toposort()])
f(numpy.zeros((10, 10, 10), dtype=theano.config.floatX)) f(np.zeros((10, 10, 10), dtype=theano.config.floatX))
def test_pooling_opt_arbitrary_dimensions(): def test_pooling_opt_arbitrary_dimensions():
...@@ -463,7 +463,7 @@ def test_pooling_opt_arbitrary_dimensions(): ...@@ -463,7 +463,7 @@ def test_pooling_opt_arbitrary_dimensions():
# create input shape: non-pooling dimensions # create input shape: non-pooling dimensions
# followed by 2 or 3 pooling dimensions # followed by 2 or 3 pooling dimensions
shp = tuple(range(2, 2 + n_non_pool_dims)) + tuple(range(5, 5 + len(ws))) shp = tuple(range(2, 2 + n_non_pool_dims)) + tuple(range(5, 5 + len(ws)))
data = numpy.random.normal(0, 1, shp).astype(theano.config.floatX) data = np.random.normal(0, 1, shp).astype(theano.config.floatX)
input = gpuarray_shared_constructor(data) input = gpuarray_shared_constructor(data)
for mode in modes: for mode in modes:
...@@ -543,8 +543,8 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -543,8 +543,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
if not dnn.dnn_available(test_ctx_name): if not dnn.dnn_available(test_ctx_name):
raise SkipTest(dnn.dnn_available.msg) raise SkipTest(dnn.dnn_available.msg)
t = T.tensor4('t') t = T.tensor4('t')
rand_tensor = numpy.asarray( rand_tensor = np.asarray(
numpy.random.rand(5, 4, 3, 2), np.random.rand(5, 4, 3, 2),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
self._compile_and_check( self._compile_and_check(
...@@ -573,11 +573,11 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -573,11 +573,11 @@ class TestDnnInferShapes(utt.InferShapeTester):
if not dnn.dnn_available(test_ctx_name): if not dnn.dnn_available(test_ctx_name):
raise SkipTest(dnn.dnn_available.msg) raise SkipTest(dnn.dnn_available.msg)
img_val = numpy.asarray(img_val, dtype=theano.config.floatX) img_val = np.asarray(img_val, dtype=theano.config.floatX)
kern_vals = numpy.asarray(kern_vals, dtype=theano.config.floatX) kern_vals = np.asarray(kern_vals, dtype=theano.config.floatX)
for subsample in subsamples: for subsample in subsamples:
out_vals = numpy.zeros( out_vals = np.zeros(
dnn.GpuDnnConv.get_out_shape(img_val.shape, kern_vals.shape, dnn.GpuDnnConv.get_out_shape(img_val.shape, kern_vals.shape,
border_mode=border_mode, border_mode=border_mode,
subsample=subsample), subsample=subsample),
...@@ -607,8 +607,8 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -607,8 +607,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
self._test_conv(T.tensor4('img'), self._test_conv(T.tensor4('img'),
T.tensor4('kerns'), T.tensor4('kerns'),
T.tensor4('out'), T.tensor4('out'),
numpy.random.rand(7, 2, 8, 4), np.random.rand(7, 2, 8, 4),
numpy.random.rand(8, 2, 4, 3), np.random.rand(8, 2, 4, 3),
border_mode, border_mode,
conv_mode, conv_mode,
[(1, 1), (2, 2)], [(1, 1), (2, 2)],
...@@ -619,8 +619,8 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -619,8 +619,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
self._test_conv(T.tensor5('img'), self._test_conv(T.tensor5('img'),
T.tensor5('kerns'), T.tensor5('kerns'),
T.tensor5('out'), T.tensor5('out'),
numpy.random.rand(10, 2, 6, 4, 11), np.random.rand(10, 2, 6, 4, 11),
numpy.random.rand(8, 2, 4, 3, 1), np.random.rand(8, 2, 4, 3, 1),
border_mode, border_mode,
conv_mode, conv_mode,
[(1, 1, 1), (2, 2, 2)], [(1, 1, 1), (2, 2, 2)],
...@@ -633,17 +633,17 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -633,17 +633,17 @@ class TestDnnInferShapes(utt.InferShapeTester):
topgrad_shape = get_conv_output_shape(img_shape, kerns_shape, topgrad_shape = get_conv_output_shape(img_shape, kerns_shape,
border_mode, subsample) border_mode, subsample)
img_val = numpy.asarray( img_val = np.asarray(
numpy.random.rand(*img_shape), np.random.rand(*img_shape),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
topgrad_vals = numpy.asarray( topgrad_vals = np.asarray(
numpy.random.rand(*topgrad_shape), np.random.rand(*topgrad_shape),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
kerns_vals = numpy.zeros(kerns_shape, dtype=theano.config.floatX) kerns_vals = np.zeros(kerns_shape, dtype=theano.config.floatX)
kerns_shape = theano.shared(numpy.asarray(kerns_shape)) kerns_shape = theano.shared(np.asarray(kerns_shape))
desc = dnn.GpuDnnConvDesc( desc = dnn.GpuDnnConvDesc(
border_mode=border_mode, border_mode=border_mode,
subsample=subsample, subsample=subsample,
...@@ -680,12 +680,12 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -680,12 +680,12 @@ class TestDnnInferShapes(utt.InferShapeTester):
img = T.tensor4('img') img = T.tensor4('img')
kerns = T.tensor4('kerns') kerns = T.tensor4('kerns')
out = T.tensor4('out') out = T.tensor4('out')
kern_vals = numpy.asarray( kern_vals = np.asarray(
numpy.random.rand(13, 14, 15, 16), np.random.rand(13, 14, 15, 16),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
out_vals = numpy.asarray( out_vals = np.asarray(
numpy.random.rand(3, 13, 5, 6), np.random.rand(3, 13, 5, 6),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
...@@ -699,7 +699,7 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -699,7 +699,7 @@ class TestDnnInferShapes(utt.InferShapeTester):
out_vals.shape[2] + kern_vals.shape[2] - 1, out_vals.shape[2] + kern_vals.shape[2] - 1,
out_vals.shape[3] + kern_vals.shape[3] - 1 out_vals.shape[3] + kern_vals.shape[3] - 1
) )
img_vals = numpy.zeros(shape, dtype=theano.config.floatX) img_vals = np.zeros(shape, dtype=theano.config.floatX)
desc = dnn.GpuDnnConvDesc( desc = dnn.GpuDnnConvDesc(
border_mode=params[0], border_mode=params[0],
subsample=params[1], subsample=params[1],
...@@ -723,8 +723,8 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -723,8 +723,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
if not dnn.dnn_available(test_ctx_name): if not dnn.dnn_available(test_ctx_name):
raise SkipTest(dnn.dnn_available.msg) raise SkipTest(dnn.dnn_available.msg)
img = T.tensor4('img') img = T.tensor4('img')
img_val = numpy.asarray( img_val = np.asarray(
numpy.random.rand(2, 3, 4, 5), np.random.rand(2, 3, 4, 5),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
...@@ -750,8 +750,8 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -750,8 +750,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
if not dnn.dnn_available(test_ctx_name): if not dnn.dnn_available(test_ctx_name):
raise SkipTest(dnn.dnn_available.msg) raise SkipTest(dnn.dnn_available.msg)
img = T.tensor5('img') img = T.tensor5('img')
img_val = numpy.asarray( img_val = np.asarray(
numpy.random.rand(2, 3, 4, 5, 6), np.random.rand(2, 3, 4, 5, 6),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
...@@ -779,16 +779,16 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -779,16 +779,16 @@ class TestDnnInferShapes(utt.InferShapeTester):
img = T.tensor4('img') img = T.tensor4('img')
img_grad = T.tensor4('img_grad') img_grad = T.tensor4('img_grad')
out = T.tensor4('out') out = T.tensor4('out')
img_val = numpy.asarray( img_val = np.asarray(
numpy.random.rand(2, 3, 4, 5), np.random.rand(2, 3, 4, 5),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
img_grad_val = numpy.asarray( img_grad_val = np.asarray(
numpy.random.rand(2, 3, 4, 5), np.random.rand(2, 3, 4, 5),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
out_val = numpy.asarray( out_val = np.asarray(
numpy.random.rand(2, 3, 4, 5), np.random.rand(2, 3, 4, 5),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
...@@ -818,16 +818,16 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -818,16 +818,16 @@ class TestDnnInferShapes(utt.InferShapeTester):
img = T.tensor5('img') img = T.tensor5('img')
img_grad = T.tensor5('img_grad') img_grad = T.tensor5('img_grad')
out = T.tensor5('out') out = T.tensor5('out')
img_val = numpy.asarray( img_val = np.asarray(
numpy.random.rand(2, 3, 4, 5, 6), np.random.rand(2, 3, 4, 5, 6),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
img_grad_val = numpy.asarray( img_grad_val = np.asarray(
numpy.random.rand(2, 3, 4, 5, 6), np.random.rand(2, 3, 4, 5, 6),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
out_val = numpy.asarray( out_val = np.asarray(
numpy.random.rand(2, 3, 4, 5, 6), np.random.rand(2, 3, 4, 5, 6),
dtype=theano.config.floatX dtype=theano.config.floatX
) )
...@@ -880,16 +880,16 @@ def test_dnn_conv_alpha_output_merge(): ...@@ -880,16 +880,16 @@ def test_dnn_conv_alpha_output_merge():
iw = 8 iw = 8
kh = 2 kh = 2
kw = 6 kw = 6
img_val = numpy.random.random((b, c, ih, iw)).astype(theano.config.floatX) img_val = np.random.random((b, c, ih, iw)).astype(theano.config.floatX)
kern_val = numpy.random.random((f, c, kh, kw)).astype(theano.config.floatX) kern_val = np.random.random((f, c, kh, kw)).astype(theano.config.floatX)
out_val = numpy.random.random((b, f, ih - kh + 1, out_val = np.random.random((b, f, ih - kh + 1,
iw - kw + 1)).astype(theano.config.floatX) iw - kw + 1)).astype(theano.config.floatX)
conv = dnn.dnn_conv(img, kern) conv = dnn.dnn_conv(img, kern)
gw = theano.grad(conv.sum(), kern) gw = theano.grad(conv.sum(), kern)
gi = theano.grad(conv.sum(), img) gi = theano.grad(conv.sum(), img)
lr = numpy.asarray(0.05, dtype=theano.config.floatX) lr = np.asarray(0.05, dtype=theano.config.floatX)
fr = lr * (conv + out) fr = lr * (conv + out)
wr = kern + lr * gw wr = kern + lr * gw
...@@ -939,10 +939,10 @@ def test_dnn_conv_grad(): ...@@ -939,10 +939,10 @@ def test_dnn_conv_grad():
iw = 8 iw = 8
kh = 2 kh = 2
kw = 2 kw = 2
img_val = numpy.random.random((b, c, ih, iw)).astype(theano.config.floatX) img_val = np.random.random((b, c, ih, iw)).astype(theano.config.floatX)
kern_val = numpy.random.random((f, c, kh, kw)).astype(theano.config.floatX) kern_val = np.random.random((f, c, kh, kw)).astype(theano.config.floatX)
out_val = numpy.random.random((b, f, ih - kw + 1, out_val = np.random.random((b, f, ih - kw + 1,
iw - kw + 1)).astype(theano.config.floatX) iw - kw + 1)).astype(theano.config.floatX)
def dconv(img, kern, out): def dconv(img, kern, out):
desc = dnn.GpuDnnConvDesc(border_mode='valid', subsample=(1, 1), desc = dnn.GpuDnnConvDesc(border_mode='valid', subsample=(1, 1),
...@@ -1008,8 +1008,8 @@ def test_conv3d_fwd(): ...@@ -1008,8 +1008,8 @@ def test_conv3d_fwd():
def run_conv3d_fwd(inputs_shape, filters_shape, subsample, def run_conv3d_fwd(inputs_shape, filters_shape, subsample,
border_mode, conv_mode): border_mode, conv_mode):
inputs_val = numpy.random.random(inputs_shape).astype(theano.config.floatX) inputs_val = np.random.random(inputs_shape).astype(theano.config.floatX)
filters_val = numpy.random.random(filters_shape).astype(theano.config.floatX) filters_val = np.random.random(filters_shape).astype(theano.config.floatX)
# Scale down the input values to prevent very large absolute errors # Scale down the input values to prevent very large absolute errors
# due to float rounding # due to float rounding
...@@ -1062,8 +1062,8 @@ def test_conv3d_bwd(): ...@@ -1062,8 +1062,8 @@ def test_conv3d_bwd():
def run_conv3d_bwd(inputs_shape, filters_shape, subsample, def run_conv3d_bwd(inputs_shape, filters_shape, subsample,
border_mode, conv_mode): border_mode, conv_mode):
inputs_val = numpy.random.random(inputs_shape).astype(theano.config.floatX) inputs_val = np.random.random(inputs_shape).astype(theano.config.floatX)
filters_val = numpy.random.random(filters_shape).astype(theano.config.floatX) filters_val = np.random.random(filters_shape).astype(theano.config.floatX)
inputs = theano.shared(inputs_val) inputs = theano.shared(inputs_val)
filters = theano.shared(filters_val) filters = theano.shared(filters_val)
...@@ -1148,8 +1148,8 @@ class test_SoftMax(test_nnet.test_SoftMax): ...@@ -1148,8 +1148,8 @@ class test_SoftMax(test_nnet.test_SoftMax):
def test_softmax_shape_0(self): def test_softmax_shape_0(self):
dims = (2, 0, 4, 5) dims = (2, 0, 4, 5)
data = numpy.arange( data = np.arange(
numpy.product(dims), np.product(dims),
dtype=theano.config.floatX dtype=theano.config.floatX
).reshape(dims) ).reshape(dims)
...@@ -1176,22 +1176,22 @@ class test_SoftMax(test_nnet.test_SoftMax): ...@@ -1176,22 +1176,22 @@ class test_SoftMax(test_nnet.test_SoftMax):
) )
def cmp(n, m, f, f_gpu): def cmp(n, m, f, f_gpu):
data = numpy.random.random((n, m)).astype('float16') data = np.random.random((n, m)).astype('float16')
gdata = numpy.asarray(data)[:, :, None, None] gdata = np.asarray(data)[:, :, None, None]
out = f(data) out = f(data)
gout = numpy.asarray(f_gpu(gdata))[:, :, 0, 0] gout = np.asarray(f_gpu(gdata))[:, :, 0, 0]
utt.assert_allclose(out, gout) utt.assert_allclose(out, gout)
self._test_softmax(x, x_gpu, f_z, f_gpu, cmp) self._test_softmax(x, x_gpu, f_z, f_gpu, cmp)
def test_softmax_grad(self): def test_softmax_grad(self):
def cmp(n, m, f, f_gpu): def cmp(n, m, f, f_gpu):
data = numpy.arange(n * m, dtype=theano.config.floatX).reshape(n, m) data = np.arange(n * m, dtype=theano.config.floatX).reshape(n, m)
gdata = numpy.asarray(data)[:, :, None, None] gdata = np.asarray(data)[:, :, None, None]
out = f(data) out = f(data)
gout = numpy.asarray(f_gpu(gdata))[:, :, 0, 0] gout = np.asarray(f_gpu(gdata))[:, :, 0, 0]
utt.assert_allclose(out, gout) utt.assert_allclose(out, gout)
x = T.matrix('x') x = T.matrix('x')
...@@ -1204,11 +1204,11 @@ class test_SoftMax(test_nnet.test_SoftMax): ...@@ -1204,11 +1204,11 @@ class test_SoftMax(test_nnet.test_SoftMax):
# Verify the grad operation # Verify the grad operation
dims = (2, 3, 4, 5) dims = (2, 3, 4, 5)
gdata = numpy.arange( gdata = np.arange(
numpy.product(dims), np.product(dims),
dtype=theano.config.floatX dtype=theano.config.floatX
).reshape(dims) ).reshape(dims)
T.verify_grad(f_gpu, [gdata], rng=numpy.random, T.verify_grad(f_gpu, [gdata], rng=np.random,
mode=mode_with_gpu) mode=mode_with_gpu)
# Verify that the CPU and GPU implementations return the same results # Verify that the CPU and GPU implementations return the same results
...@@ -1235,7 +1235,7 @@ class test_SoftMax(test_nnet.test_SoftMax): ...@@ -1235,7 +1235,7 @@ class test_SoftMax(test_nnet.test_SoftMax):
mode=mode_with_gpu mode=mode_with_gpu
) )
sorted_f = f.maker.fgraph.toposort() sorted_f = f.maker.fgraph.toposort()
val = numpy.random.rand(5).astype(theano.config.floatX) val = np.random.rand(5).astype(theano.config.floatX)
out_dnn = f(val) out_dnn = f(val)
assert(len([i assert(len([i
for i in sorted_f for i in sorted_f
...@@ -1325,11 +1325,11 @@ class test_SoftMax(test_nnet.test_SoftMax): ...@@ -1325,11 +1325,11 @@ class test_SoftMax(test_nnet.test_SoftMax):
(2, 3, 4, 66000)] (2, 3, 4, 66000)]
for inp_shape in input_shapes: for inp_shape in input_shapes:
input_val = numpy.random.normal(0, 1, inp_shape).astype(theano.config.floatX) input_val = np.random.normal(0, 1, inp_shape).astype(theano.config.floatX)
out = f(input_val) out = f(input_val)
expected_out = numpy.log(numpy.exp(input_val) / expected_out = np.log(np.exp(input_val) /
numpy.exp(input_val).sum(1)[:, None, :, :]) np.exp(input_val).sum(1)[:, None, :, :])
utt.assert_allclose(out, expected_out) utt.assert_allclose(out, expected_out)
...@@ -1357,7 +1357,7 @@ class test_SoftMax(test_nnet.test_SoftMax): ...@@ -1357,7 +1357,7 @@ class test_SoftMax(test_nnet.test_SoftMax):
assert dnn_softmax_nodes[0].op.algo == "log" assert dnn_softmax_nodes[0].op.algo == "log"
# Compare the output of the function with the reference function # Compare the output of the function with the reference function
inp = numpy.random.normal(0, 1, (5, 6)).astype(theano.config.floatX) inp = np.random.normal(0, 1, (5, 6)).astype(theano.config.floatX)
utt.assert_allclose(f(inp), f_ref(inp)) utt.assert_allclose(f(inp), f_ref(inp))
# Build the first graph and ensure that the optimization is applied # Build the first graph and ensure that the optimization is applied
...@@ -1370,7 +1370,7 @@ class test_SoftMax(test_nnet.test_SoftMax): ...@@ -1370,7 +1370,7 @@ class test_SoftMax(test_nnet.test_SoftMax):
assert dnn_softmax_nodes[0].op.algo == "log" assert dnn_softmax_nodes[0].op.algo == "log"
# Compare the output of the function with the reference function # Compare the output of the function with the reference function
inp = numpy.random.normal(0, 1, (5, 6)).astype(theano.config.floatX) inp = np.random.normal(0, 1, (5, 6)).astype(theano.config.floatX)
utt.assert_allclose(f(inp), f_ref(inp)) utt.assert_allclose(f(inp), f_ref(inp))
...@@ -1412,10 +1412,10 @@ def test_dnn_batchnorm_train(): ...@@ -1412,10 +1412,10 @@ def test_dnn_batchnorm_train():
data_shape = data_shape[:ndim] data_shape = data_shape[:ndim]
param_shape = tuple(1 if d in axes else s param_shape = tuple(1 if d in axes else s
for d, s in enumerate(data_shape)) for d, s in enumerate(data_shape))
X = 4 + 3 * numpy.random.randn(*data_shape).astype(theano.config.floatX) X = 4 + 3 * np.random.randn(*data_shape).astype(theano.config.floatX)
Dy = -1 + 2 * numpy.random.randn(*data_shape).astype(theano.config.floatX) Dy = -1 + 2 * np.random.randn(*data_shape).astype(theano.config.floatX)
Scale = numpy.random.randn(*param_shape).astype(theano.config.floatX) Scale = np.random.randn(*param_shape).astype(theano.config.floatX)
Bias = numpy.random.randn(*param_shape).astype(theano.config.floatX) Bias = np.random.randn(*param_shape).astype(theano.config.floatX)
outputs = f(X, Scale, Bias, Dy) outputs = f(X, Scale, Bias, Dy)
# compare outputs # compare outputs
utt.assert_allclose(outputs[0], outputs[0 + 3]) # out utt.assert_allclose(outputs[0], outputs[0 + 3]) # out
...@@ -1463,12 +1463,12 @@ def test_batchnorm_inference(): ...@@ -1463,12 +1463,12 @@ def test_batchnorm_inference():
data_shape = data_shape[:ndim] data_shape = data_shape[:ndim]
param_shape = tuple(1 if d in axes else s param_shape = tuple(1 if d in axes else s
for d, s in enumerate(data_shape)) for d, s in enumerate(data_shape))
X = 4 + 3 * numpy.random.randn(*data_shape).astype(theano.config.floatX) X = 4 + 3 * np.random.randn(*data_shape).astype(theano.config.floatX)
Dy = -1 + 2 * numpy.random.randn(*data_shape).astype(theano.config.floatX) Dy = -1 + 2 * np.random.randn(*data_shape).astype(theano.config.floatX)
Scale = numpy.random.randn(*param_shape).astype(theano.config.floatX) Scale = np.random.randn(*param_shape).astype(theano.config.floatX)
Bias = numpy.random.randn(*param_shape).astype(theano.config.floatX) Bias = np.random.randn(*param_shape).astype(theano.config.floatX)
Mean = numpy.random.randn(*param_shape).astype(theano.config.floatX) Mean = np.random.randn(*param_shape).astype(theano.config.floatX)
Var = numpy.random.rand(*param_shape).astype(theano.config.floatX) Var = np.random.rand(*param_shape).astype(theano.config.floatX)
outputs = f(X, Scale, Bias, Mean, Var, Dy) outputs = f(X, Scale, Bias, Mean, Var, Dy)
# compare outputs # compare outputs
utt.assert_allclose(outputs[0], outputs[1]) # out utt.assert_allclose(outputs[0], outputs[1]) # out
...@@ -1496,7 +1496,7 @@ def test_dnn_rnn_gru(): ...@@ -1496,7 +1496,7 @@ def test_dnn_rnn_gru():
rnnb = dnn.RNNBlock(theano.config.floatX, hidden_dim, depth, 'gru') rnnb = dnn.RNNBlock(theano.config.floatX, hidden_dim, depth, 'gru')
psize = rnnb.get_param_size([batch_size, input_dim]) psize = rnnb.get_param_size([batch_size, input_dim])
params_cudnn = gpuarray_shared_constructor( params_cudnn = gpuarray_shared_constructor(
numpy.zeros((psize,), dtype=theano.config.floatX)) np.zeros((psize,), dtype=theano.config.floatX))
model = Model() model = Model()
last_layer = WrapperLayer(X) last_layer = WrapperLayer(X)
...@@ -1551,9 +1551,9 @@ def test_dnn_rnn_gru(): ...@@ -1551,9 +1551,9 @@ def test_dnn_rnn_gru():
ref_grad_fns = [ref_grad_fn, ref2_grad_fn, ref3_grad_fn] ref_grad_fns = [ref_grad_fn, ref2_grad_fn, ref3_grad_fn]
cudnn_grad_fns = [cudnn_grad_fn, cudnn2_grad_fn, cudnn3_grad_fn] cudnn_grad_fns = [cudnn_grad_fn, cudnn2_grad_fn, cudnn3_grad_fn]
x_val = numpy.random.random((timesteps, batch_size, input_dim)).astype(theano.config.floatX) x_val = np.random.random((timesteps, batch_size, input_dim)).astype(theano.config.floatX)
y_val = numpy.random.random((timesteps, batch_size, hidden_dim)).astype(theano.config.floatX) y_val = np.random.random((timesteps, batch_size, hidden_dim)).astype(theano.config.floatX)
h0_val = numpy.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX) h0_val = np.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX)
ref_out = ref_fn(x_val, h0_val) ref_out = ref_fn(x_val, h0_val)
cudnn_out = cudnn_fn(x_val, h0_val) cudnn_out = cudnn_fn(x_val, h0_val)
...@@ -1595,7 +1595,7 @@ def test_dnn_rnn_gru_bidi(): ...@@ -1595,7 +1595,7 @@ def test_dnn_rnn_gru_bidi():
rnnb = dnn.RNNBlock(theano.config.floatX, hidden_dim, depth, 'gru', direction_mode='bidirectional') rnnb = dnn.RNNBlock(theano.config.floatX, hidden_dim, depth, 'gru', direction_mode='bidirectional')
psize = rnnb.get_param_size([batch_size, input_dim]) psize = rnnb.get_param_size([batch_size, input_dim])
params_cudnn = gpuarray_shared_constructor( params_cudnn = gpuarray_shared_constructor(
numpy.random.random((psize,)).astype(theano.config.floatX)) np.random.random((psize,)).astype(theano.config.floatX))
def funcs(out, params, hy=None): def funcs(out, params, hy=None):
cost = 0 cost = 0
...@@ -1618,9 +1618,9 @@ def test_dnn_rnn_gru_bidi(): ...@@ -1618,9 +1618,9 @@ def test_dnn_rnn_gru_bidi():
cudnn_grad_fns = [cudnn_grad_fn, cudnn2_grad_fn, cudnn3_grad_fn] cudnn_grad_fns = [cudnn_grad_fn, cudnn2_grad_fn, cudnn3_grad_fn]
x_val = numpy.random.random((timesteps, batch_size, input_dim)).astype(theano.config.floatX) x_val = np.random.random((timesteps, batch_size, input_dim)).astype(theano.config.floatX)
y_val = numpy.random.random((timesteps, batch_size, 2 * hidden_dim)).astype(theano.config.floatX) y_val = np.random.random((timesteps, batch_size, 2 * hidden_dim)).astype(theano.config.floatX)
h0_val = numpy.random.random((2 * depth, batch_size, hidden_dim)).astype(theano.config.floatX) h0_val = np.random.random((2 * depth, batch_size, hidden_dim)).astype(theano.config.floatX)
cudnn_fn(x_val, h0_val) cudnn_fn(x_val, h0_val)
...@@ -1645,7 +1645,7 @@ def test_dnn_rnn_lstm(): ...@@ -1645,7 +1645,7 @@ def test_dnn_rnn_lstm():
rnnb = dnn.RNNBlock(theano.config.floatX, hidden_dim, depth, 'lstm') rnnb = dnn.RNNBlock(theano.config.floatX, hidden_dim, depth, 'lstm')
psize = rnnb.get_param_size([batch_size, input_dim]) psize = rnnb.get_param_size([batch_size, input_dim])
params_cudnn = gpuarray_shared_constructor( params_cudnn = gpuarray_shared_constructor(
numpy.zeros((psize,), dtype=theano.config.floatX)) np.zeros((psize,), dtype=theano.config.floatX))
model = Model() model = Model()
last_layer = WrapperLayer(X) last_layer = WrapperLayer(X)
...@@ -1674,10 +1674,10 @@ def test_dnn_rnn_lstm(): ...@@ -1674,10 +1674,10 @@ def test_dnn_rnn_lstm():
cudnn_fn, cudnn_grad_fn = funcs(rnnb.apply(params_cudnn, X, h0, c0)[0], cudnn_fn, cudnn_grad_fn = funcs(rnnb.apply(params_cudnn, X, h0, c0)[0],
[params_cudnn]) [params_cudnn])
x_val = numpy.random.random((timesteps, batch_size, input_dim)).astype(theano.config.floatX) x_val = np.random.random((timesteps, batch_size, input_dim)).astype(theano.config.floatX)
y_val = numpy.random.random((timesteps, batch_size, hidden_dim)).astype(theano.config.floatX) y_val = np.random.random((timesteps, batch_size, hidden_dim)).astype(theano.config.floatX)
h0_val = numpy.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX) h0_val = np.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX)
c0_val = numpy.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX) c0_val = np.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX)
ref_out = ref_fn(x_val, h0_val, c0_val) ref_out = ref_fn(x_val, h0_val, c0_val)
cudnn_out = cudnn_fn(x_val, h0_val, c0_val) cudnn_out = cudnn_fn(x_val, h0_val, c0_val)
...@@ -1720,7 +1720,7 @@ def test_dnn_rnn_lstm_grad_c(): ...@@ -1720,7 +1720,7 @@ def test_dnn_rnn_lstm_grad_c():
rnnb = dnn.RNNBlock(theano.config.floatX, hidden_dim, depth, 'lstm') rnnb = dnn.RNNBlock(theano.config.floatX, hidden_dim, depth, 'lstm')
psize = rnnb.get_param_size([batch_size, input_dim]) psize = rnnb.get_param_size([batch_size, input_dim])
params_cudnn = gpuarray_shared_constructor( params_cudnn = gpuarray_shared_constructor(
numpy.zeros((psize,), dtype=theano.config.floatX)) np.zeros((psize,), dtype=theano.config.floatX))
model = Model() model = Model()
last_layer = WrapperLayer(X) last_layer = WrapperLayer(X)
...@@ -1751,10 +1751,10 @@ def test_dnn_rnn_lstm_grad_c(): ...@@ -1751,10 +1751,10 @@ def test_dnn_rnn_lstm_grad_c():
ref_grad_fn = funcs(ref_cy, model.get_params()) ref_grad_fn = funcs(ref_cy, model.get_params())
cudnn_grad_fn = funcs(cy, [params_cudnn]) cudnn_grad_fn = funcs(cy, [params_cudnn])
x_val = numpy.random.random((timesteps, batch_size, input_dim)).astype(theano.config.floatX) x_val = np.random.random((timesteps, batch_size, input_dim)).astype(theano.config.floatX)
cy_val = numpy.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX) cy_val = np.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX)
h0_val = numpy.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX) h0_val = np.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX)
c0_val = numpy.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX) c0_val = np.random.random((depth, batch_size, hidden_dim)).astype(theano.config.floatX)
ref_grads = ref_grad_fn(x_val, cy_val, h0_val, c0_val) ref_grads = ref_grad_fn(x_val, cy_val, h0_val, c0_val)
cudnn_grads = cudnn_grad_fn(x_val, cy_val, h0_val, c0_val) cudnn_grads = cudnn_grad_fn(x_val, cy_val, h0_val, c0_val)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论