Home > Enterprise >  The Adam optimizer is showing error in Keras Tensorflow
The Adam optimizer is showing error in Keras Tensorflow

Time:09-27

I was training a neural network to recognize angry and happy emotion. The code:

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras.optimizer_v1 import Adam
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Activation, Dense, MaxPool2D, Conv2D, Flatten
from tensorflow.python.keras.metrics import categorical_crossentropy
from sklearn.metrics import confusion_matrix
import itertools
import os
import shutil
import glob
import random
import matplotlib.pyplot as plt
import warnings

trainpath = 'angry-vs-happy/train'
testpath = 'angry-vs-happy/test'
validpath = 'angry-vs-happy/valid'

train_batches = tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=trainpath, target_size=(224,224), classes =['angry', 'happy'], batch_size=10)

test_batches = tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=testpath, target_size=(224,224), classes =['angry', 'happy'], batch_size=10)

valid_batches = tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=validpath, target_size=(224,224), classes =['angry', 'happy'], batch_size=10, shuffle=False)

assert train_batches.n == 1000
assert valid_batches.n == 200
assert test_batches.n == 100
assert train_batches.num_classes == valid_batches.num_classes == test_batches.num_classes == 2
imgs, labels = next(train_batches)


model = Sequential([
    Conv2D(filters=32, kernel_size=(3,3),activation = 'relu', padding='same', input_shape = (224,224,3)),
    MaxPool2D(pool_size=(2,2), strides=2),
    Conv2D(filters=64, kernel_size=(3,3),activation='relu', padding='same'),
    MaxPool2D(pool_size=(2,2), strides=2),
    Flatten(),
    Dense(units=2, activation='softmax'),
])

model.summary()

model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(x=train_batches, validation_data=valid_batches, epochs=10, verbose=2)

But it shows an error:

ValueError: ('`tf.compat.v1.keras` Optimizer (', <tensorflow.python.keras.optimizer_v1.Adam object at 0x0000022339FBEDD0>, ') 
is not supported when eager execution is enabled. Use a `tf.keras` Optimizer 
instead, or disable eager execution.')

But when I rewrite the model.compile code as :

model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.0001), loss='categorical_crossentropy', 
metrics=['accuracy'])

it shows that :

ValueError: Could not interpret optimizer identifier: <keras.optimizers.optimizer_v2.adam.Adam object at 0x0000028E41B7EE60>

CodePudding user response:

Use tf.keras.optimizers, and remove .python. from the imports. I don't see anything about tensorflow.python.keras in the documentation, so I would not use it

from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, MaxPool2D, Conv2D, Flatten
from tensorflow.keras.metrics import categorical_crossentropy
  • Related