Autokeras: Following the tutorial for text classification returns an error in Colab

Created on 3 Apr 2020  路  4Comments  路  Source: keras-team/autokeras

Bug Description

Simply copy-pasting the code from the example usage for TextClassification gives the following error in Colab:

`/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(args, *kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise

AttributeError: in user code:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:505 train_function  *
    outputs = self.distribute_strategy.run(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run  **
    return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
    return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
    return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:477 train_step  **
    self.compiled_metrics.update_state(y, y_pred, sample_weight)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:386 update_state
    self._build(y_pred, y_true)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:317 _build
    self._metrics, y_true, y_pred)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1118 map_structure_up_to
    **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1214 map_structure_with_tuple_paths_up_to
    *flat_value_lists)]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1213 <listcomp>
    results = [func(*args, **kwargs) for args in zip(flat_path_list,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1116 <lambda>
    lambda _, *values: func(*values),  # Discards the path arg.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:416 _get_metric_objects
    return [self._get_metric_object(m, y_t, y_p) for m in metrics]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:416 <listcomp>
    return [self._get_metric_object(m, y_t, y_p) for m in metrics]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:437 _get_metric_object
    y_t_rank = len(y_t.shape.as_list())

AttributeError: 'tuple' object has no attribute 'shape'

`

Bug Reproduction

Code for reproducing the bug:

`
import numpy as np
from tensorflow.keras.datasets import imdb
index_offset = 3 # word index offset
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=1000,
index_from=index_offset)
y_train = y_train.reshape(-1, 1)
y_test = y_test.reshape(-1, 1)
word_to_id = imdb.get_word_index()
word_to_id = {k: (v + index_offset) for k, v in word_to_id.items()}
word_to_id[""] = 0
word_to_id[""] = 1
word_to_id[""] = 2
id_to_word = {value: key for key, value in word_to_id.items()}
x_train = list(map(lambda sentence: ' '.join(
id_to_word[i] for i in sentence), x_train))
x_test = list(map(lambda sentence: ' '.join(
id_to_word[i] for i in sentence), x_test))
x_train = np.array(x_train, dtype=np.str)
x_test = np.array(x_test, dtype=np.str)
print(x_train.shape) # (25000,)
print(y_train.shape) # (25000, 1)
print(x_train[0][:50]) # this film was just brilliant casting

import autokeras as ak
clf = ak.TextClassifier(max_trials=10) # It tries 10 different models.
clf.fit(x_train, y_train)
predicted_y = clf.predict(x_test)
print(clf.evaluate(x_test, y_test))
`

Expected Behavior

Setup Details

Include the details about the versions of:

  • OS type and version:
  • Python: 3.6.9
  • autokeras: 1.0.2
  • keras-tuner: 1.0.1
  • scikit-learn:
  • numpy: 1.18.2
  • pandas: 1.0.3
  • tensorflow: 2.2.0-rc2

Additional context

bug report

Most helpful comment

I'm also having this issue and seems a duplicate of #1095.

It works with tensorflow 2.1.0 but I need to use a newer version to be able to export the model.

All 4 comments

I'm also having this issue and seems a duplicate of #1095.

It works with tensorflow 2.1.0 but I need to use a newer version to be able to export the model.

I am also facing the same issue on the titanic survival problem I got from the following link. https://autokeras.com/examples/titanic/

I have also done some tweaking.

import autokeras as ak

Initialize the classifier.

clf = ak.StructuredDataClassifier(max_trials=30)

x is the path to the csv file. y is the column name of the column to predict.

clf.fit(x=train, y='survived') # @ instead of using name of the column, I have used the its original values

clf.fit(x=train, y=train[['survived']])

Evaluate the accuracy of the found model.

print('Accuracy: {accuracy}'.format(accuracy=clf.evaluate(x=eval, y=eval[['survived']])))

Change the imports to

from keras.optimizers import Adam
from keras.callbacks import EarlyStopping
from keras.layers import Dense
from keras.layers import Dropout
from keras import models

This is fixed in 1.0.3 release. You can also export the model with 1.0.3. Thanks.

Was this page helpful?
0 / 5 - 0 ratings