提交 da609fe9 authored 作者: amrithasuresh's avatar amrithasuresh

Updated numpy as np

上级 89533e9f
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import sys import sys
import numpy import numpy as np
from unittest import TestCase from unittest import TestCase
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
...@@ -44,9 +44,9 @@ class TestCGer(TestCase, TestOptimizationMixin): ...@@ -44,9 +44,9 @@ class TestCGer(TestCase, TestOptimizationMixin):
self.a = tensor.tensor(dtype=dtype, broadcastable=()) self.a = tensor.tensor(dtype=dtype, broadcastable=())
self.x = tensor.tensor(dtype=dtype, broadcastable=(False,)) self.x = tensor.tensor(dtype=dtype, broadcastable=(False,))
self.y = tensor.tensor(dtype=dtype, broadcastable=(False,)) self.y = tensor.tensor(dtype=dtype, broadcastable=(False,))
self.Aval = numpy.ones((2, 3), dtype=dtype) self.Aval = np.ones((2, 3), dtype=dtype)
self.xval = numpy.asarray([1, 2], dtype=dtype) self.xval = np.asarray([1, 2], dtype=dtype)
self.yval = numpy.asarray([1.5, 2.7, 3.9], dtype=dtype) self.yval = np.asarray([1.5, 2.7, 3.9], dtype=dtype)
def function(self, inputs, outputs): def function(self, inputs, outputs):
return theano.function(inputs, outputs, return theano.function(inputs, outputs,
...@@ -59,7 +59,7 @@ class TestCGer(TestCase, TestOptimizationMixin): ...@@ -59,7 +59,7 @@ class TestCGer(TestCase, TestOptimizationMixin):
f(self.Aval[::-1, ::-1], self.xval, self.yval) f(self.Aval[::-1, ::-1], self.xval, self.yval)
def b(self, bval): def b(self, bval):
return tensor.as_tensor_variable(numpy.asarray(bval, dtype=self.dtype)) return tensor.as_tensor_variable(np.asarray(bval, dtype=self.dtype))
def test_eq(self): def test_eq(self):
self.assertTrue(CGer(True) == CGer(True)) self.assertTrue(CGer(True) == CGer(True))
...@@ -127,13 +127,13 @@ class TestCGemv(TestCase, TestOptimizationMixin): ...@@ -127,13 +127,13 @@ class TestCGemv(TestCase, TestOptimizationMixin):
self.mode = theano.compile.get_default_mode().including('fast_run') self.mode = theano.compile.get_default_mode().including('fast_run')
# matrix # matrix
self.A = tensor.tensor(dtype=dtype, broadcastable=(False, False)) self.A = tensor.tensor(dtype=dtype, broadcastable=(False, False))
self.Aval = numpy.ones((2, 3), dtype=dtype) self.Aval = np.ones((2, 3), dtype=dtype)
# vector # vector
self.x = tensor.tensor(dtype=dtype, broadcastable=(False,)) self.x = tensor.tensor(dtype=dtype, broadcastable=(False,))
self.y = tensor.tensor(dtype=dtype, broadcastable=(False,)) self.y = tensor.tensor(dtype=dtype, broadcastable=(False,))
self.xval = numpy.asarray([1, 2], dtype=dtype) self.xval = np.asarray([1, 2], dtype=dtype)
self.yval = numpy.asarray([1.5, 2.7, 3.9], dtype=dtype) self.yval = np.asarray([1.5, 2.7, 3.9], dtype=dtype)
# scalar # scalar
self.a = tensor.tensor(dtype=dtype, broadcastable=()) self.a = tensor.tensor(dtype=dtype, broadcastable=())
...@@ -144,11 +144,11 @@ class TestCGemv(TestCase, TestOptimizationMixin): ...@@ -144,11 +144,11 @@ class TestCGemv(TestCase, TestOptimizationMixin):
f = theano.function([self.A, self.x, self.y, self.a], f = theano.function([self.A, self.x, self.y, self.a],
self.a*self.y + theano.dot(self.A, self.x), self.a*self.y + theano.dot(self.A, self.x),
mode=mode) mode=mode)
Aval = numpy.ones((3, 1), dtype=self.dtype) Aval = np.ones((3, 1), dtype=self.dtype)
xval = numpy.ones((1,), dtype=self.dtype) xval = np.ones((1,), dtype=self.dtype)
yval = float('NaN') * numpy.ones((3,), dtype=self.dtype) yval = float('NaN') * np.ones((3,), dtype=self.dtype)
zval = f(Aval, xval, yval, 0) zval = f(Aval, xval, yval, 0)
assert not numpy.isnan(zval).any() assert not np.isnan(zval).any()
def test_optimizations_vm(self): def test_optimizations_vm(self):
skip_if_blas_ldflags_empty() skip_if_blas_ldflags_empty()
...@@ -165,12 +165,12 @@ class TestCGemv(TestCase, TestOptimizationMixin): ...@@ -165,12 +165,12 @@ class TestCGemv(TestCase, TestOptimizationMixin):
) )
# Assert they produce the same output # Assert they produce the same output
assert numpy.allclose(f(self.xval, self.Aval), assert np.allclose(f(self.xval, self.Aval),
numpy.dot(self.xval, self.Aval)) np.dot(self.xval, self.Aval))
# Test with negative strides on 2 dims # Test with negative strides on 2 dims
assert numpy.allclose(f(self.xval, self.Aval[::-1, ::-1]), assert np.allclose(f(self.xval, self.Aval[::-1, ::-1]),
numpy.dot(self.xval, self.Aval[::-1, ::-1])) np.dot(self.xval, self.Aval[::-1, ::-1]))
def test_optimizations_mv(self): def test_optimizations_mv(self):
skip_if_blas_ldflags_empty() skip_if_blas_ldflags_empty()
...@@ -187,11 +187,11 @@ class TestCGemv(TestCase, TestOptimizationMixin): ...@@ -187,11 +187,11 @@ class TestCGemv(TestCase, TestOptimizationMixin):
) )
# Assert they produce the same output # Assert they produce the same output
assert numpy.allclose(f(self.Aval, self.yval), assert np.allclose(f(self.Aval, self.yval),
numpy.dot(self.Aval, self.yval)) np.dot(self.Aval, self.yval))
# Test with negative strides on 2 dims # Test with negative strides on 2 dims
assert numpy.allclose(f(self.Aval[::-1, ::-1], self.yval), assert np.allclose(f(self.Aval[::-1, ::-1], self.yval),
numpy.dot(self.Aval[::-1, ::-1], self.yval)) np.dot(self.Aval[::-1, ::-1], self.yval))
def test_force_gemv_init(self): def test_force_gemv_init(self):
if check_force_gemv_init(): if check_force_gemv_init():
...@@ -203,20 +203,20 @@ class TestCGemv(TestCase, TestOptimizationMixin): ...@@ -203,20 +203,20 @@ class TestCGemv(TestCase, TestOptimizationMixin):
def t_gemv1(self, m_shp): def t_gemv1(self, m_shp):
''' test vector2 + dot(matrix, vector1) ''' ''' test vector2 + dot(matrix, vector1) '''
rng = numpy.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(numpy.array(rng.uniform(size=(m_shp[1],)), v1 = theano.shared(np.array(rng.uniform(size=(m_shp[1],)),
dtype='float32')) dtype='float32'))
v2_orig = numpy.array(rng.uniform(size=(m_shp[0],)), dtype='float32') v2_orig = np.array(rng.uniform(size=(m_shp[0],)), dtype='float32')
v2 = theano.shared(v2_orig) v2 = theano.shared(v2_orig)
m = theano.shared(numpy.array(rng.uniform(size=m_shp), m = theano.shared(np.array(rng.uniform(size=m_shp),
dtype='float32')) dtype='float32'))
f = theano.function([], v2 + tensor.dot(m, v1), f = theano.function([], v2 + tensor.dot(m, v1),
mode=self.mode) mode=self.mode)
# Assert they produce the same output # Assert they produce the same output
assert numpy.allclose(f(), assert np.allclose(f(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig) np.dot(m.get_value(), v1.get_value()) + v2_orig)
topo = [n.op for n in f.maker.fgraph.toposort()] topo = [n.op for n in f.maker.fgraph.toposort()]
assert topo == [CGemv(inplace=False)], topo assert topo == [CGemv(inplace=False)], topo
...@@ -227,8 +227,8 @@ class TestCGemv(TestCase, TestOptimizationMixin): ...@@ -227,8 +227,8 @@ class TestCGemv(TestCase, TestOptimizationMixin):
# Assert they produce the same output # Assert they produce the same output
g() g()
assert numpy.allclose(v2.get_value(), assert np.allclose(v2.get_value(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig) np.dot(m.get_value(), v1.get_value()) + v2_orig)
topo = [n.op for n in g.maker.fgraph.toposort()] topo = [n.op for n in g.maker.fgraph.toposort()]
assert topo == [CGemv(inplace=True)] assert topo == [CGemv(inplace=True)]
...@@ -237,11 +237,11 @@ class TestCGemv(TestCase, TestOptimizationMixin): ...@@ -237,11 +237,11 @@ class TestCGemv(TestCase, TestOptimizationMixin):
m.get_value(borrow=True)[::-1, ::-1], m.get_value(borrow=True)[::-1, ::-1],
borrow=True) borrow=True)
v2.set_value(v2_orig) v2.set_value(v2_orig)
assert numpy.allclose(f(), assert np.allclose(f(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig) np.dot(m.get_value(), v1.get_value()) + v2_orig)
g() g()
assert numpy.allclose(v2.get_value(), assert np.allclose(v2.get_value(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig) np.dot(m.get_value(), v1.get_value()) + v2_orig)
def test_gemv1(self): def test_gemv1(self):
skip_if_blas_ldflags_empty() skip_if_blas_ldflags_empty()
...@@ -265,12 +265,12 @@ class TestCGemv(TestCase, TestOptimizationMixin): ...@@ -265,12 +265,12 @@ class TestCGemv(TestCase, TestOptimizationMixin):
mode=self.mode) mode=self.mode)
# Matrix value # Matrix value
A_val = numpy.ones((5, 3), dtype=dtype) A_val = np.ones((5, 3), dtype=dtype)
# Different vector length # Different vector length
ones_3 = numpy.ones(3, dtype=dtype) ones_3 = np.ones(3, dtype=dtype)
ones_4 = numpy.ones(4, dtype=dtype) ones_4 = np.ones(4, dtype=dtype)
ones_5 = numpy.ones(5, dtype=dtype) ones_5 = np.ones(5, dtype=dtype)
ones_6 = numpy.ones(6, dtype=dtype) ones_6 = np.ones(6, dtype=dtype)
f(A_val, ones_3, ones_5) f(A_val, ones_3, ones_5)
f(A_val[::-1, ::-1], ones_3, ones_5) f(A_val[::-1, ::-1], ones_3, ones_5)
...@@ -286,12 +286,12 @@ class TestCGemv(TestCase, TestOptimizationMixin): ...@@ -286,12 +286,12 @@ class TestCGemv(TestCase, TestOptimizationMixin):
f = theano.function([x, y, z], f = theano.function([x, y, z],
[tensor.dot(y, x), tensor.dot(z,x)], [tensor.dot(y, x), tensor.dot(z,x)],
mode=mode_blas_opt) mode=mode_blas_opt)
vx = numpy.random.rand(3, 3) vx = np.random.rand(3, 3)
vy = numpy.random.rand(3) vy = np.random.rand(3)
vz = numpy.random.rand(3) vz = np.random.rand(3)
out = f(vx, vy, vz) out = f(vx, vy, vz)
assert numpy.allclose(out[0], numpy.dot(vy, vx)) assert np.allclose(out[0], np.dot(vy, vx))
assert numpy.allclose(out[1], numpy.dot(vz, vx)) assert np.allclose(out[1], np.dot(vz, vx))
assert len([n for n in f.maker.fgraph.apply_nodes assert len([n for n in f.maker.fgraph.apply_nodes
if isinstance(n.op, tensor.AllocEmpty)]) == 2 if isinstance(n.op, tensor.AllocEmpty)]) == 2
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论