def Bif_Test(test_length=800, train_length=800, N=400, eta=0.4, tau=400, bits=np.inf, preload=False, write=False, mask=0.1, activate='mg', beta=1.0, t=1, theta=0.2, hayes_p=1, power=1): """ Args: test_length: length of testing data train_length: length of training data a: ridge regression parameter N: number of virtual high_nodes plot: display calculated time series gamma: input gain eta: oscillation strength bits: bit precision preload: preload mask and time-series data mask: amplitude of mask values activate: activation function to be used (sin**2,tanh,mg) cv: perform leave-one-out cross validation beta: driver gain t: timestep used to solve diffeq Returns: NRMSE: Normalized Root Mean Square Error """ # Import u and m u, m, _ = load_NARMA(preload, train_length, test_length, mask, N) # Instantiate Reservoir, feed in training and predictiondatasets r1 = DelayReservoir(N=N, eta=eta, gamma=0, theta=theta, beta=beta, tau=tau, fudge=hayes_p, power=power) x = r1.calculate(u[:train_length], m, bits, t, activate) x_max = np.max(x) x_min = np.min(x) return x_min, x_max
def Classification_Test(N=400, eta=0.35, gamma=0.05, tau=400, bits=np.inf, num_waves=1000, test_size=0.1, preload=False, write=False, mask=0.1, activate='mg', beta=1.0, power=7, t=1, theta=0.2): """ Args: N: number of virtual high_nodes gamma: input gain eta: oscillation strength tau: loop delay length bits: bit precision preload: preload time-series data write: save created time-series data mask: amplitude of mask values activate: activation function to be used (sin**2,tanh,mg) beta: driver gain t: timestep used to solve diffeq power: exponent for MG equation theta: distance between virtual high_nodes Returns: Accuracy of Ridge Model on Testing Data """ X_train, X_test, y_train, y_test = make_training_testing_set(num_waves=num_waves, test_percent=test_size, preload=preload, write=write) clf = RidgeClassifier(alpha=0) m = np.array([random.choice([-mask, mask]) for i in range(N)]) # Instantiate Reservoir, feed in training and prediction data sets r1 = DelayReservoir(N=N, eta=eta, gamma=gamma, theta=theta, beta=beta, tau=tau, power=power) Xs = [X_train, X_test] new_Xs = [[], []] for k, data in enumerate(Xs): for idx in tqdm(range(len(data))): new_Xs[k].append(np.array(r1.calculate(data[idx], m, bits, t, activate))[:, -1]) new_Xs = np.array(new_Xs) clf.fit(new_Xs[0], y_train) return [clf.score(new_Xs[1], y_test), np.mean(margin(clf, X_test))]
def mg_hayes_comp(): """ Function to compare the x(t)'s for optimal parameters for Mackey-Glass and Hayes """ # Set large parameters for calculate t = 1 bits = np.inf train_length = 800 # Import data file1 = open("Data/Input_sequence.txt", "r") # Reads input and masking files. stores them in u/m file2 = open("Data/mask_2.txt", "r") contents = file1.readlines() contents2 = file2.readlines() u = [] m = [] for i in range(1000): u.append(float(contents[i][0:contents[i].find("\t")])) if i < 400: m.append(float(contents2[i][0:contents2[i].find("\n")])) file1.close() file2.close() u = np.array(u) m = np.array(m) ### MG portion, collect output activate = 'mg' r1 = DelayReservoir(N=400, eta=1, gamma=0.05, theta=0.2, beta=1, tau=400) x_mg, vn_mg = r1.calculate(u[:train_length], m, bits, t, activate, no_act_res=True) # Takes the actual output # x_mg_vn = r1.calculate(u[:train_length], m, bits, t, activate)[1] # Hayes portion, collect output activate = 'hayes' m = np.random.choice([0.1, -0.1], [1, hayes_N]) m = m.reshape(hayes_N, ) x_hayes, vn_hayes = r1.calculate(u, m, bits, t, activate, no_act_res=True) # Flatten the values x_mg = x_mg.flatten() x_hayes = x_hayes.flatten() vn_mg = vn_mg.flatten() vn_hayes = vn_hayes.flatten() # Create a copy of pre-loaded narma sequence for both hayes and mg runs u = np.reshape(u, (-1, 1)) m1 = np.reshape(m1, (1, -1)) m = np.reshape(m, (1, -1)) masked_narma_mg = u @ m1 masked_narma_mg = masked_narma_mg.flatten() masked_narma_hayes = u @ m masked_narma_hayes = masked_narma_hayes.flatten() # Create x-axis cycles = len(u) axis_mg = np.linspace(0, cycles, mg_N * len(u)) axis_hayes = np.linspace(0, cycles, hayes_N * len(u)) # Plot the data plt.figure(1) plt.plot(axis_mg, x_mg, label="Mackey-Glass") plt.plot(axis_hayes, x_hayes, label="Hayes") plt.xlabel("nth NARMA Input") plt.title("Mg vs Hayes Ouput given same NARMA Input (ind. optimal param)") plt.legend() plt.figure(2) plt.plot(axis_mg, masked_narma_mg, label="Masked Narma Input") plt.plot(axis_mg, x_mg, label="Mackey-Glass") # plt.plot(x_hayes, label="Hayes") plt.xlabel("nth NARMA Input") plt.title("Mg Ouput given NARMA Input (ind. optimal param)") plt.legend() plt.figure(3) plt.plot(axis_hayes, masked_narma_hayes, label="Masked Narma Input") plt.plot(axis_hayes, x_hayes, label="Hayes") # plt.plot(x_hayes, label="Hayes") plt.xlabel("nth NARMA Input") plt.title("Hayes Ouput given NARMA Input (ind. optimal param)") plt.legend() plt.show()
def NARMA_Test(test_length=500, train_length=500, plot=False, N=400, eta=0.4, gamma=0.05, tau=400, fudge=1.0, preload=False, write=False, mask=0.1, activate='mg', cv=True, beta=1.0, t=1, theta=0.2, power=1.0): """ Args: test_length: length of testing data train_length: length of training data N: number of virtual high_nodes plot: display calculated time series gamma: input gain eta: oscillation strength bits: bit precision preload: preload mask and time-series data mask: amplitude of mask values activate: activation function to be used (sin**2,tanh,mg) cv: perform leave-one-out cross validation beta: driver gain t: timestep used to solve diffeq, theta: distance between virtual high_nodes in time Returns: NRMSE: Normalized Root Mean Square Error """ if activate == "Hayes" and self.x_t_term / self.eta: NRSME = 1 x_test_bot = 0 return NRSME, x_test_bot # Should the parameters be those that put Hayes in unstable territory, # Import u, m, and target u, m, target = load_NARMA(preload, train_length, test_length, mask, N) # Instantiate Reservoir, feed in training and predictiondatasets r1 = DelayReservoir(N=N, eta=eta, gamma=gamma, theta=theta, beta=beta, tau=tau, fudge=fudge, power=power) x = r1.calculate(u[:train_length], m, t, activate) # Is this correct? It looks like x_test and x_test_bot are defined as the same thing x_test = r1.calculate(u[train_length:], m, t, activate) x_test_bot = r1.calculate(u[train_length:], m, t, activate) # Train using Ridge Regression with hyperparameter tuning if cv: NRMSE, y_test, y_input = cross_validate(alphas=np.logspace(-20, 5, 16), x=x, x_test=x_test, target=target) else: clf = Ridge(alpha=0) clf.fit(x, target[:train_length]) y_test = clf.predict(x_test) # Calculate NRMSE of prediction data NRMSE = np.sqrt( np.mean(np.square(y_test[50:] - target[train_length + 50:])) / np.var(target[train_length + 50:])) # Write to File if write: write_func(x, x_test) if not no_act_res: x_test_bot = 0 # If I don't want to find the x(t)-x(t-tau) term, set flag before plotting # Plot predicted Time Series if plot: plot_func(x, x_test_bot, u, y_test, target, NRMSE, train_length, N) return NRMSE, x_test_bot