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

changes to Module, removed Member from user API

上级 1a722462
"""Driver of graph construction, optimization, and linking.
"""
__docformat__ = "restructuredtext en"
import copy_reg
import cPickle
......
差异被折叠。
#!/usr/bin/env python
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 pylearn.algorithms.minimizer import make_minimizer # minimizer
from theano.printing import Print
#import sgd #until Olivier's module-import thing works better
####################
# Library-type stuff
......@@ -14,8 +12,6 @@ from theano.printing import Print
from theano.compile import module
from theano import tensor as T
from pylearn.algorithms.minimizer import minimizer_factory
class StochasticGradientDescent(module.FancyModule):
"""Fixed stepsize gradient descent"""
def __init__(self, args, cost, params, gradients=None, stepsize=None, WEIRD_STUFF=True):
......@@ -28,18 +24,18 @@ class StochasticGradientDescent(module.FancyModule):
self.stepsize_init = None
if stepsize is None:
self.stepsize = module.Member(T.dscalar())
self.stepsize = (T.dscalar())
elif isinstance(stepsize, T.TensorResult):
self.stepsize = stepsize
else:
if self.WEIRD_STUFF:
#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())
self.stepsize = (T.dscalar())
self.stepsize_init = stepsize
else:
# 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:
raise ValueError('stepsize must be a scalar', stepsize)
......@@ -62,7 +58,6 @@ class StochasticGradientDescent(module.FancyModule):
pass
@minimizer_factory('sgd')
def sgd_minimizer(stepsize=None, **args):
def m(i,c,p,g=None):
return StochasticGradientDescent(i, c, p, stepsize=stepsize, **args)
......@@ -100,6 +95,9 @@ class TanhRnn(Op):
return Apply(self, [x, z0, A], [z])
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
z = N.zeros((T+1, M))
z[0] = z0
......@@ -160,10 +158,10 @@ class ExampleRNN(Module):
self.n_vis = n_vis
#recurrent weight matrix in latent space
self.z0 = Member(T.dvector())
self.w = Member(T.dmatrix())
self.z0 = (T.dvector())
self.w = (T.dmatrix())
self.params = [self.w]
self.params = [self.z0, self.w]
#input and target
x, y = T.dmatrix(), T.dmatrix()
......@@ -175,6 +173,7 @@ class ExampleRNN(Module):
self.minimizer = minimizer([x, y], self.cost, self.params)
def _instance_initialize(self, obj):
print 'INITIALIZE EXAMPLE RNN'
n_vis = self.n_vis
rng = N.random.RandomState(2342)
......@@ -184,14 +183,14 @@ class ExampleRNN(Module):
obj.minimizer.initialize()
def test_example_rnn():
minimizer_fn = make_minimizer('sgd', stepsize = 0.001)
minimizer_fn = sgd_minimizer(stepsize = 0.001)
n_vis = 5
n_out = 3
n_hid = 4
rnn_module = ExampleRNN(n_vis, minimizer_fn)
rnn = rnn_module.make(mode='FAST_RUN')
rnn = rnn_module.make()
rng = N.random.RandomState(7722342)
x = rng.randn(10,n_vis)
......@@ -211,6 +210,7 @@ def test_example_rnn():
print i, rnn.minimizer.step_cost(x, y), rnn.minimizer.stepsize
else:
rnn.minimizer.step_cost(x, y)
assert rnn.minimizer.step_cost(x,y) < -20 #it starts around -.28
def test_WEIRD_STUFF():
n_vis = 3
......@@ -223,8 +223,8 @@ def test_WEIRD_STUFF():
LAG = 4
y[LAG:] = x[:-LAG, 0:n_vis]
minimizer_fn1 = make_minimizer('sgd', stepsize = 0.001, WEIRD_STUFF = False)
minimizer_fn2 = make_minimizer('sgd', stepsize = 0.001, WEIRD_STUFF = True)
minimizer_fn1 = sgd_minimizer(stepsize = 0.001, WEIRD_STUFF = False)
minimizer_fn2 = sgd_minimizer(stepsize = 0.001, WEIRD_STUFF = True)
rnn_module1 = ExampleRNN(n_vis, minimizer_fn1)
rnn_module2 = ExampleRNN(n_vis, minimizer_fn2)
rnn1 = rnn_module1.make(mode='FAST_RUN')
......
#!/usr/bin/env python
"""Test compile.module"""
__docformat__ = "restructuredtext en"
import cPickle, numpy, unittest
from theano.compile.module import *
import theano.tensor as T
import sys
import theano
#TODO: add test for module.make(member=init_value)
class T_test_module(unittest.TestCase):
class T_module(unittest.TestCase):
def test_whats_up_with_submembers(self):
class Blah(FancyModule):
class Blah(Module):
def __init__(self, stepsize):
super(Blah, self).__init__()
self.stepsize = Member(T.value(stepsize))
self.stepsize = T.value(stepsize)
x = T.dscalar()
self.step = Method([x], x - self.stepsize)
B = Blah(0.0)
b = B.make(mode='FAST_RUN')
assert b.stepsize == 0.0
b.step(1.0)
assert b.stepsize == 0.0
......@@ -57,8 +63,23 @@ class T_test_module(unittest.TestCase):
assert isinstance(m1.x,(gof.Result))
assert isinstance(m1.y,(gof.Result))
for i in [m1.lx[0], m1.ly[0], m1.llx[0][0], m1.lly[0][0], m1.ltx[0][0], m1.lty[0][0], m1.ldx[0]['x'], m1.ldy[0]['y'], m1.tx[0], m1.ty[0], m1.tlx[0][0], m1.tly[0][0], m1.ttx[0][0], m1.tty[0][0], m1.tdx[0]['x'], m1.tdy[0]['y'], m1.dx['x'], m1.dy['y'], m1.dlx['x'][0], m1.dly['y'][0], m1.dtx['x'][0], m1.dty['y'][0], m1.ddx['x']['x'], m1.ddy['y']['y']]:
assert isinstance(i,(gof.Result))
for i, obj in enumerate([
m1.lx[0], #0
m1.llx[0][0],
m1.ltx[0][0],
m1.ldx[0]['x'],
m1.lty[0][0],#5
m1.ldy[0]['y'],
m1.ly[0],
m1.lly[0][0],
m1.tx[0], #8
m1.ty[0], m1.tlx[0][0],
m1.tly[0][0], m1.ttx[0][0], m1.tty[0][0], m1.tdx[0]['x'],
m1.tdy[0]['y'], m1.dx['x'],
m1.dy['y'], m1.dlx['x'][0], m1.dly['y'][0],
m1.dtx['x'][0], m1.dty['y'][0], m1.ddx['x']['x'],
m1.ddy['y']['y']]):
assert isinstance(obj,(gof.Result))
inst=m1.make()
......@@ -98,23 +119,72 @@ class T_test_module(unittest.TestCase):
for i,j in zip(get_l2(),range(len(get_l2()))):
assert i[0]==j
local_test(lambda:T.dscalar(),lambda:Member(T.dscalar()))
local_test(lambda:T.value(1),lambda:Member(T.value(2)))
local_test(lambda:T.constant(1),lambda:Member(T.constant(2)))
local_test(lambda:T.dscalar(),lambda:T.dscalar())
local_test(lambda:T.value(1),lambda:T.value(2))
local_test(lambda:T.constant(1),lambda:T.constant(2))
def test_compound_structure_assignment(self):
def test_list_assign(self):
"""Test that list members can be assigned list-wise"""
def local_test(x,y):
m1=Module()
m1.l=[x(), y()]#cast Result]
#create a list with some results in it
m1.l=[x(), y()]
# create a Method that makes the second list element a shared Member
m1.f=Method([], m1.l[1])
m1.g=Method([], m1.l[0])
m = m1.make()
#assign 4 and 5 to the two results' containers in m
m.l = [4, 5]
print 'm.f', m.f()
assert numpy.all(5 == m.f())
assert numpy.all(4 == m.g())
local_test(lambda:T.dscalar(),lambda:T.dscalar())
local_test(lambda:T.value(1),lambda:T.value(2))
def test_tuple_assign(self):
"""Test that list members can be assigned tuple-wise"""
def local_test(x,y):
m1=Module()
m1.l=(x(), y())
# create a Method that makes the second list element a shared Member
m1.g=Method([], m1.l[0])
m1.f=Method([], m1.l[1])
m = m1.make()
#assign 4 and 5 to the two results' containers in m
m.l = (4, 5)
assert 5 == m.f()
assert 4 == m.g()
local_test(lambda:T.dscalar(),lambda:Member(T.dscalar()))
local_test(lambda:T.value(1),lambda:Member(T.value(2)))
local_test(lambda:T.constant(1),lambda:Member(T.constant(2)))
local_test(lambda:T.dscalar(),lambda:T.dscalar())
local_test(lambda:T.value(1),lambda:T.value(2))
def test_dict_assign(self):
"""Test that list members can be assigned dict-wise"""
def local_test(x,y):
m1=Module()
##DICT
m1.l={'x':x(), 'y':y()}
# create a Method that makes the second list element a shared Member
m1.f=Method([], m1.l['y'])
m1.g=Method([], m1.l['x'])
m = m1.make()
#assign 4 and 5 to the two results' containers in m
m.l = dict(x=4, y=5)
assert 5 == m.f()
assert 4 == m.g()
print 'dscalar test'
local_test(lambda:T.dscalar(),lambda:T.dscalar())
print 'value test'
local_test(lambda:T.value(1),lambda:T.value(2))
def test_method_in_list_or_dict(self):
......@@ -201,7 +271,7 @@ class T_test_module(unittest.TestCase):
m2=Module()
x=T.dscalar()
populate_module(m1,x)
populate_module(m2,Member(x))
populate_module(m2,x)
#m1.x and m2.x should not be shared as their is no hierarchi link between them.
inst1=m1.make()
inst2=m2.make()
......@@ -248,8 +318,8 @@ class T_test_module(unittest.TestCase):
m4=Module()
x=T.dscalar()
populate_module(m1,x)
populate_module(m2,Member(x))
populate_module(m4,Member(x))
populate_module(m2,(x))
populate_module(m4,(x))
#m1.x and m2.x should not be shared as their is no hierarchi link between them.
inst1=m1.make()
inst2=m2.make()
......@@ -325,33 +395,59 @@ class T_test_module(unittest.TestCase):
print >> sys.stderr, "MODULE TEST IMPLEMENTED BUT WE DON'T KNOW WHAT WE WANT AS A RESULT"
def test_shared_method_N(self):
"""Test that Methods can be shared an arbitrary number of times between many submodules and
internal data structures."""
#put them in subModules, sub-sub-Modules, shared between a list and a dict, shared between
#a list and a submodule with a dictionary, etc...
print >> sys.stderr, "WARNING MODULE TEST NOT IMPLEMENTED"
def test_member_method_inputs(self):
"""Test that module Members can be named as Method inputs, in which case the function will
*not* use the storage allocated for the Module's version of that Member.
si le module a un membre x et qu''une fct un parametre appele x qui n''est pas le membre cela doit etre bien traiter.
les poids ne change pas
"""
# test that explicit Method inputs don't use shared storage
M = Module()
M.x = T.dscalar()
M.y = T.dscalar()
M.f = Method([M.x], M.x + M.y)
M.g = Method([M.y], M.x - M.y)
m = M.make()
m.y = 77
assert m.f(23) == 100
assert m.x == None
m.x = 1000
assert m.g(23) == 977
assert m.y == 77
assert m.x == 1000
"""
print >> sys.stderr, "WARNING MODULE TEST NOT IMPLEMENTED"
def test_member_input_flags(self):
"""Test that we can manipulate the mutable, strict, etc. flags (see SymbolicInput) of
Method inputs"""
print >> sys.stderr, "WARNING MODULE TEST NOT IMPLEMENTED"
M = Module()
M.x = T.dvector()
M.y = T.dvector()
xval= numpy.asarray([0, 0.5])
M.f = Method([io.In(M.x,
mutable=True,
update=(M.x - M.y),
value=xval)], M.x + M.y)
m = M.make()
m.y = numpy.asarray([1, 2])
assert numpy.all(m.f(xval) == [1, 2.5])
assert numpy.all(xval == [-1, -1.5])
def test_member_output_flags(self):
"""Test that we can manipulate the output flags (just 'borrow' I think, see SymbolicOutput)
of Method outputs"""
print >> sys.stderr, "WARNING MODULE TEST NOT IMPLEMENTED"
M = Module()
M.x = T.dvector()
M.f = Method([M.x], io.Out(M.x*4, borrow=True))
m = M.make()
v0 = m.f([5, 8])
v0_copy = v0 * 1
m.f([3, 2])
assert numpy.all(v0 != v0_copy)
def test_sanity_check_mode(self):
"""Test that Module.make(self) can take the same list of Modes that function can, so we can
......@@ -396,8 +492,8 @@ class T_test_module(unittest.TestCase):
def test_pickle():
"""Test that a module can be pickled"""
M = Module()
M.x = Member(T.dmatrix())
M.y = Member(T.dmatrix())
M.x = (T.dmatrix())
M.y = (T.dmatrix())
a = T.dmatrix()
M.f = Method([a], a + M.x + M.y)
M.g = Method([a], a * M.x * M.y)
......@@ -418,13 +514,11 @@ def test_pickle():
assert m_dup.x is m_dup.g.input_storage[1].data
assert m_dup.y is m_dup.g.input_storage[2].data
from numpy.testing import *
@dec.knownfailureif(True, "These branch cuts are known to fail")
def test_pickle_aliased_memory():
try:
M = Module()
M.x = Member(T.dmatrix())
M.y = Member(T.dmatrix())
M.x = (T.dmatrix())
M.y = (T.dmatrix())
a = T.dmatrix()
M.f = Method([a], a + M.x + M.y)
M.g = Method([a], a * M.x * M.y)
......@@ -450,6 +544,9 @@ def test_pickle_aliased_memory():
assert m.y[0,0] == 3.142
m_dup.x[1,0] = 3.142
assert m_dup.y[0,0] == 3.142
except Exception, e:
raise Exception('Known Failure: These branch cuts are known to fail', str(e))
if __name__ == '__main__':
......
......@@ -70,27 +70,36 @@ class QuadraticDenoisingAA(module.Module):
# ACQUIRE/MAKE INPUT
if not input:
input = T.matrix('input')
self.input = theano.External(input)
#self.input = theano.External(input)
self.input = (input)
# HYPER-PARAMETERS
self.lr = theano.Member(T.scalar())
#self.lr = theano.Member(T.scalar())
self.lr = (T.scalar())
# PARAMETERS
if _qfilters is None:
self.qfilters = [theano.Member(T.dmatrix('q%i'%i)) for i in xrange(n_quadratic_filters)]
#self.qfilters = [theano.Member(T.dmatrix('q%i'%i)) for i in xrange(n_quadratic_filters)]
self.qfilters = [(T.dmatrix('q%i'%i)) for i in xrange(n_quadratic_filters)]
else:
self.qfilters = [theano.Member(q) for q in _qfilters]
#self.qfilters = [theano.Member(q) for q in _qfilters]
self.qfilters = [(q) for q in _qfilters]
self.w1 = theano.Member(T.matrix('w1')) if _w1 is None else theano.Member(_w1)
#self.w1 = theano.Member(T.matrix('w1')) if _w1 is None else theano.Member(_w1)
self.w1 = (T.matrix('w1')) if _w1 is None else (_w1)
if _w2 is None:
if not tie_weights:
self.w2 = theano.Member(T.matrix())
#self.w2 = theano.Member(T.matrix())
self.w2 = (T.matrix())
else:
self.w2 = self.w1.T
else:
self.w2 = theano.Member(_w2)
self.b1 = theano.Member(T.vector('b1')) if _b1 is None else theano.Member(_b1)
self.b2 = theano.Member(T.vector('b2')) if _b2 is None else theano.Member(_b2)
#self.w2 = theano.Member(_w2)
self.w2 = (_w2)
#self.b1 = theano.Member(T.vector('b1')) if _b1 is None else theano.Member(_b1)
self.b1 = (T.vector('b1')) if _b1 is None else (_b1)
#self.b2 = theano.Member(T.vector('b2')) if _b2 is None else theano.Member(_b2)
self.b2 = (T.vector('b2')) if _b2 is None else (_b2)
# # REGULARIZATION COST
# self.regularization = self.build_regularization()
......@@ -212,7 +221,8 @@ class SigmoidXEQuadraticDenoisingAA(QuadraticDenoisingAA):
"""
def build_corrupted_input(self):
self.noise_level = theano.Member(T.scalar())
#self.noise_level = theano.Member(T.scalar())
self.noise_level = (T.scalar())
return self.random.binomial(T.shape(self.input), 1, 1 - self.noise_level) * self.input
def hid_activation_function(self, activation):
......@@ -262,12 +272,17 @@ class Module_Nclass(module.FancyModule):
def __init__(self, x=None, targ=None, w=None, b=None, lr=None, regularize=False):
super(Module_Nclass, self).__init__() #boilerplate
self.x = module.Member(x) if x is not None else T.matrix('input')
self.targ = module.Member(targ) if targ is not None else T.lvector()
#self.x = module.Member(x) if x is not None else T.matrix('input')
self.x = (x) if x is not None else T.matrix('input')
#self.targ = module.Member(targ) if targ is not None else T.lvector()
self.targ = (targ) if targ is not None else T.lvector()
self.w = module.Member(w) if w is not None else module.Member(T.dmatrix())
self.b = module.Member(b) if b is not None else module.Member(T.dvector())
self.lr = module.Member(lr) if lr is not None else module.Member(T.dscalar())
#self.w = module.Member(w) if w is not None else module.Member(T.dmatrix())
self.w = (w) if w is not None else (T.dmatrix())
#self.b = module.Member(b) if b is not None else module.Member(T.dvector())
self.b = (b) if b is not None else (T.dvector())
#self.lr = module.Member(lr) if lr is not None else module.Member(T.dscalar())
self.lr = (lr) if lr is not None else (T.dscalar())
self.params = [p for p in [self.w, self.b] if p.owner is None]
......@@ -355,7 +370,8 @@ class ConvolutionalMLP(module.FancyModule):
):
super(ConvolutionalMLP, self).__init__()
self.lr = module.Member(T.scalar())
#self.lr = module.Member(T.scalar())
self.lr = (T.scalar())
self.inputs = [T.dmatrix() for i in range(window_size)]
self.targ = T.lvector()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论