1

I trained a DNN using Keras. However, I am unable to save and load the model.

from keras.models import load_model
model.save('/home/ubuntu/pynb/savedmodel.tfl')
#model.load('/home/ubuntu/pynb/savedmodel.tfl')

But I find that instead of savedmodel.tfl file, I find 3 files with .meta, .index and .data.00000-of-00001 extensions. So, when I try to load using model.load, it says that the file is not found.

I also tried other alternatives suggested on SO

model_json = model.to_json()

But I get an error AttributeError: 'DNN' object has no attribute 'to_json'

I'm using Keras version 2.0.4.

Please suggest how to save correctly and how to load from 3 files situation ?

Edit: (I'm adding code related to building the model)

net = tflearn.input_data([None, size_of_each_vector]) 
net = tflearn.embedding(net, input_dim=vocab_size, output_dim=128) 
net = tflearn.lstm(net, 128, dropout=0.6) # Set the dropout to 0.6
net = tflearn.fully_connected(net, no_of_unique_y_labels, activation='softmax') # relu or softmax
net = tflearn.regression(net, 
                     optimizer='adam',
                     learning_rate=1e-4,
                     loss='categorical_crossentropy')

model = tflearn.DNN(net, tensorboard_verbose=0)
model.fit(X_train_padded_seqs, y_train, 
       validation_set=(X_test_padded_seqs, y_test), 
       n_epoch=n_epoch,
       show_metric=True, 
       batch_size=100)
Rob
  • 14,746
  • 28
  • 47
  • 65
rajkiran
  • 332
  • 3
  • 14
  • model.save() saves to an HDF5 file, so what happens if you save to a.h5 extension rather than .tfl? Also note, model.to_json() only saves the architecture of the model. It won't save its weights or training configurations like model.save() does. With that being said, what type of object is model? Given that the error message is stating the DNN object does not have the to_json(), this indicates that your model isn't one of Keras Sequential or Functional models. Can you share your code that shows the model being instantiated? – blackHoleDetector Oct 12 '17 at 00:49
  • Thanks for your response. The error/issue is same even if I have .h5 as the extension (saves 3 files) Yes, its a DNN object. I've edited the question to add the code that shows model being instantiated – rajkiran Oct 12 '17 at 01:02
  • I'm a newbie and I was kind of expecting that model built using tflearn to work with Keras. If that assumption was wrong, can I request you to post how the same model can be built using Keras ? – rajkiran Oct 12 '17 at 01:08
  • Yes, you're going to need a Keras model to use the Keras model save functions. Keras is actually a high level language that wraps TF code into code that's relatively easier to read and work with. I suggest starting out with the Keras Sequential model. It shouldn't be difficult to map your TF model into a Keras Sequential model. The quick guide for Keras Sequential model is here: https://keras.io/getting-started/sequential-model-guide/ I also have a YouTube playlist for getting started with Keras as well here: https://www.youtube.com/playlist?list=PL_SSujepRkqyjjbWnXQBzKXXm86UQ53Ic – blackHoleDetector Oct 12 '17 at 01:15
  • Please add tags. –  Oct 12 '17 at 13:59

1 Answers1

1

The model you're using is a Tensorflow model. Your model must be a Keras model in order to take advantage of the Keras save functions.

Once you transform your model into a Keras model (if you choose to), this video goes over the different saving and loading mechanisms that Keras offers.

Also, just a quick note, model.to_json() only saves the architecture of the model. It won't save its weights or training configurations like model.save() does.

blackHoleDetector
  • 2,975
  • 2
  • 13
  • 13