提交 c57d83a0 authored 作者: David Warde-Farley's avatar David Warde-Farley

Removed useless whitespace.

上级 9cfb0b69
...@@ -48,7 +48,7 @@ In this way, we could express something like Logistic Regression like this: ...@@ -48,7 +48,7 @@ In this way, we could express something like Logistic Regression like this:
def mode(self): def mode(self):
"""Return expression of the most likely value of this distribution""" """Return expression of the most likely value of this distribution"""
We would really like to integrate out certain variables sometimes... We would really like to integrate out certain variables sometimes...
An RBM could be expressed like this: An RBM could be expressed like this:
...@@ -71,7 +71,7 @@ An RBM could be expressed like this: ...@@ -71,7 +71,7 @@ An RBM could be expressed like this:
RBM.hidden.energy(h) # an expression for the free energy RBM.hidden.energy(h) # an expression for the free energy
v_given_h = RBM.visible.conditional(h) # a random variable v_given_h = RBM.visible.conditional(h) # a random variable
Rather than program all the training algorithms into an RBM module, Rather than program all the training algorithms into an RBM module,
the idea would be to express the relationship between RBM variables so that we the idea would be to express the relationship between RBM variables so that we
could automatically recognize how to do Gibbs sampling, gradient descent on Free could automatically recognize how to do Gibbs sampling, gradient descent on Free
Energy, etc. Energy, etc.
......
...@@ -13,7 +13,7 @@ changes are proposed to make function-construction calls more ...@@ -13,7 +13,7 @@ changes are proposed to make function-construction calls more
readable and intuitive, and to make it easier to share values between readable and intuitive, and to make it easier to share values between
functions. functions.
The strategy is to The strategy is to
- introduce a new kind of ``Variable`` (``SharedVariable``) that has a container - introduce a new kind of ``Variable`` (``SharedVariable``) that has a container
associated with it, and can allow multiple functions to share a value. associated with it, and can allow multiple functions to share a value.
...@@ -59,17 +59,17 @@ The proposal is for two new ways of creating a *shared* variable: ...@@ -59,17 +59,17 @@ The proposal is for two new ways of creating a *shared* variable:
def __init__(self, name, type, value, strict): def __init__(self, name, type, value, strict):
""" """
:param name: The name for this variable (see `Variable`). :param name: The name for this variable (see `Variable`).
:param type: The type for this variable (see `Variable`). :param type: The type for this variable (see `Variable`).
:param value: A value to associate with this variable (a new container will be created). :param value: A value to associate with this variable (a new container will be created).
:param strict: True -> assignments to .value will not be cast or copied, so they must :param strict: True -> assignments to .value will not be cast or copied, so they must
have the correct type. have the correct type.
:param container: The container to use for this variable. Illegal to pass this as well :param container: The container to use for this variable. Illegal to pass this as well
as a value. as a value.
For more user-friendly constructor, see `shared` For more user-friendly constructor, see `shared`
""" """
...@@ -79,23 +79,23 @@ The proposal is for two new ways of creating a *shared* variable: ...@@ -79,23 +79,23 @@ The proposal is for two new ways of creating a *shared* variable:
value = property(...) value = property(...)
"""Read/write the non-symbolic value associated with this SharedVariable. """Read/write the non-symbolic value associated with this SharedVariable.
If the SharedVariable is shared, changes to this value will be visible to all functions using If the SharedVariable is shared, changes to this value will be visible to all functions using
this SharedVariable. If this SharedVariable is not shared, a change will not be visible to this SharedVariable. If this SharedVariable is not shared, a change will not be visible to
functions that were created before the change. functions that were created before the change.
""" """
def shared(value, name=None, strict=False, **kwargs): def shared(value, name=None, strict=False, **kwargs):
"""Return a SharedVariable Variable, initialized with a copy or reference of `value`. """Return a SharedVariable Variable, initialized with a copy or reference of `value`.
This function iterates over constructor functions (see `shared_constructor`) to find a This function iterates over constructor functions (see `shared_constructor`) to find a
suitable SharedVariable subclass. suitable SharedVariable subclass.
:note: :note:
By passing kwargs, you effectively limit the set of potential constructors to those that By passing kwargs, you effectively limit the set of potential constructors to those that
can accept those kwargs. can accept those kwargs.
""" """
... ...
...@@ -151,23 +151,23 @@ Corner cases and exotic examples can be found in the tests. ...@@ -151,23 +151,23 @@ Corner cases and exotic examples can be found in the tests.
def pfunc(params, outputs, mode=None, givens=None, updates=None) def pfunc(params, outputs, mode=None, givens=None, updates=None)
"""Function-constructor for graphs with shared variables. """Function-constructor for graphs with shared variables.
:type params: list of either Variable or Param instances. :type params: list of either Variable or Param instances.
:param params: function parameters, these are not allowed to be shared :param params: function parameters, these are not allowed to be shared
variables variables
:type outputs: list of Variables or Out instances :type outputs: list of Variables or Out instances
:param outputs: expressions to compute :param outputs: expressions to compute
:param mode: compilation mode :param mode: compilation mode
:type updates: iterable over pairs (shared_variable, new_expression). List, tuple or dict. :type updates: iterable over pairs (shared_variable, new_expression). List, tuple or dict.
:param updates: update the values for SharedVariable inputs according to these expressions :param updates: update the values for SharedVariable inputs according to these expressions
:rtype: theano.compile.Function :rtype: theano.compile.Function
:returns: a callable object that will compute the outputs (given the inputs) :returns: a callable object that will compute the outputs (given the inputs)
and update the implicit function arguments according to the `updates`. and update the implicit function arguments according to the `updates`.
""" """
... ...
...@@ -177,20 +177,20 @@ Corner cases and exotic examples can be found in the tests. ...@@ -177,20 +177,20 @@ Corner cases and exotic examples can be found in the tests.
def __init__(self, variable, default=None, mutable=False, strict=False): def __init__(self, variable, default=None, mutable=False, strict=False):
""" """
:param variable: A node in an expression graph to set with each function call. :param variable: A node in an expression graph to set with each function call.
:param default: The default value to use at call-time (can also be a Container where :param default: The default value to use at call-time (can also be a Container where
the function will find a value at call-time.) the function will find a value at call-time.)
:param name: A string to identify this parameter from function kwargs. :param name: A string to identify this parameter from function kwargs.
:param mutable: True -> function is allowed to modify this argument. :param mutable: True -> function is allowed to modify this argument.
:param strict: False -> function arguments may be copied or cast to match the :param strict: False -> function arguments may be copied or cast to match the
type required by the parameter `variable`. True -> function arguments must exactly match the type type required by the parameter `variable`. True -> function arguments must exactly match the type
required by `variable`. required by `variable`.
:param implicit: see help(theano.io.In) :param implicit: see help(theano.io.In)
""" """
Note that if some update value is not a variable, it will be cast into Note that if some update value is not a variable, it will be cast into
...@@ -210,40 +210,40 @@ simple one. ...@@ -210,40 +210,40 @@ simple one.
import numpy, theano import numpy, theano
from pfunc import pfunc from pfunc import pfunc
from sharedvalue import shared from sharedvalue import shared
from theano import tensor from theano import tensor
from theano.tensor.nnet import sigmoid from theano.tensor.nnet import sigmoid
class NNet(object): class NNet(object):
def __init__(self, def __init__(self,
input = tensor.dvector('input'), input = tensor.dvector('input'),
target = tensor.dvector('target'), target = tensor.dvector('target'),
n_input=1, n_hidden=1, n_output=1, lr=1e-3, **kw): n_input=1, n_hidden=1, n_output=1, lr=1e-3, **kw):
super(NNet, self).__init__(**kw) super(NNet, self).__init__(**kw)
self.input = input self.input = input
self.target = target self.target = target
self.lr = shared(lr, 'learning_rate') self.lr = shared(lr, 'learning_rate')
self.w1 = shared(numpy.zeros((n_hidden, n_input)), 'w1') self.w1 = shared(numpy.zeros((n_hidden, n_input)), 'w1')
self.w2 = shared(numpy.zeros((n_output, n_hidden)), 'w2') self.w2 = shared(numpy.zeros((n_output, n_hidden)), 'w2')
self.hidden = sigmoid(tensor.dot(self.w1, self.input)) self.hidden = sigmoid(tensor.dot(self.w1, self.input))
self.output = tensor.dot(self.w2, self.hidden) self.output = tensor.dot(self.w2, self.hidden)
self.cost = tensor.sum((self.output - self.target)**2) self.cost = tensor.sum((self.output - self.target)**2)
self.sgd_updates = { self.sgd_updates = {
self.w1: self.w1 - self.lr * tensor.grad(self.cost, self.w1), self.w1: self.w1 - self.lr * tensor.grad(self.cost, self.w1),
self.w2: self.w2 - self.lr * tensor.grad(self.cost, self.w2)} self.w2: self.w2 - self.lr * tensor.grad(self.cost, self.w2)}
self.sgd_step = pfunc( self.sgd_step = pfunc(
params = [self.input, self.target], params = [self.input, self.target],
outputs = [self.output, self.cost], outputs = [self.output, self.cost],
updates = self.sgd_updates) updates = self.sgd_updates)
self.compute_output = pfunc([self.input], self.output) self.compute_output = pfunc([self.input], self.output)
self.output_from_hidden = pfunc([self.hidden], self.output) self.output_from_hidden = pfunc([self.hidden], self.output)
...@@ -46,14 +46,14 @@ purpose of it is to hack it to investigate what your own particular program is d ...@@ -46,14 +46,14 @@ purpose of it is to hack it to investigate what your own particular program is d
if i == 39: if i == 39:
print 'this node is weird...', th.outputs[0][0] print 'this node is weird...', th.outputs[0][0]
self.provided_linker = linker self.provided_linker = linker
self.provided_optimizer = optimizer self.provided_optimizer = optimizer
if isinstance(linker, basestring) or linker is None: if isinstance(linker, basestring) or linker is None:
linker = predefined_linkers[linker] linker = predefined_linkers[linker]
self.linker = WrapLinkerMany([linker], [blah]) self.linker = WrapLinkerMany([linker], [blah])
if isinstance(optimizer, basestring) or optimizer is None: if isinstance(optimizer, basestring) or optimizer is None:
optimizer = predefined_optimizers[optimizer] optimizer = predefined_optimizers[optimizer]
self._optimizer = optimizer self._optimizer = optimizer
......
...@@ -59,7 +59,7 @@ class MyOp(Op): ...@@ -59,7 +59,7 @@ class MyOp(Op):
self.view_map = vmap self.view_map = vmap
self.destroyhandler_tolerate_same = destroyhandler_tolerate_same self.destroyhandler_tolerate_same = destroyhandler_tolerate_same
self.destroyhandler_tolerate_aliased = destroyhandler_tolerate_aliased self.destroyhandler_tolerate_aliased = destroyhandler_tolerate_aliased
def make_node(self, *inputs): def make_node(self, *inputs):
assert len(inputs) == self.nin assert len(inputs) == self.nin
inputs = map(as_variable, inputs) inputs = map(as_variable, inputs)
......
...@@ -23,7 +23,7 @@ class DebugLinker(gof.WrapLinker): ...@@ -23,7 +23,7 @@ class DebugLinker(gof.WrapLinker):
self.env = None self.env = None
self.compare_fn = compare_fn self.compare_fn = compare_fn
self.copy_originals = copy_originals self.copy_originals = copy_originals
if check_types not in [None, True]: if check_types not in [None, True]:
self.check_types = check_types self.check_types = check_types
......
...@@ -25,7 +25,7 @@ class symbolic_fn_callable(object): ...@@ -25,7 +25,7 @@ class symbolic_fn_callable(object):
class. class.
.. code-block:: python .. code-block:: python
class T(TheanoObject): class T(TheanoObject):
@symbolic_fn @symbolic_fn
def add(self, x): def add(self, x):
...@@ -33,7 +33,7 @@ class symbolic_fn_callable(object): ...@@ -33,7 +33,7 @@ class symbolic_fn_callable(object):
add_outputs = ... add_outputs = ...
add_updates = ... add_updates = ...
return RVal(add_outputs, add_updates) return RVal(add_outputs, add_updates)
t = T() t = T()
t.add.outputs(5) # returns `add_outputs` from when `x=theano_type(5)` t.add.outputs(5) # returns `add_outputs` from when `x=theano_type(5)`
t.add.updates(5) # returns `add_updates` from when `x=theano_type(5)` t.add.updates(5) # returns `add_updates` from when `x=theano_type(5)`
t.add.theano_function(5) # returns the `Function` compiled when `x=theano_type(5)` t.add.theano_function(5) # returns the `Function` compiled when `x=theano_type(5)`
...@@ -48,7 +48,7 @@ class symbolic_fn_callable(object): ...@@ -48,7 +48,7 @@ class symbolic_fn_callable(object):
"""Silly method to work with symbolic_fn.__get__""" """Silly method to work with symbolic_fn.__get__"""
self.o_self = o_self self.o_self = o_self
return self return self
def run_symbolic(self, *args, **kwargs): def run_symbolic(self, *args, **kwargs):
return self.o_self._get_method_impl(self.fn, self.o_self, args, kwargs, mode=self.mode) return self.o_self._get_method_impl(self.fn, self.o_self, args, kwargs, mode=self.mode)
...@@ -70,7 +70,7 @@ class symbolic_fn(object): ...@@ -70,7 +70,7 @@ class symbolic_fn(object):
def __init__(self, fn, mode=None): def __init__(self, fn, mode=None):
self.fn = fn self.fn = fn
self.callable = symbolic_fn_callable(fn, mode) self.callable = symbolic_fn_callable(fn, mode)
def __get__(self, o_self, o_cls): def __get__(self, o_self, o_cls):
return self.callable.on(o_self) return self.callable.on(o_self)
...@@ -113,19 +113,19 @@ class TheanoObject(object): ...@@ -113,19 +113,19 @@ class TheanoObject(object):
This class provides support for symbolic_fn class attributes. This class provides support for symbolic_fn class attributes.
These will be compiled on demand so that they can be used just like normal (non-symbolic) These will be compiled on demand so that they can be used just like normal (non-symbolic)
methods. methods.
The symbolic functions in a TheanoObject can share member variables that have been created The symbolic functions in a TheanoObject can share member variables that have been created
using the `symbolic_member` method. using the `symbolic_member` method.
:note: Other variables (ones not created using ``self.symbolic_member``) referred to in the :note: Other variables (ones not created using ``self.symbolic_member``) referred to in the
body of a symbolic function will *not* be shared between symbolic functions, or between body of a symbolic function will *not* be shared between symbolic functions, or between
symbolic functions and this class. These other variables will be locked away in the symbolic functions and this class. These other variables will be locked away in the
closure of a symbolic function when that function is compiled. closure of a symbolic function when that function is compiled.
:warning: It is not recommended for code to interleave :warning: It is not recommended for code to interleave
(a) changes to non-symbolic instance variables with (a) changes to non-symbolic instance variables with
(b) calls to symbolic functions that use those instance variables. (b) calls to symbolic functions that use those instance variables.
A symbolic function may be A symbolic function may be
compiled multiple times because it must be compiled for each set of argument types. compiled multiple times because it must be compiled for each set of argument types.
Each time the function is compiled, the values of non-symbolic variables will be locked Each time the function is compiled, the values of non-symbolic variables will be locked
...@@ -181,7 +181,7 @@ class TheanoObject(object): ...@@ -181,7 +181,7 @@ class TheanoObject(object):
# construct In instances for the symbolic_member instances that can automatically be # construct In instances for the symbolic_member instances that can automatically be
# included here. # included here.
module_inputs = [theano.compile.io.In( module_inputs = [theano.compile.io.In(
variable=v, variable=v,
value=v._theanoclass_container, value=v._theanoclass_container,
mutable=(v in rval.updates), mutable=(v in rval.updates),
update=rval.updates.get(v, None)) update=rval.updates.get(v, None))
...@@ -212,7 +212,7 @@ class TheanoObject(object): ...@@ -212,7 +212,7 @@ class TheanoObject(object):
v = tensor.lscalar(name) v = tensor.lscalar(name)
v._theanoclass_container = \ v._theanoclass_container = \
theano.gof.Container(v, theano.gof.Container(v,
storage = [theano._asarray(ival, dtype='int64')], storage = [theano._asarray(ival, dtype='int64')],
readonly=False) readonly=False)
assert not hasattr(v, 'set') assert not hasattr(v, 'set')
...@@ -224,5 +224,5 @@ class TheanoObject(object): ...@@ -224,5 +224,5 @@ class TheanoObject(object):
return v return v
...@@ -9,7 +9,7 @@ else: ...@@ -9,7 +9,7 @@ else:
import numpy import numpy
from copy import copy from copy import copy
from theano.compile import (SymbolicInputKit, SymbolicInput, from theano.compile import (SymbolicInputKit, SymbolicInput,
Module, module, Method, Member, In, Component) Module, module, Method, Member, In, Component)
from theano.gof import Container from theano.gof import Container
from theano.gof.python25 import deque from theano.gof.python25 import deque
...@@ -20,7 +20,7 @@ class KitComponent(Component): ...@@ -20,7 +20,7 @@ class KitComponent(Component):
""" """
Represents a SymbolicInputKit (see io.py). Represents a SymbolicInputKit (see io.py).
""" """
def __init__(self, kit): def __init__(self, kit):
super(KitComponent, self).__init__() super(KitComponent, self).__init__()
self.kit = kit self.kit = kit
...@@ -106,8 +106,8 @@ class RModule(Module): ...@@ -106,8 +106,8 @@ class RModule(Module):
if recursive: if recursive:
#Here, we recurse through all the components (inst2) contained in (inst) #Here, we recurse through all the components (inst2) contained in (inst)
#and seeds each subcomponent that is an RModule #and seeds each subcomponent that is an RModule
for path, c in self.flat_components_map(True): for path, c in self.flat_components_map(True):
if isinstance(c, RModule): if isinstance(c, RModule):
inst2 = inst inst2 = inst
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论