Keras拟合模型错误:“目前不支持session_kwargs中的某些键”

时间:2018-08-11 10:09:42

标签: python keras mpi4py

我运行以下代码来使用keras训练模型

from keras.datasets import cifar10, cifar100
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.utils.np_utils import to_categorical
from keras.callbacks import EarlyStopping

def getFitness(self, dataset):
            batchSize = 64
            input_shape = (3072, )

            if dataset == 'cifar10':
                    nbClasses = 10
                    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
            elif dataset == 'cifar100':
                    nbClasses = 100
                    (x_train, y_train), (x_test, y_test) = cifar100.load_data()

            x_train = x_train.reshape(50000, 3072)
            x_test = x_test.reshape(10000, 3072)
            x_train = x_train.astype('float32')
            x_test = x_test.astype('float32')
            x_train /= 255
            x_test /= 255
            y_train = to_categorical(y_train, nbClasses)
            y_test = to_categorical(y_test, nbClasses)

            nbLayers = self.network['nbLayers']
            nbNeurons = self.network['nbNeurons']
            activation = self.network['activation']
            optimizer = self.network['optimizer']

            model = Sequential()

            for i in range(nbLayers):
                    if i ==0:
                            model.add(Dense(nbNeurons, activation=activation, input_shape = input_shape))
                    else:
                            model.add(Dense(nbNeurons, activation=activation))
                    model.add(Dropout(0.2))
            model.add(Dense(nbClasses, activation='softmax'))

            model.compile(loss = 'categorical_crossentropy', optimizer=optimizer, metics=['accuracy'])

            model.fit(x_train, y_train, batch_size=batchSize, epochs=1, verbose=0, validation_data=(x_test, y_test), callbacks=[early_Stopper])

            fitness = model.evaluate(x_test, y_test, verbose=0)

            return fitness[1]

使用Keras时,出现以下错误,提示不支持的键-['metics']。 呼叫的完整回溯是:

File "main.py", line 113, in getFitness
  model.fit(x_train, y_train, batch_size=64, epochs=1, verbose=0, validation_data=(x_test, y_test), callbacks=[early_Stopper])
File "/home/users/mschpc/2017/bhatnags/anaconda2/envs/thesis2/lib/python2.7/site-packages/keras/engine/training.py", line 1008, in fit
  self._make_train_function()
File "/home/users/mschpc/2017/bhatnags/anaconda2/envs/thesis2/lib/python2.7/site-packages/keras/engine/training.py", line 508, in _make_train_function
  **self._function_kwargs)
File "/home/users/mschpc/2017/bhatnags/anaconda2/envs/thesis2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2695, in function
  return Function(inputs, outputs, updates=updates, **kwargs)
File "/home/users/mschpc/2017/bhatnags/anaconda2/envs/thesis2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2542, in __init__
  'time: %s', session_kwargs.keys())
ValueError: ('Some keys in session_kwargs are not supported at this time: %s', ['metics'])

我发现了这篇帖子Error when profiling keras models,该帖子修改了tensorflow库。

因此,我检查了link中的Keras库代码。但是找不到像['metics']这样的东西来修改keras库。接下来,我尝试在重新安装keras之后运行代码,但是即使这样也没有用。

P.S。我正在使用Mpi4py,并且多个处理器正在运行函数getFitness,但是不确定这是否是错误的原因。

有人可以为此提出建议吗?

5 个答案:

答案 0 :(得分:5)

是错字吗? 应该是“指标”而不是“指标”?

答案 1 :(得分:3)

有趣的是,我(几乎)有完全相同的问题,但是对我来说,错误消息的拼写是:

ValueError: ('Some keys in session_kwargs are not supported at this time: %s', dict_keys(['metrix']))

因此,在我的情况下,它是“ metrix”而不是“ metics”。 也许是由于我使用Python 3.6而不是像您一样使用2.7?

答案 2 :(得分:1)

这是您代码中的错字。适合的位置应该是metrics代替metics

答案 3 :(得分:0)

您的回调“ early_Stopper”未在任何地方定义。请确保在某处定义它。例如,它可能如下所示:

early_Stopper = EarlyStopping(monitor='val_loss',patience=5,min_delta=0,mode='auto')

答案 4 :(得分:0)

我遇到了同样的错误。我发现我在编译步骤中使用的是指标而不是指标。更正它为我解决了这个问题。 (在下面的粗体和斜体中)

classifier.compile(optimizer ='adam',loss ='binary_crossentropy', metrics = ['accuracy'])