desc stringlengths 3 26.7k | decl stringlengths 11 7.89k | bodies stringlengths 8 553k |
|---|---|---|
'Helper function to add monitoring records to the log.'
| def add_records(self, log, record_tuples):
| for (name, value) in record_tuples:
if (not name):
raise ValueError('monitor variable without name')
log.current_row[self._record_name(name)] = value
|
'Write the values of monitored variables to the log.'
| def do(self, callback_name, *args):
| logger.info('Monitoring on auxiliary data started')
value_dict = self._evaluator.evaluate(self.data_stream)
self.add_records(self.main_loop.log, value_dict.items())
logger.info('Monitoring on auxiliary data finished')
|
'Initializes the buffer or commits the values to the log.
What this method does depends on from what callback it is called
and with which arguments. When called within `before_training`, it
initializes the aggregation buffer and instructs the training
algorithm what additional computations should be carried at each
st... | def do(self, callback_name, *args):
| (data, args) = self.parse_args(callback_name, args)
if (callback_name == 'before_training'):
if (not isinstance(self.main_loop.algorithm, UpdatesAlgorithm)):
raise ValueError
self.main_loop.algorithm.add_updates(self._variables.accumulation_updates)
self.main_loop.algorithm.a... |
'Pickle the main loop object to the disk.
If `*args` contain an argument from user, it is treated as
saving path to be used instead of the one given at the
construction stage.'
| def do(self, callback_name, *args):
| logger.info('Checkpointing has started')
(_, from_user) = self.parse_args(callback_name, args)
try:
path = self.path
if from_user:
(path,) = from_user
to_add = None
if self.save_separately:
to_add = {attr: getattr(self.main_loop, attr) for attr i... |
'Initialize a shared variable with generated parameters.
Parameters
var : object
A Theano shared variable whose value will be set with values
drawn from this :class:`NdarrayInitialization` instance.
rng : :class:`numpy.random.RandomState`
shape : tuple
A shape tuple for the requested parameter array shape.'
| def initialize(self, var, rng, shape=None):
| if (not shape):
shape = var.get_value(borrow=True, return_internal_type=True).shape
var.set_value(self.generate(rng, shape))
|
'Returns parameters with their hierarchical names.
The parameter names are formed from positions of their owner bricks
in the bricks hierarchy. The variable names are used for the
parameters that do not belong to any brick.
Returns
parameter_dict : dict
A dictionary of (hierarchical name, shared variable) pairs.'
| def get_parameter_dict(self):
| return self._parameter_dict
|
'Return the values of model parameters.
The same hierarhical names as in :meth:`get_parameter_dict` are
used to uniquely identify parameters.
Returns
parameter_values : OrderedDict
Dictionary of (hierarchical name, :class:`~numpy.ndarray`)
pairs.'
| def get_parameter_values(self):
| return OrderedDict(((name, parameter.get_value()) for (name, parameter) in self.get_parameter_dict().items()))
|
'Set the values of model parameters.
The same hierarhical names as in :meth:`get_parameter_dict` are
used to uniquely identify parameters.
Parameters
parameter_values : OrderedDict
Dictionary of (hierarchical name, :class:`~numpy.ndarray`)
pairs.'
| def set_parameter_values(self, parameter_values):
| parameters = self.get_parameter_dict()
unknown = (set(parameter_values) - set(parameters))
missing = (set(parameters) - set(parameter_values))
if len(unknown):
logger.error('unknown parameter names: {}\n'.format(unknown))
if len(missing):
logger.error('missing values f... |
'Get the bricks that do not have parents.
Returns
bricks : list of :class:`~blocks.bricks.base.Brick`'
| def get_top_bricks(self):
| return self.top_bricks
|
'Initialize the quantities.'
| def initialize_quantities(self):
| self._initialized = True
for quantity in self.quantities:
quantity.initialize()
|
'Get the aggregated values.'
| def get_aggregated_values(self):
| if (not self._initialized):
raise Exception('To readout you must first initialize, thenprocess batches!')
else:
ret_vals = [q.get_aggregated_value() for q in self.quantities]
return dict(zip(self.quantity_names, ret_vals))
|
'Aggregate the results for every batch.'
| def aggregate_quantities(self, numerical_values):
| if (not self._initialized):
raise Exception('To readout you must first initialize, thenprocess batches!')
else:
for quantity in self.quantities:
quantity.aggregate(*[numerical_values[self.requires.index(requirement)] for requirement in quantity.requires])
|
'Create aggregators and collect updates.'
| def _create_aggregators(self):
| self.initialization_updates = []
self.accumulation_updates = []
self.readout_variables = OrderedDict()
for v in self.variables:
logger.debug('variable to evaluate: %s', v.name)
if (not hasattr(v.tag, 'aggregation_scheme')):
if (not self._computation_graph.has_inputs(... |
'Compiles Theano functions.
.. todo::
The current compilation method does not account for updates
attached to `ComputationGraph` elements. Compiling should
be out-sourced to `ComputationGraph` to deal with it.'
| def _compile(self):
| logger.debug('Compiling initialization and readout functions')
if self.initialization_updates:
self._initialize_fun = theano.function([], [], updates=self.initialization_updates)
else:
self._initialize_fun = None
self._readout_fun = theano.function([], [tensor.as_tensor_varia... |
'Initialize the aggregators.'
| def initialize_aggregators(self):
| self._initialized = True
if (self._initialize_fun is not None):
self._initialize_fun()
|
'Readout the aggregated values.'
| def get_aggregated_values(self):
| if (not self._initialized):
raise Exception('To readout you must first initialize, then process batches!')
ret_vals = self._readout_fun()
return OrderedDict(equizip(self.variable_names, ret_vals))
|
'Compiles Theano functions.
.. todo::
The current compilation method does not account for updates
attached to `ComputationGraph` elements. Compiling should
be out-sourced to `ComputationGraph` to deal with it.'
| def _compile(self):
| inputs = []
outputs = []
updates = None
if self.theano_buffer.accumulation_updates:
updates = OrderedDict()
updates.update(self.theano_buffer.accumulation_updates)
inputs += self.theano_buffer.inputs
if self.updates:
if (updates is None):
updates = self.up... |
'Compute the variables over a data stream.
Parameters
data_stream : instance of :class:`.DataStream`
The data stream. Only the first epoch of data is used.
Returns
A mapping from record names to the values computed on the provided
dataset.'
| def evaluate(self, data_stream):
| self.initialize_aggregators()
if (not hasattr(self, '_aggregate_fun')):
self._compile()
if (self._aggregate_fun is not None):
for batch in data_stream.get_epoch_iterator(as_dict=True):
self.process_batch(batch)
else:
logger.debug('Only data independent variab... |
'Return a new Aggregator for this variable.'
| @abstractmethod
def get_aggregator(self):
| pass
|
'Initialize accumulators for this monitored quantity.'
| @abstractmethod
def initialize(self):
| pass
|
'Aggregate results for every batch.
\*args : list of :class:`~numpy.ndarray`
The values of the variables required to aggregate the
value of the quantity.'
| @abstractmethod
def aggregate(self, *args):
| pass
|
'Obtain the result of aggregation.'
| @abstractmethod
def get_aggregated_value(self):
| pass
|
'Return a hexadecimal version of the UUID bytes.
This is necessary to store ids in an SQLite database.'
| @property
def h_uuid(self):
| return self.uuid.hex
|
'Resume a log by setting a new random UUID.
Keeps a record of the old log that this is a continuation of. It
copies the status of the old log into the new log.'
| def resume(self):
| old_uuid = self.h_uuid
old_status = dict(self.status)
self.uuid = uuid4()
self.status.update(old_status)
self.status['resumed_from'] = old_uuid
|
'Retrieve the state for pickling.
:class:`sqlite3.Connection` objects are not picklable, so the
`conn` attribute is removed and the connection re-opened upon
unpickling.'
| def __getstate__(self):
| state = self.__dict__.copy()
if ('_conn' in state):
del state['_conn']
self.resume()
return state
|
'Filter the given variables.
Parameters
variables : list of :class:`~tensor.TensorVariable`'
| def __call__(self, variables):
| if self.roles:
variables = [var for var in variables if has_roles(var, self.roles, self.each_role)]
if (self.bricks is not None):
filtered_variables = []
for var in variables:
var_brick = get_brick(var)
if (var_brick is None):
continue
... |
'Decorator to make application properties.
Parameters
name : str
The name the property should take.
Examples
>>> class Foo(Brick):
... @application
... def apply(self, x):
... return x + 1
... @apply.property(\'inputs\')
... def apply_inputs(self):
... return [\'foo\', \'bar\']
>>> foo =... | def property(self, name):
| if (not isinstance(name, six.string_types)):
raise ValueError
def wrap_property(application_property):
self.properties[name] = application_property.__name__
return application_property
return wrap_property
|
'Decorator to assign a delegate application.
An application method can assign a delegate application. Whenever
an attribute is not available, it will be requested from the
delegate instead.
Examples
>>> class Foo(Brick):
... @application(outputs=[\'baz\'])
... def apply(self, x):
... return x + 1
... ... | def delegate(self, f):
| self.delegate_function = f.__name__
return f
|
'Instantiate :class:`BoundApplication` for each :class:`Brick`.'
| def __get__(self, instance, owner):
| if (instance is None):
return self
if (not hasattr(instance, '_bound_applications')):
instance._bound_applications = {}
key = '{}.{}'.format(self.brick.__name__, self.application_name)
return instance._bound_applications.setdefault(key, BoundApplication(self, instance))
|
'Allocate shared variables for parameters.
Based on the current configuration of this :class:`Brick` create
Theano shared variables to store the parameters. After allocation,
parameters are accessible through the :attr:`parameters` attribute.
This method calls the :meth:`allocate` method of all children
first, allowin... | def allocate(self):
| if hasattr(self, 'allocation_args'):
missing_config = [arg for arg in self.allocation_args if (getattr(self, arg) is NoneAllocation)]
if missing_config:
raise ValueError('allocation config not set: {}'.format(', '.join(missing_config)))
if (not self.allocation_config_p... |
'Brick implementation of parameter initialization.
Implement this if your brick needs to allocate its parameters.
.. warning::
This method should never be called directly. Call
:meth:`initialize` instead.'
| def _allocate(self):
| pass
|
'Initialize parameters.
Intialize parameters, such as weight matrices and biases.
Notes
If the brick has not allocated its parameters yet, this method will
call the :meth:`allocate` method in order to do so.'
| def initialize(self):
| if hasattr(self, 'initialization_args'):
missing_config = [arg for arg in self.initialization_args if (getattr(self, arg) is NoneInitialization)]
if missing_config:
raise ValueError('initialization config not set: {}'.format(', '.join(missing_config)))
if (not self.all... |
'Brick implementation of parameter initialization.
Implement this if your brick needs to initialize its parameters.
.. warning::
This method should never be called directly. Call
:meth:`initialize` instead.'
| def _initialize(self):
| pass
|
'Push the configuration for allocation to child bricks.
Bricks can configure their children, based on their own current
configuration. This will be automatically done by a call to
:meth:`allocate`, but if you want to override the configuration of
child bricks manually, then you can call this function manually.'
| def push_allocation_config(self):
| self._push_allocation_config()
self.allocation_config_pushed = True
for child in self.children:
try:
child.push_allocation_config()
except Exception:
self.allocation_config_pushed = False
raise
|
'Brick implementation of configuring child before allocation.
Implement this if your brick needs to set the configuration of its
children before allocation.
.. warning::
This method should never be called directly. Call
:meth:`push_allocation_config` instead.'
| def _push_allocation_config(self):
| pass
|
'Push the configuration for initialization to child bricks.
Bricks can configure their children, based on their own current
configuration. This will be automatically done by a call to
:meth:`initialize`, but if you want to override the configuration
of child bricks manually, then you can call this function manually.'
| def push_initialization_config(self):
| self._push_initialization_config()
self.initialization_config_pushed = True
for child in self.children:
try:
child.push_initialization_config()
except Exception:
self.initialization_config_pushed = False
raise
|
'Brick implementation of configuring child before initialization.
Implement this if your brick needs to set the configuration of its
children before initialization.
.. warning::
This method should never be called directly. Call
:meth:`push_initialization_config` instead.'
| def _push_initialization_config(self):
| pass
|
'Get dimension of an input/output variable of a brick.
Parameters
name : str
The name of the variable.'
| def get_dim(self, name):
| raise ValueError('No dimension information for {} available'.format(name))
|
'Get list of dimensions for a set of input/output variables.
Parameters
names : list
The variable names.
Returns
dims : list
The dimensions of the sources.'
| def get_dims(self, names):
| return [self.get_dim(name) for name in names]
|
'Returns unique path to this brick in the application graph.'
| def get_unique_path(self):
| if self.parents:
parent = min(self.parents, key=attrgetter('name'))
return (parent.get_unique_path() + [self])
else:
return [self]
|
'Return hierarhical name for a parameter.
Returns a path of the form ``brick1/brick2/brick3.parameter1``. The
delimiter is configurable.
Parameters
delimiter : str
The delimiter used to separate brick names in the path.'
| def get_hierarchical_name(self, parameter, delimiter=BRICK_DELIMITER):
| return '{}.{}'.format(delimiter.join(([''] + [brick.name for brick in self.get_unique_path()])), parameter.name)
|
'Returns Brick\'s Theano RNG, or a default one.
The default seed can be set through ``blocks.config``.'
| @property
def theano_rng(self):
| if (not hasattr(self, '_theano_rng')):
self._theano_rng = MRG_RandomStreams(self.theano_seed)
return self._theano_rng
|
'Distribute the source across the targets.
Parameters
\*\*kwargs : dict
The source and the target variables.
Returns
output : list
The new target variables.'
| @application
def apply(self, **kwargs):
| result = super(Distribute, self).apply(kwargs.pop(self.source_name), as_list=True)
for (i, name) in enumerate(self.target_names):
result[i] += kwargs.pop(name)
if len(kwargs):
raise ValueError
return result
|
'Perform the preprocessing of the attended.
Stage 1 of the attention mechanism, see :class:`AbstractAttention`
docstring for an explanation of stages. The default implementation
simply returns attended.
Parameters
attended : :class:`~theano.Variable`
The attended.
Returns
preprocessed_attended : :class:`~theano.Variabl... | @application(inputs=['attended'], outputs=['preprocessed_attended'])
def preprocess(self, attended):
| return attended
|
'Extract glimpses from the attended given the current states.
Stage 2 of the attention mechanism, see :class:`AbstractAttention`
for an explanation of stages. If `preprocessed_attended` is not
given, should trigger the stage 1.
This application method *must* declare its inputs and outputs.
The glimpses to be carried ov... | @abstractmethod
def take_glimpses(self, attended, preprocessed_attended=None, attended_mask=None, **kwargs):
| pass
|
'Return sensible initial values for carried over glimpses.
Parameters
batch_size : int or :class:`~theano.Variable`
The batch size.
attended : :class:`~theano.Variable`
The attended.
Returns
initial_glimpses : list of :class:`~theano.Variable`
The initial values for the requested glimpses. These might
simply consist of... | @abstractmethod
def initial_glimpses(self, batch_size, attended):
| pass
|
'Compute weights from energies in softmax-like fashion.
.. todo ::
Use :class:`~blocks.bricks.Softmax`.
Parameters
energies : :class:`~theano.Variable`
The energies. Must be of the same shape as the mask.
attended_mask : :class:`~theano.Variable`
The mask for the attended. The index in the sequence must be
the first di... | @application
def compute_weights(self, energies, attended_mask):
| energies = (energies - energies.max(axis=0))
unnormalized_weights = tensor.exp(energies)
if attended_mask:
unnormalized_weights *= attended_mask
normalization = (unnormalized_weights.sum(axis=0) + tensor.all((1 - attended_mask), axis=0))
return (unnormalized_weights / normalization)
|
'Compute weighted averages of the attended sequence vectors.
Parameters
weights : :class:`~theano.Variable`
The weights. The shape must be equal to the attended shape
without the last dimension.
attended : :class:`~theano.Variable`
The attended. The index in the sequence must be the first
dimension.
Returns
weighted_av... | @application
def compute_weighted_averages(self, weights, attended):
| return (tensor.shape_padright(weights) * attended).sum(axis=0)
|
'Compute attention weights and produce glimpses.
Parameters
attended : :class:`~tensor.TensorVariable`
The sequence, time is the 1-st dimension.
preprocessed_attended : :class:`~tensor.TensorVariable`
The preprocessed sequence. If ``None``, is computed by calling
:meth:`preprocess`.
attended_mask : :class:`~tensor.Tens... | @application(outputs=['weighted_averages', 'weights'])
def take_glimpses(self, attended, preprocessed_attended=None, attended_mask=None, **states):
| energies = self.compute_energies(attended, preprocessed_attended, states)
weights = self.compute_weights(energies, attended_mask)
weighted_averages = self.compute_weighted_averages(weights, attended)
return (weighted_averages, weights.T)
|
'Preprocess the sequence for computing attention weights.
Parameters
attended : :class:`~tensor.TensorVariable`
The attended sequence, time is the 1-st dimension.'
| @application(inputs=['attended'], outputs=['preprocessed_attended'])
def preprocess(self, attended):
| return self.attended_transformer.apply(attended)
|
'Compute next states taking glimpses on the way.'
| @abstractmethod
def apply(self, **kwargs):
| pass
|
'Compute glimpses given the current states.'
| @abstractmethod
def take_glimpses(self, **kwargs):
| pass
|
'Compute next states given current states and glimpses.'
| @abstractmethod
def compute_states(self, **kwargs):
| pass
|
'Compute glimpses with the attention mechanism.
A thin wrapper over `self.attention.take_glimpses`: takes care
of choosing and renaming the necessary arguments.
Parameters
\*\*kwargs
Must contain the attended, previous step states and glimpses.
Can optionaly contain the attended mask and the preprocessed
attended.
Retu... | @application
def take_glimpses(self, **kwargs):
| states = dict_subset(kwargs, self._state_names, pop=True)
glimpses = dict_subset(kwargs, self._glimpse_names, pop=True)
glimpses_needed = dict_subset(glimpses, self.previous_glimpses_needed)
result = self.attention.take_glimpses(kwargs.pop(self.attended_name), kwargs.pop(self.preprocessed_attended_name,... |
'Compute current states when glimpses have already been computed.
Combines an application of the `distribute` that alter the
sequential inputs of the wrapped transition and an application of
the wrapped transition. All unknown keyword arguments go to
the wrapped transition.
Parameters
\*\*kwargs
Should contain everythi... | @application
def compute_states(self, **kwargs):
| normal_inputs = [name for name in self._sequence_names if ('mask' not in name)]
sequences = dict_subset(kwargs, normal_inputs, pop=True)
glimpses = dict_subset(kwargs, self._glimpse_names, pop=True)
if self.add_contexts:
kwargs.pop(self.attended_name)
kwargs.pop(self.attended_mask_name, ... |
'Process a sequence attending the attended context every step.
In addition to the original sequence this method also requires
its preprocessed version, the one computed by the `preprocess`
method of the attention mechanism. Unknown keyword arguments
are passed to the wrapped transition.
Parameters
\*\*kwargs
Should con... | @recurrent
def do_apply(self, **kwargs):
| attended = kwargs[self.attended_name]
preprocessed_attended = kwargs.pop(self.preprocessed_attended_name)
attended_mask = kwargs.get(self.attended_mask_name)
sequences = dict_subset(kwargs, self._sequence_names, pop=True, must_have=False)
states = dict_subset(kwargs, self._state_names, pop=True)
... |
'Preprocess a sequence attending the attended context at every step.
Preprocesses the attended context and runs :meth:`do_apply`. See
:meth:`do_apply` documentation for further information.'
| @application
def apply(self, **kwargs):
| preprocessed_attended = self.attention.preprocess(kwargs[self.attended_name])
return self.do_apply(**dict_union(kwargs, {self.preprocessed_attended_name: preprocessed_attended}))
|
'Perform lookup.
Parameters
indices : :class:`~tensor.TensorVariable`
The indices of interest. The dtype must be integer.
Returns
output : :class:`~tensor.TensorVariable`
Representations for the indices of the query. Has :math:`k+1`
dimensions, where :math:`k` is the number of dimensions of the
`indices` parameter. The... | @application(inputs=['indices'], outputs=['output'])
def apply(self, indices):
| check_theano_variable(indices, None, ('int', 'uint'))
output_shape = ([indices.shape[i] for i in range(indices.ndim)] + [self.dim])
return self.W[indices.flatten()].reshape(output_shape)
|
'Perform the convolution.
Parameters
input_ : :class:`~tensor.TensorVariable`
A 4D tensor with the axes representing batch size, number of
channels, image height, and image width.
Returns
output : :class:`~tensor.TensorVariable`
A 4D tensor of filtered images (feature maps) with dimensions
representing batch size, numb... | @application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
| if (self.image_size == (None, None)):
input_shape = None
else:
input_shape = (self.batch_size, self.num_channels)
input_shape += self.image_size
output = self.conv2d_impl(input_, self.W, input_shape=input_shape, subsample=self.step, border_mode=self.border_mode, filter_shape=((self.n... |
'Apply the pooling (subsampling) transformation.
Parameters
input_ : :class:`~tensor.TensorVariable`
An tensor with dimension greater or equal to 2. The last two
dimensions will be downsampled. For example, with images this
means that the last two dimensions should represent the height
and width of your image.
Returns
... | @application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
| output = pool_2d(input_, self.pooling_size, stride=self.step, mode=self.mode, pad=self.padding, ignore_border=self.ignore_border)
return output
|
'Apply the linear transformation.
Parameters
input_ : :class:`~tensor.TensorVariable`
The input on which to apply the transformation
Returns
output : :class:`~tensor.TensorVariable`
The transformed input plus optional bias'
| @application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
| output = tensor.dot(input_, self.W)
if getattr(self, 'use_bias', True):
output += self.b
return output
|
'Apply the linear transformation.
Parameters
input_ : :class:`~tensor.TensorVariable`
The input on which to apply the transformation
Returns
output : :class:`~tensor.TensorVariable`
The transformed input plus optional bias'
| @application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
| (b,) = self.parameters
return (input_ + b)
|
'Apply the maxout transformation.
Parameters
input_ : :class:`~tensor.TensorVariable`
The input on which to apply the transformation
Returns
output : :class:`~tensor.TensorVariable`
The transformed input'
| @application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
| last_dim = input_.shape[(-1)]
output_dim = (last_dim // self.num_pieces)
new_shape = ([input_.shape[i] for i in range((input_.ndim - 1))] + [output_dim, self.num_pieces])
output = tensor.max(input_.reshape(new_shape, ndim=(input_.ndim + 1)), axis=input_.ndim)
return output
|
'Apply the linear transformation followed by maxout.
Parameters
input_ : :class:`~tensor.TensorVariable`
The input on which to apply the transformations
Returns
output : :class:`~tensor.TensorVariable`
The transformed input'
| @application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
| pre_activation = self.linear.apply(input_)
output = self.maxout.apply(pre_activation)
return output
|
'Standard softmax.
Parameters
input_ : :class:`~theano.Variable`
A matrix, each row contains unnormalized log-probabilities of a
distribution.
Returns
output_ : :class:`~theano.Variable`
A matrix with probabilities in each row for each distribution
from `input_`.'
| @application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
| return tensor.nnet.softmax(input_)
|
'Normalize log-probabilities.
Converts unnormalized log-probabilities (exponents of which do not
sum to one) into actual log-probabilities (exponents of which sum
to one).
Parameters
input_ : :class:`~theano.Variable`
A matrix, each row contains unnormalized log-probabilities of a
distribution.
Returns
output : :class:... | @application(inputs=['input_'], outputs=['output'])
def log_probabilities(self, input_):
| shifted = (input_ - input_.max(axis=1, keepdims=True))
return (shifted - tensor.log(tensor.exp(shifted).sum(axis=1, keepdims=True)))
|
'Computationally stable cross-entropy for pre-softmax values.
Parameters
y : :class:`~tensor.TensorVariable`
In the case of a matrix argument, each row represents a
probabilility distribution. In the vector case, each element
represents a distribution by specifying the position of 1 in a
1-hot vector.
x : :class:`~tens... | @application(inputs=['y', 'x'], outputs=['output'])
def categorical_cross_entropy(self, application_call, y, x):
| x = self.log_probabilities(x)
application_call.add_auxiliary_variable(x.copy(name='log_probabilities'))
if (y.ndim == (x.ndim - 1)):
indices = ((tensor.arange(y.shape[0]) * x.shape[1]) + y)
cost = (- x.flatten()[indices])
elif (y.ndim == x.ndim):
cost = (- (x * y).sum(axis=1))
... |
'Calls :meth:`wrap` for all applications of the base class.'
| def __call__(self, mcs, name, bases, namespace):
| if (not (len(bases) == 1)):
raise ValueError('can only wrap one class')
(base,) = bases
for attribute in base.__dict__.values():
if isinstance(attribute, Application):
self.wrap(attribute, namespace)
namespace['__doc__'] = _wrapped_class_doc.format(base.__name__, ... |
'Wrap an application of the base brick.
This method should be overriden to write into its
`namespace` argument all required changes.
Parameters
mcs : type
The metaclass.
wrapped : :class:`~blocks.bricks.base.Application`
The application to be wrapped.
namespace : dict
The namespace of the class being created.'
| @abstractmethod
def wrap(self, wrapped, namespace):
| pass
|
'Return initial states for an application call.
Default implementation assumes that the recurrent application
method is called `apply`. It fetches the state names
from `apply.states` and a returns a zero matrix for each of them.
:class:`SimpleRecurrent`, :class:`LSTM` and :class:`GatedRecurrent`
override this method w... | @application
def initial_states(self, batch_size, *args, **kwargs):
| result = []
for state in self.apply.states:
dim = self.get_dim(state)
if (dim == 0):
result.append(tensor.zeros((batch_size,)))
else:
result.append(tensor.zeros((batch_size, dim)))
return result
|
'Apply the simple transition.
Parameters
inputs : :class:`~tensor.TensorVariable`
The 2D inputs, in the shape (batch, features).
states : :class:`~tensor.TensorVariable`
The 2D states, in the shape (batch, features).
mask : :class:`~tensor.TensorVariable`
A 1D binary array in the shape (batch,) which is 1 if
there is d... | @recurrent(sequences=['inputs', 'mask'], states=['states'], outputs=['states'], contexts=[])
def apply(self, inputs, states, mask=None):
| next_states = (inputs + tensor.dot(states, self.W))
next_states = self.children[0].apply(next_states)
if mask:
next_states = ((mask[:, None] * next_states) + ((1 - mask[:, None]) * states))
return next_states
|
'Apply the Long Short Term Memory transition.
Parameters
states : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of current states in the shape
(batch_size, features). Required for `one_step` usage.
cells : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of current cells in the shape
(batch_size, fea... | @recurrent(sequences=['inputs', 'mask'], states=['states', 'cells'], contexts=[], outputs=['states', 'cells'])
def apply(self, inputs, states, cells, mask=None):
| def slice_last(x, no):
return x[:, (no * self.dim):((no + 1) * self.dim)]
activation = (tensor.dot(states, self.W_state) + inputs)
in_gate = self.gate_activation.apply((slice_last(activation, 0) + (cells * self.W_cell_to_in)))
forget_gate = self.gate_activation.apply((slice_last(activation, 1) +... |
'Apply the gated recurrent transition.
Parameters
states : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of current states in the shape
(batch_size, dim). Required for `one_step` usage.
inputs : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of inputs in the shape (batch_size,
dim)
gate_inputs : :c... | @recurrent(sequences=['mask', 'inputs', 'gate_inputs'], states=['states'], outputs=['states'], contexts=[])
def apply(self, inputs, gate_inputs, states, mask=None):
| gate_values = self.gate_activation.apply((states.dot(self.state_to_gates) + gate_inputs))
update_values = gate_values[:, :self.dim]
reset_values = gate_values[:, self.dim:]
states_reset = (states * reset_values)
next_states = self.activation.apply((states_reset.dot(self.state_to_state) + inputs))
... |
'Applies forward and backward networks and concatenates outputs.'
| @application
def apply(self, *args, **kwargs):
| forward = self.children[0].apply(as_list=True, *args, **kwargs)
backward = [x[::(-1)] for x in self.children[1].apply(reverse=True, as_list=True, *args, **kwargs)]
return [tensor.concatenate([f, b], axis=2) for (f, b) in equizip(forward, backward)]
|
'Apply the stack of transitions.
This is the undecorated implementation of the apply method.
A method with an @apply decoration should call this method with
`iterate=True` to indicate that the iteration over all steps
should be done internally by this method. A method with a
`@recurrent` method should have `iterate=Fal... | def do_apply(self, *args, **kwargs):
| nargs = len(args)
args_names = (self.apply.sequences + self.apply.contexts)
assert (nargs <= len(args_names))
kwargs.update(zip(args_names[:nargs], args))
if kwargs.get('reverse', False):
raise NotImplementedError
results = []
last_states = None
for (level, transition) in enumera... |
'Apply the stack of transitions.
Parameters
low_memory : bool
Use the slow, but also memory efficient, implementation of
this code.
\*args : :class:`~tensor.TensorVariable`, optional
Positional argumentes in the order in which they appear in
`self.apply.sequences` followed by `self.apply.contexts`.
\*\*kwargs : :class:... | @application
def apply(self, *args, **kwargs):
| if kwargs.pop('low_memory', False):
return self.low_memory_apply(*args, **kwargs)
return self.do_apply(*args, **kwargs)
|
'Returns the average cost over the minibatch.
The cost is computed by averaging the sum of per token costs for
each sequence over the minibatch.
.. warning::
Note that, the computed cost can be problematic when batches
consist of vastly different sequence lengths.
Parameters
outputs : :class:`~tensor.TensorVariable`
Th... | @application
def cost(self, application_call, outputs, mask=None, **kwargs):
| costs = self.cost_matrix(outputs, mask=mask, **kwargs)
cost = tensor.mean(costs.sum(axis=0))
add_role(cost, COST)
application_call.add_auxiliary_variable(((costs.sum() / mask.sum()) if (mask is not None) else costs.mean()), name='per_sequence_element')
return cost
|
'Returns generation costs for output sequences.
See Also
:meth:`cost` : Scalar cost.'
| @application
def cost_matrix(self, application_call, outputs, mask=None, **kwargs):
| batch_size = outputs.shape[1]
states = dict_subset(kwargs, self._state_names, must_have=False)
contexts = dict_subset(kwargs, self._context_names, must_have=False)
feedback = self.readout.feedback(outputs)
inputs = self.fork.apply(feedback, as_dict=True)
results = self.transition.apply(mask=mask... |
'A sequence generation step.
Parameters
outputs : :class:`~tensor.TensorVariable`
The outputs from the previous step.
Notes
The contexts, previous states and glimpses are expected as keyword
arguments.'
| @recurrent
def generate(self, outputs, **kwargs):
| states = dict_subset(kwargs, self._state_names)
contexts = dict_subset(kwargs, self._context_names, must_have=False)
glimpses = dict_subset(kwargs, self._glimpse_names)
next_glimpses = self.transition.take_glimpses(as_dict=True, **dict_union(states, glimpses, contexts))
next_readouts = self.readout.... |
'Produce outputs from readouts.
Parameters
readouts : :class:`~theano.Variable`
Readouts produced by the :meth:`readout` method of
a `(batch_size, readout_dim)` shape.'
| @abstractmethod
def emit(self, readouts):
| pass
|
'Compute generation cost of outputs given readouts.
Parameters
readouts : :class:`~theano.Variable`
Readouts produced by the :meth:`readout` method
of a `(..., readout dim)` shape.
outputs : :class:`~theano.Variable`
Outputs whose cost should be computed. Should have as many
or one less dimensions compared to `readout`... | @abstractmethod
def cost(self, readouts, outputs):
| pass
|
'Compute initial outputs for the generator\'s first step.
In the notation from the :class:`BaseSequenceGenerator`
documentation this method should compute :math:`y_0`.'
| @abstractmethod
def initial_outputs(self, batch_size):
| pass
|
'Compute the readout vector from states, glimpses, etc.
Parameters
\*\*kwargs: dict
Contains sequence generator states, glimpses,
contexts and feedback from the previous outputs.'
| @abstractmethod
def readout(self, **kwargs):
| pass
|
'Feeds outputs back to be used as inputs of the transition.'
| @abstractmethod
def feedback(self, outputs):
| pass
|
'Implements the respective method of :class:`Readout`.'
| @abstractmethod
def emit(self, readouts):
| pass
|
'Implements the respective method of :class:`Readout`.'
| @abstractmethod
def cost(self, readouts, outputs):
| pass
|
'Implements the respective method of :class:`Readout`.'
| @abstractmethod
def initial_outputs(self, batch_size):
| pass
|
'Implements the respective method of :class:`Readout`.'
| @abstractmethod
def feedback(self, outputs):
| pass
|
'Quick access to the (data stream, epoch iterator) pair.'
| @property
def iteration_state(self):
| return (self.data_stream, self.epoch_iterator)
|
'A shortcut for `self.log.status`.'
| @property
def status(self):
| return self.log.status
|
'Starts the main loop.
The main loop ends when a training extension makes
a `training_finish_requested` record in the log.'
| def run(self):
| logging.basicConfig()
self.profile.current = []
if hasattr(self._model, 'check_sanity'):
self._model.check_sanity(self.algorithm)
with change_recursion_limit(config.recursion_limit):
self.original_sigint_handler = signal.signal(signal.SIGINT, self._handle_epoch_interrupt)
self.or... |
'Find an extension with a given name.
Parameters
name : str
The name of the extension looked for.
Notes
Will crash if there no or several extension found.'
| def find_extension(self, name):
| return unpack([extension for extension in self.extensions if (extension.name == name)], singleton=True)
|
'Checks whether the current training should be terminated.
Parameters
level : {\'epoch\', \'batch\'}
The level at which this check was performed. In some cases, we
only want to quit after completing the remained of the epoch.'
| def _check_finish_training(self, level):
| if (self.log.current_row.get('training_finish_requested', False) or self.status.get('batch_interrupt_received', False)):
raise TrainingFinish
if ((level == 'epoch') and self.status.get('epoch_interrupt_received', False)):
raise TrainingFinish
|
'Print a report of timing information to standard output.
Parameters
f : object, optional
An object with a ``write`` method that accepts string inputs.
Can be a file object, ``sys.stdout``, etc. Defaults to
``sys.stderr``.'
| def report(self, f=sys.stderr):
| total = sum((v for (k, v) in self.total.items() if (len(k) == 1)))
def print_report(keys, level=0):
subtotal = 0
for key in keys:
if (len(key) > (level + 1)):
continue
subtotal += self.total[key]
section = ' '.join(key[(-1)].split('_'))
... |
'The operation to perform when an item is inserted/appended.'
| def _setitem(self, key, value):
| pass
|
'The operation to perform when an item is deleted.'
| def _delitem(self, key):
| pass
|
'Parameters
weight_init_std : éã¿ã®æšæºåå·®ãæå®ïŒe.g. 0.01ïŒ
\'relu\'ãŸãã¯\'he\'ãæå®ããå Žåã¯ãHeã®åæå€ããèšå®
\'sigmoid\'ãŸãã¯\'xavier\'ãæå®ããå Žåã¯ãXavierã®åæå€ããèšå®'
| def __init_weight(self, weight_init_std):
| all_size_list = (([self.input_size] + self.hidden_size_list) + [self.output_size])
for idx in range(1, len(all_size_list)):
scale = weight_init_std
if (str(weight_init_std).lower() in ('relu', 'he')):
scale = np.sqrt((2.0 / all_size_list[(idx - 1)]))
elif (str(weight_init_std... |
'Parameters
x : å
¥åããŒã¿
t : æåž«ã©ãã«
Returns'
| def loss(self, x, t):
| y = self.predict(x)
weight_decay = 0
for idx in range(1, (self.hidden_layer_num + 2)):
W = self.params[('W' + str(idx))]
weight_decay += ((0.5 * self.weight_decay_lambda) * np.sum((W ** 2)))
return (self.last_layer.forward(y, t) + weight_decay)
|
'Parameters
x : å
¥åããŒã¿
t : æåž«ã©ãã«
Returns
grads[\'W1\']ãgrads[\'W2\']ã...ã¯åå±€ã®éã¿
grads[\'b1\']ãgrads[\'b2\']ã...ã¯åå±€ã®ãã€ã¢ã¹'
| def numerical_gradient(self, x, t):
| loss_W = (lambda W: self.loss(x, t))
grads = {}
for idx in range(1, (self.hidden_layer_num + 2)):
grads[('W' + str(idx))] = numerical_gradient(loss_W, self.params[('W' + str(idx))])
grads[('b' + str(idx))] = numerical_gradient(loss_W, self.params[('b' + str(idx))])
return grads
|
'Parameters
x : å
¥åããŒã¿
t : æåž«ã©ãã«
Returns
grads[\'W1\']ãgrads[\'W2\']ã...ã¯åå±€ã®éã¿
grads[\'b1\']ãgrads[\'b2\']ã...ã¯åå±€ã®ãã€ã¢ã¹'
| def gradient(self, x, t):
| self.loss(x, t)
dout = 1
dout = self.last_layer.backward(dout)
layers = list(self.layers.values())
layers.reverse()
for layer in layers:
dout = layer.backward(dout)
grads = {}
for idx in range(1, (self.hidden_layer_num + 2)):
grads[('W' + str(idx))] = (self.layers[('Affin... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.