TF tells me to use Keras optimizer, tells me the opposite when I change it? I have this piece of code:

model = Sequential()
model.add(Dense(32, input_dim=9, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='relu'))  

learning_rate = 0.001
(1) optimizer = tf.train.AdamOptimizer(learning_rate)
(2) optimizer = tf.keras.optimizers.Adam(lr=learning_rate, decay=1e-6)

model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['mse'])


For the optimizer, at first I had line (1). It worked, but I got the following warning when making ModelCheckpoints:


  WARNING:tensorflow:TensorFlow optimizers do not make it possible to access optimizer attributes or optimizer state after instantiation. As a result, we cannot save the optimizer as part of the model save file.You will have to compile your model again after loading it. Prefer using a Keras optimizer instead (see keras.io/optimizers).


Apparently, you have to use the Keras optimizer if you want to use this. Ok, no problem, so I commented (1) out, and inserted (2).

Now it doesn't even want to compile anymore, and gives me this error instead:


  ValueError: optimizer must be an instance of tf.train.Optimizer, not a class 'tensorflow.python.keras.optimizers.Adam'


This sounds a bit contradictory to me? What am I doing wrong?
The error is telling you to use the actual keras optimizer, not the tensorflow.keras class. Import Adam directly from keras library and it runs ok:

from keras.models import Sequential 
from keras.layers import Dense, Dropout 
from keras.optimizers import Adam 

model = Sequential() 
model.add(Dense(32, input_dim=9, activation='relu')) 
model.add(Dense(128, activation='relu')) 
model.add(Dropout(0.2)) 
model.add(Dense(1, activation='relu'))  

learning_rate = 0.001 
optimizer = Adam(lr=learning_rate, decay=1e-6)

model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['mse'])