lisa-lab/pylearn2 — pull request #512 by SuperElectric (2014-01-18 22:03:54+00:00)
Title: Added dtypes to spaces.

Summary: Spaces now (optionally) specify data type in addition to data geometry. The behavior of existing code is unaffected.

Motivation: All Spaces used to force a cast to float whenever space.np_format_as or space.format_as. This was done because Theano didn't support other datatypes on the GPU. However, to those not expecting this feature, it can be a problem, particularly if the result of format_as is not even intended to be processed on the GPU. Also, it seems likely that Theano will support non-float datatypes in the future.

Methods affected:

format_as(batch, target_space)
If to_space has a dtype, it will cast the formatted batch to that dtype.

validate(batch), np_validate(batch):
If self has a dtype, this will throw an exception if batch's datatype cannot be trivially cast to self.dtype (e.g. if batch is complex and the space isn't).

get_origin_batch(batch_size, dtype), make_theano_batch(...), make_shared_batch(...)
As before, if dtype is specified, this will return a batch of that dtype.
If dtype is omitted (None), these will return a batch of self.dtype. If self.dtype is also None, this will throw an exception.

Other changes:

There was a lot of code duplication between format_as() and np_format_as(). The logic of what they do is the same, with only a few differences here and there to accommodate API differences between symbolic and numeric batches. Therefore, these functions now only check whether the batch is symbolic or numeric, before passing it on to _format_as(). This method only branches on batch type for those few lines that need to, thus eliminating code duplication. Subclasses of Space now only need to override _format_as_impl().

For the same reason, np_batch_size() and np_valiate() now wrap _batch_size and _validate. Subclasses implement _batch_size_impl() and _validate_impl().

For CompositeSpaces, the dtype can either be simple or composite. A composite dtype is a nested tuple of dtypes. A simple dtype is interpreted as a nested tuple of dtypes where all the leaves are the same.

As in Theano, 'floatX' is a legal dtype string. It is internally converted to theano.config.floatX.

Backwards compatibility:

All existing code should behave as before. This is because all spaces have theano.config.floatX as their default dtype. Unless they are constructed with a different dtype argument, they should cast all batches to floatX as before.