提交 80728319 authored 作者: Benjamin Scellier's avatar Benjamin Scellier

file theano/gpuarray/tests/test_subtensor.py

上级 e6576b19
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import unittest import unittest
import theano import theano
...@@ -47,8 +47,8 @@ def test_advinc_subtensor1(): ...@@ -47,8 +47,8 @@ def test_advinc_subtensor1():
# Test the second case in the opt local_gpu_advanced_incsubtensor1 # Test the second case in the opt local_gpu_advanced_incsubtensor1
for shp in [(3, 3), (3, 3, 3)]: for shp in [(3, 3), (3, 3, 3)]:
shared = gpuarray_shared_constructor shared = gpuarray_shared_constructor
xval = numpy.arange(numpy.prod(shp), dtype='float32').reshape(shp) + 1 xval = np.arange(np.prod(shp), dtype='float32').reshape(shp) + 1
yval = numpy.empty((2,) + shp[1:], dtype='float32') yval = np.empty((2,) + shp[1:], dtype='float32')
yval[:] = 10 yval[:] = 10
x = shared(xval, name='x') x = shared(xval, name='x')
y = tensor.tensor(dtype='float32', y = tensor.tensor(dtype='float32',
...@@ -61,7 +61,7 @@ def test_advinc_subtensor1(): ...@@ -61,7 +61,7 @@ def test_advinc_subtensor1():
rval = f(yval) rval = f(yval)
rep = xval.copy() rep = xval.copy()
rep[[0, 2]] += yval rep[[0, 2]] += yval
assert numpy.allclose(rval, rep) assert np.allclose(rval, rep)
def test_advinc_subtensor1_dtype(): def test_advinc_subtensor1_dtype():
...@@ -69,8 +69,8 @@ def test_advinc_subtensor1_dtype(): ...@@ -69,8 +69,8 @@ def test_advinc_subtensor1_dtype():
shp = (3, 4) shp = (3, 4)
for dtype1, dtype2 in [('float32', 'int8'), ('float32', 'float64')]: for dtype1, dtype2 in [('float32', 'int8'), ('float32', 'float64')]:
shared = gpuarray_shared_constructor shared = gpuarray_shared_constructor
xval = numpy.arange(numpy.prod(shp), dtype=dtype1).reshape(shp) + 1 xval = np.arange(np.prod(shp), dtype=dtype1).reshape(shp) + 1
yval = numpy.empty((2,) + shp[1:], dtype=dtype2) yval = np.empty((2,) + shp[1:], dtype=dtype2)
yval[:] = 10 yval[:] = 10
x = shared(xval, name='x') x = shared(xval, name='x')
y = tensor.tensor(dtype=yval.dtype, y = tensor.tensor(dtype=yval.dtype,
...@@ -83,7 +83,7 @@ def test_advinc_subtensor1_dtype(): ...@@ -83,7 +83,7 @@ def test_advinc_subtensor1_dtype():
rval = f(yval) rval = f(yval)
rep = xval.copy() rep = xval.copy()
rep[[0, 2]] += yval rep[[0, 2]] += yval
assert numpy.allclose(rval, rep) assert np.allclose(rval, rep)
def test_advinc_subtensor1_vector_scalar(): def test_advinc_subtensor1_vector_scalar():
...@@ -91,8 +91,8 @@ def test_advinc_subtensor1_vector_scalar(): ...@@ -91,8 +91,8 @@ def test_advinc_subtensor1_vector_scalar():
shp = (3,) shp = (3,)
for dtype1, dtype2 in [('float32', 'int8'), ('float32', 'float64')]: for dtype1, dtype2 in [('float32', 'int8'), ('float32', 'float64')]:
shared = gpuarray_shared_constructor shared = gpuarray_shared_constructor
xval = numpy.arange(numpy.prod(shp), dtype=dtype1).reshape(shp) + 1 xval = np.arange(np.prod(shp), dtype=dtype1).reshape(shp) + 1
yval = numpy.asarray(10, dtype=dtype2) yval = np.asarray(10, dtype=dtype2)
x = shared(xval, name='x') x = shared(xval, name='x')
y = tensor.tensor(dtype=yval.dtype, y = tensor.tensor(dtype=yval.dtype,
broadcastable=(False,) * len(yval.shape), broadcastable=(False,) * len(yval.shape),
...@@ -104,14 +104,14 @@ def test_advinc_subtensor1_vector_scalar(): ...@@ -104,14 +104,14 @@ def test_advinc_subtensor1_vector_scalar():
rval = f(yval) rval = f(yval)
rep = xval.copy() rep = xval.copy()
rep[[0, 2]] += yval rep[[0, 2]] += yval
assert numpy.allclose(rval, rep) assert np.allclose(rval, rep)
def test_incsub_f16(): def test_incsub_f16():
shp = (3, 3) shp = (3, 3)
shared = gpuarray_shared_constructor shared = gpuarray_shared_constructor
xval = numpy.arange(numpy.prod(shp), dtype='float16').reshape(shp) + 1 xval = np.arange(np.prod(shp), dtype='float16').reshape(shp) + 1
yval = numpy.empty((2,) + shp[1:], dtype='float16') yval = np.empty((2,) + shp[1:], dtype='float16')
yval[:] = 2 yval[:] = 2
x = shared(xval, name='x') x = shared(xval, name='x')
y = tensor.tensor(dtype='float16', y = tensor.tensor(dtype='float16',
...@@ -124,7 +124,7 @@ def test_incsub_f16(): ...@@ -124,7 +124,7 @@ def test_incsub_f16():
rval = f(yval) rval = f(yval)
rep = xval.copy() rep = xval.copy()
rep[[0, 2]] += yval rep[[0, 2]] += yval
assert numpy.allclose(rval, rep) assert np.allclose(rval, rep)
expr = tensor.inc_subtensor(x[1:], y) expr = tensor.inc_subtensor(x[1:], y)
f = theano.function([y], expr, mode=mode_with_gpu) f = theano.function([y], expr, mode=mode_with_gpu)
...@@ -133,7 +133,7 @@ def test_incsub_f16(): ...@@ -133,7 +133,7 @@ def test_incsub_f16():
rval = f(yval) rval = f(yval)
rep = xval.copy() rep = xval.copy()
rep[1:] += yval rep[1:] += yval
assert numpy.allclose(rval, rep) assert np.allclose(rval, rep)
class G_advancedsubtensor(test_subtensor.TestAdvancedSubtensor): class G_advancedsubtensor(test_subtensor.TestAdvancedSubtensor):
...@@ -159,7 +159,7 @@ def test_adv_subtensor(): ...@@ -159,7 +159,7 @@ def test_adv_subtensor():
# Test the advancedsubtensor on gpu. # Test the advancedsubtensor on gpu.
shp = (2, 3, 4) shp = (2, 3, 4)
shared = gpuarray_shared_constructor shared = gpuarray_shared_constructor
xval = numpy.arange(numpy.prod(shp), dtype=theano.config.floatX).reshape(shp) xval = np.arange(np.prod(shp), dtype=theano.config.floatX).reshape(shp)
idx1, idx2 = tensor.ivectors('idx1', 'idx2') idx1, idx2 = tensor.ivectors('idx1', 'idx2')
idxs = [idx1, None, slice(0, 2, 1), idx2, None] idxs = [idx1, None, slice(0, 2, 1), idx2, None]
x = shared(xval, name='x') x = shared(xval, name='x')
...@@ -171,27 +171,27 @@ def test_adv_subtensor(): ...@@ -171,27 +171,27 @@ def test_adv_subtensor():
idx2_val = [0, 1] idx2_val = [0, 1]
rval = f(idx1_val, idx2_val) rval = f(idx1_val, idx2_val)
rep = xval[idx1_val, None, slice(0, 2, 1), idx2_val, None] rep = xval[idx1_val, None, slice(0, 2, 1), idx2_val, None]
assert numpy.allclose(rval, rep) assert np.allclose(rval, rep)
class test_gpudiagonal(unittest.TestCase): class test_gpudiagonal(unittest.TestCase):
def test_matrix(self): def test_matrix(self):
x = tensor.matrix() x = tensor.matrix()
np_x = numpy.arange(77).reshape(7, 11).astype(theano.config.floatX) np_x = np.arange(77).reshape(7, 11).astype(theano.config.floatX)
fn = theano.function([x], GpuDiagonal()(x), mode=mode_with_gpu) fn = theano.function([x], GpuDiagonal()(x), mode=mode_with_gpu)
assert numpy.allclose(fn(np_x), np_x.diagonal()) assert np.allclose(fn(np_x), np_x.diagonal())
fn = theano.function([x], GpuDiagonal(2)(x), mode=mode_with_gpu) fn = theano.function([x], GpuDiagonal(2)(x), mode=mode_with_gpu)
assert numpy.allclose(fn(np_x), np_x.diagonal(2)) assert np.allclose(fn(np_x), np_x.diagonal(2))
fn = theano.function([x], GpuDiagonal(-3)(x), mode=mode_with_gpu) fn = theano.function([x], GpuDiagonal(-3)(x), mode=mode_with_gpu)
assert numpy.allclose(fn(np_x), np_x.diagonal(-3)) assert np.allclose(fn(np_x), np_x.diagonal(-3))
def test_tensor(self): def test_tensor(self):
x = tensor.tensor4() x = tensor.tensor4()
np_x = numpy.arange(30107).reshape(7, 11, 17, 23).astype(theano.config.floatX) np_x = np.arange(30107).reshape(7, 11, 17, 23).astype(theano.config.floatX)
for offset, axis1, axis2 in [ for offset, axis1, axis2 in [
(1, 0, 1), (-1, 0, 1), (0, 1, 0), (-2, 1, 0), (1, 0, 1), (-1, 0, 1), (0, 1, 0), (-2, 1, 0),
(-3, 1, 0), (-2, 2, 0), (3, 3, 0), (-1, 3, 2), (-3, 1, 0), (-2, 2, 0), (3, 3, 0), (-1, 3, 2),
(2, 2, 3), (-1, 2, 1), (1, 3, 1), (-1, 1, 3)]: (2, 2, 3), (-1, 2, 1), (1, 3, 1), (-1, 1, 3)]:
assert numpy.allclose( assert np.allclose(
GpuDiagonal(offset, axis1, axis2)(x).eval({x: np_x}), GpuDiagonal(offset, axis1, axis2)(x).eval({x: np_x}),
np_x.diagonal(offset, axis1, axis2)) np_x.diagonal(offset, axis1, axis2))
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论