Khosraw Azizi
Khosraw Azizi

Reputation: 81

"ValueError: Unknown optimizer: momentum" correct name for Momentum Optimizer?

I am trying to train my program using the Momentum optimizer but when I input "momentum" as the optimizer, it gives me this error:

ValueError: Unknown optimizer: momentum

The code I am using is:

import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
import time

start_time = time.time()


data = tf.keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = data.load_data()

class_names = ['T-shirt', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot']

train_images = train_images/255.0

test_images = test_images/255.0

model = keras.Sequential([
                           keras.layers.Flatten(input_shape=(28, 28)),
                           keras.layers.Dense(100, activation="relu"),
                           keras.layers.Dense(10, activation="softmax")
])

model.compile(optimizer="Ftrl", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

model.fit(train_images, train_labels, epochs=5)

test_loss, test_acc = model.evaluate(test_images, test_labels)

print("Test acc is:", test_acc)
print("--- %s seconds ---" % (time.time() - start_time))

I tried typing momentum in different ways but I can't seem to fix the correct name. What is it?

Upvotes: 1

Views: 689

Answers (1)

Geeocode
Geeocode

Reputation: 5797

Tensorflow has no plain "momentum" optimizer: tensorflow.org/api_docs/python/tf/optimizers in TensorFlow. Though Tutorialpoints references to it.

Nevertheless it has MomentumOptimizer() class.

So you should first define a MomentumOptimizer() class instance, then you can pass through as parameter to the compile() method.

Note: lr(learning rate) and m(momentum) parameters need to be defined by you.

momentum = tf.train.MomentumOptimizer(lr, m)
model.compile(optimizer=momentum, loss="sparse_categorical_crossentropy", metrics=["accuracy"])

Upvotes: 2

Related Questions