keras中的回调给出KeyError:“度量”同时预测

xxls0lw8  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(210)

我收到以下错误:

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
nr7wwzry

nr7wwzry1#

回调的参数只有在fit调用中使用的值(在本例中为verbose、epochs和steps)。

out_batch.set_model(model)

然后在callback的方法中使用self.model.metrics访问它。
以下是回调实现及其修复:

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=None):
        self.step += 1
        for k in self.model.metrics:
            if k.name not in self.metric_cache.keys():
                self.metric_cache[k.name] = 0.0
            self.metric_cache[k.name] += logs.get(k.name)
        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()

我得到的输出是:

step: 10/240 ...  - loss: 2.2448 - accuracy: 0.2207
step: 20/240 ...  - loss: 2.0735 - accuracy: 0.3876
step: 30/240 ...  - loss: 1.8155 - accuracy: 0.4899
step: 40/240 ...  - loss: 1.5696 - accuracy: 0.5502
step: 50/240 ...  - loss: 1.3779 - accuracy: 0.6002
step: 60/240 ...  - loss: 1.2252 - accuracy: 0.6412

编辑:

要修复混淆矩阵的错误ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets,您应该更改

confusion_matrix(np.argmax(y_train, axis=1), pred_train)

confusion_matrix(np.argmax(y_train, axis=1), np.argmax(pred_train, axis=1))

因为您需要以与训练标签相同方式转换预测标签

相关问题