ktrain/lroptimize/lrfinder.py
def find(self, train_data, steps_per_epoch, use_gen=False,
             start_lr=1e-7, lr_mult=1.01, max_epochs=None, 
             batch_size=U.DEFAULT_BS, workers=1, use_multiprocessing=False, verbose=1):
        """
        Track loss as learning rate is increased.
        NOTE: batch_size is ignored when train_data is instance of Iterator.
        """

        # check arguments and initialize
        if train_data is None:
            raise ValueError('train_data is required')
        #U.data_arg_check(train_data=train_data, train_required=True)
        self.lrs = []
        self.losses = []

         # compute steps_per_epoch
        #num_samples = U.nsamples_from_data(train_data)
        #if U.is_iter(train_data):
            #use_gen = True
            #steps_per_epoch = num_samples // train_data.batch_size
        #else:
            #use_gen = False
            #steps_per_epoch = np.ceil(num_samples/batch_size)

        # max_epochs and lr_mult are None, set max_epochs
        # using sample size of 1500 batches
        if max_epochs is None and lr_mult is None:
            max_epochs = int(np.ceil(1500./steps_per_epoch))

        if max_epochs:
            epochs = max_epochs
            num_batches = epochs * steps_per_epoch
            end_lr = 10 if start_lr < 10 else start_lr * 10
            self.lr_mult = (end_lr / start_lr) ** (1 / num_batches)
        else:
            epochs = 1024
            self.lr_mult = lr_mult

        # Save weights into a file
        new_file, self._weightfile = tempfile.mkstemp()
        self.model.save_weights(self._weightfile)

        # Remember the original learning rate
        original_lr = K.get_value(self.model.optimizer.lr)

        # Set the initial learning rate
        K.set_value(self.model.optimizer.lr, start_lr)

        callback = LambdaCallback(on_batch_end=lambda batch, logs: self.on_batch_end(batch, logs))


        if use_gen:
            if version.parse(tf.__version__) < version.parse('2.0'):
                fit_fn = self.model.fit_generator
            else:
                fit_fn = self.model.fit
            
            fit_fn(train_data, steps_per_epoch=steps_per_epoch, 
                   epochs=epochs, 
                   workers=workers, use_multiprocessing=use_multiprocessing,
                   verbose=verbose,
                   callbacks=[callback])
        else:
            self.model.fit(train_data[0], train_data[1],
                            batch_size=batch_size, epochs=epochs, verbose=verbose,
                            callbacks=[callback])


        # Restore the weights to the state before model fitting
        self.model.load_weights(self._weightfile)
        self._weightfile=None

        # Restore the original learning rate
        K.set_value(self.model.optimizer.lr, original_lr)


        return

        Track loss as learning rate is increased.
        NOTE: batch_size is ignored when train_data is instance of Iterator.
        
Track loss as learning rate is increased.
NOTE: batch_size is ignored when train_data is instance of Iterator.
Track loss as learning rate is increased . NOTE : batch_size is ignored when train_data is instance of Iterator .
def find(self, train_data, steps_per_epoch, use_gen=False,
             start_lr=1e-7, lr_mult=1.01, max_epochs=None, 
             batch_size=U.DEFAULT_BS, workers=1, use_multiprocessing=False, verbose=1):
        if train_data is None:
            raise ValueError('train_data is required')
        self.lrs = []
        self.losses = []
        if max_epochs is None and lr_mult is None:
            max_epochs = int(np.ceil(1500./steps_per_epoch))
        if max_epochs:
            epochs = max_epochs
            num_batches = epochs * steps_per_epoch
            end_lr = 10 if start_lr < 10 else start_lr * 10
            self.lr_mult = (end_lr / start_lr) ** (1 / num_batches)
        else:
            epochs = 1024
            self.lr_mult = lr_mult
        new_file, self._weightfile = tempfile.mkstemp()
        self.model.save_weights(self._weightfile)
        original_lr = K.get_value(self.model.optimizer.lr)
        K.set_value(self.model.optimizer.lr, start_lr)
        callback = LambdaCallback(on_batch_end=lambda batch, logs: self.on_batch_end(batch, logs))
        if use_gen:
            if version.parse(tf.__version__) < version.parse('2.0'):
                fit_fn = self.model.fit_generator
            else:
                fit_fn = self.model.fit
            fit_fn(train_data, steps_per_epoch=steps_per_epoch, 
                   epochs=epochs, 
                   workers=workers, use_multiprocessing=use_multiprocessing,
                   verbose=verbose,
                   callbacks=[callback])
        else:
            self.model.fit(train_data[0], train_data[1],
                            batch_size=batch_size, epochs=epochs, verbose=verbose,
                            callbacks=[callback])
        self.model.load_weights(self._weightfile)
        self._weightfile=None
        K.set_value(self.model.optimizer.lr, original_lr)
        return
def find ( self , train_data , steps_per_epoch , use_gen = False , start_lr = 1e-7 , lr_mult = 1.01 , max_epochs = None , batch_size = U . DEFAULT_BS , workers = 1 , use_multiprocessing = False , verbose = 1 ) : if train_data is None : raise ValueError ( 'train_data is required' ) self . lrs = [ ] self . losses = [ ] if max_epochs is None and lr_mult is None : max_epochs = int ( np . ceil ( 1500. / steps_per_epoch ) ) if max_epochs : epochs = max_epochs num_batches = epochs * steps_per_epoch end_lr = 10 if start_lr < 10 else start_lr * 10 self . lr_mult = ( end_lr / start_lr ) ** ( 1 / num_batches ) else : epochs = 1024 self . lr_mult = lr_mult new_file , self . _weightfile = tempfile . mkstemp ( ) self . model . save_weights ( self . _weightfile ) original_lr = K . get_value ( self . model . optimizer . lr ) K . set_value ( self . model . optimizer . lr , start_lr ) callback = LambdaCallback ( on_batch_end = lambda batch , logs : self . on_batch_end ( batch , logs ) ) if use_gen : if version . parse ( tf . __version__ ) < version . parse ( '2.0' ) : fit_fn = self . model . fit_generator else : fit_fn = self . model . fit fit_fn ( train_data , steps_per_epoch = steps_per_epoch , epochs = epochs , workers = workers , use_multiprocessing = use_multiprocessing , verbose = verbose , callbacks = [ callback ] ) else : self . model . fit ( train_data [ 0 ] , train_data [ 1 ] , batch_size = batch_size , epochs = epochs , verbose = verbose , callbacks = [ callback ] ) self . model . load_weights ( self . _weightfile ) self . _weightfile = None K . set_value ( self . model . optimizer . lr , original_lr ) return
Track loss as learning rate is increased.
Track loss as learning rate is increased .
"""
        Track loss as learning rate is increased.
        NOTE: batch_size is ignored when train_data is instance of Iterator.
        """ # check arguments and initialize #U.data_arg_check(train_data=train_data, train_required=True) # compute steps_per_epoch #num_samples = U.nsamples_from_data(train_data) #if U.is_iter(train_data): #use_gen = True #steps_per_epoch = num_samples // train_data.batch_size #else: #use_gen = False #steps_per_epoch = np.ceil(num_samples/batch_size) # max_epochs and lr_mult are None, set max_epochs # using sample size of 1500 batches # Save weights into a file # Remember the original learning rate # Set the initial learning rate # Restore the weights to the state before model fitting # Restore the original learning rate
self
train_data
steps_per_epoch
use_gen
start_lr
lr_mult
max_epochs
batch_size
workers
use_multiprocessing
verbose