hycis / bidirectional_RNN

bidirectional lstm
MIT License
153 stars 52 forks source link

Load_weights not working #6

Closed cjmcmurtrie closed 9 years ago

cjmcmurtrie commented 9 years ago

It looks like there may be a problem with the load_weights method when trying to re-load the bi-directional model after a previous training session.

  File "D:/Final project/TimeNLPTest/KerasVec2Vec.py", line 130, in buildBiLSTM
    model.load_weights(dir + "StackedLSTM E-D 13 epochs 50 512 20 relu mse adam 2015-08-23 10.55.41.621000 .wt")
  File "C:\Anaconda\lib\site-packages\keras\models.py", line 492, in load_weights
    for k in range(f.attrs['nb_layers']):
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (D:\Build\h5py\h5py-2.5.x\h5py\_objects.c:2475)
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (D:\Build\h5py\h5py-2.5.x\h5py\_objects.c:2432)
  File "C:\Anaconda\lib\site-packages\h5py\_hl\attrs.py", line 52, in __getitem__
    attr = h5a.open(self._id, self._e(name))
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (D:\Build\h5py\h5py-2.5.x\h5py\_objects.c:2475)
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (D:\Build\h5py\h5py-2.5.x\h5py\_objects.c:2432)
  File "h5py\h5a.pyx", line 77, in h5py.h5a.open (D:\Build\h5py\h5py-2.5.x\h5py\h5a.c:2079)
KeyError: "Can't open attribute (Can't locate attribute: 'nb_layers')"

I can try and work with you to sort this out, what do you think the problem could be?

cjmcmurtrie commented 9 years ago

To be clear the model I used when saving those weights was identitical to the one I used when loading them:

model = Sequential()
model.add(BiDirectionLSTM(embedding_size, hidden_size, init=initialize))
model.add(Dense(hidden_size, hidden_size, init=initialize))
model.add(Activation('relu'))
model.add(RepeatVector(maxlen))
model.add(LSTM(hidden_size, hidden_size, return_sequences=True, init=initialize))
model.add(TimeDistributedDense(hidden_size, output_size, activation='softmax', init=initialize))
cjmcmurtrie commented 9 years ago

Ignore this, I found the problem.

TELSER1 commented 8 years ago

@cjmcmurtrie Could you clarify what the issue was? I'm having a similar problem.

bryan-lunt commented 8 years ago

I have this same problem, and I'd like to know what the solution was.

bryan-lunt commented 8 years ago

@TELSER1 Non-Existant and/or 0 byte h5 file.

erlizhou commented 8 years ago

I also have this problem @cjmcmurtrie @bryan-lunt can you tell me the solution?

hycis commented 8 years ago

Maybe you can post your code up here, otherwise you may also wish to try out mozi bilstm https://github.com/hycis/Mozi/blob/master/example/imdb_bilstm.py

bryan-lunt commented 8 years ago

I think it was that my h5 file was bad.

bryan-lunt commented 8 years ago

@erlizhou It was definitely a non-existant file.

bryan-lunt commented 8 years ago

I guess it needs a separate issue on the master branch, but it seems that load_weights will create the .h5 file if it doesn't exist. I don't like that behavior.

bryan-lunt commented 8 years ago

I fixed this in my own keras repo. https://github.com/bryan-lunt/keras/tree/fix_load_weights

You can comment on my pull-request on the main keras repo. https://github.com/fchollet/keras/pull/1736

south-ocean commented 6 years ago

@erlizhou I am also face this problem, do you solve it?

south-ocean commented 6 years ago

@bryan-lunt Hello, I can't find the solution from the link that you give, can you say it again, thank you.

bryan-lunt commented 6 years ago

@south-ocean You can clone my forked git repo and see the difference between that branch and the master it was based off of. I haven't delt with Keras for quite a while. The problem I was facing (judging by my comments) was that I was trying to load an h5 file that did not exist.

Hemanth-Mydugolam commented 6 years ago

@cjmcmurtrie can you please give me brief about the error how to solve it.

brianleegit commented 6 years ago

@cjmcmurtrie Hi, I also have the same problem, can you tell me how you fix it?

bryan-lunt commented 6 years ago

The fix to the problem I was having is right here:

https://github.com/bryan-lunt/keras/commit/c23579e059c9cd88a956b7eac9a69f7d7e276fc8

I believe that it's already been merged into the main branch of keras.

The problem ( my problem) was that if you asked for an hdf5 file that did not already exist, an empty one would be created automatically.

This change prevents that. If you don't want to change keras for that, you can write your own code that will check for the existence of the file before trying to load it with keras.

yuyifan1991 commented 6 years ago

who solve the problem? who can tell me ?