Error of `tf.data.Dataset` only supports Python-style iteration in eager mode or within tf.function

38 Views Asked by At

I am trying to train a neural network classifier for adversarial robustness tool box (ART)

with following code

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from keras.optimizers import RMSprop
#from scikeras.wrappers import KerasClassifier
from art.estimators.classification import KerasClassifier
# define the input shape
input_shape = (122,)

# define the model architecture
model = Sequential()
model.add(Dense(64, input_shape=input_shape, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(2, activation='sigmoid'))

learning_rate = 0.01
optimizer = RMSprop(learning_rate=learning_rate)
# compile the model
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])


# train the model
model.fit(X_train, y_train, epochs=10)
classifier = KerasClassifier(model=model, clip_values=(0, 1), use_logits=False)

I am getting the following error (full stack)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[157], line 25
     21 model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
     24 # train the model
---> 25 model.fit(X_train, y_train, epochs=10)
     26 classifier = KerasClassifier(model=model, clip_values=(0, 1), use_logits=False)

File /opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:123, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    120     filtered_tb = _process_traceback_frames(e.__traceback__)
    121     # To get the full stack trace, call:
    122     # `keras.config.disable_traceback_filtering()`
--> 123     raise e.with_traceback(filtered_tb) from None
    124 finally:
    125     del filtered_tb

File /opt/conda/lib/python3.10/site-packages/tensorflow/python/data/ops/dataset_ops.py:502, in DatasetV2.__iter__(self)
    500     return iterator_ops.OwnedIterator(self)
    501 else:
--> 502   raise RuntimeError("`tf.data.Dataset` only supports Python-style "
    503                      "iteration in eager mode or within tf.function.")

RuntimeError: `tf.data.Dataset` only supports Python-style iteration in eager mode or within tf.function.

The code for reading the dataset is

data = pd.read_csv('/kaggle/input/nslkdd/KDDTrain+.txt')

Please help

The model is not getting trained

0

There are 0 best solutions below