简体   繁体   中英

How do I resolve the "TypeError: 'module' object is not callable" issue when trying to use the rmsprop optimizer?

Here is my code for the neural network I'm trying to get up:

from keras import layers
from keras import models
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator

train_dir = 'C:/Users/BaskaranBadr/Documents/Deep Learning/cats_and_dogs_small/train'
validation_dir = 'C:/Users/BaskaranBadr/Documents/Deep Learning/cats_and_dogs_small/validation'

model = models.Sequential()
model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape = (150,150,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation='relu', input_shape = (150,150,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128, (3,3), activation='relu', input_shape = (150,150,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128, (3,3), activation='relu', input_shape = (150,150,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))


model.compile(loss='binarycrossentropy', optimizer=optimizers.rmsprop_v2(lr=0.0001), metrics = ['acc'])

The error I keep getting is this:

Traceback (most recent call last):
File "c:\Users\BaskaranBadr\Documents\Deep Learning\CatDogClassifier.py", line 24, in <module>
    model.compile(loss='binarycrossentropy', optimizer=optimizers.rmsprop_v2(lr=0.0001), metrics = ['acc'])
TypeError: 'module' object is not callable

I don't know if rmsprop_v2 is exist or not, or it is rmsprop of keras.optimizer_v2 , you can check this link of keras .
If you want use RMSprop , you can follow this way:

import tensorflow as tf

optim = tf.keras.optimizers.RMSprop(lr=0.0001)
model.compile(loss='binarycrossentropy', optimizer=optim, metrics = ['acc'])

rmsprop_v2 is just an alias for rmsprop module inside optimizers module (see keras on GitHub ). You shouldn't use this alias. Just

from keras import optimizers

and then

opt = optimizers.RMSprop(learning_rate=0.0001)
model.compile(loss='binarycrossentropy', optimizer=opt, metrics = ['acc'])

To access Keras in Tensorflow 1, use 'import keras'. Most older code and tutorials need their imports rewritten as 'from tensorflow.keras import X' when using Tensorflow 2.

Use:

from tensorflow.keras.optimizers import RMSprop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM