tune.py
def tune_cnn_with_gridsearch():
        """Grid search to identify best hyperparameters for CNN model."""
        cnn_model_values = []
        n_epoch_list = [100, 200, 300, 400, 500]                                # 5
        batch_size_list = [16, 32, 64, 128, 256]                                # 5
        learning_rate_list = [.0001, .0005, .00001, .00005]                     # 4
        dropout_rate_list = [0.2, 0.5, 0.7]                                     # 3

        try:
            for n_epoch in n_epoch_list:
                for batch_size in batch_size_list:
                    for learning_rate in learning_rate_list:
                        for dropout_rate in dropout_rate_list:
                            for num_filt_1 in [8, 16, 32]:              # CNN ONLY  # 3
                                for num_filt_2 in [10, 20, 30, 40]:     # CNN ONLY  # 4
                                    for num_fc_1 in [10, 20, 30, 40]:   # CNN ONLY  # 4
                                        CNN = TrainModel(CNNModel, n_epoch, batch_size, learning_rate, dropout_rate)
                                        CNN.model.num_filt_1 = num_filt_1
                                        CNN.model.num_filt_2 = num_filt_2
                                        CNN.model.num_fc_1 = num_fc_1
                                        CNN.model.build_model()
                                        CNN.calculate_helpers()
                                        acc = CNN.train_model()
                                        CNN.reset_model()

                                        results = [acc, n_epoch, batch_size, learning_rate, dropout_rate, num_filt_1, num_filt_2, num_fc_1]
                                        cnn_model_values.append(results)
        except:
            pass
        finally:
            best_cnn_run = max(cnn_model_values, key=lambda x: x[0])
            logger.info('Best CNN run: {}'.format(best_cnn_run))
            logger.info('All CNN runs: {}'.format(cnn_model_values))
Grid search to identify best hyperparameters for CNN model.
Grid search to identify best hyperparameters for CNN model.
Grid search to identify best hyperparameters for CNN model .
def tune_cnn_with_gridsearch():
        cnn_model_values = []
        n_epoch_list = [100, 200, 300, 400, 500]                                
        batch_size_list = [16, 32, 64, 128, 256]                                
        learning_rate_list = [.0001, .0005, .00001, .00005]                     
        dropout_rate_list = [0.2, 0.5, 0.7]                                     
        try:
            for n_epoch in n_epoch_list:
                for batch_size in batch_size_list:
                    for learning_rate in learning_rate_list:
                        for dropout_rate in dropout_rate_list:
                            for num_filt_1 in [8, 16, 32]:              # CNN ONLY  
                                for num_filt_2 in [10, 20, 30, 40]:     # CNN ONLY  
                                    for num_fc_1 in [10, 20, 30, 40]:   # CNN ONLY  
                                        CNN = TrainModel(CNNModel, n_epoch, batch_size, learning_rate, dropout_rate)
                                        CNN.model.num_filt_1 = num_filt_1
                                        CNN.model.num_filt_2 = num_filt_2
                                        CNN.model.num_fc_1 = num_fc_1
                                        CNN.model.build_model()
                                        CNN.calculate_helpers()
                                        acc = CNN.train_model()
                                        CNN.reset_model()
                                        results = [acc, n_epoch, batch_size, learning_rate, dropout_rate, num_filt_1, num_filt_2, num_fc_1]
                                        cnn_model_values.append(results)
        except:
            pass
        finally:
            best_cnn_run = max(cnn_model_values, key=lambda x: x[0])
            logger.info('Best CNN run: {}'.format(best_cnn_run))
            logger.info('All CNN runs: {}'.format(cnn_model_values))
def tune_cnn_with_gridsearch ( ) : cnn_model_values = [ ] n_epoch_list = [ 100 , 200 , 300 , 400 , 500 ] batch_size_list = [ 16 , 32 , 64 , 128 , 256 ] learning_rate_list = [ .0001 , .0005 , .00001 , .00005 ] dropout_rate_list = [ 0.2 , 0.5 , 0.7 ] try : for n_epoch in n_epoch_list : for batch_size in batch_size_list : for learning_rate in learning_rate_list : for dropout_rate in dropout_rate_list : for num_filt_1 in [ 8 , 16 , 32 ] : for num_filt_2 in [ 10 , 20 , 30 , 40 ] : for num_fc_1 in [ 10 , 20 , 30 , 40 ] : CNN = TrainModel ( CNNModel , n_epoch , batch_size , learning_rate , dropout_rate ) CNN . model . num_filt_1 = num_filt_1 CNN . model . num_filt_2 = num_filt_2 CNN . model . num_fc_1 = num_fc_1 CNN . model . build_model ( ) CNN . calculate_helpers ( ) acc = CNN . train_model ( ) CNN . reset_model ( ) results = [ acc , n_epoch , batch_size , learning_rate , dropout_rate , num_filt_1 , num_filt_2 , num_fc_1 ] cnn_model_values . append ( results ) except : pass finally : best_cnn_run = max ( cnn_model_values , key = lambda x : x [ 0 ] ) logger . info ( 'Best CNN run: {}' . format ( best_cnn_run ) ) logger . info ( 'All CNN runs: {}' . format ( cnn_model_values ) )
Grid search to identify best hyperparameters for CNN model.
Grid search to identify best hyperparameters for CNN model .
"""Grid search to identify best hyperparameters for CNN model.""" # 5 # 5 # 4 # 3 # CNN ONLY  # 3 # CNN ONLY  # 4 # CNN ONLY  # 4