提交 4e6fd916 authored 作者: james@X40's avatar james@X40

merge

"""Driver of graph construction, optimization, and linking. """Driver of graph construction, optimization, and linking.
""" """
__docformat__ = "restructuredtext en"
import copy_reg import copy_reg
import cPickle import cPickle
......
差异被折叠。
#!/usr/bin/env python #!/usr/bin/env python
import numpy as N import numpy as N
from theano import Op, Apply, tensor as T, Module, Member, Method, Mode, compile from theano import Op, Apply, tensor as T, Module, Method, Mode, compile
from theano.gof import OpSub, TopoOptimizer from theano.gof import OpSub, TopoOptimizer
from pylearn.algorithms.minimizer import make_minimizer # minimizer
from theano.printing import Print from theano.printing import Print
from theano.tests import unittest_tools from theano.tests import unittest_tools
#import sgd #until Olivier's module-import thing works better
#################### ####################
# Library-type stuff # Library-type stuff
...@@ -15,8 +13,6 @@ from theano.tests import unittest_tools ...@@ -15,8 +13,6 @@ from theano.tests import unittest_tools
from theano.compile import module from theano.compile import module
from theano import tensor as T from theano import tensor as T
from pylearn.algorithms.minimizer import minimizer_factory
class StochasticGradientDescent(module.FancyModule): class StochasticGradientDescent(module.FancyModule):
"""Fixed stepsize gradient descent""" """Fixed stepsize gradient descent"""
def __init__(self, args, cost, params, gradients=None, stepsize=None, WEIRD_STUFF=True): def __init__(self, args, cost, params, gradients=None, stepsize=None, WEIRD_STUFF=True):
...@@ -29,18 +25,18 @@ class StochasticGradientDescent(module.FancyModule): ...@@ -29,18 +25,18 @@ class StochasticGradientDescent(module.FancyModule):
self.stepsize_init = None self.stepsize_init = None
if stepsize is None: if stepsize is None:
self.stepsize = module.Member(T.dscalar()) self.stepsize = (T.dscalar())
elif isinstance(stepsize, T.TensorResult): elif isinstance(stepsize, T.TensorResult):
self.stepsize = stepsize self.stepsize = stepsize
else: else:
if self.WEIRD_STUFF: if self.WEIRD_STUFF:
#TODO: why is this necessary? why does the else clause not work? #TODO: why is this necessary? why does the else clause not work?
# self.stepsize = module.Member(T.dscalar(), init = stepsize) # self.stepsize = module.Member(T.dscalar(), init = stepsize)
self.stepsize = module.Member(T.dscalar()) self.stepsize = (T.dscalar())
self.stepsize_init = stepsize self.stepsize_init = stepsize
else: else:
# self.stepsize = module.Member(T.value(stepsize)) # self.stepsize = module.Member(T.value(stepsize))
self.stepsize = module.Member(T.constant(stepsize))#work! self.stepsize = (T.constant(stepsize))#work!
if self.stepsize.ndim != 0: if self.stepsize.ndim != 0:
raise ValueError('stepsize must be a scalar', stepsize) raise ValueError('stepsize must be a scalar', stepsize)
...@@ -63,7 +59,6 @@ class StochasticGradientDescent(module.FancyModule): ...@@ -63,7 +59,6 @@ class StochasticGradientDescent(module.FancyModule):
pass pass
@minimizer_factory('sgd')
def sgd_minimizer(stepsize=None, **args): def sgd_minimizer(stepsize=None, **args):
def m(i,c,p,g=None): def m(i,c,p,g=None):
return StochasticGradientDescent(i, c, p, stepsize=stepsize, **args) return StochasticGradientDescent(i, c, p, stepsize=stepsize, **args)
...@@ -101,6 +96,9 @@ class TanhRnn(Op): ...@@ -101,6 +96,9 @@ class TanhRnn(Op):
return Apply(self, [x, z0, A], [z]) return Apply(self, [x, z0, A], [z])
def perform(self, node, (x,z0,A), out): def perform(self, node, (x,z0,A), out):
assert x is not None
assert z0 is not None
assert A is not None
T,M = x.shape T,M = x.shape
z = N.zeros((T+1, M)) z = N.zeros((T+1, M))
z[0] = z0 z[0] = z0
...@@ -161,10 +159,10 @@ class ExampleRNN(Module): ...@@ -161,10 +159,10 @@ class ExampleRNN(Module):
self.n_vis = n_vis self.n_vis = n_vis
#recurrent weight matrix in latent space #recurrent weight matrix in latent space
self.z0 = Member(T.dvector()) self.z0 = (T.dvector())
self.w = Member(T.dmatrix()) self.w = (T.dmatrix())
self.params = [self.w] self.params = [self.z0, self.w]
#input and target #input and target
x, y = T.dmatrix(), T.dmatrix() x, y = T.dmatrix(), T.dmatrix()
...@@ -176,6 +174,7 @@ class ExampleRNN(Module): ...@@ -176,6 +174,7 @@ class ExampleRNN(Module):
self.minimizer = minimizer([x, y], self.cost, self.params) self.minimizer = minimizer([x, y], self.cost, self.params)
def _instance_initialize(self, obj): def _instance_initialize(self, obj):
print 'INITIALIZE EXAMPLE RNN'
n_vis = self.n_vis n_vis = self.n_vis
rng = N.random.RandomState(unittest_tools.fetch_seed(2342)) rng = N.random.RandomState(unittest_tools.fetch_seed(2342))
...@@ -185,14 +184,14 @@ class ExampleRNN(Module): ...@@ -185,14 +184,14 @@ class ExampleRNN(Module):
obj.minimizer.initialize() obj.minimizer.initialize()
def test_example_rnn(): def test_example_rnn():
minimizer_fn = make_minimizer('sgd', stepsize = 0.001) minimizer_fn = sgd_minimizer(stepsize = 0.001)
n_vis = 5 n_vis = 5
n_out = 3 n_out = 3
n_hid = 4 n_hid = 4
rnn_module = ExampleRNN(n_vis, minimizer_fn) rnn_module = ExampleRNN(n_vis, minimizer_fn)
rnn = rnn_module.make(mode='FAST_RUN') rnn = rnn_module.make()
rng = N.random.RandomState(unittest_tools.fetch_seed(7722342)) rng = N.random.RandomState(unittest_tools.fetch_seed(7722342))
x = rng.randn(10,n_vis) x = rng.randn(10,n_vis)
...@@ -212,6 +211,7 @@ def test_example_rnn(): ...@@ -212,6 +211,7 @@ def test_example_rnn():
print i, rnn.minimizer.step_cost(x, y), rnn.minimizer.stepsize print i, rnn.minimizer.step_cost(x, y), rnn.minimizer.stepsize
else: else:
rnn.minimizer.step_cost(x, y) rnn.minimizer.step_cost(x, y)
assert rnn.minimizer.step_cost(x,y) < -20 #it starts around -.28
def test_WEIRD_STUFF(): def test_WEIRD_STUFF():
n_vis = 3 n_vis = 3
...@@ -224,8 +224,8 @@ def test_WEIRD_STUFF(): ...@@ -224,8 +224,8 @@ def test_WEIRD_STUFF():
LAG = 4 LAG = 4
y[LAG:] = x[:-LAG, 0:n_vis] y[LAG:] = x[:-LAG, 0:n_vis]
minimizer_fn1 = make_minimizer('sgd', stepsize = 0.001, WEIRD_STUFF = False) minimizer_fn1 = sgd_minimizer(stepsize = 0.001, WEIRD_STUFF = False)
minimizer_fn2 = make_minimizer('sgd', stepsize = 0.001, WEIRD_STUFF = True) minimizer_fn2 = sgd_minimizer(stepsize = 0.001, WEIRD_STUFF = True)
rnn_module1 = ExampleRNN(n_vis, minimizer_fn1) rnn_module1 = ExampleRNN(n_vis, minimizer_fn1)
rnn_module2 = ExampleRNN(n_vis, minimizer_fn2) rnn_module2 = ExampleRNN(n_vis, minimizer_fn2)
rnn1 = rnn_module1.make(mode='FAST_RUN') rnn1 = rnn_module1.make(mode='FAST_RUN')
......
...@@ -473,15 +473,6 @@ class GemmLocalOptimizer(LocalOptimizer): ...@@ -473,15 +473,6 @@ class GemmLocalOptimizer(LocalOptimizer):
return [T.add(*new_add_inputs)] return [T.add(*new_add_inputs)]
return False return False
@staticmethod
def failure_callback(exc, nav, repl_pairs):
"""WRITEME"""
if not isinstance(exc, InconsistencyError):
traceback.print_exc()
else:
#print 'GEMM caused cycle, it happens.'
pass
@staticmethod @staticmethod
def _as_scalar(res): def _as_scalar(res):
"""Return None or a TensorResult whose type is in T.float_scalar_types""" """Return None or a TensorResult whose type is in T.float_scalar_types"""
...@@ -579,11 +570,11 @@ class GemmLocalOptimizer(LocalOptimizer): ...@@ -579,11 +570,11 @@ class GemmLocalOptimizer(LocalOptimizer):
# TODO: This could be an equilibriumOptmizer, but I don't know how to combine an OpKeyOptimizer and # TODO: This could be an equilibriumOptmizer, but I don't know how to combine an OpKeyOptimizer and
# an EquilibriumOptimizer. # an EquilibriumOptimizer.
compile.optdb.register('inplace_gemm_0', OpKeyOptimizer(GemmLocalOptimizer(), compile.optdb.register('inplace_gemm_0', OpKeyOptimizer(GemmLocalOptimizer(),
failure_callback=GemmLocalOptimizer.failure_callback), 70.00, 'fast_run', 'inplace', 'gemm') failure_callback=OpKeyOptimizer.warn_inplace), 70.00, 'fast_run', 'inplace', 'gemm')
compile.optdb.register('inplace_gemm_1', OpKeyOptimizer(GemmLocalOptimizer(), compile.optdb.register('inplace_gemm_1', OpKeyOptimizer(GemmLocalOptimizer(),
failure_callback=GemmLocalOptimizer.failure_callback), 70.01, 'fast_run', 'inplace', 'gemm') failure_callback=OpKeyOptimizer.warn_inplace), 70.01, 'fast_run', 'inplace', 'gemm')
compile.optdb.register('inplace_gemm_2', OpKeyOptimizer(GemmLocalOptimizer(), compile.optdb.register('inplace_gemm_2', OpKeyOptimizer(GemmLocalOptimizer(),
failure_callback=GemmLocalOptimizer.failure_callback), 70.02, 'fast_run', 'inplace', 'gemm') failure_callback=OpKeyOptimizer.warn_inplace), 70.02, 'fast_run', 'inplace', 'gemm')
class Dot22(GemmRelated): class Dot22(GemmRelated):
"""Compute a matrix-matrix product. """Compute a matrix-matrix product.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论