提交 0784ab7b authored 作者: Frédéric Bastien's avatar Frédéric Bastien 提交者: GitHub

Merge pull request #5829 from Amrithasuresh/master

Updated numpy as np #4218
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import time import time
import numpy import numpy as np
import theano import theano
from theano import tensor as tt from theano import tensor as tt
...@@ -19,8 +19,8 @@ f_lazyifelse = theano.function([a, b, x, y], z_lazy) ...@@ -19,8 +19,8 @@ f_lazyifelse = theano.function([a, b, x, y], z_lazy)
val1 = 0. val1 = 0.
val2 = 1. val2 = 1.
big_mat1 = numpy.ones((10000, 1000)) big_mat1 = np.ones((10000, 1000))
big_mat2 = numpy.ones((10000, 1000)) big_mat2 = np.ones((10000, 1000))
n_times = 10 n_times = 10
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
import theano.tensor as tt import theano.tensor as tt
rng = numpy.random rng = np.random
N = 400 N = 400
feats = 784 feats = 784
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
import theano.tensor as tt import theano.tensor as tt
...@@ -19,6 +19,6 @@ polynomial = components.sum() ...@@ -19,6 +19,6 @@ polynomial = components.sum()
calculate_polynomial = theano.function(inputs=[coefficients, x], calculate_polynomial = theano.function(inputs=[coefficients, x],
outputs=polynomial) outputs=polynomial)
test_coeff = numpy.asarray([1, 0, 2], dtype=numpy.float32) test_coeff = np.asarray([1, 0, 2], dtype=np.float32)
print(calculate_polynomial(test_coeff, 3)) print(calculate_polynomial(test_coeff, 3))
# 19.0 # 19.0
...@@ -85,7 +85,7 @@ class SumDiffOp(theano.Op): ...@@ -85,7 +85,7 @@ class SumDiffOp(theano.Op):
# 3. Testing apparatus # 3. Testing apparatus
import numpy import numpy as np
from theano.gof import Op, Apply from theano.gof import Op, Apply
from theano import tensor, function, printing from theano import tensor, function, printing
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
...@@ -93,7 +93,7 @@ from theano.tests import unittest_tools as utt ...@@ -93,7 +93,7 @@ from theano.tests import unittest_tools as utt
class TestProdOp(utt.InferShapeTester): class TestProdOp(utt.InferShapeTester):
rng = numpy.random.RandomState(43) rng = np.random.RandomState(43)
def setUp(self): def setUp(self):
super(TestProdOp, self).setUp() super(TestProdOp, self).setUp()
...@@ -103,14 +103,14 @@ class TestProdOp(utt.InferShapeTester): ...@@ -103,14 +103,14 @@ class TestProdOp(utt.InferShapeTester):
x = theano.tensor.matrix() x = theano.tensor.matrix()
y = theano.tensor.matrix() y = theano.tensor.matrix()
f = theano.function([x, y], self.op_class()(x, y)) f = theano.function([x, y], self.op_class()(x, y))
x_val = numpy.random.rand(5, 4) x_val = np.random.rand(5, 4)
y_val = numpy.random.rand(5, 4) y_val = np.random.rand(5, 4)
out = f(x_val, y_val) out = f(x_val, y_val)
assert numpy.allclose(x_val * y_val, out) assert np.allclose(x_val * y_val, out)
def test_gradient(self): def test_gradient(self):
utt.verify_grad(self.op_class(), [numpy.random.rand(5, 4), utt.verify_grad(self.op_class(), [np.random.rand(5, 4),
numpy.random.rand(5, 4)], np.random.rand(5, 4)],
n_tests=1, rng=TestProdOp.rng) n_tests=1, rng=TestProdOp.rng)
def test_infer_shape(self): def test_infer_shape(self):
...@@ -118,14 +118,14 @@ class TestProdOp(utt.InferShapeTester): ...@@ -118,14 +118,14 @@ class TestProdOp(utt.InferShapeTester):
y = tensor.dmatrix() y = tensor.dmatrix()
self._compile_and_check([x, y], [self.op_class()(x, y)], self._compile_and_check([x, y], [self.op_class()(x, y)],
[numpy.random.rand(5, 6), [np.random.rand(5, 6),
numpy.random.rand(5, 6)], np.random.rand(5, 6)],
self.op_class) self.op_class)
class TestSumDiffOp(utt.InferShapeTester): class TestSumDiffOp(utt.InferShapeTester):
rng = numpy.random.RandomState(43) rng = np.random.RandomState(43)
def setUp(self): def setUp(self):
super(TestSumDiffOp, self).setUp() super(TestSumDiffOp, self).setUp()
...@@ -135,10 +135,10 @@ class TestSumDiffOp(utt.InferShapeTester): ...@@ -135,10 +135,10 @@ class TestSumDiffOp(utt.InferShapeTester):
x = theano.tensor.matrix() x = theano.tensor.matrix()
y = theano.tensor.matrix() y = theano.tensor.matrix()
f = theano.function([x, y], self.op_class()(x, y)) f = theano.function([x, y], self.op_class()(x, y))
x_val = numpy.random.rand(5, 4) x_val = np.random.rand(5, 4)
y_val = numpy.random.rand(5, 4) y_val = np.random.rand(5, 4)
out = f(x_val, y_val) out = f(x_val, y_val)
assert numpy.allclose([x_val + y_val, x_val - y_val], out) assert np.allclose([x_val + y_val, x_val - y_val], out)
def test_gradient(self): def test_gradient(self):
def output_0(x, y): def output_0(x, y):
...@@ -147,11 +147,11 @@ class TestSumDiffOp(utt.InferShapeTester): ...@@ -147,11 +147,11 @@ class TestSumDiffOp(utt.InferShapeTester):
def output_1(x, y): def output_1(x, y):
return self.op_class()(x, y)[1] return self.op_class()(x, y)[1]
utt.verify_grad(output_0, [numpy.random.rand(5, 4), utt.verify_grad(output_0, [np.random.rand(5, 4),
numpy.random.rand(5, 4)], np.random.rand(5, 4)],
n_tests=1, rng=TestSumDiffOp.rng) n_tests=1, rng=TestSumDiffOp.rng)
utt.verify_grad(output_1, [numpy.random.rand(5, 4), utt.verify_grad(output_1, [np.random.rand(5, 4),
numpy.random.rand(5, 4)], np.random.rand(5, 4)],
n_tests=1, rng=TestSumDiffOp.rng) n_tests=1, rng=TestSumDiffOp.rng)
def test_infer_shape(self): def test_infer_shape(self):
...@@ -161,14 +161,14 @@ class TestSumDiffOp(utt.InferShapeTester): ...@@ -161,14 +161,14 @@ class TestSumDiffOp(utt.InferShapeTester):
# adapt the choice of the next instruction to the op under test # adapt the choice of the next instruction to the op under test
self._compile_and_check([x, y], self.op_class()(x, y), self._compile_and_check([x, y], self.op_class()(x, y),
[numpy.random.rand(5, 6), [np.random.rand(5, 6),
numpy.random.rand(5, 6)], np.random.rand(5, 6)],
self.op_class) self.op_class)
# as_op exercice # as_op exercice
import theano import theano
import numpy import numpy as np
from theano.compile.ops import as_op from theano.compile.ops import as_op
...@@ -180,7 +180,7 @@ def infer_shape_numpy_dot(node, input_shapes): ...@@ -180,7 +180,7 @@ def infer_shape_numpy_dot(node, input_shapes):
@as_op(itypes=[theano.tensor.fmatrix, theano.tensor.fmatrix], @as_op(itypes=[theano.tensor.fmatrix, theano.tensor.fmatrix],
otypes=[theano.tensor.fmatrix], infer_shape=infer_shape_numpy_dot) otypes=[theano.tensor.fmatrix], infer_shape=infer_shape_numpy_dot)
def numpy_add(a, b): def numpy_add(a, b):
return numpy.add(a, b) return np.add(a, b)
def infer_shape_numpy_add_sub(node, input_shapes): def infer_shape_numpy_add_sub(node, input_shapes):
...@@ -192,13 +192,13 @@ def infer_shape_numpy_add_sub(node, input_shapes): ...@@ -192,13 +192,13 @@ def infer_shape_numpy_add_sub(node, input_shapes):
@as_op(itypes=[theano.tensor.fmatrix, theano.tensor.fmatrix], @as_op(itypes=[theano.tensor.fmatrix, theano.tensor.fmatrix],
otypes=[theano.tensor.fmatrix], infer_shape=infer_shape_numpy_add_sub) otypes=[theano.tensor.fmatrix], infer_shape=infer_shape_numpy_add_sub)
def numpy_add(a, b): def numpy_add(a, b):
return numpy.add(a, b) return np.add(a, b)
@as_op(itypes=[theano.tensor.fmatrix, theano.tensor.fmatrix], @as_op(itypes=[theano.tensor.fmatrix, theano.tensor.fmatrix],
otypes=[theano.tensor.fmatrix], infer_shape=infer_shape_numpy_add_sub) otypes=[theano.tensor.fmatrix], infer_shape=infer_shape_numpy_add_sub)
def numpy_sub(a, b): def numpy_sub(a, b):
return numpy.sub(a, b) return np.sub(a, b)
if __name__ == "__main__": if __name__ == "__main__":
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
class DoubleOp(theano.Op): class DoubleOp(theano.Op):
...@@ -21,8 +21,8 @@ x = theano.tensor.matrix() ...@@ -21,8 +21,8 @@ x = theano.tensor.matrix()
f = theano.function([x], DoubleOp()(x)) f = theano.function([x], DoubleOp()(x))
inp = numpy.random.rand(5,5) inp = np.random.rand(5,5)
out = f(inp) out = f(inp)
assert numpy.allclose(inp*2, out) assert np.allclose(inp*2, out)
print(inp) print(inp)
print(out) print(out)
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
import theano.tensor as T import theano.tensor as T
rng = numpy.random rng = np.random
N = 400 N = 400
feats = 784 feats = 784
...@@ -13,7 +13,7 @@ training_steps = 10000 ...@@ -13,7 +13,7 @@ training_steps = 10000
x = T.matrix("x") x = T.matrix("x")
y = T.vector("y") y = T.vector("y")
w = theano.shared(rng.randn(feats).astype(theano.config.floatX), name="w") w = theano.shared(rng.randn(feats).astype(theano.config.floatX), name="w")
b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name="b") b = theano.shared(np.asarray(0., dtype=theano.config.floatX), name="b")
x.tag.test_value = D[0] x.tag.test_value = D[0]
y.tag.test_value = D[1] y.tag.test_value = D[1]
#print "Initial model:" #print "Initial model:"
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import pycuda.autoinit import pycuda.autoinit
import pycuda.driver as drv import pycuda.driver as drv
import numpy import numpy as np
from pycuda.compiler import SourceModule from pycuda.compiler import SourceModule
mod = SourceModule(""" mod = SourceModule("""
...@@ -14,13 +14,13 @@ __global__ void multiply_them(float *dest, float *a, float *b) ...@@ -14,13 +14,13 @@ __global__ void multiply_them(float *dest, float *a, float *b)
multiply_them = mod.get_function("multiply_them") multiply_them = mod.get_function("multiply_them")
a = numpy.random.randn(400).astype(numpy.float32) a = np.random.randn(400).astype(np.float32)
b = numpy.random.randn(400).astype(numpy.float32) b = np.random.randn(400).astype(np.float32)
dest = numpy.zeros_like(a) dest = np.zeros_like(a)
multiply_them( multiply_them(
drv.Out(dest), drv.In(a), drv.In(b), drv.Out(dest), drv.In(a), drv.In(b),
block=(400,1,1), grid=(1,1)) block=(400,1,1), grid=(1,1))
assert numpy.allclose(dest, a*b) assert np.allclose(dest, a*b)
print(dest) print(dest)
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
import theano.tensor as T import theano.tensor as T
...@@ -18,6 +18,6 @@ polynomial = components.sum() ...@@ -18,6 +18,6 @@ polynomial = components.sum()
calculate_polynomial = theano.function(inputs=[coefficients, x], calculate_polynomial = theano.function(inputs=[coefficients, x],
outputs=polynomial) outputs=polynomial)
test_coeff = numpy.asarray([1, 0, 2], dtype=numpy.float32) test_coeff = np.asarray([1, 0, 2], dtype=np.float32)
print(calculate_polynomial(test_coeff, 3)) print(calculate_polynomial(test_coeff, 3))
# 19.0 # 19.0
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
import theano.tensor as tt import theano.tensor as tt
rng = numpy.random rng = np.random
N = 400 N = 400
feats = 784 feats = 784
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# Theano tutorial # Theano tutorial
# Solution to Exercise in section 'Loop' # Solution to Exercise in section 'Loop'
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
import theano.tensor as tt import theano.tensor as tt
...@@ -52,7 +52,7 @@ polynomial = components.sum() ...@@ -52,7 +52,7 @@ polynomial = components.sum()
calculate_polynomial1 = theano.function(inputs=[coefficients, x], calculate_polynomial1 = theano.function(inputs=[coefficients, x],
outputs=polynomial) outputs=polynomial)
test_coeff = numpy.asarray([1, 0, 2], dtype=numpy.float32) test_coeff = np.asarray([1, 0, 2], dtype=np.float32)
print(calculate_polynomial1(test_coeff, 3)) print(calculate_polynomial1(test_coeff, 3))
# 19.0 # 19.0
...@@ -68,7 +68,7 @@ max_coefficients_supported = 10000 ...@@ -68,7 +68,7 @@ max_coefficients_supported = 10000
full_range = tt.arange(max_coefficients_supported) full_range = tt.arange(max_coefficients_supported)
outputs_info = tt.as_tensor_variable(numpy.asarray(0, 'float64')) outputs_info = tt.as_tensor_variable(np.asarray(0, 'float64'))
components, updates = theano.scan(fn=lambda coeff, power, prior_value, free_var: components, updates = theano.scan(fn=lambda coeff, power, prior_value, free_var:
prior_value + (coeff * (free_var ** power)), prior_value + (coeff * (free_var ** power)),
...@@ -80,6 +80,6 @@ polynomial = components[-1] ...@@ -80,6 +80,6 @@ polynomial = components[-1]
calculate_polynomial = theano.function(inputs=[coefficients, x], calculate_polynomial = theano.function(inputs=[coefficients, x],
outputs=polynomial, updates=updates) outputs=polynomial, updates=updates)
test_coeff = numpy.asarray([1, 0, 2], dtype=numpy.float32) test_coeff = np.asarray([1, 0, 2], dtype=np.float32)
print(calculate_polynomial(test_coeff, 3)) print(calculate_polynomial(test_coeff, 3))
# 19.0 # 19.0
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
# Solution to Exercise in section 'Configuration Settings and Compiling Modes' # Solution to Exercise in section 'Configuration Settings and Compiling Modes'
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
import theano.tensor as tt import theano.tensor as tt
theano.config.floatX = 'float32' theano.config.floatX = 'float32'
rng = numpy.random rng = np.random
N = 400 N = 400
feats = 784 feats = 784
...@@ -21,7 +21,7 @@ training_steps = 10000 ...@@ -21,7 +21,7 @@ training_steps = 10000
x = tt.matrix("x") x = tt.matrix("x")
y = tt.vector("y") y = tt.vector("y")
w = theano.shared(rng.randn(feats).astype(theano.config.floatX), name="w") w = theano.shared(rng.randn(feats).astype(theano.config.floatX), name="w")
b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name="b") b = theano.shared(np.asarray(0., dtype=theano.config.floatX), name="b")
x.tag.test_value = D[0] x.tag.test_value = D[0]
y.tag.test_value = D[1] y.tag.test_value = D[1]
#print "Initial model:" #print "Initial model:"
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
x, y, z = theano.tensor.vectors('xyz') x, y, z = theano.tensor.vectors('xyz')
f = theano.function([x, y, z], [(x + y + z) * 2]) f = theano.function([x, y, z], [(x + y + z) * 2])
xv = numpy.random.rand(10).astype(theano.config.floatX) xv = np.random.rand(10).astype(theano.config.floatX)
yv = numpy.random.rand(10).astype(theano.config.floatX) yv = np.random.rand(10).astype(theano.config.floatX)
zv = numpy.random.rand(10).astype(theano.config.floatX) zv = np.random.rand(10).astype(theano.config.floatX)
f(xv, yv, zv) f(xv, yv, zv)
...@@ -7,13 +7,13 @@ ...@@ -7,13 +7,13 @@
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import theano import theano
import theano.tensor as tt import theano.tensor as tt
theano.config.floatX = 'float32' theano.config.floatX = 'float32'
rng = numpy.random rng = np.random
N = 400 N = 400
feats = 784 feats = 784
...@@ -25,7 +25,7 @@ training_steps = 10000 ...@@ -25,7 +25,7 @@ training_steps = 10000
x = theano.shared(D[0], name="x") x = theano.shared(D[0], name="x")
y = theano.shared(D[1], name="y") y = theano.shared(D[1], name="y")
w = theano.shared(rng.randn(feats).astype(theano.config.floatX), name="w") w = theano.shared(rng.randn(feats).astype(theano.config.floatX), name="w")
b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name="b") b = theano.shared(np.asarray(0., dtype=theano.config.floatX), name="b")
x.tag.test_value = D[0] x.tag.test_value = D[0]
y.tag.test_value = D[1] y.tag.test_value = D[1]
#print "Initial model:" #print "Initial model:"
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论