我导入了这些包
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.models import Model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.utils import np_utils
from keras import optimizer_v2
from keras.optimizer_v2 import adam
数据,在训练集和测试集之间分割
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# convert class vectors to binary class matrices
y_train = keras.utils.np_utils.to_categorical(y_train, 10)
y_test = keras.utils.np_utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5),activation='relu',input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
这就是我得到错误的地方
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizer_v2.adam(learning_rate=0.01),metrics=['accuracy'])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-64ffa4d65c71> in <module>
----> 1 model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizer_v2.adam(learning_rate=0.01),metrics=['accuracy'])
TypeError: 'module' object is not callable
如何解决这个错误?
keras.optimizer_v2.adam
为模块。替换为keras.optimizer_v2.adam.Adam