Eager mode optimizers

Viewed 829

Only TF native optimizers are supported in Eager mode I'm getting this error with every optimiser I have tried in the following:

def create_model():
    model = tf.keras.models.Sequential([tf.keras.layers.Dense(512,activation=tf.nn.relu, input_shape=(784,)), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10, activation=tf.nn.softmax) ])

    opt = tf.train.GradientDescentOptimizer
    model.compile(optimizer = opt, 
           loss=tf.keras.losses.sparse_categorical_crossentropy, metrics= ['accuracy']) 

    return model

So my question is, what is a 'TF native optimizer' please? Thanks.

2 Answers

Short answer: Change from opt = tf.train.GradientDescentOptimizer to opt = tf.train.GradientDescentOptimizer(<your desired learning rate>).

Longer answer: In the snippet provided above, you're passing a class (tf.train.GradientDescentOptimizer) instead of an object to model.compile. The error message is thus complaining that the type of the opt argument is incorrect.

Hope that helps.

(A recent commit will hopefully result in a better error message in a future release)

In addition to ash's answer, another possible cause of the "Only TF native optimizers are supported in Eager mode" error is using a tf.keras optimizer rather than a tf.train optimizer.

For example:

# Gives error
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

# Also gives error
model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.RMSprop(), metrics=['accuracy'])

# Correct
model.compile(loss='binary_crossentropy', optimizer=tf.train.RMSPropOptimizer(learning_rate=0.001), metrics=['accuracy'])
Related