textgenrnn icon indicating copy to clipboard operation
textgenrnn copied to clipboard

error saving model when training with context

Open jeremybmerrill opened this issue 6 years ago • 7 comments

Hi! This is a great library, thanks for making and maintaining it. It's fun to play with and the code is easy to follow. :)

I'm trying to train a model with context labels, using a Colab notebook (and locally). Once the training finishes, at https://github.com/minimaxir/textgenrnn/blob/master/textgenrnn/textgenrnn.py#L225-L227, there's an error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-d7d7168cce4b> in <module>()
      3 
      4 textgen.reset()
----> 5 textgen.train_from_file(file_path, new_model=True, num_epochs=2, gen_epochs=5, context=True)

/usr/local/lib/python3.6/dist-packages/textgenrnn/textgenrnn.py in train_from_file(self, file_path, header, delim, new_model, context, is_csv, **kwargs)
    320         if new_model:
    321             self.train_new_model(
--> 322                 texts, context_labels=context_labels, **kwargs)
    323         else:
    324             self.train_on_texts(texts, context_labels=context_labels, **kwargs)

/usr/local/lib/python3.6/dist-packages/textgenrnn/textgenrnn.py in train_new_model(self, texts, context_labels, num_epochs, gen_epochs, batch_size, dropout, validation, save_epochs, multi_gpu, **kwargs)
    291                             save_epochs=save_epochs,
    292                             multi_gpu=multi_gpu,
--> 293                             **kwargs)
    294 
    295     def save(self, weights_path="textgenrnn_weights_saved.hdf5"):

/usr/local/lib/python3.6/dist-packages/textgenrnn/textgenrnn.py in train_on_texts(self, texts, context_labels, batch_size, num_epochs, verbose, new_model, gen_epochs, train_size, max_gen_length, validation, dropout, via_new_model, save_epochs, multi_gpu, **kwargs)
    225         if context_labels is not None:
    226             self.model = Model(inputs=self.model.input[0],
--> 227                                outputs=self.model.output[1])
    228 
    229     def train_new_model(self, texts, context_labels=None, num_epochs=50,

/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

/usr/local/lib/python3.6/dist-packages/keras/engine/network.py in __init__(self, *args, **kwargs)
     91                 'inputs' in kwargs and 'outputs' in kwargs):
     92             # Graph network
---> 93             self._init_graph_network(*args, **kwargs)
     94         else:
     95             # Subclassed network

/usr/local/lib/python3.6/dist-packages/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name)
    157                                  'must come from `keras.layers.Input`. '
    158                                  'Received: ' + str(x) +
--> 159                                  ' (missing previous layer metadata).')
    160             # Check that x is an input tensor.
    161             layer, node_index, tensor_index = x._keras_history

ValueError: Input tensors to a Model must come from `keras.layers.Input`. Received: Tensor("strided_slice:0", shape=(40,), dtype=float32) (missing previous layer metadata).

This error also occurs in a colab version of the context iPython notebook in the docs folder here in this repo. I wonder if an API changed between this summer and the present day?

I'm not enough of a Keras expert to know what it should be. (i.e. there's no error if you remove the indexes on those lines, but I'm not sure if that's doing the semantically "right" thing.)

jeremybmerrill avatar Dec 04 '18 17:12 jeremybmerrill

Entirely possible something changed on the Keras side (I haven't tested much with context workflows).

Looking into it.

minimaxir avatar Dec 06 '18 20:12 minimaxir

any progress?

LilChickenz avatar Jan 11 '19 05:01 LilChickenz

I get the same error. What version of Keras does it work on? I may just downgrade it.

dontstopcoding avatar Feb 02 '19 22:02 dontstopcoding

I'm getting the same error.

durakkerem avatar Feb 05 '19 05:02 durakkerem

Same error, bails out at the end:

Traceback (most recent call last):
  File "tweet_generator.py", line 70, in <module>
    batch_size=cfg['batch_size'])
  File "/usr/local/lib/python3.7/site-packages/textgenrnn/textgenrnn.py", line 230, in train_on_texts
    outputs=self.model.output[1])
  File "/usr/local/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/keras/engine/network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/keras/engine/network.py", line 159, in _init_graph_network
    ' (missing previous layer metadata).')
ValueError: Input tensors to a Model must come from `keras.layers.Input`. Received: Tensor("strided_slice:0", shape=(40,), dtype=float32) (missing previous layer metadata).

nikolai3d avatar Feb 18 '19 18:02 nikolai3d

I'm still having the same issue. Has anyone tried downgrading Keras?

tadleta avatar Mar 26 '19 07:03 tadleta

I'm still having the same issue. Has anyone tried downgrading Keras?

I tried to use it with lower version keras (think it was 2.2.x maybe?) but then I got other issues like #41

now with eras 2.4.3 I got the same problem the op has mentioned (though I used minimaxir/tweet-generator)):

2765/2765 [==============================] - 1250s 452ms/step - loss: 1.3567
Traceback (most recent call last):
  File "tweet_generator.py", line 70, in <module>
    batch_size=cfg['batch_size'])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/textgenrnn/textgenrnn.py", line 278, in train_on_texts
    outputs=self.model.output[1])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 242, in __new__
    return functional.Functional(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/training/tracking/base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/functional.py", line 115, in __init__
    self._init_graph_network(inputs, outputs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/training/tracking/base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/functional.py", line 144, in _init_graph_network
    self._validate_graph_inputs_and_outputs()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/functional.py", line 639, in _validate_graph_inputs_and_outputs
    ' (missing previous layer metadata).')
ValueError: Input tensors to a Functional must come from `tf.keras.Input`. Received: Tensor("strided_slice:0", shape=(40,), dtype=float32) (missing previous layer metadata).

any ideas how to resolve it and save the model?

ggegoge avatar Aug 25 '20 10:08 ggegoge