我收到以下错误:
KeyError Traceback (most recent call last)
<ipython-input-254-f01ba8163f7d> in <module>
1 out_batch = NBatchLogger(display=1000)
2 model.fit(X_train, Y_train, epochs=1000, batch_size=250,verbose = 0,
----> 3 callbacks=[out_batch])
1 frames
<ipython-input-247-55bb2505c62e> in on_batch_end(self, batch, logs)
14 def on_batch_end(self, batch, logs={}):
15 self.step += 1
---> 16 for k in self.params['metrics']:
17 if k in logs:
18 self.metric_cache[k] = self.metric_cache.get(k, 0) + logs[k]
KeyError: 'metrics
下面是我的代码:
class PrintProgress(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs):
if epoch % 100 == 0: print('Epoch', epoch)
class NBatchLogger(keras.callbacks.Callback):
"""
A Logger that log average performance per `display` steps.
"""
def __init__(self, display):
self.step = 0
self.display = display
self.metric_cache = {}
def on_batch_end(self, batch, logs={}):
self.step += 1
for k in self.params['metrics']:
if k in logs:
self.metric_cache[k] = self.metric_cache.get(k, 0) + logs[k]
if self.step % self.display == 0:
metrics_log = ''
for (k, v) in self.metric_cache.items():
val = v / self.display
if abs(val) > 1e-3:
metrics_log += ' - %s: %.4f' % (k, val)
else:
metrics_log += ' - %s: %.4e' % (k, val)
print('step: {}/{} ... {}'.format(self.step,
self.params['steps'],
metrics_log))
self.metric_cache.clear()
tf.keras.backend.clear_session(
)
当试图计算混淆矩阵时
confusion_matrix(np.argmax(Y_train, axis = 1), pred_train)
出现以下错误:
ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets
1条答案
按热度按时间nr7wwzry1#
回调的参数只有在fit调用中使用的值(在本例中为verbose、epochs和steps)。
然后在callback的方法中使用
self.model.metrics
访问它。以下是回调实现及其修复:
我得到的输出是:
编辑:
要修复混淆矩阵的错误
ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets
,您应该更改至
因为您需要以与训练标签相同方式转换预测标签