Keras KeyError: 'metrics' line ---> 13 callbacks=callbacks while executing model.fit()
Keras KeyError: 'metrics' line ---> 13 callbacks=callbacks while executing model.fit()
当我偶然发现这个问题时,我正在 Coursera 中试用这门课程。每当我尝试 运行 model.fit() 时,它都会显示此错误。
显示错误:
KeyError Traceback (most recent call last)
<ipython-input-83-0ef54ef3afb9> in <module>()
11 validation_steps = len(x_val) // batch_size,
12 epochs=12,
---> 13 callbacks=callbacks
14 )
3 frames
/usr/local/lib/python3.6/dist-packages/livelossplot/generic_keras.py in on_train_begin(self, logs)
29
30 def on_train_begin(self, logs={}):
---> 31 self.liveplot.set_metrics([metric for metric in self.params['metrics'] if not metric.startswith('val_')])
32
33 # slightly convolved due to model.complie(loss=...) stuff
KeyError: 'metrics'
这是我的实际代码:
from tensorflow.keras.layers import Dense, Input, Dropout,Flatten, Conv2D
from tensorflow.keras.layers import BatchNormalization, Activation, MaxPooling2D
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.callbacks import ModelCheckpoint
正在初始化 CNN
model = Sequential()
第一次卷积
model.add(Conv2D(32,(5,5), padding='same', input_shape=(64, 128, 1)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
第二个卷积层
model.add(Conv2D(64, (5,5), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
扁平化
model.add(Flatten())
全连接层
model.add(Dense(1024))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.4))
model.add(Dense(4, activation='softmax'))
学习率调度和编译模型
initial_learning_rate=0.005
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate = initial_learning_rate,
decay_steps=5,
decay_rate=0.96,
staircase=True
)
optimizer = Adam(learning_rate=lr_schedule)
model.compile(loss='categorical_crossentropy', optimizer=optimizer , metrics=["accuracy"])
model.summary()
训练模型
checkpoint = ModelCheckpoint('model_weight.h5', monitor='val_loss',
save_weights_only=True, mode='min', verbose=0)
callbacks=[PlotLossesCallback(), checkpoint]
batch_size=32
history = model.fit(
datagen_train.flow(x_train, y_train, batch_size=batch_size, shuffle=True),
steps_per_epoch = len(x_train) // batch_size,
validation_data = datagen_val.flow(x_val, y_val, batch_size=batch_size, shuffle=True),
validation_steps = len(x_val) // batch_size,
epochs=12,
callbacks=callbacks
)
我该如何解决这个问题?
livelossplot.tf_keras
在 Tensorflow 2.1+ 版本中不起作用,使用 pip install tensorflow==2.1
将您的 TensorFlow 版本从 2.2 降级到 Tensorflow 2.1,它将起作用并绘制您的模型训练图。
您必须更新 livelossplot
才能使用 tensorflow 2.x 版本。最新 API 工作有重大变化。而不是使用 tf_keras
使用 PlotLossesKeras
.
from livelossplot import PlotLossesKeras
model.fit(X_train, Y_train,
epochs=10,
validation_data=(X_test, Y_test),
callbacks=[PlotLossesKeras()],
verbose=0)
尝试更改您的导入语句
from livelossplot.tf_keras import PlotLossesCallback
至
from livelossplot.inputs.tf_keras import PlotLossesCallback
当我偶然发现这个问题时,我正在 Coursera 中试用这门课程。每当我尝试 运行 model.fit() 时,它都会显示此错误。
显示错误:
KeyError Traceback (most recent call last)
<ipython-input-83-0ef54ef3afb9> in <module>()
11 validation_steps = len(x_val) // batch_size,
12 epochs=12,
---> 13 callbacks=callbacks
14 )
3 frames
/usr/local/lib/python3.6/dist-packages/livelossplot/generic_keras.py in on_train_begin(self, logs)
29
30 def on_train_begin(self, logs={}):
---> 31 self.liveplot.set_metrics([metric for metric in self.params['metrics'] if not metric.startswith('val_')])
32
33 # slightly convolved due to model.complie(loss=...) stuff
KeyError: 'metrics'
这是我的实际代码:
from tensorflow.keras.layers import Dense, Input, Dropout,Flatten, Conv2D
from tensorflow.keras.layers import BatchNormalization, Activation, MaxPooling2D
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.callbacks import ModelCheckpoint
正在初始化 CNN
model = Sequential()
第一次卷积
model.add(Conv2D(32,(5,5), padding='same', input_shape=(64, 128, 1)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
第二个卷积层
model.add(Conv2D(64, (5,5), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
扁平化
model.add(Flatten())
全连接层
model.add(Dense(1024))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.4))
model.add(Dense(4, activation='softmax'))
学习率调度和编译模型
initial_learning_rate=0.005
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate = initial_learning_rate,
decay_steps=5,
decay_rate=0.96,
staircase=True
)
optimizer = Adam(learning_rate=lr_schedule)
model.compile(loss='categorical_crossentropy', optimizer=optimizer , metrics=["accuracy"])
model.summary()
训练模型
checkpoint = ModelCheckpoint('model_weight.h5', monitor='val_loss',
save_weights_only=True, mode='min', verbose=0)
callbacks=[PlotLossesCallback(), checkpoint]
batch_size=32
history = model.fit(
datagen_train.flow(x_train, y_train, batch_size=batch_size, shuffle=True),
steps_per_epoch = len(x_train) // batch_size,
validation_data = datagen_val.flow(x_val, y_val, batch_size=batch_size, shuffle=True),
validation_steps = len(x_val) // batch_size,
epochs=12,
callbacks=callbacks
)
我该如何解决这个问题?
livelossplot.tf_keras
在 Tensorflow 2.1+ 版本中不起作用,使用 pip install tensorflow==2.1
将您的 TensorFlow 版本从 2.2 降级到 Tensorflow 2.1,它将起作用并绘制您的模型训练图。
您必须更新 livelossplot
才能使用 tensorflow 2.x 版本。最新 API 工作有重大变化。而不是使用 tf_keras
使用 PlotLossesKeras
.
from livelossplot import PlotLossesKeras
model.fit(X_train, Y_train,
epochs=10,
validation_data=(X_test, Y_test),
callbacks=[PlotLossesKeras()],
verbose=0)
尝试更改您的导入语句
from livelossplot.tf_keras import PlotLossesCallback
至
from livelossplot.inputs.tf_keras import PlotLossesCallback