Implementing Neural Networks

In the implementing neural networks lesson, my code keeps saying ValueError: (‘Could not interpret optimizer identifier:’, <keras.optimizers.Adam object at 0x7fed1ddee5c0>)[Implementing neural networks]

My code looks like this:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import Normalizer
from sklearn.compose import ColumnTransformer
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import InputLayer
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from keras.optimizers import Adam
dataset = pd.read_csv(‘life_expectancy.csv’)



dataset = dataset.drop([‘Country’], axis = 1)

labels = dataset.iloc[:, -1]

features = dataset.iloc[:, 0:-1]

features = pd.get_dummies(features)

features_train, features_test, labels_train, labels_test = train_test_split(features, labels, test_size = 0.20, random_state=23)

numerical_features = features.select_dtypes(include=[‘float64’, ‘int64’])

numerical_columns = numerical_features.columns

ct = ColumnTransformer([(“only numeric”, StandardScaler(), numerical_columns)], remainder=‘passthrough’)

features_train_scaled = ct.fit_transform(features_train)

features_test_scaled = ct.transform(features_test)

my_model = Sequential()

input = InputLayer(input_shape = (features.shape[1], ))


my_model.add(Dense(64, activation = “relu”))



opt = Adam(learning_rate = 0.1)

my_model.compile(loss = ‘mse’, metrics = [‘mae’], optimizer = opt) <----- I think this is the line of code that is breaking my entire project, but I can’t figure out what is wrong with it


I haven’t taken this course, but based on the error message, there is something wrong with the optimizer identifier (I know, very obvious). When I looked at the line you indicated as the potential problem line, I saw optimizer = opt. Your opt variable is an instance of Adam, which was imported two times:

The second import statement (from keras.optimizers import Adam) overrides the first, since both cannot have the same name.

In addition to the fact that these are two different imports that are importing as the same variable (which can cause a lot of bugs), keras and tensorflow.keras are two seperate libraries, which are incompatible with each other.

On the line where you write mymodel.compile, you are mixing the two different libraries— mymodel is using a tensorflow.keras import, while opt is using a keras.optimizers import. This is what is causing the error.

I assume that you only meant to import one of the two Adam optimizers that you are importing. I would suggest removing the keras.optimizers import statement and keeping the tensorflow.keras.optimizers one, as the rest of your code uses tensorflow.keras, not keras.

I hope this helps!

Thank you so much I was just following the hints under each step. Yeah that was what was messing up my code

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.