Pickle load error

681 views
Skip to first unread message

Tae-Ho Kim

unread,
Apr 15, 2014, 2:45:50 PM4/15/14
to pylear...@googlegroups.com
I'm implmenting DAE myself and trying to dump and load the model using cPickle.

1) I use following code to save my model

...
da = dA(numpy_rng=rng, theano_rng=theano_rng, input=x, n_visible=n_visible, n_hidden=n_hidden)  
...
f = open('test.pkl','wb')
cPickle.dump(da,f)

2) I use following code to load my model
...
import cPickle as pkl
f = open('test.pkl','rb')
pkl.load(f)

3) Then the error occurs

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-3c9e2e208177> in <module>()
----> 1 pkl.load(f)

/opt/lisa/os/epd-7.1.2/lib/python2.7/copy_reg.py in _reconstructor(cls, base, state)
     46 def _reconstructor(cls, base, state):
     47     if base is object:
---> 48         obj = object.__new__(cls)
     49     else:
     50         obj = base.__new__(cls, state)

TypeError: ('object.__new__(X): X is not a type object (module)', <function _reconstructor at 0x7fa65a9c2cf8>, (<module 'dA' from 'dA.pyc'>, <type 'object'>, None))

Did I do sth wrong?

Thanks
Tae-Ho Kim

Frédéric Bastien

unread,
Apr 15, 2014, 3:55:30 PM4/15/14
to pylear...@googlegroups.com
Can you try again with the module pickle instead of cpickle. It will give a better back-trace.

Do the DAE object have a __getstate__ and __setstate__ methods?

Fred


--
You received this message because you are subscribed to the Google Groups "pylearn-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylearn-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ian Goodfellow

unread,
Apr 15, 2014, 3:57:43 PM4/15/14
to pylear...@googlegroups.com
Did you dynamically modify some of the methods of your DAE?

Tae-Ho Kim

unread,
Apr 15, 2014, 4:26:43 PM4/15/14
to pylear...@googlegroups.com, no...@nouiz.org
No It doesn't have those methods.
When I use pickle instead of cpickle, it returns:

In [1]: import pickle

In [2]: pickle.load(open('test.pkl'))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-8bfa9dbb4487> in <module>()
----> 1 pickle.load(open('test.pkl'))

/opt/lisa/os/epd-7.1.2/lib/python2.7/pickle.py in load(file)
   1376
   1377 def load(file):
-> 1378     return Unpickler(file).load()
   1379
   1380 def loads(str):

/opt/lisa/os/epd-7.1.2/lib/python2.7/pickle.py in load(self)
    856             while 1:
    857                 key = read(1)
--> 858                 dispatch[key](self)
    859         except _Stop, stopinst:
    860             return stopinst.value

/opt/lisa/os/epd-7.1.2/lib/python2.7/pickle.py in load_global(self)
   1088         module = self.readline()[:-1]
   1089         name = self.readline()[:-1]
-> 1090         klass = self.find_class(module, name)
   1091         self.append(klass)
   1092     dispatch[GLOBAL] = load_global

/opt/lisa/os/epd-7.1.2/lib/python2.7/pickle.py in find_class(self, module, name)
   1124         __import__(module)
   1125         mod = sys.modules[module]
-> 1126         klass = getattr(mod, name)
   1127         return klass
   1128

AttributeError: 'module' object has no attribute 'dA'

Tae-Ho Kim

unread,
Apr 15, 2014, 4:35:44 PM4/15/14
to pylear...@googlegroups.com
Actually what i'm doing is to implement convolutional autoencoder in theano, and I chose dA for the starting point. (code at http://deeplearning.net/tutorial/code/dA.py)
I changed some methods but I don't think those are crucial. I attached my dA class.

class dA(object):
    def __init__(self, numpy_rng, n_visible, n_hidden, theano_rng=None, input=None,
                 W=None, bhid=None, bvis=None):
        self.n_visible = n_visible
        self.n_hidden = n_hidden

        # create a Theano random generator that gives symbolic random values
        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))

        # note : W' was written as `W_prime` and b' as `b_prime`
        if not W:
            initial_W = numpy.asarray(numpy_rng.uniform(
                      low=-4 * numpy.sqrt(6. / (n_hidden + n_visible)),
                      high=4 * numpy.sqrt(6. / (n_hidden + n_visible)),
                      size=(n_visible, n_hidden)), dtype=theano.config.floatX)
            W = theano.shared(value=initial_W, name='W', borrow=True)

        if not bvis:
            bvis = theano.shared(value=numpy.zeros(n_visible, dtype=theano.config.floatX), borrow=True)
        if not bhid:
            bhid = theano.shared(value=numpy.zeros(n_hidden, dtype=theano.config.floatX), name='b', borrow=True)

        self.W = W
        self.b = bhid
        self.b_prime = bvis
        self.W_prime = self.W.T
        self.theano_rng = theano_rng
        if input == None:
            self.x = T.matrix(name='input')
        else:
            self.x = input

        self.params = [self.W, self.b, self.b_prime]

    def corruption(self, input, corruption_level):
        return  self.theano_rng.binomial(size=input.shape, n=1,
                                         p=1 - corruption_level,
                                         dtype=theano.config.floatX) * input

    def encode(self, input):
        return T.tanh(T.dot(input, self.W) + self.b)
        #return T.nnet.sigmoid(T.dot(input, self.W) + self.b)
       
    def decode(self, hidden):
        return T.dot(hidden, self.W_prime) + self.b_prime
        #return T.nnet.sigmoid(T.dot(hidden, self.W_prime) + self.b_prime)
   
    def reconstruct(self, input):
        return self.decode(self.encode(input))

    def get_cost_updates(self, corruption_level, learning_rate, L1):
        tilde_x = self.corruption(self.x, corruption_level)
        y = self.encode(tilde_x)
        z = self.decode(y)
       
        L = ((self.x - z) ** 2).sum(axis=1).mean()
        reg_units = T.abs_(self.encode(self.x)).sum(axis=1).mean()
        cost = L + reg_units * L1

        gparams = T.grad(cost, self.params)
        updates = []
        for param, gparam in zip(self.params, gparams):
            updates.append((param, param - learning_rate * gparam))

        return (cost, updates)

Tae-Ho Kim

unread,
Apr 15, 2014, 8:21:25 PM4/15/14
to pylear...@googlegroups.com
Once I added a line ' from dA import dA', the problem solved,
Thank you Fred, Ian.
Message has been deleted

rajkumar...@gmail.com

unread,
May 24, 2018, 6:36:27 AM5/24/18
to pylearn-users
Thank you in 2018!
Reply all
Reply to author
Forward
0 new messages