<commit_before>import numpy as np
from ..utils import env_paths as paths
from base import Train
import time


class TrainModel(Train):
    def __init__(self, model, model_logger, model_directory, output_freq=1, pickle_f_custom_freq=None,
                 f_custom_eval=None):
        super(TrainModel, self).__init__(model, model_logger, model_directory, pickle_f_custom_freq, f_custom_eval)
        self.output_freq = output_freq

    def train_model(self, f_train, train_args, f_test, test_args, f_validate, validation_args,
                    n_train_batches=600, n_valid_batches=1, n_test_batches=1, n_epochs=100, anneal=None):
        self.write_to_logger("### MODEL PARAMS ###")
        self.write_to_logger(self.model.model_info())
        self.write_to_logger("### TRAINING PARAMS ###")
        self.write_to_logger(
            "Train -> %s: %s" % (";".join(train_args['inputs'].keys()), str(train_args['inputs'].values())))
        self.write_to_logger(
            "Test -> %s: %s" % (";".join(test_args['inputs'].keys()), str(test_args['inputs'].values())))
        if anneal is not None:
            for t in anneal:
                key, freq, rate, min_val = t
                self.write_to_logger(
                    "Anneal %s %0.4f after %i epochs with minimum value %f." % (key, rate, int(freq), min_val))

        self.write_to_logger("### TRAINING MODEL ###")

        if True:  
            if self.custom_eval_func is not None:
                test_auprc = self.custom_eval_func(self.model)
                self.test_auprc_list.append((0,test_auprc))
                print >>self.testeval_csv, '0, {}'.format(test_auprc)
                self.testeval_csv.flush()

        done_looping = False
        epoch = 0
        prev_epoch_elbo = None

        def is_stopping_criterion_met(prev_epoch_elbo, this_epoch_elbo):
            if prev_epoch_elbo is None:
                #print 'First epoch'
                return False
            else:
                #self.model_logger.info('prev epoch elbo = {}, this epoch elbo = {}'.format(prev_epoch_elbo, this_epoch_elbo))
                rel_elbo_change = (prev_epoch_elbo - this_epoch_elbo)/prev_epoch_elbo
                #self.model_logger.info('relative elbo change = {:.4e}'.format(rel_elbo_change))
                return abs(rel_elbo_change) < 1e-8

        print 'Training...'
        while (epoch < n_epochs):
            epoch += 1
            start_time = time.time()
            train_outputs = []
            for i in xrange(n_train_batches):<commit_msg>Adding ELBO change threshold as model hyperparameter<commit_after>import numpy as np
from ..utils import env_paths as paths
from base import Train
import time


class TrainModel(Train):
    def __init__(self, model, model_logger, model_directory, output_freq=1, pickle_f_custom_freq=None,
                 f_custom_eval=None, elbo_change_threshold=1e-6):
        super(TrainModel, self).__init__(model, model_logger, model_directory, pickle_f_custom_freq, f_custom_eval)
        self.output_freq = output_freq

    def train_model(self, f_train, train_args, f_test, test_args, f_validate, validation_args,
                    n_train_batches=600, n_valid_batches=1, n_test_batches=1, n_epochs=100, anneal=None):
        self.write_to_logger("### MODEL PARAMS ###")
        self.write_to_logger(self.model.model_info())
        self.write_to_logger("### TRAINING PARAMS ###")
        self.write_to_logger(
            "Train -> %s: %s" % (";".join(train_args['inputs'].keys()), str(train_args['inputs'].values())))
        self.write_to_logger(
            "Test -> %s: %s" % (";".join(test_args['inputs'].keys()), str(test_args['inputs'].values())))
        if anneal is not None:
            for t in anneal:
                key, freq, rate, min_val = t
                self.write_to_logger(
                    "Anneal %s %0.4f after %i epochs with minimum value %f." % (key, rate, int(freq), min_val))

        self.write_to_logger("### TRAINING MODEL ###")

        if True:  
            if self.custom_eval_func is not None:
                test_auprc = self.custom_eval_func(self.model)
                self.test_auprc_list.append((0,test_auprc))
                print >>self.testeval_csv, '0, {}'.format(test_auprc)
                self.testeval_csv.flush()

        done_looping = False
        epoch = 0
        prev_epoch_elbo = None

        def is_stopping_criterion_met(prev_epoch_elbo, this_epoch_elbo):
            if prev_epoch_elbo is None:
                #print 'First epoch'
                return False
            else:
                #self.model_logger.info('prev epoch elbo = {}, this epoch elbo = {}'.format(prev_epoch_elbo, this_epoch_elbo))
                rel_elbo_change = (prev_epoch_elbo - this_epoch_elbo)/prev_epoch_elbo
                #self.model_logger.info('relative elbo change = {:.4e}'.format(rel_elbo_change))
                return rel_elbo_change < elbo_change_threshold

        print 'Training...'
        while (epoch < n_epochs):
            epoch += 1
            start_time = time.time()
            train_outputs = []
            for i in xrange(n_train_batches):
{'fraction_non_alphanumeric': 0.11415259172976121, 'fraction_numerical': 0.006988934187536401, 'mean_word_length': 2.416445623342175, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '4000089', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1637, 'n_words': 334}