<commit_before>from blocks.extensions import SimpleExtension, TrainingExtension

from progressbar import ProgressBar, Percentage, ETA, Bar

class LogToFile(SimpleExtension):
    def __init__(self, file_name, **kwargs):
        kwargs.setdefault("before_first_epoch", True)
        kwargs.setdefault("on_resumption", True)
        kwargs.setdefault("after_training", True)
        kwargs.setdefault("after_every_epoch", True)
        kwargs.setdefault("on_interrupt", True)

        self.file_name = file_name

        super(LogToFile, self).__init__(**kwargs)

    def do(self, which_callback, *args):
        self.main_loop.log.to_dataframe().to_csv(self.file_name)

class EpochProgress(TrainingExtension):
    def __init__(self, batch_per_epoch, **kwargs):
        super(EpochProgress, self).__init__(**kwargs)
        self.batch_per_epoch = batch_per_epoch

    def before_epoch(self):
        widgets = [Percentage(), ' ', Bar(),
                   ' ', ETA(), ' ']
        self.progress = ProgressBar(widgets=widgets, maxval=self.batch_per_epoch).start()
        self.on_batch = 0

    def before_batch(self, batch):
        self.progress.update(self.on_batch)
        self.on_batch += 1

import shutil
import os
from blocks.dump import save_parameter_values
import datetime

class ExperimentSaver(TrainingExtension):
    def __init__(self, dest_directory, src_directory, **kwargs):
        super(ExperimentSaver, self).__init__(**kwargs)
        self.dest_directory = dest_directory
        self.src_directory = src_directory

        self.params_path = os.path.join(self.dest_directory, 'params')

    def params_path_for_epoch(self, i):
        return os.path.join(self.params_path, str(i))

    def before_training(self):
        if os.path.exists(self.dest_directory):
            time_string = datetime.datetime.now().strftime('%m-%d-%H-%M-%S') 
            move_to = self.dest_directory + time_string + "_backup"
            shutil.move(self.dest_directory, move_to)

        os.mkdir(self.dest_directory)
        src_path = os.path.join(self.dest_directory, 'src')

        os.mkdir(self.params_path)

        def ignore(path, names):
            # TODO actually manage paths correctly
            if path == self.dest_directory or path == './' + self.dest_directory:
                return names
            else:
                return []

        shutil.copytree(self.src_directory, src_path, ignore=ignore)

    def after_epoch(self):
        log = self.main_loop.log
        epoch_done = log.status.epochs_done
        params = self.main_loop.model.get_param_values()
        path = self.params_path_for_epoch(epoch_done)
        save_parameter_values(params, path)<commit_msg>Write logs as well as make folder at start of main loop<commit_after>from blocks.extensions import SimpleExtension, TrainingExtension

from progressbar import ProgressBar, Percentage, ETA, Bar

class LogToFile(SimpleExtension):
    def __init__(self, file_name, **kwargs):
        kwargs.setdefault("before_first_epoch", True)
        kwargs.setdefault("on_resumption", True)
        kwargs.setdefault("after_training", True)
        kwargs.setdefault("after_every_epoch", True)
        kwargs.setdefault("on_interrupt", True)

        self.file_name = file_name

        super(LogToFile, self).__init__(**kwargs)

    def do(self, which_callback, *args):
        self.main_loop.log.to_dataframe().to_csv(self.file_name)

class EpochProgress(TrainingExtension):
    def __init__(self, batch_per_epoch, **kwargs):
        super(EpochProgress, self).__init__(**kwargs)
        self.batch_per_epoch = batch_per_epoch

    def before_epoch(self):
        widgets = [Percentage(), ' ', Bar(),
                   ' ', ETA(), ' ']
        self.progress = ProgressBar(widgets=widgets, maxval=self.batch_per_epoch).start()
        self.on_batch = 0

    def before_batch(self, batch):
        self.progress.update(self.on_batch)
        self.on_batch += 1

import shutil
import os
from blocks.dump import save_parameter_values
import datetime

class ExperimentSaver(TrainingExtension):
    def __init__(self, dest_directory, src_directory, **kwargs):
        super(ExperimentSaver, self).__init__(**kwargs)
        self.dest_directory = dest_directory
        self.src_directory = src_directory

        self.params_path = os.path.join(self.dest_directory, 'params')
        self.log_path = os.path.join(self.dest_directory, 'log.csv')

        self.write_src()

    def params_path_for_epoch(self, i):
        return os.path.join(self.params_path, str(i))

    def write_src(self):
        if os.path.exists(self.dest_directory):
            time_string = datetime.datetime.now().strftime('%m-%d-%H-%M-%S') 
            move_to = self.dest_directory + time_string + "_backup"
            shutil.move(self.dest_directory, move_to)

        os.mkdir(self.dest_directory)
        src_path = os.path.join(self.dest_directory, 'src')

        os.mkdir(self.params_path)

        def ignore(path, names):
            # TODO actually manage paths correctly
            if path == self.dest_directory or path == './' + self.dest_directory:
                return names
            else:
                return []

        shutil.copytree(self.src_directory, src_path, ignore=ignore)

    def after_epoch(self):
        log = self.main_loop.log
        epoch_done = log.status.epochs_done
        params = self.main_loop.model.get_param_values()
        path = self.params_path_for_epoch(epoch_done)
        save_parameter_values(params, path)

        log.to_dataframe().to_csv(self.log_path)
{'fraction_non_alphanumeric': 0.1085980972895351, 'fraction_numerical': 0.0007180039490217197, 'mean_word_length': 3.005751258087707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1263555', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1660, 'n_words': 307}