コード例 #1
0
 def __init__(self,
              eta=0.9,
              gamma=0.0,
              beta=1.0,
              theta=0.2,
              tau=400,
              N=400,
              power=7,
              fudge=1.0):
     DelayReservoir.__init__(self,
                             eta=0.9,
                             gamma=0.0,
                             beta=1.0,
                             theta=0.2,
                             tau=400,
                             N=400,
                             power=7,
                             fudge=fudge)
     self.eta = eta
     self.gamma = gamma
     self.beta = beta
     self.tau = tau
     self.N = N
     self.power = power
     self.fudge = fudge
コード例 #2
0
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
コード例 #3
0
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))]
コード例 #4
0
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()
コード例 #5
0
def NARMA_Test(test_length=800,
               train_length=800,
               num_loops=1,
               a=0,
               plot=True,
               N=400,
               eta=0.4,
               gamma=0.05,
               phi=np.pi / 6,
               tau=400,
               bits=8,
               preload=False):
    """
    Args:
        test_length: length of testing data
        train_length: length of training data
        num_loops: number of delay loops in reservoir
        a: ridge regression parameter
        N: number of virtual nodes
        plot: display calculated time series
        gamma: input gain
        eta: oscillation strength
        phi: phase of MZN
        r: loop delay length 
        bits: bit precision
        preload: preload mask and time-series data

    Returns:
        NRMSE: Normalized Root Mean Square Error
    """

    #Import u and m
    if preload:
        file1 = open("data/Input_sequence.txt", "r")
        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)
    #Randomly initialize u and m
    else:
        u = np.random.rand(train_length + test_length) / 2.
        m = np.array(
            [random.choice([-0.1, 0.1]) for i in range(N // num_loops)])

    #Calculate NARMA10 target
    target = NARMA_Generator(len(u), u)

    #Instantiate Reservoir, feed in training and verification datasets
    r1 = DelayReservoir(N=N // num_loops,
                        eta=eta,
                        gamma=gamma,
                        theta=0.2,
                        loops=num_loops,
                        phi=phi)
    x = r1.calculateMZNBit(u[:train_length], m, bits)
    #x_ideal = r1.calculateMZN(u[:train_length],m)
    x_test = r1.calculateMZNBit(u[train_length:], m, bits)
    #x_test_ideal = r1.calculateMZN(u[train_length:],m)

    #Train using Ridge Regression
    #clf = RidgeCV(alphas = a,fit_intercept = True)
    clf = Ridge(alpha=a)
    clf.fit(x, target[:train_length])
    y_test = clf.predict(x_test)
    y_input = clf.predict(x)

    #Calculate NRMSE
    NRMSE = np.sqrt(np.mean(np.square(y_test[50:]-target[train_length+50:]))/\
            np.var(target[train_length+50:]))

    NRMSEi = np.sqrt(np.mean(np.square(y_input-target[:train_length]))/\
            np.var(target[:train_length]))

    #Write to File
    '''
    x_total = np.concatenate((x,x_test))
    x_total = x_total.flatten(order='C')
    file1 = open("data/64_bit_test_x.txt","w+")
    file2 = open("data/64_bit_test_y.txt","w+")
    for i in range(2*320000):
        file1.write("%f"%x_total[i]+"\n")
        if(i < 1600):
            file2.write("%f"%target[i]+"\n")
    file1.close()
    '''

    #Plot predicted Time Series
    if (plot == True):
        #fig, (ax1,ax2) = plt.subplots(2,1)
        #ax1.plot(x.flatten()[5000:])
        #ax2.plot(x_ideal.flatten()[5000:])
        #plt.plot(x.flatten()[:1200])
        plt.plot(y_test[50:], label='Prediction')
        plt.plot(target[train_length + 50:], label='Target')
        plt.title('NRMSE = %f' % NRMSE)
        plt.legend()
        plt.show()

    return NRMSE
コード例 #6
0
def NARMA_Test_Compare(test_length=200,
                       train_length=800,
                       num_loops=1,
                       a=0,
                       plot=True,
                       N=400,
                       eta=0.5,
                       gamma=1,
                       phi=np.pi / 4,
                       r=1):
    """
    Compare with pre-determined NARMA10 series

    Args:
        test_length: length of verification data
        train_length: length of training data
        num_loops: number of delay loops in reservoir
        a: list of ridge regression constants for hyperparameter tuning
        N: number of virtual nodes
        plot: display calculated time series
        gamma: input gain
        eta: oscillation strength
        phi: phase of MZN
        r: loop length ratio

    Returns:
        NRMSE: Normalized Root Mean Square Error
    """

    #Import u and m
    file1 = open("data/uin_and_target.txt", "r")
    file2 = open("data/Mask.txt", "r")
    contents = file1.readlines()
    contents2 = file2.readlines()
    u = []
    target = []
    m = []
    for i in range(1000):
        u.append(float(contents[i][0:contents[i].find("\t")]))
        target.append(float(contents[i][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)
    target = np.array(target)

    #Instantiate Reservoir, feed in training and verification datasets
    r1 = DelayReservoir(N=N // num_loops,
                        eta=eta,
                        gamma=gamma,
                        theta=0.2,
                        loops=num_loops,
                        phi=phi)
    x = r1.calculateMZN(u[:train_length], m)
    x_test = r1.calculateMZN(u[train_length:], m)

    x = []
    file3 = open("data/X_node.txt", "r")
    contents3 = file3.readlines()
    print(len(contents3))
    for i in range(400000):
        x.append(float(contents3[i][:contents3[i].find("\n")]))

    x = np.array(x)
    x = x.reshape((-1, 1))
    x = x.reshape((1000, 400))

    #Train using Ridge Regression
    clf = Ridge(alpha=a, fit_intercept=True)
    clf.fit(x[:800], target[:train_length])
    w = clf.coef_
    y_train = x @ w
    y_test = clf.predict(x[800:])

    #Write to file

    x_total = np.concatenate((x, x_test))
    x_total = x_total.flatten(order='C')
    file3 = open("data/y_train2.txt", "w+")
    for i in range(800):
        file3.write("%f" % y_train[i] + "\n")
    file3.close()

    #Calculate NRMSE
    NRMSE = np.sqrt(np.mean(np.square(y_test[50:]-target[train_length+50:]))/\
            np.var(target[train_length+50:]))

    #Plot predicted Time Series

    if (plot == True):
        plt.plot(y_test[50:], label='Prediction')
        plt.plot(target[train_length + 50:], label='Target')
        plt.title('NRMSE = %f' % NRMSE)
        plt.legend()
        plt.show()

    return NRMSE
コード例 #7
0
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