def create_pyramidal_basal_soma_rate_diff_monitor(monitor_name, model, layer_num, cell_location, dynamics_parameters, update_frequency): layers = model.get_layers() _, layer = layers[layer_num] leak_conductance = dynamics_parameters['leak_conductance'] apical_conductance = dynamics_parameters['apical_conductance'] basal_conductance = dynamics_parameters['basal_conductance'] transfer_function = create_transfer_function( dynamics_parameters['transfer_function']) scaling_factor = basal_conductance / ( leak_conductance + basal_conductance + apical_conductance) def get_pyramidal_basal_soma_rate_diff(iter_number): pyramidal_somatic_potentials = layer.get_pyramidal_somatic_potentials( ) pyramidal_basal_potentials = layer.get_pyramidal_basal_potentials() soma_rate = transfer_function( pyramidal_somatic_potentials[cell_location, None]) basal_rate = transfer_function( scaling_factor * pyramidal_basal_potentials[cell_location, None]) value = soma_rate - basal_rate return float(value) var_name = 'pyramidal_basal_soma_rate_diff' return GenericMonitor(var_name, get_pyramidal_basal_soma_rate_diff, update_frequency=update_frequency)
def __init__(self, model, input_output_stream, dynamics_parameters, monitors=list()): self.model = model self.input_output_stream = input_output_stream self.iteration_number = 0 self.dynamics_parameters = dynamics_parameters self.background_noise_std = dynamics_parameters['background_noise_std'] self.transfer_function = create_transfer_function(dynamics_parameters['transfer_function']) # visualise_transfer_function(self.transfer_function) self.plastic_feedforward_weights = dynamics_parameters['plastic_feedforward_weights'] self.plastic_predict_weights = dynamics_parameters['plastic_predict_weights'] self.plastic_interneuron_weights = dynamics_parameters['plastic_interneuron_weights'] self.plastic_feedback_weights = dynamics_parameters['plastic_feedback_weights'] self.monitors = monitors self.testing_phase = False
def create_error_monitor(monitor_name, model, input_output_stream, error_type, dynamics_parameters, update_frequency): _, last_layer = model.get_layers()[-1] transfer_function = create_transfer_function( dynamics_parameters['transfer_function']) if error_type == 'sum_squares_error': def get_error(num_iters): target = input_output_stream.get_output_targets(num_iters) if target is None: return None else: target_rate = transfer_function(target) output_rate = transfer_function( last_layer.get_pyramidal_somatic_potentials()) error = np.sum((target_rate - output_rate)**2) return float(error) elif error_type == 'sum_squares_potential_error': def get_error(num_iters): target = input_output_stream.get_output_targets(num_iters) if target is None: return None else: target_potential = target output_potential = last_layer.get_pyramidal_somatic_potentials( ) error = np.sum((target_potential - output_potential)**2) return float(error) else: raise Exception('Invalid error type {}'.format(error_type)) var_name = error_type return GenericMonitor(monitor_name, var_name, get_error, update_frequency=update_frequency)
class MSERateLoss(nn.Module): def __init__(self, activation_function, reduction='mean'): super(MSERateLoss, self).__init__() self.reduction = reduction self.activation_function = activation_function def forward(self, input, target): return mse_loss(self.activation_function.forward(input), self.activation_function(target), reduction=self.reduction) if __name__ == '__main__': transfer_function = create_transfer_function( config=transfer_function_config) target_network_train_dataset = TargetNetworkDataset( input_sequence_data_path= './datasets/target_network/train_input_sequence_size20_examples500.npy', target_network_weights_path= './target_network_weights/4_layer_sf_2x6x10/', transfer_function=transfer_function) target_network_test_dataset = TargetNetworkDataset( input_sequence_data_path= './datasets/target_network/test_input_sequence_size20_examples500.npy', target_network_weights_path= './target_network_weights/4_layer_sf_2x6x10/', transfer_function=transfer_function)
def create_target_network_experiment(config, experiment_name, train_data_path, test_data_path, example_iterations, self_predict_phase_length, training_phase_length, test_phase_length, target_network_weights_path, model_file=None): experiment = Experiment(config=config, experiment_name=experiment_name, model_file=model_file) experiment.example_iterations = example_iterations transfer_function = create_transfer_function( experiment.dynamics['transfer_function']) target_network_forward_weights_list = get_target_network_forward_weights_list( target_network_weights_path) # input_sequence = np.random.uniform(-1, 1, (num_train_examples, self.input_size)) input_sequence = np.load(train_data_path) output_sequence = compute_non_linear_transform( input_sequence, transfer_function, target_network_forward_weights_list) if test_phase_length > 0: # test_input_sequence = np.random.uniform(-1, 1, (num_test_examples, self.input_size)) test_input_sequence = np.load(test_data_path) test_output_sequence = compute_non_linear_transform( test_input_sequence, transfer_function, target_network_forward_weights_list) input_stream = CompositeStream([ CyclingStream((experiment.input_size, 1), input_sequence, example_iterations), CyclingStream((experiment.input_size, 1), input_sequence, example_iterations), CyclingStream((experiment.input_size, 1), test_input_sequence, example_iterations) ], [ 0, self_predict_phase_length, self_predict_phase_length + training_phase_length ]) output_stream = CompositeStream([ NoneStream((experiment.output_size, 1)), SmoothStream( CyclingStream((experiment.output_size, 1), output_sequence, example_iterations), 30), SmoothStream( CyclingStream( (experiment.output_size, 1), test_output_sequence, example_iterations), 30) ], [ 0, self_predict_phase_length, self_predict_phase_length + training_phase_length ]) else: input_stream = CompositeStream([ CyclingStream((experiment.input_size, 1), input_sequence, example_iterations), CyclingStream((experiment.input_size, 1), input_sequence, example_iterations) ], [0, self_predict_phase_length]) output_stream = CompositeStream([ NoneStream((experiment.output_size, 1)), SmoothStream( CyclingStream((experiment.output_size, 1), output_sequence, example_iterations), 30) ], [0, self_predict_phase_length]) experiment.set_input_output_stream( InputOutputStream(input_stream, output_stream)) experiment.initialise_dynamics_simulator() #experiment.dynamics_simulator.save_model(save_location='./saved_models', name='') return experiment