def create_default_experiment(final_layer, dataset, n_epochs=100, **overwrite_args): # make special case for this, since we access dataset.X here, # which might not exist if 'splitter' not in overwrite_args: n_trials = len(dataset.X) splitter = FixedTrialSplitter(n_train_trials=n_trials // 2, valid_set_fraction=0.2) else: splitter = overwrite_args['splitter'] monitors = [MisclassMonitor(), LossMonitor(), RuntimeMonitor()] stop_criterion = MaxEpochs(n_epochs) exp_args = dict( splitter=splitter, preprocessor=None, iterator=BalancedBatchIterator(batch_size=45), loss_expression=lasagne.objectives.categorical_crossentropy, updates_expression=lasagne.updates.adam, updates_modifier=None, monitors=monitors, stop_criterion=stop_criterion, remember_best_chan='valid_misclass', run_after_early_stop=True, batch_modifier=None) exp_args.update(**overwrite_args) exp = Experiment(final_layer, dataset, **exp_args) return exp
def setup_after_stop_training(self): # also remember old monitor chans, will be put back into # monitor chans after experiment finished self.old_monitor_chans = deepcopy(self.monitor_chans) self.remember_extension.reset_to_best_model(self.monitor_chans, self.all_params) loss_to_reach = self.monitor_chans['train_loss'][-1] self.stop_criterion = Or(stop_criteria=[ MaxEpochs(num_epochs=self.remember_extension.best_epoch * 2), ChanBelow(chan_name='valid_loss', target_value=loss_to_reach) ]) log.info("Train loss to reach {:.5f}".format(loss_to_reach))
def run( ex, data_folder, subject_id, n_chans, only_return_exp, ): start_time = time.time() assert (only_return_exp is False) or (n_chans is not None) ex.info['finished'] = False load_sensor_names = None train_filename = 'A{:02d}T.mat'.format(subject_id) test_filename = 'A{:02d}E.mat'.format(subject_id) train_filepath = os.path.join(data_folder, train_filename) test_filepath = os.path.join(data_folder, test_filename) # trial ivan in milliseconds # these are the samples that will be predicted, so for a # network with 2000ms receptive field # 1500 means the first receptive field goes from -500 to 1500 segment_ival = [1500, 4000] train_loader = BCICompetition4Set2A(train_filepath, load_sensor_names=load_sensor_names) test_loader = BCICompetition4Set2A(test_filepath, load_sensor_names=load_sensor_names) # Preprocessing pipeline in [(function, {args:values)] logic cnt_preprocessors = [(resample_cnt, { 'newfs': 250.0 }), (bandpass_cnt, { 'low_cut_hz': 0, 'high_cut_hz': 38, }), (exponential_standardize_cnt, {})] marker_def = { '1- Right Hand': [1], '2 - Left Hand': [2], '3 - Rest': [3], '4 - Feet': [4] } train_signal_proc = SignalProcessor(set_loader=train_loader, segment_ival=segment_ival, cnt_preprocessors=cnt_preprocessors, marker_def=marker_def) train_set = CntSignalMatrix(signal_processor=train_signal_proc, sensor_names='all') test_signal_proc = SignalProcessor(set_loader=test_loader, segment_ival=segment_ival, cnt_preprocessors=cnt_preprocessors, marker_def=marker_def) test_set = CntSignalMatrix(signal_processor=test_signal_proc, sensor_names='all') from braindecode.mywyrm.clean import MaxAbsCleaner train_cleaner = MaxAbsCleaner(segment_ival=[0, 4000], threshold=800, marker_def=marker_def) test_cleaner = MaxAbsCleaner(segment_ival=[0, 4000], threshold=800, marker_def=marker_def) combined_set = CombinedCleanedSet(train_set, test_set, train_cleaner, test_cleaner) if not only_return_exp: combined_set.load() in_chans = train_set.get_topological_view().shape[1] input_time_length = 1000 # implies how many crops are processed in parallel, does _not_ determine receptive field size # receptive field size is determined by model architecture num_filters_time = 25 filter_time_length = 10 num_filters_spat = 25 pool_time_length = 3 pool_time_stride = 3 num_filters_2 = 50 filter_length_2 = 10 num_filters_3 = 100 filter_length_3 = 10 num_filters_4 = 200 filter_length_4 = 10 final_dense_length = 2 n_classes = 4 final_nonlin = softmax first_nonlin = elu first_pool_mode = 'max' first_pool_nonlin = identity later_nonlin = elu later_pool_mode = 'max' later_pool_nonlin = identity drop_in_prob = 0.0 drop_prob = 0.5 batch_norm_alpha = 0.1 double_time_convs = False split_first_layer = True batch_norm = True # ensure reproducibility by resetting lasagne/theano random generator lasagne.random.set_rng(RandomState(34734)) d5net = Deep5Net(in_chans=in_chans, input_time_length=input_time_length, num_filters_time=num_filters_time, filter_time_length=filter_time_length, num_filters_spat=num_filters_spat, pool_time_length=pool_time_length, pool_time_stride=pool_time_stride, num_filters_2=num_filters_2, filter_length_2=filter_length_2, num_filters_3=num_filters_3, filter_length_3=filter_length_3, num_filters_4=num_filters_4, filter_length_4=filter_length_4, final_dense_length=final_dense_length, n_classes=n_classes, final_nonlin=final_nonlin, first_nonlin=first_nonlin, first_pool_mode=first_pool_mode, first_pool_nonlin=first_pool_nonlin, later_nonlin=later_nonlin, later_pool_mode=later_pool_mode, later_pool_nonlin=later_pool_nonlin, drop_in_prob=drop_in_prob, drop_prob=drop_prob, batch_norm_alpha=batch_norm_alpha, double_time_convs=double_time_convs, split_first_layer=split_first_layer, batch_norm=batch_norm) final_layer = d5net.get_layers()[-1] print_layers(final_layer) dataset_splitter = SeveralSetsSplitter(valid_set_fraction=0.2, use_test_as_valid=False) iterator = CntWindowTrialIterator( batch_size=45, input_time_length=input_time_length, n_sample_preds=get_n_sample_preds(final_layer)) monitors = [ LossMonitor(), CntTrialMisclassMonitor(input_time_length=input_time_length), RuntimeMonitor() ] #debug: n_no_decrease_max_epochs = 2 #debug: n_max_epochs = 4 n_no_decrease_max_epochs = 80 n_max_epochs = 800 #100 # real values for paper were 80 and 800 stop_criterion = Or([ NoDecrease('valid_misclass', num_epochs=n_no_decrease_max_epochs), MaxEpochs(num_epochs=n_max_epochs) ]) dataset = combined_set splitter = dataset_splitter loss_expression = categorical_crossentropy updates_expression = adam updates_modifier = MaxNormConstraintWithDefaults({}) remember_best_chan = 'valid_misclass' run_after_early_stop = True exp = Experiment(final_layer, dataset, splitter, None, iterator, loss_expression, updates_expression, updates_modifier, monitors, stop_criterion, remember_best_chan, run_after_early_stop, batch_modifier=None) if only_return_exp: return exp exp.setup() exp.run() end_time = time.time() run_time = end_time - start_time ex.info['finished'] = True ex.info['runtime'] = run_time for key in exp.monitor_chans: ex.info[key] = exp.monitor_chans[key][-1] save_pkl_artifact(ex, exp.monitor_chans, 'monitor_chans.pkl')
def run(ex, data_folder, subject_id, n_chans, train_inds, test_inds, sets_like_fbcsp_paper, clean_train, stop_chan, filt_order, low_cut_hz, loss_expression, network, only_return_exp, run_after_early_stop): start_time = time.time() assert (only_return_exp is False) or (n_chans is not None) ex.info['finished'] = False # trial ival in milliseconds # these are the samples that will be predicted, so for a # network with 2000ms receptive field # 1500 means the first receptive field goes from -500 to 1500 train_segment_ival = [1500, 4000] test_segment_ival = [0, 4000] if sets_like_fbcsp_paper: if subject_id in [4, 5, 6, 7, 8, 9]: train_inds = [3] elif subject_id == 1: train_inds = [1, 3] else: assert subject_id in [2, 3] train_inds = [1, 2, 3] train_loader = MultipleBCICompetition4Set2B(subject_id, session_ids=train_inds, data_folder=data_folder) test_loader = MultipleBCICompetition4Set2B(subject_id, session_ids=test_inds, data_folder=data_folder) # Preprocessing pipeline in [(function, {args:values)] logic cnt_preprocessors = [(resample_cnt, { 'newfs': 250.0 }), (bandpass_cnt, { 'low_cut_hz': low_cut_hz, 'high_cut_hz': 38, 'filt_order': filt_order, }), (exponential_standardize_cnt, {})] marker_def = {'1- Left Hand': [1], '2 - Right Hand': [2]} train_signal_proc = SignalProcessor(set_loader=train_loader, segment_ival=train_segment_ival, cnt_preprocessors=cnt_preprocessors, marker_def=marker_def) train_set = CntSignalMatrix(signal_processor=train_signal_proc, sensor_names='all') test_signal_proc = SignalProcessor(set_loader=test_loader, segment_ival=test_segment_ival, cnt_preprocessors=cnt_preprocessors, marker_def=marker_def) test_set = CntSignalMatrix(signal_processor=test_signal_proc, sensor_names='all') if clean_train: train_cleaner = BCICompetitionIV2ABArtefactMaskCleaner( marker_def=marker_def) else: train_cleaner = NoCleaner() test_cleaner = BCICompetitionIV2ABArtefactMaskCleaner( marker_def=marker_def) combined_set = CombinedCleanedSet(train_set, test_set, train_cleaner, test_cleaner) if not only_return_exp: combined_set.load() lasagne.random.set_rng(RandomState(34734)) in_chans = train_set.get_topological_view().shape[1] input_time_length = 1000 # implies how many crops are processed in parallel, does _not_ determine receptive field size # receptive field size is determined by model architecture if network == 'deep': final_layer = create_deep_net(in_chans, input_time_length) else: assert network == 'shallow' final_layer = create_shallow_net(in_chans, input_time_length) dataset_splitter = SeveralSetsSplitter(valid_set_fraction=0.2, use_test_as_valid=False) iterator = CntWindowTrialIterator( batch_size=45, input_time_length=input_time_length, n_sample_preds=get_n_sample_preds(final_layer)) monitors = [ LossMonitor(), CntTrialMisclassMonitor(input_time_length=input_time_length), KappaMonitor(input_time_length=iterator.input_time_length, mode='max'), RuntimeMonitor() ] #debug: n_no_decrease_max_epochs = 2 #debug: n_max_epochs = 4 n_no_decrease_max_epochs = 80 n_max_epochs = 800 #100 # real values for paper were 80 and 800 remember_best_chan = 'valid_' + stop_chan stop_criterion = Or([ NoDecrease(remember_best_chan, num_epochs=n_no_decrease_max_epochs), MaxEpochs(num_epochs=n_max_epochs) ]) dataset = combined_set splitter = dataset_splitter updates_expression = adam updates_modifier = MaxNormConstraintWithDefaults({}) exp = Experiment(final_layer, dataset, splitter, None, iterator, loss_expression, updates_expression, updates_modifier, monitors, stop_criterion, remember_best_chan, run_after_early_stop, batch_modifier=None) if only_return_exp: return exp exp.setup() exp.run() end_time = time.time() run_time = end_time - start_time ex.info['finished'] = True for key in exp.monitor_chans: ex.info[key] = exp.monitor_chans[key][-1] ex.info['runtime'] = run_time save_pkl_artifact(ex, exp.monitor_chans, 'monitor_chans.pkl')
def _run_experiments_with_string(self, experiment_index, train_str): assert experiment_index >= self._get_start_id() assert experiment_index < self._get_stop_id() lasagne.random.set_rng(RandomState(9859295)) # Save train string now, will be overwritten later after # input dimensions determined, save now for debug in # case of crash if not self._dry_run: self._save_train_string(train_str, experiment_index) starttime = time.time() train_dict = self._load_without_layers(train_str) log.info("With params...") if not self._quiet: pprint(train_dict['original_params']) if self._dry_run: # Do not do the loading or training... # Only go until here to show the train params return if self._batch_test: # TODO: put into function # load layers, load data with dimensions of the layer # create experiment with max epochs 2, run from braindecode.datasets.random import RandomSet train_str = train_str.replace('in_cols', '1') train_str = train_str.replace('in_sensors', '32') train_dict = yaml_parse.load(train_str) layers = load_layers_from_dict(train_dict) final_layer = layers[-1] n_chans = layers[0].shape[1] n_classes = final_layer.output_shape[1] n_samples = 500000 # set n sample perds in case of cnt model if (np.any([hasattr(l, 'n_stride') for l in layers])): n_sample_preds = get_n_sample_preds(final_layer) log.info("Setting n_sample preds automatically to {:d}".format( n_sample_preds)) for monitor in train_dict['exp_args']['monitors']: if hasattr(monitor, 'n_sample_preds'): monitor.n_sample_preds = n_sample_preds train_dict['exp_args'][ 'iterator'].n_sample_preds = n_sample_preds log.info("Input window length is {:d}".format( get_model_input_window(final_layer))) # make at least batches n_samples = int(n_sample_preds * 1.5 * 200) dataset = RandomSet(topo_shape=[n_samples, n_chans, 1, 1], y_shape=[n_samples, n_classes]) dataset.load() splitter = FixedTrialSplitter(n_train_trials=int(n_samples * 0.8), valid_set_fraction=0.1) train_dict['exp_args']['preprocessor'] = None train_dict['exp_args']['stop_criterion'] = MaxEpochs(1) train_dict['exp_args']['iterator'].batch_size = 1 # TODO: set stop criterion to max epochs =1 # change batch_size in iterator exp = Experiment(final_layer, dataset, splitter, **train_dict['exp_args']) exp.setup() exp.run_until_early_stop() datasets = exp.dataset_provider.get_train_valid_test(exp.dataset) for batch_size in range(32, 200, 5): train_dict['exp_args']['stop_criterion'].num_epochs += 2 log.info("Running with batch size {:d}".format(batch_size)) train_dict['exp_args']['iterator'].batch_size = batch_size exp.run_until_stop(datasets, remember_best=False) return dataset = train_dict['dataset'] dataset.load() iterator = train_dict['exp_args']['iterator'] splitter = train_dict['dataset_splitter'] if dataset.__class__.__name__ == 'EpilepsySet': log.info("Reducing to float16 for epilepsy set...") dataset.seizure_topo = np.float16(dataset.seizure_topo) dataset.non_seizure_topo = np.float16(dataset.non_seizure_topo) else: # todo: remove this? log.info( "Determining dataset dimensions to set possible model params..." ) train_set = splitter.split_into_train_valid_test(dataset)['train'] batch_gen = iterator.get_batches(train_set, shuffle=True) dummy_batch_topo = batch_gen.next()[0] del train_set # not for ultrasound: assert 'in_sensors' in train_str # not for cnt net assert 'in_rows' in train_str # not for resnet: assert 'in_cols' in train_str train_str = train_str.replace('in_sensors', str(dummy_batch_topo.shape[1])) train_str = train_str.replace('in_rows', str(dummy_batch_topo.shape[2])) train_str = train_str.replace('in_cols', str(dummy_batch_topo.shape[3])) self._save_train_string(train_str, experiment_index) # reset rng for actual loading of layers, so you can reproduce it # when you load the file later lasagne.random.set_rng(RandomState(9859295)) train_dict = yaml_parse.load(train_str) layers = load_layers_from_dict(train_dict) final_layer = layers[-1] assert len( np.setdiff1d( layers, lasagne.layers.get_all_layers(final_layer))) == 0, ( "All layers " "should be used, unused {:s}".format( str( np.setdiff1d( layers, lasagne.layers.get_all_layers(final_layer))))) # Set n sample preds in case of cnt model if (np.any([hasattr(l, 'n_stride') for l in layers])): # Can this be moved up and duplication in if clause( batch test, # more above) be removed? n_sample_preds = get_n_sample_preds(final_layer) log.info("Setting n_sample preds automatically to {:d}".format( n_sample_preds)) for monitor in train_dict['exp_args']['monitors']: if hasattr(monitor, 'n_sample_preds'): monitor.n_sample_preds = n_sample_preds train_dict['exp_args']['iterator'].n_sample_preds = n_sample_preds log.info("Input window length is {:d}".format( get_model_input_window(final_layer))) if not self._cross_validation: # for now lets not do that, current models seem fine again. # if (dataset.__class__.__name__ == 'EpilepsySet') and self._pred_loss_hack: # from braindecode.epilepsy.experiment import EpilepsyExperiment # log.info("Creating epilepsy experiment with the pred loss hack") # exp = EpilepsyExperiment(final_layer, dataset, splitter, # **train_dict['exp_args']) # else: exp = Experiment(final_layer, dataset, splitter, **train_dict['exp_args']) exp.setup() exp.run() endtime = time.time() model = exp.final_layer # dummy predictions targets predictions = [0, 3, 1, 2, 3, 4] targets = [3, 4, 1, 2, 3, 4] result_or_results = Result( parameters=train_dict['original_params'], templates={}, training_time=endtime - starttime, monitor_channels=exp.monitor_chans, predictions=predictions, targets=targets) else: # cross validation assert False, ( "cross validation not used in long time, not up to date" " for example targets predictions not added") # default 5 folds for now n_folds = train_dict['num_cv_folds'] exp_cv = ExperimentCrossValidation(final_layer, dataset, exp_args=train_dict['exp_args'], n_folds=n_folds, shuffle=self._shuffle) exp_cv.run() endtime = time.time() result_or_results = [] for i_fold in xrange(n_folds): res = Result(parameters=train_dict['original_params'], templates={}, training_time=endtime - starttime, monitor_channels=exp_cv.all_monitor_chans[i_fold], predictions=[0, 3, 1, 2, 3, 4], targets=[3, 4, 1, 2, 3, 4]) result_or_results.append(res) model = exp_cv.all_layers if not os.path.exists(self._folder_paths[experiment_index]): os.makedirs(self._folder_paths[experiment_index]) result_file_name = self._get_result_save_path(experiment_index) log.info("Saving result to {:s}...".format(result_file_name)) with open(result_file_name, 'w') as resultfile: pickle.dump(result_or_results, resultfile) model_file_name = self._get_model_save_path(experiment_index) param_file_name = model_file_name.replace('.pkl', '.npy') np.save(param_file_name, lasagne.layers.get_all_param_values(model)) # Possibly make kaggle submission file if isinstance(dataset, KaggleGraspLiftSet) and splitter.use_test_as_valid: experiment_save_id = int( self._base_save_paths[experiment_index].split("/")[-1]) create_submission_csv_for_one_subject( self._folder_paths[experiment_index], exp.dataset, iterator, train_dict['exp_args']['preprocessor'], final_layer, experiment_save_id) elif isinstance( dataset, AllSubjectsKaggleGraspLiftSet) and splitter.use_test_as_valid: experiment_save_id = int( self._base_save_paths[experiment_index].split("/")[-1]) create_submission_csv_for_all_subject_model( self._folder_paths[experiment_index], exp.dataset, exp.dataset_provider, iterator, final_layer, experiment_save_id) elif isinstance(splitter, SeveralSetsSplitter): pass # nothing to do in this case # very hacky create predictions targets :) # Not done earlier as there were weird theano crashes if exp.monitors[2].__class__.__name__ == 'CntTrialMisclassMonitor': del dataset del exp add_labels_to_cnt_exp_result( self._base_save_paths[experiment_index])
def run(ex, data_folder, subject_id, n_chans, clean_train, low_cut_hz, train_start_ms,kappa_mode, loss_expression, network, filt_order, only_return_exp,): start_time = time.time() assert (only_return_exp is False) or (n_chans is not None) ex.info['finished'] = False valid_subject_id = subject_ other_subject_ids = range(1,subject_id) + range(subject_id+1, 10) other_sets = [create_dataset( data_folder, other_sid, train_start_ms, low_cut_hz, filt_order, clean_train) for other_sid in other_subject_ids] test_set = create_dataset( data_folder, subject_id, train_start_ms, low_cut_hz, filt_order, clean_train) combined_set = other_sets + [test_set] def merge_train_test(single_combined_set): return concatenate_sets(single_combined_set.train_set, single_combined_set.test_set) if not only_return_exp: for i_set, this_set in enumerate(combined_set): log.info("Loading {:d} of {:d}".format(i_set + 1, len(combined_set))) this_set.load() merged_sets = [merge_train_test(s) for s in combined_set] combined_set = CombinedSet(merged_sets) in_chans = merged_sets[0].get_topological_view().shape[1] else: in_chans = n_chans input_time_length = 1000 # implies how many crops are processed in parallel, does _not_ determine receptive field size # receptive field size is determined by model architecture # ensure reproducibility by resetting lasagne/theano random generator lasagne.random.set_rng(RandomState(34734)) if network == 'deep': final_layer = create_deep_net(in_chans, input_time_length) else: assert network == 'shallow' final_layer = create_shallow_net(in_chans, input_time_length) print_layers(final_layer) dataset_splitter = SeveralSetsSplitter(valid_set_fraction=0.1, use_test_as_valid=False) iterator = CntWindowTrialIterator(batch_size=45,input_time_length=input_time_length, n_sample_preds=get_n_sample_preds(final_layer)) monitors = [LossMonitor(), CntTrialMisclassMonitor(input_time_length=input_time_length), KappaMonitor(input_time_length=iterator.input_time_length, mode=kappa_mode), RuntimeMonitor(),] #debug: n_no_decrease_max_epochs = 2 #debug: n_max_epochs = 4 n_no_decrease_max_epochs = 80 n_max_epochs = 800#100 # real values for paper were 80 and 800 stop_criterion = Or([NoDecrease('valid_misclass', num_epochs=n_no_decrease_max_epochs), MaxEpochs(num_epochs=n_max_epochs)]) dataset = combined_set splitter = dataset_splitter updates_expression = adam updates_modifier = MaxNormConstraintWithDefaults({}) remember_best_chan = 'valid_misclass' run_after_early_stop=True exp = Experiment(final_layer, dataset,splitter,None,iterator, loss_expression,updates_expression, updates_modifier, monitors, stop_criterion, remember_best_chan, run_after_early_stop, batch_modifier=None) if only_return_exp: return exp exp.setup() exp.run() end_time = time.time() run_time = end_time - start_time ex.info['finished'] = True for key in exp.monitor_chans: ex.info[key] = exp.monitor_chans[key][-1] ex.info['runtime'] = run_time save_pkl_artifact(ex, exp.monitor_chans, 'monitor_chans.pkl') save_npy_artifact(ex, lasagne.layers.get_all_param_values(exp.final_layer), 'model_params.npy')
def run( ex, data_folder, subject_id, n_chans, clean_train, low_cut_hz, train_start_ms, kappa_mode, loss_expression, filt_order, only_return_exp, ): start_time = time.time() assert (only_return_exp is False) or (n_chans is not None) ex.info['finished'] = False load_sensor_names = None train_filename = 'A{:02d}T.mat'.format(subject_id) test_filename = 'A{:02d}E.mat'.format(subject_id) train_filepath = os.path.join(data_folder, train_filename) test_filepath = os.path.join(data_folder, test_filename) # trial ivan in milliseconds # these are the samples that will be predicted, so for a # network with 2000ms receptive field # 1500 means the first receptive field goes from -500 to 1500 train_segment_ival = [train_start_ms, 4000] test_segment_ival = [0, 4000] train_loader = BCICompetition4Set2A(train_filepath, load_sensor_names=load_sensor_names) test_loader = BCICompetition4Set2A(test_filepath, load_sensor_names=load_sensor_names) # Preprocessing pipeline in [(function, {args:values)] logic cnt_preprocessors = [(resample_cnt, { 'newfs': 250.0 }), (bandpass_cnt, { 'low_cut_hz': low_cut_hz, 'high_cut_hz': 38, 'filt_order': filt_order, }), (exponential_standardize_cnt, {})] marker_def = { '1- Right Hand': [1], '2 - Left Hand': [2], '3 - Rest': [3], '4 - Feet': [4] } train_signal_proc = SignalProcessor(set_loader=train_loader, segment_ival=train_segment_ival, cnt_preprocessors=cnt_preprocessors, marker_def=marker_def) train_set = CntSignalMatrix(signal_processor=train_signal_proc, sensor_names='all') test_signal_proc = SignalProcessor(set_loader=test_loader, segment_ival=test_segment_ival, cnt_preprocessors=cnt_preprocessors, marker_def=marker_def) test_set = CntSignalMatrix(signal_processor=test_signal_proc, sensor_names='all') if clean_train: train_cleaner = BCICompetitionIV2ABArtefactMaskCleaner( marker_def=marker_def) else: train_cleaner = NoCleaner() test_cleaner = BCICompetitionIV2ABArtefactMaskCleaner( marker_def=marker_def) combined_set = CombinedCleanedSet(train_set, test_set, train_cleaner, test_cleaner) if not only_return_exp: combined_set.load() in_chans = train_set.get_topological_view().shape[1] else: in_chans = n_chans input_time_length = 1000 # implies how many crops are processed in parallel, does _not_ determine receptive field size # receptive field size is determined by model architecture # ensure reproducibility by resetting lasagne/theano random generator lasagne.random.set_rng(RandomState(34734)) final_layer = create_deep_net(in_chans, input_time_length) print_layers(final_layer) dataset_splitter = SeveralSetsSplitter(valid_set_fraction=0.2, use_test_as_valid=False) iterator = CntWindowTrialIterator( batch_size=45, input_time_length=input_time_length, n_sample_preds=get_n_sample_preds(final_layer)) monitors = [ LossMonitor(), CntTrialMisclassMonitor(input_time_length=input_time_length), KappaMonitor(input_time_length=iterator.input_time_length, mode=kappa_mode), RuntimeMonitor(), ] #debug: n_no_decrease_max_epochs = 2 #debug: n_max_epochs = 4 n_no_decrease_max_epochs = 80 n_max_epochs = 800 #100 # real values for paper were 80 and 800 stop_criterion = Or([ NoDecrease('valid_misclass', num_epochs=n_no_decrease_max_epochs), MaxEpochs(num_epochs=n_max_epochs) ]) dataset = combined_set splitter = dataset_splitter updates_expression = adam updates_modifier = MaxNormConstraintWithDefaults({}) remember_best_chan = 'valid_misclass' run_after_early_stop = True exp = Experiment(final_layer, dataset, splitter, None, iterator, loss_expression, updates_expression, updates_modifier, monitors, stop_criterion, remember_best_chan, run_after_early_stop, batch_modifier=None) if only_return_exp: return exp exp.setup() exp.run() end_time = time.time() run_time = end_time - start_time ex.info['finished'] = True for key in exp.monitor_chans: ex.info[key] = exp.monitor_chans[key][-1] ex.info['runtime'] = run_time save_pkl_artifact(ex, exp.monitor_chans, 'monitor_chans.pkl') save_npy_artifact(ex, lasagne.layers.get_all_param_values(exp.final_layer), 'model_params.npy')
def run_exp(i_fold): # ensure reproducibility by resetting lasagne/theano random generator lasagne.random.set_rng(RandomState(34734)) d5net = Deep5Net(in_chans=in_chans, input_time_length=input_time_length, num_filters_time=num_filters_time, filter_time_length=filter_time_length, num_filters_spat=num_filters_spat, pool_time_length=pool_time_length, pool_time_stride=pool_time_stride, num_filters_2=num_filters_2, filter_length_2=filter_length_2, num_filters_3=num_filters_3, filter_length_3=filter_length_3, num_filters_4=num_filters_4, filter_length_4=filter_length_4, final_dense_length=final_dense_length, n_classes=n_classes, final_nonlin=final_nonlin, first_nonlin=first_nonlin, first_pool_mode=first_pool_mode, first_pool_nonlin=first_pool_nonlin, later_nonlin=later_nonlin, later_pool_mode=later_pool_mode, later_pool_nonlin=later_pool_nonlin, drop_in_prob=drop_in_prob, drop_prob=drop_prob, batch_norm_alpha=batch_norm_alpha, double_time_convs=double_time_convs, split_first_layer=split_first_layer, batch_norm=batch_norm) final_layer = d5net.get_layers()[-1] final_layer = ClipLayer(final_layer, 1e-4, 1 - 1e-4) dataset_splitter = CntTrialSingleFoldSplitter(n_folds=10, i_test_fold=i_fold, shuffle=True) iterator = CntWindowTrialIterator( batch_size=45, input_time_length=input_time_length, n_sample_preds=get_n_sample_preds(final_layer)) monitors = [ LossMonitor(), CntTrialMisclassMonitor(input_time_length=input_time_length), KappaMonitor(input_time_length=iterator.input_time_length, mode='max'), RuntimeMonitor() ] #n_no_decrease_max_epochs = 2 #n_max_epochs = 4 n_no_decrease_max_epochs = 80 n_max_epochs = 800 # real values for paper were 80 and 800 remember_best_chan = 'valid_' + stop_chan stop_criterion = Or([ NoDecrease(remember_best_chan, num_epochs=n_no_decrease_max_epochs), MaxEpochs(num_epochs=n_max_epochs) ]) dataset = combined_set splitter = dataset_splitter updates_expression = adam updates_modifier = MaxNormConstraintWithDefaults({}) preproc = None exp = Experiment(final_layer, dataset, splitter, preproc, iterator, loss_expression, updates_expression, updates_modifier, monitors, stop_criterion, remember_best_chan, run_after_early_stop, batch_modifier=None) if only_return_exp: return exp exp.setup() exp.run() return exp