提交 aa00203b authored 作者: Olivier Breuleux's avatar Olivier Breuleux

merge

...@@ -169,194 +169,6 @@ class T_OpFromGraph(unittest.TestCase): ...@@ -169,194 +169,6 @@ class T_OpFromGraph(unittest.TestCase):
assert numpy.all(11.0 == fn(xv, yv, zv)) assert numpy.all(11.0 == fn(xv, yv, zv))
class T_state(unittest.TestCase):
def test_accumulator(self):
"""Test low-level interface with state."""
x = T.scalar('x')
s = T.scalar('s')
fn, states = program_states(inputs = [x], outputs = [], states = [(s, 0, s+x)])
sum = 0
for inc in [1, 4, 5,23, -324]:
sum += inc
fn.run([inc], states)
assert sum == states[0].value
def test_misc0(self):
fn_inc, states_inc = function_states(\
inputs = [x], outputs = [], states = [(s, 0, s+x)])
fn_inc2, states_inc2 = function_states(\
inputs = [x], outputs = [], states = [(s, 0, s+x)])
fn_inc_copy = copy.copy(fn_inc) #USE fn copy
# run() is like __call__, but requires an explicit state argument
fn_inc.run([5], states_inc) #run on own state object
fn_inc2.run([3], states_inc) #run on compatible state object
assert states_inc[0].value == 8
states_inc_copy = copy.copy(states_inc) #USE state copy
fn_inc_copy.run([2], states_inc_copy)
assert states_inc[0].value == 10 #compatible
fn_dec, states_dec = function_states(\
inputs = [x], outputs = [], states = [(s, states_inc[0], s-x)])
try:
fn_inc.run([5], states_dec) # wrong kind of state for given program
self.fail("fn accepted an invalid state argument")
except SpecificException:
raise NotImplementedError() #TODO
except Exception:
self.fail("fn accepted an invalid state argument")
def test_perceptron(self):
"""Test high-level state interface."""
mu0 = numpy.array([1.0,0.0])
mu1 = numpy.array([0.0,0.1])
si0 = numpy.ones_like(mu0) #unit variance
si1 = numpy.ones_like(mu1) #unit variance
#implicit internal state
label = random.bernoulli(0.5)
#implicit internal state for each DiagGaussian
x = label * random.DiagGaussian(mu0, si0) \
+ (1 - label) * random.DiagGaussian(mu1,si1)
w = T.tensor.dvector()
b = T.tensor.dscalar()
lr = 0.01
decision = dot(x,w) + b > 0
new_w = w + neq(label, decision) * lr * x
new_b = b + neq(label, decision) * (label * (-lr) + (1-label)*lr)
init_w = numpy.array([0.0, 0.0])
init_b = 0.0
io_stream = T.function([], [label, x])
perceptron_learn = T.function([x, label], [decision],
state={
'w':(w, init_w, update_w),
'b':(b, init_b, update_b),
'lr':(lr, 0.01)})
perceptron_use = T.function([x], [decision],
state={
'w':(w, perceptron_learn.shared['w']),
'b':(b, perceptron_learn.shared['b'])})
errs = 0
for i in xrange(100):
il, ix = io_stream()
d0 = perceptron_use(ix)
d1 = perceptron_learn(ix, il)
assert d0 == d1
errs += (d0 != d1)
print d0
print 'errs =', errs
def test_shared(self):
"""Test shared r/w state."""
x = T.scalar('x')
s = T.scalar('s')
fn_inc, states_inc = function_states(\
inputs = [x], outputs = [], states = [(s, 0, s+x)])
fn_dec, states_dec = function_states(\
inputs = [x], outputs = [], states = [(s, states_inc[0], s-x)])
sum = 0
for inc in [1, 4, 5,23, -324]:
sum += inc
fn_inc.run([inc], states_inc)
assert sum == states_inc[0].value
a = sum
for inc in [1, 4, 5,23, -324]:
sum -= inc
fn_dec(inc)
assert sum == 0
assert states_inc[0].value == sum
for inc in [1, 4, 5,23, -324]:
sum -= inc
fn_dec(inc)
assert sum == -a
assert states_inc[0].value == sum
class T_dict_interface(unittest.TestCase):
def test_keyword(self):
x = T.scalar('x')
y = T.scalar('y')
s = T.scalar('s')
fn = function(input_kw = {'a':x, 'b':y}, outputs = [], state = {'s':(s, 0, s+x/y)})
try:
fn(1, 1)
self.fail("non-keyword call accepted!")
except SpecificException:
raise NotImplementedError()
except Exception:
self.fail("non-keyword call accepted!")
try:
fn(a=1)
self.fail("incomplete call accepted!")
except SpecificException:
raise NotImplementedError()
except Exception:
self.fail("incomplete call accepted!")
try:
fn(a=1, b=1, c=1)
self.fail("overcomplete call accepted!")
except SpecificException:
raise NotImplementedError()
except Exception:
self.fail("overcomplete call accepted!")
def test_aliased_state(self):
"""Test keyword input and copy."""
x = T.scalar('x')
y = T.scalar('y')
s = T.scalar('s')
fn = function(input_kw = {'a':x, 'b':y}, outputs = [], state = {'s':(s, 0, s+x/y)})
fn2 = fn.copy()
fn3 = fn.copy()
fn(a=2, b=5)
fn2(a=5, b=2)
fn3(b=2, a=5)
assert fn.state['s'] == 2.0/5
assert fn2.state['s'] == 5.0/2
assert fn3.state['s'] == 5.0/2
#fn and fn3 use the same sort of state, so this is OK.
fn3.state = fn.state
fn.state['s'] = 0
fn(a=1, b=1) #increment the shared state
assert fn3.state['s'] == 1
fn3(a=-1, b=1) #decrement the shared state
assert fn.state['s'] == 0
if __name__ == '__main__': if __name__ == '__main__':
if 1: if 1:
......
...@@ -513,7 +513,7 @@ DotTester = make_tester(name = 'DotTester', ...@@ -513,7 +513,7 @@ DotTester = make_tester(name = 'DotTester',
def verify_grad(testcase, op, pt, n_tests=1, rng=numpy.random, eps=0.0000001, tol=0.0001, def verify_grad(testcase, op, pt, n_tests=1, rng=numpy.random, eps=0.0000001, tol=0.0001,
linker='c&py'): linker='c&py'):
"""testcase.failUnless( analytic gradient matches finite-diff gradient) """ """testcase.failUnless(analytic gradient matches finite-diff gradient)"""
pt = [numpy.asarray(p) for p in pt] pt = [numpy.asarray(p) for p in pt]
for test_num in xrange(n_tests): for test_num in xrange(n_tests):
......
...@@ -610,7 +610,6 @@ class CAReduce(Op): ...@@ -610,7 +610,6 @@ class CAReduce(Op):
all_code = [("", "")] * nnested + [(task0_decl, "")] + [("", "")] * (len(axis) - 2) + [("", code1), ""] all_code = [("", "")] * nnested + [(task0_decl, "")] + [("", "")] * (len(axis) - 2) + [("", code1), ""]
else: else:
all_code = [task0_decl + code1] all_code = [task0_decl + code1]
loop = cgen.make_loop([order, range(nnested) + ['x'] * len(axis)], [idtype, odtype], all_code, sub) loop = cgen.make_loop([order, range(nnested) + ['x'] * len(axis)], [idtype, odtype], all_code, sub)
return decl, checks, alloc, loop return decl, checks, alloc, loop
......
...@@ -376,127 +376,6 @@ def clone_get_equiv(i, o, copy_inputs_and_orphans = True): ...@@ -376,127 +376,6 @@ def clone_get_equiv(i, o, copy_inputs_and_orphans = True):
return d return d
## Previous version
# for input in i:
# if copy_inputs_and_orphans:
# cpy = input.clone()
# cpy.owner = None
# cpy.index = None
# d[input] = cpy
# else:
# d[input] = input
#
# def clone_helper(result):
# if result in d:
# return d[result]
# node = result.owner
# if node is None: # result is an orphan
# if copy_inputs_and_orphans:
# cpy = result.clone()
# d[result] = cpy
# else:
# d[result] = result
# return d[result]
# else:
# new_node = node.clone_with_new_inputs([clone_helper(input) for input in node.inputs])
# d[node] = new_node
# for output, new_output in zip(node.outputs, new_node.outputs):
# d[output] = new_output
# return d[result]
#
# for output in o:
# clone_helper(output)
#
# return d
# def clone_with_new_inputs(i, o, new_i):
# equiv = clone_with_new_inputs_get_equiv(i, o, new_i)
# return [equiv[input] for input in i], [equiv[output] for output in o]
# def clone_with_new_inputs_get_equiv(i, o, new_i, copy_orphans = True):
# # note: this does not exactly mirror Apply.clone_with_new_inputs
# # here it is possible to give different types to new_i and then
# # make_node is called on the ops instead of clone_with_new_inputs
# # whenever the type is different.
# d = {}
# for input, new_input in zip(i, new_i):
# d[input] = new_input
# def clone_helper(result):
# if result in d:
# return d[result]
# node = result.owner
# if node is None: # result is an orphan
# if copy_orphans:
# cpy = result.clone()
# d[result] = cpy
# else:
# d[result] = result
# return d[result]
# else:
# cloned_inputs = [clone_helper(input) for input in node.inputs]
# if any(input != cloned_input for input, cloned_input in zip(node.inputs, cloned_inputs)):
# new_node = node.op.make_node(*cloned_inputs)
# else:
# new_node = node.clone_with_new_inputs(cloned_inputs)
# d[node] = new_node
# for output, new_output in zip(node.outputs, new_node.outputs):
# d[output] = new_output
# return d[result]
# for output in o:
# clone_helper(output)
# return d
def clone_with_equiv(i, o, d, missing_input_policy = 'fail', orphan_policy = 'copy'):
def clone_helper(result):
if result in d:
return d[result]
node = result.owner
if node is None: # result is an input or an orphan not in d
if isinstance(result, Value):
if orphan_policy == 'copy':
d[result] = copy(result)
elif orphan_policy == 'keep':
d[result] = result
else:
raise ValueError("unknown orphan_policy: '%s'" % orphan_policy)
else:
if missing_input_policy == 'fail':
raise ValueError("missing input: %s" % result)
elif missing_input_policy == 'keep':
d[result] = result
else:
raise ValueError("unknown missing_input_policy: '%s'" % missing_input_policy)
return d[result]
else:
cloned_inputs = [clone_helper(input) for input in node.inputs]
if all(input is cloned_input for input, cloned_input in zip(node.inputs, cloned_inputs)):
new_node = node
else:
new_node = node.clone_with_new_inputs(cloned_inputs, strict = False)
# if any(input != cloned_input for input, cloned_input in zip(node.inputs, cloned_inputs)):
# new_node = node.op.make_node(*cloned_inputs)
# else:
# new_node = node.clone_with_new_inputs(cloned_inputs)
d[node] = new_node
for output, new_output in zip(node.outputs, new_node.outputs):
d[output] = new_output
return d[result]
for output in o:
clone_helper(output)
return [d[input] for input in i], [d[output] for output in o]
def general_toposort(r_out, deps, debug_print = False): def general_toposort(r_out, deps, debug_print = False):
""" """
@note: deps(i) should behave like a pure function (no funny business with @note: deps(i) should behave like a pure function (no funny business with
...@@ -561,8 +440,6 @@ def io_toposort(i, o, orderings = {}): ...@@ -561,8 +440,6 @@ def io_toposort(i, o, orderings = {}):
return [o for o in topo if isinstance(o, Apply)] return [o for o in topo if isinstance(o, Apply)]
default_leaf_formatter = str default_leaf_formatter = str
default_node_formatter = lambda op, argstrings: "%s(%s)" % (op.op, default_node_formatter = lambda op, argstrings: "%s(%s)" % (op.op,
", ".join(argstrings)) ", ".join(argstrings))
...@@ -667,3 +544,4 @@ def view_roots(r): ...@@ -667,3 +544,4 @@ def view_roots(r):
else: else:
return [r] return [r]
...@@ -1041,7 +1041,7 @@ class Dot(Op): ...@@ -1041,7 +1041,7 @@ class Dot(Op):
return Apply(self, inputs, outputs) return Apply(self, inputs, outputs)
def perform(self, node, (x, y), (z, )): def perform(self, node, (x, y), (z, )):
z[0] = numpy.dot(x, y) z[0] = numpy.asarray(numpy.dot(x, y))
def grad(self, (x, y), (gz,)): def grad(self, (x, y), (gz,)):
if gz.type.ndim == 0: if gz.type.ndim == 0:
return gz * y, gz * x return gz * y, gz * x
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论