changeset 1419:cff305ad9f60

TensorFnDataset - added x_ attribute that caches the dataset function return value, but does not get pickled.
author James Bergstra <bergstrj@iro.umontreal.ca>
date Fri, 04 Feb 2011 16:05:22 -0500
parents 383d4c061546
children 7374d676c9b0
files pylearn/dataset_ops/protocol.py
diffstat 1 files changed, 12 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/dataset_ops/protocol.py	Fri Feb 04 16:03:25 2011 -0500
+++ b/pylearn/dataset_ops/protocol.py	Fri Feb 04 16:05:22 2011 -0500
@@ -92,13 +92,20 @@
             self.fn, self.fn_args = fn
         except:
             self.fn, self.fn_args = fn, ()
+    def __getstate__(self):
+        rval = dict(self.__dict__)
+        if 'x_' in rval:
+            del rval['x_']
+        return rval
 
     def __eq__(self, other):
         return super(TensorFnDataset, self).__eq__(other) and self.fn == other.fn \
                 and self.fn_args == other.fn_args
 
     def __hash__(self):
-        return super(TensorFnDataset, self).__hash__() ^ hash(self.fn) ^ hash(self.fn_args)
+        return (super(TensorFnDataset, self).__hash__()
+                ^ hash(self.fn)
+                ^ hash(self.fn_args))
 
     def __str__(self):
         try:
@@ -107,7 +114,10 @@
             return "%s{%s}" % (self.__class__.__name__, self.fn, self.fn_args)
 
     def perform(self, node, (idx,), (z,)):
-        x = self.fn(*self.fn_args)
+        try:
+            x = self.x_
+        except:
+            x = self.x_ = self.fn(*self.fn_args)
         if idx.ndim == 0:
             z[0] = x[int(idx)]
         else: