utils – Utilities functions operating on the graph

Reference

exception theano.gof.utils.MethodNotDefined

To be raised by functions defined as part of an interface.

When the user sees such an error, it is because an important interface function has been left out of an implementation class.

theano.gof.utils.add_tag_trace(thing, user_line=1)

Add tag.trace to an node or variable.

The argument is returned after being affected (inplace).

Parameters:
  • thing – The object where we add .tag.trace.
  • user_line – The max number of user line to keep.

Notes

We alse use config.traceback.limit for the maximum number of stack level we look.

theano.gof.utils.deprecated(filename, msg='')

Decorator which will print a warning message on the first call.

Use it like this:

@deprecated(‘myfile’, ‘do something different...’) def fn_name(...)

...

And it will print:

WARNING myfile.fn_name deprecated. do something different...
theano.gof.utils.difference(seq1, seq2)

Returns all elements in seq1 which are not in seq2: i.e seq1\seq2.

theano.gof.utils.flatten(a)

Recursively flatten tuple, list and set in a list.

theano.gof.utils.give_variables_names(variables)

Gives unique names to an iterable of variables. Modifies input.

This function is idempotent.

theano.gof.utils.hash_from_dict(d)

Work around the fact that dict are not hashable in python.

This request that all object have a sorted order that depend only on the key of the object. We support only integer/float/string keys.

Also, we transform values that are list into tuple as list are not hashable.

Notes

Special case for OrderedDict, it use the order of the dict, so the key don’t need to be sortable.

theano.gof.utils.hash_from_file(file_path)

Return the MD5 hash of a file.

theano.gof.utils.memoize(f)

Cache the return value for each tuple of arguments (which must be hashable).

theano.gof.utils.remove(predicate, coll)

Return those items of collection for which predicate(item) is true.

Examples

>>> def even(x):
...     return x % 2 == 0
>>> remove(even, [1, 2, 3, 4])
[1, 3]
theano.gof.utils.simple_extract_stack(f=None, limit=None)

This is traceback.extract_stack from python 2.7 with this change:

  • Comment the update of the cache.

This is because this update cause an call to os.stat to get the line content. This cause too much long on cluster.

theano.gof.utils.toposort(prereqs_d)

Sorts prereqs_d.keys() topologically.

prereqs_d[x] contains all the elements that must come before x in the ordering.

theano.gof.utils.uniq(seq)

Do not use set, this must always return the same value at the same index. If we just exchange other values, but keep the same pattern of duplication, we must keep the same order.