Esempio n. 1
0
 def initialize(self):
     """ Initialize data containers and theano functions for training."""
     self.rng = RandomState(30948348)
     self.data_batches = []
     self.y_batches = []
     self.input_time_length = get_input_time_length(self.cnt_model)
     self.n_sample_preds = get_n_sample_preds(self.cnt_model)
     self.n_classes = self.cnt_model.output_shape[1]
     # create train function
     log.info("Compile train function...")
     self._create_train_function()
     log.info("Done compiling train function.")
Esempio n. 2
0
 def initialize(self):
     """ Initialize data containers and theano functions for training."""
     self.rng = RandomState(30948348)
     self.data_batches = []
     self.y_batches = []
     self.input_time_length = get_input_time_length(self.cnt_model)
     self.n_sample_preds = get_n_sample_preds(self.cnt_model)
     self.n_classes = self.cnt_model.output_shape[1]
     # create train function
     log.info("Compile train function...")
     self._create_train_function()
     log.info("Done compiling train function.")
Esempio n. 3
0
def compute_trial_acts(model, i_layer, iterator, train_set):
    """Compute activations per trial per sample for given layer of the model.
    
    Parameters
    ----------
    model: Lasagne layer
        Final layer of the model.
    i_layer: int
        Index of layer to compute activations for.
    iterator: DatasetIterator
        Iterator to get batches from.
    train_set: Dataset (Cnt)
        Dataset to use.
    
    Returns
    -------
    trial_acts: 3darray of float
        Activations per trial per sample. #trialx#channelx#sample
    """
    # compute number of inputs per trial
    i_trial_starts, i_trial_ends = compute_trial_start_end_samples(
        train_set.y,
        check_trial_lengths_equal=True,
        input_time_length=iterator.input_time_length)
    # +1 since trial ends is inclusive
    n_trial_len = i_trial_ends[0] - i_trial_starts[0] + 1
    n_inputs_per_trial = int(
        np.ceil(n_trial_len / float(iterator.n_sample_preds)))
    log.info("Create theano function...")
    all_layers = lasagne.layers.get_all_layers(model)
    all_out_fn = create_pred_fn(all_layers[i_layer])
    assert (iterator.input_time_length == get_input_time_length(model))
    assert (iterator.n_sample_preds == get_n_sample_preds(model))
    log.info("Compute activations...")
    all_outs_per_batch = [
        all_out_fn(batch[0])
        for batch in iterator.get_batches(train_set, False)
    ]
    batch_sizes = [
        len(batch[0]) for batch in iterator.get_batches(train_set, False)
    ]
    all_outs_per_batch = np.array(all_outs_per_batch)
    n_trials = len(i_trial_starts)
    log.info("Transform to trial activations...")
    trial_acts = get_trial_acts(all_outs_per_batch,
                                batch_sizes,
                                n_trials=n_trials,
                                n_inputs_per_trial=n_inputs_per_trial,
                                n_trial_len=n_trial_len,
                                n_sample_preds=iterator.n_sample_preds)
    log.info("Done.")
    return trial_acts
Esempio n. 4
0
def compute_trial_acts(model, i_layer, iterator, train_set):
    """Compute activations per trial per sample for given layer of the model.
    
    Parameters
    ----------
    model: Lasagne layer
        Final layer of the model.
    i_layer: int
        Index of layer to compute activations for.
    iterator: DatasetIterator
        Iterator to get batches from.
    train_set: Dataset (Cnt)
        Dataset to use.
    
    Returns
    -------
    trial_acts: 3darray of float
        Activations per trial per sample. #trialx#channelx#sample
    """
    # compute number of inputs per trial
    i_trial_starts, i_trial_ends =  compute_trial_start_end_samples(
                train_set.y, check_trial_lengths_equal=True,
                input_time_length=iterator.input_time_length)
    # +1 since trial ends is inclusive
    n_trial_len = i_trial_ends[0] - i_trial_starts[0] + 1
    n_inputs_per_trial = int(np.ceil(n_trial_len / float(iterator.n_sample_preds)))
    log.info("Create theano function...")
    all_layers = lasagne.layers.get_all_layers(model)
    all_out_fn = create_pred_fn(all_layers[i_layer])
    assert(iterator.input_time_length == get_input_time_length(model))
    assert(iterator.n_sample_preds == get_n_sample_preds(model))
    log.info("Compute activations...")
    all_outs_per_batch = [all_out_fn(batch[0]) 
          for batch in iterator.get_batches(train_set, False)]
    batch_sizes = [len(batch[0]) for batch in iterator.get_batches(train_set, False)]
    all_outs_per_batch = np.array(all_outs_per_batch)
    n_trials = len(i_trial_starts)
    log.info("Transform to trial activations...")
    trial_acts = get_trial_acts(all_outs_per_batch,
                                  batch_sizes, n_trials=n_trials, 
                                n_inputs_per_trial=n_inputs_per_trial,
                                  n_trial_len=n_trial_len, 
                                n_sample_preds=iterator.n_sample_preds)
    log.info("Done.")
    return trial_acts
Esempio n. 5
0
def create_experiment(yaml_filename, seed=9859295):
    """Utility function to create experiment from yaml file"""
    # for reproducibility for layer weights
    # should be same seed as in experiment_runner.py
    lasagne.random.set_rng(RandomState(seed))
    train_dict = yaml_parse.load(open(yaml_filename, 'r'))
    layers = load_layers_from_dict(train_dict)
    final_layer = layers[-1]
    dataset = train_dict['dataset']
    splitter = train_dict['dataset_splitter']
    if (np.any([hasattr(l, 'n_stride') for l in layers])):
        n_sample_preds = get_n_sample_preds(final_layer)
        # for backwards compatibility input time length also
        input_time_length = get_input_time_length(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
            if hasattr(monitor, 'input_time_length'):
                monitor.input_time_length = input_time_length

        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)))
    # add early stop chan, encessary for backwards compatibility
    exp_args = train_dict['exp_args']
    exp_args['remember_best_chan'] = train_dict['exp_args'].pop(
        'remember_best_chan', 'valid_misclass')
    exp_args['run_after_early_stop'] = train_dict['exp_args'].pop(
        'run_after_early_stop', True)
    exp = Experiment(final_layer, dataset, splitter, **exp_args)
    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)))))
    return exp
Esempio n. 6
0
def create_experiment(yaml_filename, seed=9859295):
    """Utility function to create experiment from yaml file"""
    # for reproducibility for layer weights
    # should be same seed as in experiment_runner.py
    lasagne.random.set_rng(RandomState(seed))
    train_dict = yaml_parse.load(open(yaml_filename, 'r'))
    layers = load_layers_from_dict(train_dict)
    final_layer = layers[-1]
    dataset = train_dict['dataset'] 
    splitter = train_dict['dataset_splitter']
    if (np.any([hasattr(l, 'n_stride') for l in layers])):
        n_sample_preds =  get_n_sample_preds(final_layer)
        # for backwards compatibility input time length also
        input_time_length = get_input_time_length(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
            if hasattr(monitor, 'input_time_length'):
                monitor.input_time_length = input_time_length
                
        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)))
    # add early stop chan, encessary for backwards compatibility
    exp_args = train_dict['exp_args']
    exp_args['remember_best_chan'] = train_dict['exp_args'].pop('remember_best_chan',
        'valid_misclass')
    exp_args['run_after_early_stop'] = train_dict['exp_args'].pop('run_after_early_stop',
        True)
    exp = Experiment(final_layer, dataset, splitter,
                    **exp_args)
    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)))))
    return exp
Esempio n. 7
0
 def get_n_samples_pred_window(self):
     return get_input_time_length(self.model)