question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In case of using TextClassifier, cannot export keras model

See original GitHub issue

Bug Description

In case of using TextClassifier, I export final fitted model as keras and it occurs the following exception.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-27597b12761d> in <module>()
      3 
      4 model_filename = 'models/text_model.h5'
----> 5 classifier.export_keras_model(model_filename)

~\workspace\auto-keras\autokeras\autokeras\supervised.py in export_keras_model(self, model_file_name)
    183     def export_keras_model(self, model_file_name):
    184         """ Exports the best Keras model to the given filename. """
--> 185         self.cnn.best_model.produce_keras_model().save(model_file_name)
    186 
    187     def predict(self, x_test):

~\workspace\auto-keras\autokeras\autokeras\nn\graph.py in produce_keras_model(self)
    572     def produce_keras_model(self):
    573         """Build a new keras model based on the current graph."""
--> 574         return KerasModel(self).model
    575 
    576     def _layer_ids_in_order(self, layer_ids):

~\workspace\auto-keras\autokeras\autokeras\nn\graph.py in __init__(self, graph)
    724                     edge_input_tensor = node_list[u]
    725 
--> 726                 temp_tensor = keras_layer(edge_input_tensor)
    727                 node_list[v] = temp_tensor
    728 

~\AppData\Roaming\Python\Python36\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
    412                 # Raise exceptions in case the input is not compatible
    413                 # with the input_spec specified in the layer constructor.
--> 414                 self.assert_input_compatibility(inputs)
    415 
    416                 # Collect input shapes to build layer.

~\AppData\Roaming\Python\Python36\site-packages\keras\engine\base_layer.py in assert_input_compatibility(self, inputs)
    309                                      self.name + ': expected ndim=' +
    310                                      str(spec.ndim) + ', found ndim=' +
--> 311                                      str(K.ndim(x)))
    312             if spec.max_ndim is not None:
    313                 ndim = K.ndim(x)

ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=3

Reproducing Steps

Training

import pandas as pd
import keras
from autokeras import TextClassifier
from sklearn.model_selection import train_test_split
from keras.utils import plot_model
from keras.models import load_model

def read_csv(filename):
    data = pd.read_csv(filename, header=None)

    features = []
    labels = []
    for i in range(data[0].shape[0]):
        features.append(data[0][i])
        labels.append(data[1][i])
        
    return features, labels

filename = 'data/happiness.csv'
features, labels = read_csv(filename)
train_features, test_features, train_labels, test_labels = train_test_split(features, labels, train_size=0.9, test_size=0.1)
classifier = TextClassifier(verbose=True)
classifier.fit(x=train_features, y=train_labels, time_limit=2 * 60)
classifier.final_fit(train_features, train_labels, test_features, test_labels, retrain=True)
y = classifier.evaluate(test_features, test_labels)
print(y)

Export keras model

from keras.utils import plot_model
from keras.models import load_model

model_filename = 'models/text_model.h5'
classifier.export_keras_model(model_filename)

Expected Behavior

It should export a keras model in normally.

Setup Details

Include the details about the versions of:

  • OS type and version: Windows 10
  • Python: Python3.6
  • autokeras: 0.3.5
  • scikit-learn: 0.20.1
  • numpy: 1.15.4
  • keras: 2.2.2
  • scipy: 1.1.0
  • tensorflow: 1.10.0
  • pytorch: 1.0.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lhidekicommented, Dec 30, 2018

If exporting as PyTorch model, I could do that like this.

import torch

model_filename = 'models/text_model.h5'

model = classifier.cnn.best_model.produce_model()
torch.save(model, model_filename)
0reactions
yfq512commented, Jan 3, 2020

@lhideki I have the same question when I use StructuredDataClassifier,Do you solve it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

keras model weights can't be loaded successfully
My current env is ubuntu, python=3.7.9, tf=2.3.0, keras=2.4.0 Code is pasted below, TextClassifier is a customized keras model.
Read more >
Basic text classification | TensorFlow Core
One way to do so is to use the tf.keras.callbacks.EarlyStopping callback. Export the model. In the code above, you applied the TextVectorization layer ......
Read more >
Text classification from scratch - Keras
Build a model ... We choose a simple 1D convnet starting with an Embedding layer. from tensorflow.keras import layers # A integer ...
Read more >
Practical Text Classification With Python and Keras
In this case, you'll use the baseline model to compare it to the more advanced methods involving (deep) neural networks, the meat and...
Read more >
MATLAB importKerasNetwork - MathWorks
net = importKerasNetwork( modelfile , Name,Value ) imports a pretrained TensorFlow-Keras network and its weights with additional options specified by one or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found