minimaxir / textgenrnn

Easily train your own text-generating neural network of any size and complexity on any text dataset with a few lines of code.
Other
4.94k stars 752 forks source link

Non-standard / complete hdf5? #36

Open kengberg opened 6 years ago

kengberg commented 6 years ago

If I save a .hdf5-file from Collaboratory and try uploading it with:

from textgenrnn import textgenrnn uploaded = files.upload() all_files = [(name, os.path.getmtime(name)) for name in os.listdir()] latest_file = sorted(all_files, key=lambda x: -x[1])[0][0]

and then

textgen = textgenrnn(latest_file) textgen.generate(20, temperature=0.8)

Then it throws this error:

`--------------------------------------------------------------------------- ValueError Traceback (most recent call last)

in () ----> 1 textgen = textgenrnn(latest_file) 2 textgen.generate(20, temperature=0.8) /usr/local/lib/python3.6/dist-packages/textgenrnn/textgenrnn.py in __init__(self, weights_path, vocab_path, config_path, name) 63 self.model = textgenrnn_model(self.num_classes, 64 cfg=self.config, ---> 65 weights_path=weights_path) 66 self.indices_char = dict((self.vocab[c], c) for c in self.vocab) 67 /usr/local/lib/python3.6/dist-packages/textgenrnn/model.py in textgenrnn_model(num_classes, cfg, context_size, weights_path, dropout, optimizer) 36 model = Model(inputs=[input], outputs=[output]) 37 if weights_path is not None: ---> 38 model.load_weights(weights_path, by_name=True) 39 model.compile(loss='categorical_crossentropy', optimizer=optimizer) 40 /usr/local/lib/python3.6/dist-packages/keras/engine/topology.py in load_weights(self, filepath, by_name, skip_mismatch, reshape) 2662 load_weights_from_hdf5_group_by_name( 2663 f, self.layers, skip_mismatch=skip_mismatch, -> 2664 reshape=reshape) 2665 else: 2666 load_weights_from_hdf5_group( /usr/local/lib/python3.6/dist-packages/keras/engine/topology.py in load_weights_from_hdf5_group_by_name(f, layers, skip_mismatch, reshape) 3462 ' weight(s), but the saved weights' + 3463 ' have ' + str(len(weight_values)) + -> 3464 ' element(s).') 3465 # Set values. 3466 for i in range(len(weight_values)): ValueError: Layer #2 (named "rnn_1") expects 3 weight(s), but the saved weights have 6 element(s).` Where as if I upload the .hdf5 from here: [https://eblong.com/zarf/ftp/ifdb-titles-epoch50.hdf5](url) - Or use one of the predefines ones from the weights-folder.. It works fine..
minimaxir commented 6 years ago

Are you sure the config matches the config the model was trained on? The error message implies that it was trained using bidirectional: True.

kengberg commented 6 years ago

Well - if I train a model, save the hdf5-weights - upload it, without changing any settings - the it still throws me an error..

I've just added the 6 lines to your original Colaboratory document..

minimaxir commented 6 years ago

You'd need to upload and specify the config and the vocab as well when creating the textgenrnn instance. (Colaboratory can only upload one file at a time, which makes this inconvenient)

kengberg commented 6 years ago

Well - I just thought it would work, now that the other examples slided past without problems..

I can't just manually change the settings in the second codeblock of yours?

minimaxir commented 6 years ago

The other examples assume the model is in the default textgenrnn configuration. (128 cell, 2 layers, not bidirectional). The Colaboratory config deliberately uses a different configuration to make full utilization of the GPU.

kengberg commented 6 years ago

Even if I write the parameters from the config.json-file, in the settings-box in colaboratory and run it - not even then, will it run without errors..

ghost commented 6 years ago

Solution to that topic is to provide VOCAB, WEIGHT & CONFIG that was used for training back to the function that should evaluate/create new texts:

textgen = textgenrnn(weights_path='textgenrnn_weights.hdf5', vocab_path='textgenrnn_vocab.json', config_path='textgenrnn_config.json') textgen.generate_samples()

kengberg commented 6 years ago

Yess..

The problem is to do that in Collaboratory.