提交 115f6012 authored 作者: Frédéric Bastien's avatar Frédéric Bastien 提交者: GitHub

Merge pull request #5179 from kvmanohar22/import_numpy

Import numpy
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import sys import sys
import time import time
...@@ -198,7 +198,7 @@ print(mod.pretty(mode=mode)) ...@@ -198,7 +198,7 @@ print(mod.pretty(mode=mode))
m = mod.make(mode=mode) m = mod.make(mode=mode)
neg, nout, nhid, niter = [int(a) for a in sys.argv[1:]] neg, nout, nhid, niter = [int(a) for a in sys.argv[1:]]
rng = numpy.random.RandomState(342) rng = np.random.RandomState(342)
m.w = rng.rand(nout, nhid) m.w = rng.rand(nout, nhid)
m.a = rng.randn(nhid) * 0.0 m.a = rng.randn(nhid) * 0.0
m.b = rng.randn(nout) * 0.0 m.b = rng.randn(nout) * 0.0
......
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy as N import numpy as np
import sys import sys
import time import time
from six.moves import xrange from six.moves import xrange
...@@ -10,7 +10,7 @@ from six.moves import xrange ...@@ -10,7 +10,7 @@ from six.moves import xrange
neg, nout, nhid, niter = [int(a) for a in sys.argv[1:]] neg, nout, nhid, niter = [int(a) for a in sys.argv[1:]]
lr = 0.01 lr = 0.01
rng = N.random.RandomState(342) rng = np.random.RandomState(342)
w = rng.rand(nout, nhid) w = rng.rand(nout, nhid)
a = rng.randn(nhid) * 0.0 a = rng.randn(nhid) * 0.0
...@@ -22,38 +22,38 @@ dot_time = 0.0 ...@@ -22,38 +22,38 @@ dot_time = 0.0
t = time.time() t = time.time()
for i in xrange(niter): for i in xrange(niter):
tt = time.time() tt = time.time()
d = N.dot(x, w) d = np.dot(x, w)
dot_time += time.time() - tt dot_time += time.time() - tt
hid = N.tanh(d + a) hid = np.tanh(d + a)
tt = time.time() tt = time.time()
d = N.dot(hid, w.T) d = np.dot(hid, w.T)
dot_time += time.time() - tt dot_time += time.time() - tt
out = N.tanh(d + b) out = np.tanh(d + b)
g_out = out - x g_out = out - x
err = 0.5 * N.sum(g_out**2) err = 0.5 * np.sum(g_out**2)
g_hidwt = g_out * (1.0 - out**2) g_hidwt = g_out * (1.0 - out**2)
b -= lr * N.sum(g_hidwt, axis=0) b -= lr * np.sum(g_hidwt, axis=0)
tt = time.time() tt = time.time()
g_hid = N.dot(g_hidwt, w) g_hid = np.dot(g_hidwt, w)
dot_time += time.time() - tt dot_time += time.time() - tt
g_hidin = g_hid * (1.0 - hid**2) g_hidin = g_hid * (1.0 - hid**2)
tt = time.time() tt = time.time()
d = N.dot(g_hidwt.T, hid) d = np.dot(g_hidwt.T, hid)
dd = N.dot(x.T, g_hidin) dd = np.dot(x.T, g_hidin)
dot_time += time.time() - tt dot_time += time.time() - tt
gw = (d + dd) gw = (d + dd)
w -= lr * gw w -= lr * gw
a -= lr * N.sum(g_hidin, axis=0) a -= lr * np.sum(g_hidin, axis=0)
total_time = time.time() - t total_time = time.time() - t
print('time: ',total_time, 'err: ', err) print('time: ',total_time, 'err: ', err)
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import sys, timeit, time import sys, timeit, time
import numpy import numpy as np
import theano, theano.tensor.signal.conv import theano, theano.tensor.signal.conv
try: try:
...@@ -17,16 +17,16 @@ if len(sys.argv)>6: ...@@ -17,16 +17,16 @@ if len(sys.argv)>6:
setup=""" setup="""
import sys, timeit, time import sys, timeit, time
import numpy import numpy as np
import theano, theano.tensor.signal.conv import theano, theano.tensor.signal.conv
img_shape = int(sys.argv[1]), int(sys.argv[2]) img_shape = int(sys.argv[1]), int(sys.argv[2])
ker_shape = int(sys.argv[3]), int(sys.argv[4]) ker_shape = int(sys.argv[3]), int(sys.argv[4])
dtype = sys.argv[5] dtype = sys.argv[5]
img = theano.shared(numpy.ones(img_shape, dtype=dtype)) img = theano.shared(np.ones(img_shape, dtype=dtype))
ker = theano.shared(numpy.ones(ker_shape, dtype=dtype)) ker = theano.shared(np.ones(ker_shape, dtype=dtype))
out = theano.shared(numpy.ones((2,2,2), dtype=dtype)) out = theano.shared(np.ones((2,2,2), dtype=dtype))
""" """
T = timeit.Timer("f()", T = timeit.Timer("f()",
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import sys, timeit import sys, timeit
import numpy import numpy as np
import scikits.image.opencv import scikits.image.opencv
try: try:
...@@ -16,13 +16,13 @@ if len(sys.argv)>6: ...@@ -16,13 +16,13 @@ if len(sys.argv)>6:
nb_call=int(sys.argv[6]) nb_call=int(sys.argv[6])
T = timeit.Timer("f()",""" T = timeit.Timer("f()","""
import scikits.image.opencv, sys, numpy import scikits.image.opencv, sys, numpy as np
img_shape = int(sys.argv[1]), int(sys.argv[2]) img_shape = int(sys.argv[1]), int(sys.argv[2])
ker_shape = int(sys.argv[3]), int(sys.argv[4]) ker_shape = int(sys.argv[3]), int(sys.argv[4])
dtype = sys.argv[5] dtype = sys.argv[5]
img = numpy.ones(img_shape, dtype=dtype) img = np.ones(img_shape, dtype=dtype)
ker = numpy.ones(ker_shape, dtype=dtype) ker = np.ones(ker_shape, dtype=dtype)
def f(): def f():
scikits.image.opencv.cvFilter2D(img, ker) scikits.image.opencv.cvFilter2D(img, ker)
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import theano import theano
import numpy as N import numpy as np
from theano import tensor as T from theano import tensor as T
from theano.tensor import nnet as NN from theano.tensor import nnet as NN
from six.moves import xrange from six.moves import xrange
...@@ -50,9 +50,9 @@ class RegressionLayer(M.Module): ...@@ -50,9 +50,9 @@ class RegressionLayer(M.Module):
if input_size and target_size: if input_size and target_size:
# initialize w and b in a special way using input_size and target_size # initialize w and b in a special way using input_size and target_size
sz = (input_size, target_size) sz = (input_size, target_size)
rng = N.random.RandomState(seed) rng = np.random.RandomState(seed)
obj.w = rng.uniform(size = sz, low = -0.5, high = 0.5) obj.w = rng.uniform(size = sz, low = -0.5, high = 0.5)
obj.b = N.zeros(target_size) obj.b = np.zeros(target_size)
obj.stepsize = 0.01 obj.stepsize = 0.01
# here we call the default_initialize method, which takes all the name: value # here we call the default_initialize method, which takes all the name: value
# pairs in init and sets the property with that name to the provided value # pairs in init and sets the property with that name to the provided value
...@@ -93,8 +93,8 @@ def test_module_advanced_example(): ...@@ -93,8 +93,8 @@ def test_module_advanced_example():
profmode = PrintEverythingMode(theano.gof.OpWiseCLinker(), 'fast_run') profmode = PrintEverythingMode(theano.gof.OpWiseCLinker(), 'fast_run')
data_x = N.random.randn(4, 10) data_x = np.random.randn(4, 10)
data_y = [ [int(x)] for x in (N.random.randn(4) > 0)] data_y = [ [int(x)] for x in (np.random.randn(4) > 0)]
model = SpecifiedRegressionLayer(regularize = False).make(input_size = 10, model = SpecifiedRegressionLayer(regularize = False).make(input_size = 10,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论