def run(res, trn):
    esn = pyESN.ESN(n_inputs=1,
                    n_outputs=3,
                    n_reservoir=res,
                    spectral_radius=0.9,
                    sparsity=0.002,
                    random_state=42)

    spin_off = 0
    trainN = trn
    testN = 2000
    sol_matrix = np.zeros([testN, 3])
    np.shape(sol_matrix)

    esn.fit(np.ones(trainN - spin_off), dataT[spin_off:trainN, :])

    prediction = esn.predict(np.ones(testN))

    error_mat = prediction - dataT[trainN:trainN + testN]
    horizon = 0
    while (horizon < testN) and \
      (abs(error_mat[horizon, 0]) < 0.5) and \
      (abs(error_mat[horizon, 1]) < 0.5) and \
      (abs(error_mat[horizon, 2]) < 0.5):
        horizon += 1

    return (np.sum(np.sum(np.square(error_mat[:horizon])))) / float(
        np.sum(np.sum(np.square(dataT[trainN:trainN + horizon])))), horizon
Ejemplo n.º 2
0
    def __init__(self, input_dim, output_dim=1):

        self.input_dim = input_dim

        self.ESN = pyESN.ESN(n_inputs=input_dim,
                             n_outputs=output_dim,
                             n_reservoir=50,
                             sparsity=0.9,
                             spectral_radius=1,
                             noise=5,
                             input_scaling=True,
                             teacher_forcing=True,
                             random_state=42)
Ejemplo n.º 3
0
import matplotlib

matplotlib.use('Agg')
import pyESN
import matplotlib.pyplot as plt
import numpy as np
from numpy import genfromtxt

data = genfromtxt('lorenz_data_F8_new_std.csv', delimiter=",")
dataT = np.transpose(data)
np.shape(dataT)

esn = pyESN.ESN(n_inputs=1,
                n_outputs=1,
                n_reservoir=5000,
                spectral_radius=1.5,
                random_state=42)

trainN = 50000
testN = 2000
sol_matrix = np.zeros([testN, 40])
np.shape(sol_matrix)

for ptr in range(0, 40):
    pred_training = esn.fit(np.ones(trainN), dataT[0:trainN, ptr])
    prediction = esn.predict(np.ones(testN))
    print("test error: \n" + str(
        np.sqrt(
            np.mean((prediction.flatten() -
                     dataT[trainN:trainN + testN, ptr])**2))))
Ejemplo n.º 4
0
def run(res, spr, reg):
    esn = pyESN.ESN(n_inputs=1,
                    n_outputs=4,
                    n_reservoir=res,
                    spectral_radius=spr,
                    sparsity=0.002,
                    reg=reg,
                    random_state=42)

    spin_off = 2000
    trainN = 2200
    testN = 2000
    sol_matrix = np.zeros([testN, 64])

    count = 0
    for ptr in range(0, 16):
        pred_training = esn.fit(np.ones(trainN - spin_off),
                                dataT[spin_off:trainN, count:count + 4])
        prediction = esn.predict(np.ones(testN))
        sol_matrix[:, count:count + 4] = (prediction)
        count = count + 4

    plt.figure(figsize=(15, 15))
    for ptr in range(0, 32):
        plt.subplot(8, 4, ptr + 1)
        plt.plot(range(trainN - 1000, trainN + testN),
                 dataT[trainN - 1000:trainN + testN, ptr],
                 'k',
                 label="target system")
        plt.plot(range(trainN, trainN + testN),
                 sol_matrix[:, ptr],
                 'y',
                 label="free running ESN")
        lo, hi = plt.ylim()
        plt.plot([trainN, trainN], [lo + np.spacing(1), hi - np.spacing(1)],
                 'k:')
        plt.legend(loc=(0.61, 1.1), fontsize='x-small')
        plt.ylabel('grid point ' + str(ptr), fontsize=14)

    plt.savefig('time_series_' + str(res) + '_' + str(spr) + '_' + str(reg) +
                '.png')

    [t, grid] = np.meshgrid((np.arange(trainN, trainN + testN)),
                            np.arange(0, 64))

    diff_mat = dataT[trainN:trainN + testN, :] - sol_matrix[0:testN, :]
    v = np.linspace(-3, 3, 10, endpoint=True)

    plt.figure(figsize=(45, 45))
    plt.subplot(3, 1, 1)
    v = np.linspace(-3, 3, 10, endpoint=True)
    plt.contourf(np.transpose(t),
                 np.transpose(grid),
                 dataT[trainN:trainN + testN, :],
                 cmap=cm.jet,
                 vmin=-3,
                 vmax=3)

    plt.colorbar(ticks=v)
    plt.rcParams['xtick.labelsize'] = 40
    plt.rcParams['ytick.labelsize'] = 40
    plt.xlabel('$dt$', fontsize=40)
    plt.ylabel('Grid Point', fontsize=40)
    plt.title('Truth', fontsize=40)

    plt.subplot(3, 1, 2)

    plt.contourf(np.transpose(t),
                 np.transpose(grid),
                 sol_matrix[0:testN, :],
                 cmap=cm.jet,
                 vmin=-3,
                 vmax=3)
    plt.colorbar(ticks=v)
    #cbar.ax.set_ylabel('verbosity coefficient')
    #plt.clim(-3,3)

    plt.rcParams['xtick.labelsize'] = 40
    plt.rcParams['ytick.labelsize'] = 40
    plt.xlabel('$dt$', fontsize=40)
    plt.ylabel('Grid Point', fontsize=40)
    plt.title('Prediction', fontsize=40)

    plt.subplot(3, 1, 3)
    plt.contourf(np.transpose(t),
                 np.transpose(grid),
                 diff_mat,
                 cmap=cm.jet,
                 vmin=-3,
                 vmax=3)
    plt.colorbar(ticks=v)
    #cbar.ax.set_ylabel('verbosity coefficient')
    plt.rcParams['xtick.labelsize'] = 40
    plt.rcParams['ytick.labelsize'] = 40
    plt.xlabel('$dt$', fontsize=40)
    plt.ylabel('Grid Point', fontsize=40)
    plt.title('Error Matrix', fontsize=40)

    plt.savefig('contourplots_' + str(res) + '_' + str(spr) + '_' + str(reg) +
                '.png')
Ejemplo n.º 5
0
data = genfromtxt('lorenz_data_F8_new_std.csv', delimiter=",")
dataT = np.transpose(data)
np.shape(dataT)

# trainN = 50000
trainN = 30000  # lover number for debugging on laptop
# testN = 2000
testN = 600  # lover number for debugging on laptop
sol_matrix = np.zeros([testN, 40])
np.shape(sol_matrix)

for ptr in range(0, 40):
    esn = pyESN.ESN(
        n_inputs=1,
        n_outputs=1,
        # n_reservoir=5000,
        n_reservoir=160,  # lover number for debugging on laptop
        spectral_radius=0.9,
        sparsity=0.002,
        random_state=42)
    pred_training = esn.fit(np.ones(trainN), dataT[0:trainN, ptr])
    prediction = esn.predict(np.ones(testN))
    print(
        ptr, "test error: \n" + str(
            np.sqrt(
                np.mean(
                    (prediction.flatten() - dataT[trainN:trainN + testN, ptr])
                    **2))))

    sol_matrix[:, ptr] = np.transpose(prediction)

np.shape(sol_matrix)
Ejemplo n.º 6
0
if len(sys.argv) != 2:
    print("bad command line argumentas - pass the number of reservoirs")
    exit(1)

reservoirs = int(sys.argv[1])

data = genfromtxt('lorenz_63_modified.csv', delimiter=",")
dataT = np.transpose(data)
dataT = np.transpose(dataT)
# dataT=dataT[2500:,:]
np.shape(dataT)

esn = pyESN.ESN(n_inputs=1,
                n_outputs=3,
                n_reservoir=reservoirs,
                spectral_radius=0.9,
                sparsity=0.002,
                random_state=42)

spin_off = 0
trainN = 40000
testN = 2000
sol_matrix = np.zeros([testN, 3])
np.shape(sol_matrix)

pred_training = esn.fit(np.ones(trainN - spin_off),
                        dataT[spin_off:trainN, :],
                        inspect=True)

prediction = esn.predict(np.ones(testN))
Ejemplo n.º 7
0
import matplotlib

matplotlib.use('Agg')
import pyESN
import matplotlib.pyplot as plt
import numpy as np
from numpy import genfromtxt
from matplotlib import cm

data = genfromtxt('KS_data.csv', delimiter=",")
dataT = np.transpose(data)

esn = pyESN.ESN(n_inputs=1,
                n_outputs=4,
                n_reservoir=5000,
                spectral_radius=0.95,
                sparsity=0.002,
                reg=0.0001,
                random_state=42)

spin_off = 2000
trainN = 22000
testN = 2000
sol_matrix = np.zeros([testN, 64])

count = 0
for ptr in range(0, 16):
    pred_training = esn.fit(np.ones(trainN - spin_off), dataT[spin_off:trainN,
                                                              count:count + 4])
    prediction = esn.predict(np.ones(testN))
    sol_matrix[:, count:count + 4] = (prediction)