def train_ESN(X, Y, Xte, Yte, embedding_method, n_dim, w_ridge, n_internal_units=None, spectral_radius=None, connectivity=None, input_scaling=None, noise_level=None, reservoir=None): num_classes = Y.shape[1] # Initialize reservoir if reservoir is None: reservoir = Reservoir(n_internal_units, spectral_radius, connectivity, input_scaling, noise_level) elif n_internal_units is None \ or spectral_radius is None \ or connectivity is None \ or input_scaling is None \ or noise_level is None: raise RuntimeError('Reservoir parameters missing') # Initialize timer time_tr_start = time.time() # Compute reservoir states res_states = reservoir.get_states(X, embedding=embedding_method, n_dim=n_dim, train=True, bidir=False) # Readout training readout = Ridge(alpha=w_ridge) readout.fit(res_states, Y) training_time = (time.time() - time_tr_start) / 60 # Test res_states_te = reservoir.get_states(Xte, embedding=embedding_method, n_dim=n_dim, train=False, bidir=False) logits = readout.predict(res_states_te) pred_class = np.argmax(logits, axis=1) true_class = np.argmax(Yte, axis=1) accuracy = accuracy_score(true_class, pred_class) if num_classes > 2: f1 = f1_score(true_class, pred_class, average='weighted') else: f1 = f1_score(true_class, pred_class, average='binary') return accuracy, f1, training_time
def __init__(self): spi = busio.SPI(clock=board.SCK_1, MISO=board.MISO_1, MOSI=board.MOSI_1) #ADCs use second SPI port of RPi cs = digitalio.DigitalInOut(board.D16) mcp = MCP.MCP3008(spi, cs) self.pi = pigpio.pi() self.NUM_NEOPIXELS = 118 self.pixels = neopixel.NeoPixel(board.D12, self.NUM_NEOPIXELS, auto_write=False) self.reservoir = Reservoir(self.pixels) self.windmills = Windmills(self.pi, mcp) self.solarPanels = SolarPanels(mcp) self.city = City(self.pixels) self.fuelCell = FuelCell(self.pixels, self.pi) self.distributionMiddle = DistributionLine(self.pixels, 101, 4) self.distributionRight = DistributionLine(self.pixels, 105, 5, True) self.transmissionLine = TransmissionLines(self.pixels) self.wattownSign = WattownSign(self.pixels) self.substation = Substation(self.pixels) self.stopEvent = threading.Event() self.stopEvent.clear() super().__init__()
def createAndUpdateReservoir(index): (varF, F, init) = addReservoirParameters(I, bn.getRandomParameters, N, K, isConstantConnectivity=constK) res = Reservoir(I, L, input_fp, directory + 'reservoir_%d_output' % index, N, varF, F, init) res.update(numberOfUpdates) # print('Reservoir %d has vector representation\n' % i) # print(res.getHistoryAsVectors()) return res # if i % 5 == 0: print(index, '. . .')
def echo_state_network(data_matrix, label, reservoir): # Initialize reservoir if reservoir is None: reservoir = Reservoir(n_internal_units, spectral_radius, connectivity, input_scaling, noise_level) res_states = reservoir.get_states(data_matrix, embedding=embedding_method, n_dim=None, train=True, bidir=False) # Readout training readout = Ridge(alpha=w_ridge) readout.fit(res_states, label) return reservoir, readout
def _fillSite(self, site, adjacentSites): initialDepth = site.getPotentialOilDepth() for adjacentSite in adjacentSites: initialReserves = self._getInitialReserves() if self._depthBracket(initialDepth) == self._depthBracket(adjacentSite.getPotentialOilDepth()): self._siteCount += 1 reservoir = site.getReservoir() if reservoir is None: self._reservoirCount += 1 reservoir = Reservoir(initialDepth, initialReserves) site.setReservoir(reservoir) reservoir.join(adjacentSite.getPotentialOilDepth(), initialReserves) adjacentSite.setReservoir(reservoir) else: self._reservoirCount += 1 site.setReservoir(Reservoir(initialDepth, initialReserves))
def updateReservoir(index): ## (varF, F, init) = addReservoirParameters(1, bn.getParametersFromFile, ## 5, directory + linkages_filename, ## directory + functions_filename, ## directory + initial_filename) (varF, F, init) = addReservoirParameters(I, bn.getRandomParameters, N, K, isConstantConnectivity=constK) r = Reservoir(I, L, directory + inputs_filename, outputs_filepath + '_reservoir_%d_output' % index, N, varF, F, init) r.update(numberOfUpdates) print('done at index %d' % index) return r
import numpy as np import pandas as pd import scipy.stats as stats from matplotlib import pyplot as plt import seaborn as sns import booleanNetwork as bn from reservoir import Reservoir from sklearn.linear_model import Lasso from sklearn.linear_model import LinearRegression fp = '/Users/maxnotarangelo/Documents/ISB/BN_realization/time_series_data.csv' N = 100 numberOfInputs = 1 L = 5 (random_linkages, random_functions, random_init) = bn.getRandomParameters(N, 2) r = Reservoir(numberOfInputs, L, fp, N, random_linkages, random_functions, random_init) r.update(100) reservoir_data_file = '/Users/maxnotarangelo/Documents/ISB/log.csv' rdf = pd.read_csv(reservoir_data_file) rdf.head() func_data_file = '/Users/maxnotarangelo/Documents/ISB/BN_realization/function_data.csv' fdf = pd.read_csv(func_data_file) fdf.head() rdf['Majority'] = fdf.get('Majority') X = rdf.drop(['Input Node 1', 'Majority'], axis=1) lm = LinearRegression()
from reservoir import Reservoir ensembleSize = 50 N = 10 K = 2 I = 1 L = 5 input_fp = '/Users/maxnotarangelo/Documents/ISB/BN_realization/time_series_data.csv' directory = '/Users/maxnotarangelo/Documents/ISB/rc_ensemble_data/' np.random.seed(0) ensemble = [] for i in range(ensembleSize): (links, funcs, inits) = bn.getRandomParameters(N, K) res = Reservoir(I, L, input_fp, directory + 'reservoir_%d_output' % i, N, links, funcs, inits) res.update(100) # print('Reservoir %d has vector representation\n' % i) # print(res.getHistoryAsVectors()) ensemble.append(res) if i % 5 == 0: print(i, '. . .') success = 0 failure = 0 data_real = None print('first stage completed.') for res in ensemble: df = pd.DataFrame(res.getHistoryAsVectors()) # print('df:\n', df)
def unigen(a,b): def u(): return uniform(a,b) return u def zerogen(): def zero(): return 0 return zero # perturbation size = initial separation gamma0 = 10**-8 ITERATIONS = 400 # matrix and initial activation randomization r = Reservoir(p, q, r) r.randomize_matrices(norm) r.WI = random_matrix(q,p+1, unigen(-0.1,0.1)) r.W = random_matrix(q,q+1, normgen(0,10**(-0.8))) r.randomize_vectors(normgen(0,1)) # make a copy of the reservoir r2 = copy.deepcopy(r) #perturbation = random_vector(q) #perturbation = times(perturbation, gamma0 / vector_len(perturbation)) perturbation = [0]*q perturbation[randrange(q)] = gamma0 #print(perturbation) r2.X = plus(r2.X, perturbation)
def train_BDESN(X, Y, Xte, Yte, embedding_method, n_dim, fc_layout, batch_size, num_epochs, p_drop, w_l2, learning_rate, seed=None, n_internal_units=None, spectral_radius=None, connectivity=None, input_scaling=None, noise_level=None, reservoir=None): num_classes = Y.shape[1] # Compute reservoir states if reservoir is None: reservoir = Reservoir(n_internal_units, spectral_radius, connectivity, input_scaling, noise_level) elif n_internal_units is None \ or spectral_radius is None \ or connectivity is None \ or input_scaling is None \ or noise_level is None: raise RuntimeError('Reservoir parameters missing') res_states = reservoir.get_states(X, embedding=embedding_method, n_dim=n_dim, train=True, bidir=True) res_states_te = reservoir.get_states(Xte, embedding=embedding_method, n_dim=n_dim, train=False, bidir=True) n_data, input_size = res_states.shape graph = tf.Graph() with graph.as_default(): if seed is not None: tf.set_random_seed(seed) # ============= MLP ============== # tf Graph input nn_input = tf.placeholder(shape=(None, input_size), dtype=tf.float32) nn_output = tf.placeholder(shape=(None, num_classes), dtype=tf.float32) keep_prob = tf.placeholder(dtype=tf.float32) # MLP logits = build_network(nn_input, input_size, fc_layout, num_classes, keep_prob) loss_track, pred_class, training_time = \ train_tf_model('BDESN', graph, res_states, Y, res_states_te, Yte, n_data, batch_size, num_epochs, nn_input, keep_prob, logits, nn_output, w_l2, learning_rate, p_drop) true_class = np.argmax(Yte, axis=1) accuracy = accuracy_score(true_class, pred_class) if num_classes > 2: f1 = f1_score(true_class, pred_class, average='weighted') else: f1 = f1_score(true_class, pred_class, average='binary') return loss_track, accuracy, f1, training_time
class WattownBoard(threading.Thread): def __init__(self): spi = busio.SPI(clock=board.SCK_1, MISO=board.MISO_1, MOSI=board.MOSI_1) #ADCs use second SPI port of RPi cs = digitalio.DigitalInOut(board.D16) mcp = MCP.MCP3008(spi, cs) self.pi = pigpio.pi() self.NUM_NEOPIXELS = 118 self.pixels = neopixel.NeoPixel(board.D12, self.NUM_NEOPIXELS, auto_write=False) self.reservoir = Reservoir(self.pixels) self.windmills = Windmills(self.pi, mcp) self.solarPanels = SolarPanels(mcp) self.city = City(self.pixels) self.fuelCell = FuelCell(self.pixels, self.pi) self.distributionMiddle = DistributionLine(self.pixels, 101, 4) self.distributionRight = DistributionLine(self.pixels, 105, 5, True) self.transmissionLine = TransmissionLines(self.pixels) self.wattownSign = WattownSign(self.pixels) self.substation = Substation(self.pixels) self.stopEvent = threading.Event() self.stopEvent.clear() super().__init__() def join(self, timeout=None): self.stopEvent.set() self.resetBoard() threading.Thread.join(self, timeout) def run(self): while not self.stopEvent.wait( 0.1): # provides delay as well instead of sleep currentTime = self.getTimeMilliseconds() self.pixels.show() self.windmills.update(currentTime) self.fuelCell.update(currentTime) self.distributionMiddle.update(currentTime) self.distributionRight.update(currentTime) def releaseResources(self): self.pi.stop() def resetBoard(self): self.pixels.fill((0, 0, 0)) self.windmills.stopWindmills() self.fuelCell.stopPulsing() self.distributionMiddle.stopPowerFlow() self.distributionRight.stopPowerFlow() self.pixels.show() self.windmills.update(0) self.fuelCell.update(0) self.distributionMiddle.update(0) self.distributionRight.update(0) #provides interface with city lights coefficient def lightCityBlocks(self, cityLightsCoefficient, supplyTripped=False, offColourBlack=False): numBlocksToLight = round(cityLightsCoefficient * 3) self.city.lightCityBlocks(numBlocksToLight, supplyTripped, offColourBlack) def lightReservoir(self, reservoirLevel): levelsToLight = round(reservoirLevel / 25) self.reservoir.setWaterLevel(levelsToLight) def getTimeMilliseconds(self): return int(round(time.time() * 1000))
def __init__( self, # reservoir reservoir=None, n_internal_units=None, spectral_radius=None, leak=None, connectivity=None, input_scaling=None, noise_level=None, n_drop=None, bidir=False, circle=False, # dim red dimred_method=None, n_dim=None, # representation mts_rep=None, w_ridge_embedding=None, # readout readout_type=None, w_ridge=None, mlp_layout=None, num_epochs=None, w_l2=None, nonlinearity=None, svm_gamma=1.0, svm_C=1.0, ): """ Build and evaluate a RC-based classifier. The training and test MTS are multidimensional arrays of shape [N,T,V], with - N = number of samples - T = number of time steps in each sample - V = number of variables in each sample Training and test labels have shape [N,C], with C the number of classes The dataset consists of: X, Y = training data and respective labels Xte, Yte = test data and respective labels Reservoir parameters: reservoir = precomputed reservoir (oject of class 'Reservoir'); if None, the following structural hyperparameters must be specified n_internal_units = processing units in the reservoir spectral_radius = largest eigenvalue of the reservoir matrix of connection weights leak = amount of leakage in the reservoir state update (optional) connectivity = percentage of nonzero connection weights input_scaling = scaling of the input connection weights noise_level = deviation of the Gaussian noise injected in the state update n_drop = number of transient states to drop bidir = use a bidirectional reservoir (True or false) Dimensionality reduction parameters: dimred_method = procedure for reducing the number of features in the sequence of reservoir states; possible options are: None (no dimensionality reduction), 'pca' or 'tenpca' n_dim = number of resulting dimensions after the dimensionality reduction procedure Representation parameters: mts_rep = type of MTS representation. It can be 'last' (last state), 'output' (output model space), or 'reservoir' (reservoir model space) w_ridge_embedding = regularization parameter of the ridge regression in the output model space and reservoir model space representation; ignored if mts_rep == None Readout parameters: readout_type = type of readout used for classification. It can be 'lin' (ridge regression), 'mlp' (multiplayer perceptron), 'svm' (support vector machine), or None. If None, the input representations will be saved instead: this is useful for clustering and visualization. w_ridge = regularization parameter of the ridge regression readout (only for readout_type=='lin') mlp_layout = tuple with the sizes of MLP layers, e.g. (20, 10) defines a MLP with 2 layers of 20 and 10 units respectively. (only for readout_type=='mlp') num_epochs = number of iterations during the optimization (only for readout_type=='mlp') w_l2 = weight of the L2 regularization (only for readout_type=='mlp') nonlinearity = type of activation function {'relu', 'tanh', 'logistic', 'identity'} (only for readout_type=='mlp') svm_gamma = bandwith of the RBF kernel (only for readout_type=='svm') svm_C = regularization for SVM hyperplane (only for readout_type=='svm') """ self.n_drop = n_drop self.bidir = bidir self.dimred_method = dimred_method self.mts_rep = mts_rep self.readout_type = readout_type self.svm_gamma = svm_gamma # Initialize reservoir if reservoir is None: self._reservoir = Reservoir(n_internal_units=n_internal_units, spectral_radius=spectral_radius, leak=leak, connectivity=connectivity, input_scaling=input_scaling, noise_level=noise_level, circle=circle) else: self._reservoir = reservoir # Initialize dimensionality reduction method if dimred_method is not None: if dimred_method.lower() == 'pca': self._dim_red = PCA(n_components=n_dim) elif dimred_method.lower() == 'tenpca': self._dim_red = tensorPCA(n_components=n_dim) else: raise RuntimeError('Invalid dimred method ID') # Initialize ridge regression model if mts_rep == 'output' or mts_rep == 'reservoir': self._ridge_embedding = Ridge(alpha=w_ridge_embedding, fit_intercept=True) # Initialize readout type if self.readout_type is not None: if self.readout_type == 'lin': # Ridge regression self.readout = Ridge(alpha=w_ridge) elif self.readout_type == 'svm': # SVM readout self.readout = SVC(C=svm_C, kernel='precomputed') elif readout_type == 'mlp': # MLP (deep readout) # pass self.readout = MLPClassifier( hidden_layer_sizes=mlp_layout, activation=nonlinearity, alpha=w_l2, batch_size=32, learning_rate='adaptive', # 'constant' or 'adaptive' learning_rate_init=0.001, max_iter=num_epochs, early_stopping=False, # if True, set validation_fraction > 0 validation_fraction=0.0 # used for early stopping ) else: raise RuntimeError('Invalid readout type')
functionsToApproximate = [f_utils.parity] # Test parameter set data = np.zeros((len(N_list), len(L_list))) for i in range(len(N_list)): for j in range(len(L_list)): L = L_list[j] * N_list[i] // 100 # Initialize reservoir bn_directory = os.getcwd() + '/BN_realization/' directory = os.getcwd() + '/' varF, F, init = bn.getRandomParameters(N_list[i] + I, K + (L / N_list[i]), isConstantConnectivity=False) res = Reservoir(I, L, bn_directory + 'time_series_data_3.csv', directory + 'experiment1_2018-08-03.csv', N_list[i], varF, F, init) # Train and test output layer output = OutputLayer(res, O, functionsToApproximate, functionInputs, delay, dataStreamLength, nonRecursiveArgs=[[(0, 2)]]) output.train(trainingSize) output.test(testSize) # add results to data print('adding data')
def System_Object_Creation(T, Input_Data_File, element_stochastic_components, SystemObjects, Element_Dict, Flushing_Group_Dict, stochastic_flow_list, op_policy_params=None): # Purpose: To create all reservoir/reach/junction/diversion objects (instances of classes, e.g., Reservoir) # Inputs: # (1) All inputs created automatically within SedSim_v2.py using various functions called there. # Create all system objects (reservoirs, channels, diversions, junctions), already loaded in their correct simulation order. for i in SystemObjects["Ordered Simulation List"]: if Element_Dict[i]["Type"] == "Reservoir": SystemObjects[i] = Reservoir( i, T, Input_Data_File, Element_Dict[i], stochastic_components=element_stochastic_components[i], op_policy_params=op_policy_params) # If the simulated system will have reservoirs with flushing performed, need to create a dictionary storing flushing group ID # and non_drawdown group ID, to be fed back to each reservoir and stored there. if SystemObjects[i].Flushing_Group_ID is not None: # If reservoir has a flushing group ID, create such a key in the Flushing ID dictionary. if SystemObjects[ i].Flushing_Group_ID not in Flushing_Group_Dict[ "Flushing Group ID"]: # Store reservoir names with this ID in a list. Flushing_Group_Dict["Flushing Group ID"][ SystemObjects[i].Flushing_Group_ID] = [] Flushing_Group_Dict["Flushing Group ID"][ SystemObjects[i].Flushing_Group_ID].append( SystemObjects[i].name) else: # Key ID already exists. Add Reservoir name to list. Flushing_Group_Dict["Flushing Group ID"][ SystemObjects[i].Flushing_Group_ID].append( SystemObjects[i].name) if SystemObjects[i].Non_Flushing_Group_ID is not None: # If reservoir has a flushing group NON-DRAWDOWN ID, create such a key in the Non-drawdown Flushing ID dictionary. if SystemObjects[ i].Non_Flushing_Group_ID not in Flushing_Group_Dict[ "Non Drawdown Flushing Group ID"]: # Store reservoir names with this ID in a list. Flushing_Group_Dict["Non Drawdown Flushing Group ID"][ SystemObjects[i].Non_Flushing_Group_ID] = [] Flushing_Group_Dict["Non Drawdown Flushing Group ID"][ SystemObjects[i].Non_Flushing_Group_ID].append( SystemObjects[i].name) else: # Key ID already exists. Add Reservoir name to list. Flushing_Group_Dict["Non Drawdown Flushing Group ID"][ SystemObjects[i].Non_Flushing_Group_ID].append( SystemObjects[i].name) elif Element_Dict[i]["Type"] == "Reach": SystemObjects[i] = Channel(i, T, Input_Data_File, Element_Dict[i], element_stochastic_components[i]) elif Element_Dict[i]["Type"] == "Junction": SystemObjects[i] = Junction(i, T, Input_Data_File, Element_Dict[i], element_stochastic_components[i], stochastic_flow_list) elif Element_Dict[i]["Type"] == "Diversion": SystemObjects[i] = Diversion_Channel( i, T, Input_Data_File, Element_Dict[i], element_stochastic_components[i]) elif Element_Dict[i]["Type"] == "Bypass Structure": SystemObjects[i] = Bypass_Structure(i, T, Input_Data_File, Element_Dict[i]) # Store the Flushing Group Dictionary as part of each reservoir's attributes for i in SystemObjects["Ordered Simulation List"]: if Element_Dict[i]["Type"] == "Reservoir": SystemObjects[i].Flushing_Dictionary = Flushing_Group_Dict return SystemObjects, Flushing_Group_Dict, element_stochastic_components
from reservoir import Reservoir res = Reservoir(width=5, length=7, height=1) res.set_fluid(1, 0.25, 4000) res.set_media((2, 2, 1), (1, 1, 0.4), 0.08) res.create_well(2, 2, 0, 0, 100, flow=-5.615 * 11, pressure=500) res.create_well(3, 4, 0, 0, 100, flow=5.615 * 10, pressure=500) res.run_steps(1000, 0.1) # 2231
N = 5 # number of network nodes I = 1 # number of input nodes L = 2 # number of connections per input # File paths input_fp = reservoir.directory + reservoir.inputs_filename output_fp = reservoir.outputs_filepath # Network parameters linkages = bn.varF functions = bn.F initialNodes = bn.init np.random.seed(3) res = Reservoir(I, L, input_fp, output_fp, N, linkages, functions, initialNodes) res.getHistoryAsVectors() res.update(100) df = pd.DataFrame(res.getHistoryAsVectors()) res.getHistoryAsVectors() # X_train = np.array(df.iloc[:-20,:-1].sort_index(1)) # X_test = np.array(df.iloc[-20:,:-1].sort_index(1)) # y_train = df.iloc[:-20,-1] # y_test = df.iloc[-20:,-1] for i in range(5, 90, 5): X_train = df.iloc[:i, :-1] X_test = df.iloc[i:, :-1] y_train = df.iloc[:i, -1]
accuracy_list = np.zeros((n_runs, 3)) f1_list = np.zeros((n_runs, 3)) max_batches = X.shape[0]//batch_size # Track the training loss (RNN:0, BDESN:1) loss_track = np.zeros((2, int(np.ceil(max_batches*num_epochs)))) for r in range(n_runs): print('Executing run ', r+1, ' of ', n_runs, '...') # Set seed for PRNG seed = r if use_seed else None np.random.seed(seed) # Initialize common reservoir reservoir = Reservoir(n_internal_units, spectral_radius, connectivity, input_scaling, noise_level) if TRAIN_ESN: print('--- Training ESN ---') accuracy_list[r, 0], f1_list[r, 0], training_time[r, 0] = \ train_ESN(X=X, Y=Y, Xte=Xte, Yte=Yte, n_internal_units=n_internal_units, spectral_radius=spectral_radius, connectivity=connectivity, input_scaling=input_scaling, noise_level=noise_level, embedding_method='identity', n_dim=None,
def __init__( self, # reservoir reservoir=None, n_internal_units=None, spectral_radius=None, leak=None, connectivity=None, input_scaling=None, noise_level=None, n_drop=None, bidir=False, circle=False, # dim red dimred_method=None, n_dim=None, # representation mts_rep=None, w_ridge_embedding=None, # readout readout_type=None, w_ridge=None, mlp_layout=None, num_epochs=None, p_drop=None, w_l2=None, nonlinearity=None, svm_gamma=1.0, svm_C=1.0, seed=None): """ Build and evaluate a RC-based classifier. The training and test MTS are multidimensional arrays of shape [N,T,V], with - N = number of samples - T = number of time steps in each sample - V = number of variables in each sample Training and test labels have shape [N,1] Dataset parameters: X, Y = training data and respective labels Xte, Yte = test data and respective labels Reservoir parameters: reservoir = precomputed reservoir (oject of class 'Reservoir'); if None, the following structural hyperparameters must be specified n_internal_units = processing units in the reservoir spectral_radius = largest eigenvalue of the reservoir matrix of connection weights leak = amount of leakage in the reservoir state update (optional) connectivity = percentage of nonzero connection weights input_scaling = scaling of the input connection weights noise_level = deviation of the Gaussian noise injected in the state update n_drop = number of transient states to drop bidir = use a bidirectional reservoir (True or false) Dimensionality reduction parameters: dimred_method = procedure for reducing the number of features in the sequence of reservoir states; possible options are: None (no dimensionality reduction), 'pca' or 'tenpca' n_dim = number of resulting dimensions after the dimensionality reduction procedure Representation parameters: mts_rep = type of MTS representation. It can be 'last' (last state), 'output' (output model space), or 'reservoir' (reservoir model space) w_ridge_embedding = regularization parameter of the ridge regression in the output model space and reservoir model space representation; ignored if mts_rep == None Readout parameters: readout_type = type of readout used for classification. It can be 'lin' (ridge regression), 'mlp' (multiplayer perceptron) or 'svm' w_ridge = regularization parameter of the ridge regression readout (only for readout_type=='lin') mlp_layout = list with the sizes of MLP layers, e.g. [in_dim,20,10,n_classes] defines a MLP with 2 layers of 20 and 10 units respectively. First entry in the list is the input size, last is the number of classes (only for readout_type=='mlp') num_epochs = number of iterations during the optimization (only for readout_type=='mlp') p_drop = dropout probability (only for readout_type=='mlp') w_l2 = weight of the L2 regularization (only for readout_type=='mlp') nonlinearity = type of activation function {'relu', 'tanh', 'sigmoid', 'lin' 'maxout', 'kaf'} (only for readout_type=='mlp') seed = if not None, set the seed in TF for determinisitc behavior (only for readout_type=='mlp') svm_gamma = bandwith of the RBF kernel (only for readout_type=='svm') svm_C = regularization for SVM hyperplane (only for readout_type=='svm') """ self.n_drop = n_drop self.bidir = bidir self.dimred_method = dimred_method self.mts_rep = mts_rep self.readout_type = readout_type self.svm_gamma = svm_gamma self.p_drop = p_drop self.mlp_layout = mlp_layout self.nonlinearity = nonlinearity self.seed = seed self.w_l2 = w_l2 self.num_epochs = num_epochs # Initialize reservoir if reservoir is None: self._reservoir = Reservoir(n_internal_units=n_internal_units, spectral_radius=spectral_radius, leak=leak, connectivity=connectivity, input_scaling=input_scaling, noise_level=noise_level, circle=circle) else: self._reservoir = reservoir # Initialize dimensionality reduction method if dimred_method is not None: if dimred_method.lower() == 'pca': self._dim_red = PCA(n_components=n_dim) elif dimred_method.lower() == 'tenpca': self._dim_red = tensorPCA(n_components=n_dim) else: raise RuntimeError('Invalid dimred method ID') # Initialize ridge regression model if mts_rep == 'output' or mts_rep == 'reservoir': self._ridge_embedding = Ridge(alpha=w_ridge_embedding, fit_intercept=True) # Initialize readout type if self.readout_type == 'lin': # Ridge regression self.readout = Ridge(alpha=w_ridge) elif self.readout_type == 'svm': # SVM readout self.readout = SVC(C=svm_C, kernel='precomputed') elif readout_type == 'mlp': # MLP (deep readout) pass else: raise RuntimeError('Invalid reservoir type')
def ljapexp(q=150, iterations=40, gamma0=10**-8, sigma=0.1): # input, hidden and output layer size p = 1 #q = 150 r = 1 #300 # strength of input activations INPUT_STRENGTH = 10**0 def input_dist(): return INPUT_STRENGTH * uniform(-1, 1) def normgen(mu, sigma): def n(): return normalvariate(mu, sigma) return n def unigen(a, b): def u(): return uniform(a, b) return u def zerogen(): def zero(): return 0 return zero # matrix and initial activation randomization r = Reservoir(p, q, r) r.randomize_matrices(normgen(0, 1)) r.WI = random_matrix(q, p + 1, unigen(-0.1, 0.1)) r.W = random_matrix(q, q + 1, normgen(0, sigma)) #r.randomize_vectors(normgen(0,1)) # make a copy of the reservoir r2 = copy.deepcopy(r) #perturbation = random_vector(q) #perturbation = times(perturbation, gamma0 / vector_len(perturbation)) priemersum = 0 for perturbed_node in range(q): r.randomize_vectors(normgen(0, 1)) perturbation = [0 for _ in range(q)] perturbation[perturbed_node] = gamma0 r2.X = plus(r2.X, perturbation) difvec = plus(times(r.X, -1), r2.X) exponentsum = 0 lambdy = [0] * iterations for it in range(0, iterations): # introduce a random input r.I = random_vector(r.p, input_dist) r2.I = r.I # run one setp r.fire() r2.fire() # calculate the difference difvec = plus(times(r.X, -1), r2.X) gammaK = vector_len(difvec) # renormalize r2.X = plus(r.X, times(difvec, gamma0 / gammaK)) # record lambda mylog = math.log(gammaK / gamma0) lambdy[it] = mylog priemer = sum(lambdy) / iterations disperzia = sum([(x - priemer) * (x - priemer) for x in lambdy]) / iterations priemersum += priemer print("\rnodeindex=%d" % perturbed_node, end="") return priemersum / q
def main(): assert opt.dataset in ['gowalla', 'lastfm'] device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') cur_dir = os.getcwd() train_dataset = MultiSessionsGraph(cur_dir + '/../datasets/' + opt.dataset, phrase='train') train_loader = DataLoader(train_dataset, batch_size=opt.batch_size, shuffle=True) train_for_res, _ = load_data_valid( os.path.expanduser( os.path.normpath(cur_dir + '/../datasets/' + opt.dataset + '/raw/train.txt.csv')), 0) max_train_item = max(max(max(train_for_res[0])), max(train_for_res[1])) max_train_user = max(train_for_res[2]) test_dataset = MultiSessionsGraph(cur_dir + '/../datasets/' + opt.dataset, phrase='test1') test_loader = DataLoader(test_dataset, batch_size=opt.batch_size, shuffle=False) test_for_res = load_testdata( os.path.expanduser( os.path.normpath(cur_dir + '/../datasets/' + opt.dataset + '/raw/test1.txt.csv'))) max_item = max(max(max(test_for_res[0])), max(test_for_res[1])) max_user = max(test_for_res[2]) pre_max_item = max_train_item pre_max_user = max_train_user log_dir = cur_dir + '/../log/' + str(opt.dataset) + '/paper200/' + str( opt) + '_fix_new_entropy(rank)_on_union+' + str(opt.u) + 'tanh*u_AGCN***GAG-win' + str(opt.win_size) \ + '***concat3_linear_tanh_in_e2s_' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if not os.path.exists(log_dir): os.makedirs(log_dir) logging.warning('logging to {}'.format(log_dir)) writer = SummaryWriter(log_dir) if opt.dataset == 'gowalla': n_item = 30000 n_user = 33005 else: n_item = 10000 n_user = 984 model = GNNModel(hidden_size=opt.hidden_size, n_item=n_item, n_user=n_user, u=opt.u).to(device) optimizer = torch.optim.Adam(model.parameters(), lr=opt.lr, weight_decay=opt.l2) scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[2, 3], gamma=opt.lr_dc) logging.warning(model) # offline training on 'train' and test on 'test1' logging.warning('*********Begin offline training*********') updates_per_epoch = len(train_loader) updates_count = 0 for train_epoch in tqdm(range(opt.epoch)): forward(model, train_loader, device, writer, train_epoch, optimizer=optimizer, train_flag=True, max_item_id=max_train_item, last_update=updates_count) scheduler.step() updates_count += updates_per_epoch with torch.no_grad(): forward(model, test_loader, device, writer, train_epoch, train_flag=False, max_item_id=max_item) # reservoir construction with 'train' logging.warning( '*********Constructing the reservoir with offline training data*********' ) res = Reservoir(train_for_res, opt.res_size) res.update(train_for_res) # test and online training on 'test2~5' logging.warning('*********Begin online training*********') now = time.asctime() for test_epoch in tqdm(range(1, 6)): if test_epoch != 1: test_dataset = MultiSessionsGraph(cur_dir + '/../datasets/' + opt.dataset, phrase='test' + str(test_epoch)) test_loader = DataLoader(test_dataset, batch_size=opt.batch_size, shuffle=False) test_for_res = load_testdata( os.path.expanduser( os.path.normpath(cur_dir + '/../datasets/' + opt.dataset + '/raw/test' + str(test_epoch) + '.txt.csv'))) pre_max_item = max_item pre_max_user = max_user max_item = max(max(max(test_for_res[0])), max(test_for_res[1])) max_user = max(test_for_res[2]) # test on the current test set # no need to test on test1 because it's done in the online training part # epoch + 10 is a number only for the visualization convenience with torch.no_grad(): forward(model, test_loader, device, writer, test_epoch + 10, train_flag=False, max_item_id=max_item) # reservoir sampling sampled_data = fix_new_entropy_on_union(cur_dir, now, opt, model, device, res.data, test_for_res, len(test_for_res[0]) // opt.win_size, pre_max_item, pre_max_user, ent='wass') # cast the sampled set to dataset sampled_dataset = MultiSessionsGraph(cur_dir + '/../datasets/' + opt.dataset, phrase='sampled' + now, sampled_data=sampled_data) sampled_loader = DataLoader(sampled_dataset, batch_size=opt.batch_size, shuffle=True) # update with the sampled set forward(model, sampled_loader, device, writer, test_epoch + opt.epoch, optimizer=optimizer, train_flag=True, max_item_id=max_item, last_update=updates_count) updates_count += len(test_loader) scheduler.step() res.update(test_for_res) os.remove('../datasets/' + opt.dataset + '/processed/sampled' + now + '.pt')
return u def zerogen(): def zero(): return 0 return zero # perturbation size = initial separation gamma0 = 10**-8 ITERATIONS = 400 # matrix and initial activation randomization r = Reservoir(p, q, r) r.randomize_matrices(norm) r.WI = random_matrix(q, p + 1, unigen(-0.1, 0.1)) r.W = random_matrix(q, q + 1, normgen(0, 10**(-0.8))) r.randomize_vectors(normgen(0, 1)) # make a copy of the reservoir r2 = copy.deepcopy(r) #perturbation = random_vector(q) #perturbation = times(perturbation, gamma0 / vector_len(perturbation)) perturbation = [0] * q perturbation[randrange(q)] = gamma0 #print(perturbation) r2.X = plus(r2.X, perturbation)
def print_state(self): print('Storage\tRelease\tInflows\tSpillage') str_format = '\t'.join(['{:7.2f}'for i in range(4)]) print(str_format.format(self.storage, self.release, self.inflows, self.spillage)) print('-' * 79) ### Traits listeners ########### def _release_changed(self, new): """When the release is higher than zero, warn all the inhabitants of the valley. """ if new > 0: print('Warning, we are releasing {} hm3 of water'.format(new)) if __name__ == '__main__': projectA = Reservoir( name = 'Project A', max_storage = 30, max_release = 100.0, hydraulic_head = 60, efficiency = 0.8 ) state = ReservoirState(reservoir=projectA, storage=10) state.release = 90 state.inflows = 0 state.print_state()
"2D_convection_datasets/x_y_temperature_deltaT_10.npy", n_repeat=1) train_data = np.concatenate( (train_data_2, train_data_4, train_data_6, train_data_8, train_data_10)) n_sequence, sequence_length, spatial_points = train_data.shape b = Reservoir(n_res=2000, res_scale=1, res_encoding='phase', res_enc_param=1.5 * np.pi, input_scale=1, input_encoding='phase', input_enc_param=1.5 * np.pi, random_projection='simulation', weights_type='complex gaussian', activation_fun='intensity', activation_param=10, parallel_runs=n_sequence, bias_scale=0.2, leak_rate=0.15, pred_horizon=1, rec_pred_steps=1, forget=50, train_method='ridge', train_param=1e3, verbose=1) params = [{ 'res_scale': np.array([.5, 1, 2]), 'input_scale': np.array([.5, 1, 2]), 'bias_scale': np.array([0.1, 0.5, 1]), 'train_param': np.array([1e-2, 1e2, 1e4]), 'leak_rate': np.array([0.1, 0.3]),
def ljapexp(q=150, iterations=40, gamma0=10**-8, sigma=0.1): # input, hidden and output layer size p = 1 #q = 150 r = 1 #300 # strength of input activations INPUT_STRENGTH = 10**0 def input_dist(): return INPUT_STRENGTH*uniform(-1,1) def normgen(mu, sigma): def n(): return normalvariate(mu, sigma) return n def unigen(a,b): def u(): return uniform(a,b) return u def zerogen(): def zero(): return 0 return zero # matrix and initial activation randomization r = Reservoir(p, q, r) r.randomize_matrices(normgen(0,1)) r.WI = random_matrix(q,p+1, unigen(-0.1,0.1)) r.W = random_matrix(q,q+1, normgen(0,sigma)) #r.randomize_vectors(normgen(0,1)) # make a copy of the reservoir r2 = copy.deepcopy(r) #perturbation = random_vector(q) #perturbation = times(perturbation, gamma0 / vector_len(perturbation)) priemersum = 0 for perturbed_node in range(q): r.randomize_vectors(normgen(0,1)) perturbation = [0 for _ in range(q)] perturbation[perturbed_node] = gamma0 r2.X = plus(r2.X, perturbation) difvec = plus(times(r.X, -1), r2.X) exponentsum = 0 lambdy = [0]*iterations for it in range(0, iterations): # introduce a random input r.I = random_vector(r.p, input_dist) r2.I = r.I # run one setp r.fire() r2.fire() # calculate the difference difvec = plus(times(r.X, -1), r2.X) gammaK = vector_len(difvec) # renormalize r2.X = plus(r.X, times(difvec, gamma0 / gammaK)) # record lambda mylog = math.log(gammaK / gamma0) lambdy[it] = mylog priemer = sum(lambdy) / iterations disperzia = sum([ (x-priemer)*(x-priemer) for x in lambdy]) / iterations priemersum += priemer print("\rnodeindex=%d" % perturbed_node, end="") return priemersum / q
class RC_model(object): def __init__( self, # reservoir reservoir=None, n_internal_units=None, spectral_radius=None, leak=None, connectivity=None, input_scaling=None, noise_level=None, n_drop=None, bidir=False, circle=False, # dim red dimred_method=None, n_dim=None, # representation mts_rep=None, w_ridge_embedding=None, # readout readout_type=None, w_ridge=None, mlp_layout=None, num_epochs=None, w_l2=None, nonlinearity=None, svm_gamma=1.0, svm_C=1.0, ): """ Build and evaluate a RC-based classifier. The training and test MTS are multidimensional arrays of shape [N,T,V], with - N = number of samples - T = number of time steps in each sample - V = number of variables in each sample Training and test labels have shape [N,C], with C the number of classes The dataset consists of: X, Y = training data and respective labels Xte, Yte = test data and respective labels Reservoir parameters: reservoir = precomputed reservoir (oject of class 'Reservoir'); if None, the following structural hyperparameters must be specified n_internal_units = processing units in the reservoir spectral_radius = largest eigenvalue of the reservoir matrix of connection weights leak = amount of leakage in the reservoir state update (optional) connectivity = percentage of nonzero connection weights input_scaling = scaling of the input connection weights noise_level = deviation of the Gaussian noise injected in the state update n_drop = number of transient states to drop bidir = use a bidirectional reservoir (True or false) Dimensionality reduction parameters: dimred_method = procedure for reducing the number of features in the sequence of reservoir states; possible options are: None (no dimensionality reduction), 'pca' or 'tenpca' n_dim = number of resulting dimensions after the dimensionality reduction procedure Representation parameters: mts_rep = type of MTS representation. It can be 'last' (last state), 'output' (output model space), or 'reservoir' (reservoir model space) w_ridge_embedding = regularization parameter of the ridge regression in the output model space and reservoir model space representation; ignored if mts_rep == None Readout parameters: readout_type = type of readout used for classification. It can be 'lin' (ridge regression), 'mlp' (multiplayer perceptron), 'svm' (support vector machine), or None. If None, the input representations will be saved instead: this is useful for clustering and visualization. w_ridge = regularization parameter of the ridge regression readout (only for readout_type=='lin') mlp_layout = tuple with the sizes of MLP layers, e.g. (20, 10) defines a MLP with 2 layers of 20 and 10 units respectively. (only for readout_type=='mlp') num_epochs = number of iterations during the optimization (only for readout_type=='mlp') w_l2 = weight of the L2 regularization (only for readout_type=='mlp') nonlinearity = type of activation function {'relu', 'tanh', 'logistic', 'identity'} (only for readout_type=='mlp') svm_gamma = bandwith of the RBF kernel (only for readout_type=='svm') svm_C = regularization for SVM hyperplane (only for readout_type=='svm') """ self.n_drop = n_drop self.bidir = bidir self.dimred_method = dimred_method self.mts_rep = mts_rep self.readout_type = readout_type self.svm_gamma = svm_gamma # Initialize reservoir if reservoir is None: self._reservoir = Reservoir(n_internal_units=n_internal_units, spectral_radius=spectral_radius, leak=leak, connectivity=connectivity, input_scaling=input_scaling, noise_level=noise_level, circle=circle) else: self._reservoir = reservoir # Initialize dimensionality reduction method if dimred_method is not None: if dimred_method.lower() == 'pca': self._dim_red = PCA(n_components=n_dim) elif dimred_method.lower() == 'tenpca': self._dim_red = tensorPCA(n_components=n_dim) else: raise RuntimeError('Invalid dimred method ID') # Initialize ridge regression model if mts_rep == 'output' or mts_rep == 'reservoir': self._ridge_embedding = Ridge(alpha=w_ridge_embedding, fit_intercept=True) # Initialize readout type if self.readout_type is not None: if self.readout_type == 'lin': # Ridge regression self.readout = Ridge(alpha=w_ridge) elif self.readout_type == 'svm': # SVM readout self.readout = SVC(C=svm_C, kernel='precomputed') elif readout_type == 'mlp': # MLP (deep readout) # pass self.readout = MLPClassifier( hidden_layer_sizes=mlp_layout, activation=nonlinearity, alpha=w_l2, batch_size=32, learning_rate='adaptive', # 'constant' or 'adaptive' learning_rate_init=0.001, max_iter=num_epochs, early_stopping=False, # if True, set validation_fraction > 0 validation_fraction=0.0 # used for early stopping ) else: raise RuntimeError('Invalid readout type') def train(self, X, Y=None): time_start = time.time() # ============ Compute reservoir states ============ res_states = self._reservoir.get_states(X, n_drop=self.n_drop, bidir=self.bidir) # ============ Dimensionality reduction of the reservoir states ============ if self.dimred_method.lower() == 'pca': # matricize N_samples = res_states.shape[0] res_states = res_states.reshape(-1, res_states.shape[2]) # ..transform.. red_states = self._dim_red.fit_transform(res_states) # ..and put back in tensor form red_states = red_states.reshape(N_samples, -1, red_states.shape[1]) elif self.dimred_method.lower() == 'tenpca': red_states = self._dim_red.fit_transform(res_states) else: # Skip dimensionality reduction red_states = res_states # ============ Generate representation of the MTS ============ coeff_tr = [] biases_tr = [] # Output model space representation if self.mts_rep == 'output': if self.bidir: X = np.concatenate((X, X[:, ::-1, :]), axis=2) for i in range(X.shape[0]): self._ridge_embedding.fit(red_states[i, 0:-1, :], X[i, self.n_drop + 1:, :]) coeff_tr.append(self._ridge_embedding.coef_.ravel()) biases_tr.append(self._ridge_embedding.intercept_.ravel()) input_repr = np.concatenate( (np.vstack(coeff_tr), np.vstack(biases_tr)), axis=1) # Reservoir model space representation elif self.mts_rep == 'reservoir': for i in range(X.shape[0]): self._ridge_embedding.fit(red_states[i, 0:-1, :], red_states[i, 1:, :]) coeff_tr.append(self._ridge_embedding.coef_.ravel()) biases_tr.append(self._ridge_embedding.intercept_.ravel()) input_repr = np.concatenate( (np.vstack(coeff_tr), np.vstack(biases_tr)), axis=1) # Last state representation elif self.mts_rep == 'last': input_repr = red_states[:, -1, :] # Mean state representation elif self.mts_rep == 'mean': input_repr = np.mean(red_states, axis=1) else: raise RuntimeError('Invalid representation ID') # ============ Apply readout ============ if self.readout_type == None: # Just store the input representations self.input_repr = input_repr elif self.readout_type == 'lin': # Ridge regression self.readout.fit(input_repr, Y) elif self.readout_type == 'svm': # SVM readout Ktr = squareform(pdist(input_repr, metric='sqeuclidean')) Ktr = np.exp(-self.svm_gamma * Ktr) self.readout.fit(Ktr, np.argmax(Y, axis=1)) self.input_repr_tr = input_repr # store them to build test kernel elif self.readout_type == 'mlp': # MLP (deep readout) self.readout.fit(input_repr, Y) tot_time = (time.time() - time_start) / 60 return tot_time def test(self, Xte, Yte): # ============ Compute reservoir states ============ res_states_te = self._reservoir.get_states(Xte, n_drop=self.n_drop, bidir=self.bidir) # ============ Dimensionality reduction of the reservoir states ============ if self.dimred_method.lower() == 'pca': # matricize N_samples_te = res_states_te.shape[0] res_states_te = res_states_te.reshape(-1, res_states_te.shape[2]) # ..transform.. red_states_te = self._dim_red.transform(res_states_te) # ..and put back in tensor form red_states_te = red_states_te.reshape(N_samples_te, -1, red_states_te.shape[1]) elif self.dimred_method.lower() == 'tenpca': red_states_te = self._dim_red.transform(res_states_te) else: # Skip dimensionality reduction red_states_te = res_states_te # ============ Generate representation of the MTS ============ coeff_te = [] biases_te = [] # Output model space representation if self.mts_rep == 'output': if self.bidir: Xte = np.concatenate((Xte, Xte[:, ::-1, :]), axis=2) for i in range(Xte.shape[0]): self._ridge_embedding.fit(red_states_te[i, 0:-1, :], Xte[i, self.n_drop + 1:, :]) coeff_te.append(self._ridge_embedding.coef_.ravel()) biases_te.append(self._ridge_embedding.intercept_.ravel()) input_repr_te = np.concatenate( (np.vstack(coeff_te), np.vstack(biases_te)), axis=1) # Reservoir model space representation elif self.mts_rep == 'reservoir': for i in range(Xte.shape[0]): self._ridge_embedding.fit(red_states_te[i, 0:-1, :], red_states_te[i, 1:, :]) coeff_te.append(self._ridge_embedding.coef_.ravel()) biases_te.append(self._ridge_embedding.intercept_.ravel()) input_repr_te = np.concatenate( (np.vstack(coeff_te), np.vstack(biases_te)), axis=1) # Last state representation elif self.mts_rep == 'last': input_repr_te = red_states_te[:, -1, :] # Mean state representation elif self.mts_rep == 'mean': input_repr_te = np.mean(red_states_te, axis=1) else: raise RuntimeError('Invalid representation ID') # ============ Apply readout ============ if self.readout_type == 'lin': # Ridge regression logits = self.readout.predict(input_repr_te) pred_class = np.argmax(logits, axis=1) elif self.readout_type == 'svm': # SVM readout Kte = cdist(input_repr_te, self.input_repr_tr, metric='sqeuclidean') Kte = np.exp(-self.svm_gamma * Kte) pred_class = self.readout.predict(Kte) elif self.readout_type == 'mlp': # MLP (deep readout) pred_class = self.readout.predict(input_repr_te) pred_class = np.argmax(pred_class, axis=1) accuracy, f1 = compute_test_scores(pred_class, Yte) return accuracy, f1
window = 3 delay = 1 dataStreamLength = 10 trainingSize = 150 testSize = 150 O = 2**(2**window) seed = 0 np.random.seed(seed) bn_directory = os.getcwd() + '/BN_realization/' directory = os.getcwd() + '/' print(os.getcwd()) varF, F, init = bn.getRandomParameters(N + I, K, isConstantConnectivity=False) res = Reservoir(I, L, bn_directory + 'time_series_data_3.csv', directory + 'test_2018-08-03.csv', N, varF, F, init) print('reservoir initialized') functionInputs = [f_utils.getInputTuple(window) for i in range(O)] functionsToApproximate, functionVectors = [], [] for i in range(O): function_vector = f_utils.convertIntToBinaryVector(i, 2**window) func = f_utils.convertVectorToFunction(function_vector) functionsToApproximate.append(func) # Only for printing purposes functionVectors.append(function_vector) output = OutputLayer(res, O, functionsToApproximate, functionInputs, delay, dataStreamLength) output.train(trainingSize)
func = f_utils.convertVectorToFunction(function_vector) functionsToApproximate.append(func) # Test parameter set data = np.zeros((len(N_list), len(L_list))) for i in range(len(N_list)): for j in range(len(L_list)): L = L_list[j] * N_list[i] // 100 # Initialize reservoir bn_directory = os.getcwd() + '/../BN_realization/' directory = os.getcwd() + '/../output/' varF, F, init = bn.getRandomParameters(N_list[i] + I, K + (L / N_list[i]), isConstantConnectivity=False) res = Reservoir(I, L, bn_directory + 'time_series_data_3.csv', directory + 'recursive_funs_'+str(seed)+'.csv', N_list[i], varF, F, init) # Train and test output layer output = OutputLayer(res, O, functionsToApproximate, functionInputs, delay, dataStreamLength, nonRecursiveArgs=recursiveArgs) output.train(trainingSize) output.test(testSize) # add results to data data[i, j] = sum([output.successRates[k] for k in range(O)]) / O #for k in range(O) : # print("{0:d},{1:.3f},{2:d},{3:d},{4:d},{5:.5f}".format( seed,K,N_list[i],L_list[j],k,output.successRates[k] )) time = int(time.clock() - start)