def setup_and_parse_conf(): parser = ArgumentParser() parser.add_argument('config', help='Path to configuration file') parser.add_argument('--output_dir', help='where to store output') arguments = parser.parse_args() with open(arguments.config) as f: config = json.loads(f.read()) _output_dir = conf_dir =\ path.dirname(path.join(os.getcwd(), arguments.config)) if arguments.output_dir: _output_dir = path.join(os.getcwd(), arguments.output) training_file = path.join(_output_dir, 'training-dataset.pickle') test_file = path.join(_output_dir, 'test-dataset.pickle') if path.exists(training_file) and path.exists(test_file): _training_data = pickle.load(open(training_file, 'r')) _test_data = pickle.load(open(test_file, 'r')) print "Using pickled training and test data from files (%s, %s)" % ( training_file, test_file) else: _training_data = create_datasets(**config['datasets']['training'])[0] _test_data = create_datasets(**config['datasets']['test'])[0] pickle.dump(_training_data, open(training_file, 'w')) pickle.dump(_test_data, open(test_file, 'w')) print "Created fresh training and test data, stored in (%s, %s)" % ( training_file, test_file) class AnalyticsConfiguration(): output_dir = _output_dir # System n_cores = config['system']['n_cores'] # Datasets training_data = _training_data test_data = _test_data # Reservoir connectivity = config['reservoir']['connectivity'] # Distribution n_nodes_range = range(*config['distribution']['n_nodes_range']) n_samples = config['distribution']['n_samples'] def input_connectivity_fn(self, *args): return eval(config['distribution']['input_connectivity_fn'])(*args) def output_connectivity_fn(self, *args): return eval( config['distribution']['output_connectivity_fn'])(*args) return AnalyticsConfiguration()
def setup_and_parse_conf(): parser = ArgumentParser() parser.add_argument('config', help='Path to configuration file') parser.add_argument('--output_dir', help='where to store output') arguments = parser.parse_args() with open(arguments.config) as f: config = json.loads(f.read()) _output_dir = conf_dir =\ path.dirname(path.join(os.getcwd(), arguments.config)) if arguments.output_dir: _output_dir = path.join(os.getcwd(), arguments.output) training_file = path.join(_output_dir, 'training-dataset.pickle') test_file = path.join(_output_dir, 'test-dataset.pickle') if path.exists(training_file) and path.exists(test_file): _training_data = pickle.load(open(training_file, 'r')) _test_data = pickle.load(open(test_file, 'r')) print "Using pickled training and test data from files (%s, %s)" % (training_file, test_file) else: _training_data = create_datasets(**config['datasets']['training'])[0] _test_data = create_datasets(**config['datasets']['test'])[0] pickle.dump(_training_data, open(training_file, 'w')) pickle.dump(_test_data, open(test_file, 'w')) print "Created fresh training and test data, stored in (%s, %s)" % (training_file, test_file) class AnalyticsConfiguration(): output_dir = _output_dir # System n_cores = config['system']['n_cores'] # Datasets training_data = _training_data test_data = _test_data # Reservoir connectivity = config['reservoir']['connectivity'] # Distribution n_nodes_range = range(*config['distribution']['n_nodes_range']) n_samples = config['distribution']['n_samples'] def input_connectivity_fn(self, *args): return eval(config['distribution']['input_connectivity_fn'])(*args) def output_connectivity_fn(self, *args): return eval(config['distribution']['output_connectivity_fn'])(*args) return AnalyticsConfiguration()
def estimate_reservoir_distribution(n_samples, n_nodes, connectivity, input_connectivity_range, window_size): results = [] datasets = create_datasets(20, task_size=200, window_size=window_size, dataset_type='temporal_parity') training_dataset, test_dataset = datasets[:-1], datasets[-1] for input_connectivity in input_connectivity_range: logging.info('Sampling N={} with L={}.'.format(n_samples, input_connectivity)) for sample in range(n_samples): try: reservoir = RBNNode(connectivity=connectivity, output_dim=n_nodes, input_connectivity=input_connectivity) readout = Oger.nodes.RidgeRegressionNode( input_dim=reservoir.output_dim, output_dim=1) flow = mdp.Flow([reservoir, readout], verbose=1) flow.train([None, training_dataset]) accuracy = calculate_accuracy(flow, test_dataset) #cc = measure_computational_capability(reservoir, 100, window_size) #result = [input_connectivity, accuracy, cc] results.append([accuracy, reservoir]) #results.append(result) logging.info(accuracy) except Exception as e: logging.error(e) logging.error('Exception occured, Continuing anyways') logging.info(results) return results
def create_dataset(): dataset_type = default_input('Dataset [temporal_parity, temporal_density]', 'temporal_parity') n_datasets = default_input('Datasets', 10) task_size = default_input('Dataset length', 200) window_size = default_input('Window size', 3) datasets = temporal.create_datasets(n_datasets, task_size=task_size, window_size=window_size, dataset_type=dataset_type) dataset_description = '[{}-{}-{}-{}]'.format(dataset_type, n_datasets, task_size, window_size) return datasets, dataset_description
def create_dataset(): dataset_type = default_input( 'Dataset [temporal_parity, temporal_density]', 'temporal_parity') n_datasets = default_input('Datasets', 10) task_size = default_input('Dataset length', 200) window_size = default_input('Window size', 3) datasets = temporal.create_datasets( n_datasets, task_size=task_size, window_size=window_size, dataset_type=dataset_type) dataset_description = '[{}-{}-{}-{}]'.format( dataset_type, n_datasets, task_size, window_size) return datasets, dataset_description
def estimate_reservoir_distribution(n_samples, n_nodes, connectivity, input_connectivity_range, window_size): results = [] datasets = create_datasets( 20, task_size=200, window_size=window_size, dataset_type='temporal_parity') training_dataset, test_dataset = datasets[:-1], datasets[-1] for input_connectivity in input_connectivity_range: logging.info('Sampling N={} with L={}.' .format(n_samples, input_connectivity)) for sample in range(n_samples): try: reservoir = RBNNode(connectivity=connectivity, output_dim=n_nodes, input_connectivity=input_connectivity) readout = Oger.nodes.RidgeRegressionNode( input_dim=reservoir.output_dim, output_dim=1) flow = mdp.Flow([reservoir, readout], verbose=1) flow.train([None, training_dataset]) accuracy = calculate_accuracy(flow, test_dataset) #cc = measure_computational_capability(reservoir, 100, window_size) #result = [input_connectivity, accuracy, cc] results.append([accuracy, reservoir]) #results.append(result) logging.info(accuracy) except Exception as e: logging.error(e) logging.error('Exception occured, Continuing anyways') logging.info(results) return results
from tasks.temporal import create_datasets from rbn.sklearn_rbn_node import RBNNode from sklearn.linear_model import Ridge datasets = create_datasets(10, task_size=200, window_size=3, dataset_type='temporal_parity') training_datasets, test_dataset = datasets[:-1], datasets[-1] rbn_reservoir = RBNNode(connectivity=3, input_connectivity=50, n_nodes=100, output_connectivity=0) readout_layer = Ridge() for td in training_datasets[:1]: #rbn_reservoir.reset_state() reservoir_input, expected_output = td states = rbn_reservoir.execute(reservoir_input) readout_layer.fit(states, expected_output) predictions = readout_layer.predict(states) for prediction in predictions: prediction[0] = 1 if prediction[0] > 0.5 else 0 errors = sum(predictions != expected_output) accuracy = 1 - float(errors) / len(predictions)
from tasks.temporal import create_datasets from rbn.sklearn_rbn_node import RBNNode from sklearn.linear_model import Ridge datasets = create_datasets( 10, task_size=200, window_size=3, dataset_type='temporal_parity') training_datasets, test_dataset = datasets[:-1], datasets[-1] rbn_reservoir = RBNNode(connectivity=3, input_connectivity=50, n_nodes=100, output_connectivity=0) readout_layer = Ridge() for td in training_datasets[:1]: rbn_reservoir.reset_state() reservoir_input, expected_output = td states = rbn_reservoir.execute(reservoir_input) readout_layer.fit(states, expected_output) predictions = readout_layer.predict(states) for prediction in predictions: prediction[0] = 1 if prediction[0] > 0.5 else 0 errors = sum(predictions != expected_output) accuracy = 1 - float(errors) / len(predictions)