<commit_before>from __future__ import absolute_import

import numpy as np

from ._base import StoppingCriteriaBase


class MinImproveScore(StoppingCriteriaBase):

    def __init__(self, k, threshold):
        self.k = k
        self.threshold = threshold
        self.last_epoch = None
        self.counter = 0

    def stop(self, net):
        latest_epoch = net.cur_epoch

        if self.last_epoch == latest_epoch:
            # Already checked for this epoch.
            return False
        elif latest_epoch < 2 * self.k:
            # Not enough data.
            return False
        else:
            self.last_epoch = latest_epoch

            cur_score = np.mean(
                net.losses[latest_epoch-self.k:latest_epoch, 1]
            )

            last_score = np.mean(
                net.losses[latest_epoch-2*self.k:latest_epoch-self.k, 1]
            )

            if cur_score - last_score > self.threshold:
                return False
            else:
                print('Stop at EPOCH {epoch}, '
                      'average score of last {k} epochs '
                      'improved smaller than {threshold}\n'
                      'Latest score : {latest_score} | '
                      'Latest loss  : {latest_loss}'
                      .format(epoch = latest_epoch,
                              k = self.k,
                              threshold = self.threshold,
                              latest_score = net.losses[-1,1],
                              latest_loss = net.losses[-1,2]))
                return True<commit_msg>update min improve sc<commit_after>from __future__ import absolute_import

import numpy as np

from ._base import StoppingCriteriaBase


class MinImproveScore(StoppingCriteriaBase):

    def __init__(self, k, threshold):
        self.k = k
        self.threshold = threshold
        self.last_update = None
        self.counter = 0

    def stop(self, net):
        latest_update = len(net.losses)

        if self.last_update == latest_update:
            # Already checked for this update.
            return False
        elif latest_update < 2 * self.k:
            # Not enough data.
            return False
        else:
            self.last_update = latest_update

            cur_score = np.mean(
                net.losses[latest_update-self.k:latest_update, 1]
            )

            last_score = np.mean(
                net.losses[latest_update-2*self.k:latest_update-self.k, 1]
            )

            if cur_score - last_score > self.threshold:
                return False
            else:
                print('Stop at epoch {epoch}, '
                      'iteration {iteration}\n'
                      'average score of last {k} epochs '
                      'improved smaller than {threshold}\n'
                      'Latest score : {latest_score} | '
                      'Latest loss  : {latest_loss}'
                      .format(epoch=net.cur_epoch,
                              iteration=net.cur_iteration,
                              k=self.k,
                              threshold=self.threshold,
                              latest_score=net.losses[-1,2],
                              latest_loss=net.losses[-1,3]))
                return True
{'fraction_non_alphanumeric': 0.07802937576499389, 'fraction_numerical': 0.0055079559363525096, 'mean_word_length': 1.3053596614950636, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4729961', 'n_tokens_mistral': 880, 'n_tokens_neox': 845, 'n_words': 200}