def mainFunction():
    data = generate_data(ndata, 20000, 50, Ngrid=N)

    training_data = data[:, :trainLength]
    test_data = data[:, trainLength:trainLength+testLength]

    sigma, sigma_skip = [(1, 1), (3, 1), (5, 1), (5, 2), (7, 1), (7, 2), (7, 3)][id-1]
    patch_radius = sigma//2
    input_size = [1, 9, 25, 9, 49, 16, 9][id-1]

    param_grid = {"n_reservoir": [50, 200, 400], "spectral_radius": [0.1, 0.5, 0.8, 0.95, 1.1, 1.5, 3.0], "leak_rate": [.05, .2, .5 , .7, .9, .95],
                  "random_seed": [42, 41, 40, 39],  "sparseness": [.1, .2], "noise_level": [0.0001, 0.00001], "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]}
    fixed_params = {"n_output": 1, "n_input": input_size, "solver": "lsqr", "weight_generation": "advanced"}

    gridsearch = GridSearchP(param_grid, fixed_params, esnType=ESN)

    print("start fitting...")

    sys.stdout.flush()
    results = gridsearch.fit(
        training_data[0, :, N//2-patch_radius:N//2+patch_radius+1, N//2-patch_radius:N//2+patch_radius+1][:, ::sigma_skip, ::sigma_skip].reshape((trainLength, -1)), training_data[1, :, N//2, N//2].reshape((trainLength, 1)),
        [(test_data[0, :, N//2-patch_radius:N//2+patch_radius+1, N//2-patch_radius:N//2+patch_radius+1][:, ::sigma_skip, ::sigma_skip].reshape((testLength, -1)), test_data[1, :, N//2, N//2].reshape((testLength, 1)))],
        printfreq=100, verbose=2, n_jobs=16)
    print("results:\r\n")
    print(results)
    print("")

    print("\r\nBest result (mse =  {0}):\r\n".format(gridsearch._best_mse))
    print("best parameters {0}".format(gridsearch._best_params))
def mainFunction():
    data = generate_data(ndata, 20000, 50, Ngrid=N)

    input_data = data[:-prediction_length, N//2-patch_radius:N//2+patch_radius+1, N//2-patch_radius:N//2+patch_radius+1][:, ::sigma_skip, ::sigma_skip].reshape((-1, input_size))
    output_data = data[prediction_length:, N//2, N//2].reshape((-1, 1))

    param_grid = {"n_reservoir": [50, 200, 300, 400], "spectral_radius": [0.1, 0.5, 0.8, 0.95, 1.0, 1.1, 1.2, 1.5, 1.75, 2.5, 3.0],
                  "leak_rate": [.05, .2, .5, .7, .9, .95], "random_seed": [42, 41, 40, 39], "sparseness": [.05, .1, .2],
                  "noise_level": [0.0001, 0.00001], "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6], [5e-7], [5e-8]]}
    fixed_params = {"n_output": 1, "n_input": input_size, "solver": "lsqr", "weight_generation": "advanced"}

    gridsearch = GridSearchP(param_grid, fixed_params, esnType=ESN)

    print("start fitting...")

    sys.stdout.flush()
    results = gridsearch.fit(input_data[:trainLength], output_data[:trainLength],
                             [(input_data[trainLength:trainLength+testLength], output_data[trainLength:trainLength+testLength])],
                             printfreq=100, verbose=2, n_jobs=16)
    print("results:\r\n")
    print(results)
    print("")

    print("\r\nBest result (mse =  {0}):\r\n".format(gridsearch._best_mse))
    print("best parameters {0}".format(gridsearch._best_params))
Example #3
0
def mainFunction():
    global output_weights, frame_output_weights, last_states

    data = generate_data(ndata, 20000, 50, Ngrid=N)

    training_data = data[:, :trainLength]
    test_data = data[:,trainLength:trainLength+testLength]


    sigma, sigma_skip = [(1,1), (3, 1), (5, 1), (5, 2), (7, 1), (7, 2), (7, 3)][id-1]
    patch_radius = sigma//2
    input_size = [1, 9, 25, 9, 49, 16, 9][id-1]

    """
    #approximate the input scaling using the MI for a small square
    average_mi = np.zeros(int(np.ceil(sigma/patch_radius)**2))
    mi = np.zeros_like(average_mi)
    for y in range(N//2-5, N//2+5):
        for x in range(N//2-5, N//2+5):
            #Scott's rule
            std_output = np.std(training_data[0, :, y, x])
            nbins = int(np.ceil(2.0/(3.5*std_output/np.power(trainLength, 1.0/3.0))))

            patch_data = training_data[1, :, N//2-patch_radius:N//2+patch_radius+1, N//2-patch_radius:N//2+patch_radius+1][:, ::sigma_skip, ::sigma_skip].reshape((trainLength, -1))
            for i in range(len(mi)):
                 mi[i] = calculate_mutualinformation(patch_data[:, i], training_data[0, :, y, x], nbins)
            mi = mi / np.max(mi)
            average_mi += mi
    average_mi = average_mi / (10*10)
    """

    input_scaling = None
    if (useInputScaling):
        #approximate the input scaling using the MI
        input_scaling = calculate_esn_mi_input_scaling(
                                training_data[1, :, N//2-patch_radius:N//2+patch_radius+1, N//2-patch_radius:N//2+patch_radius+1][:, ::sigma_skip, ::sigma_skip].reshape((trainLength, -1)),
                                training_data[0, :, N//2, N//2]
                                )

    param_grid = {"n_reservoir": [50, 200, 400], "spectral_radius": [0.1, 0.5, 0.8, 0.95, 1.0, 1.1, 1.5, 3.0], "leak_rate": [.05, .2, .5 , .7, .9, .95],
                "random_seed": [42,41,40,39],  "sparseness": [.1, .2], "noise_level": [0.0001, 0.00001], "regression_parameters": [[5e-2],[5e-3],[5e-4],[5e-5],[5e-6]]}
    fixed_params = {"n_output": 1, "n_input": input_size, "solver": "lsqr", "weight_generation": "advanced", "input_scaling" : input_scaling}

    gs = GridSearchP(param_grid, fixed_params, esnType=ESN)

    print("start fitting...")

    sys.stdout.flush()
    results = gs.fit(
                training_data[1, :, N//2-patch_radius:N//2+patch_radius+1, N//2-patch_radius:N//2+patch_radius+1][:, ::sigma_skip, ::sigma_skip].reshape((trainLength, -1)), training_data[0, :, N//2, N//2].reshape((trainLength, 1)),
                [(test_data[1, :, N//2-patch_radius:N//2+patch_radius+1, N//2-patch_radius:N//2+patch_radius+1][:, ::sigma_skip, ::sigma_skip].reshape((testLength, -1)), test_data[0, :, N//2, N//2].reshape((testLength, 1)))],
                printfreq=100, verbose=2, n_jobs=16)
    print("results:\r\n")
    print(results)
    print("")

    print("\r\nBest result (mse =  {0}):\r\n".format(gs._best_mse))
    print("best parameters {0}".format(gs._best_params))
def mainFunction():
    global output_weights, frame_output_weights, last_states

    inputData, outputData = generate_data(ndata, 20000, 50, Ngrid=N)

    #id: 1-21

    print(inputData.shape)

    param_grid = {
        "n_reservoir": [50, 200, 400],
        "spectral_radius": [0.1, 0.5, 0.8, 0.95, 1.0, 1.1, 1.5, 3.0],
        "leak_rate": [.05, .2, .5, .7, .9, .95],
        "random_seed": [42, 41, 40, 39],
        "sparseness": [.1, .2],
        "noise_level": [0.0001, 0.00001],
        "input_density":
        [n / inputData.shape[1] for n in [5, 10, 15, 20, 50, 100]],
        "regression_parameters": [[5e4], [5e3], [5e2], [5e1], [5e0], [5e-1],
                                  [5e-2], [5e-3], [5e-4]]
    }

    #predict 4 pixels in the centre of the inner square
    fixed_params = {
        "n_output": 4,
        "n_input": inputData.shape[1],
        "solver": "lsqr",
        "weight_generation": "advanced"
    }

    gs = GridSearchP(param_grid, fixed_params, esnType=ESN)

    print("start fitting...")

    sys.stdout.flush()
    results = gs.fit(inputData[:trainLength],
                     outputData[:trainLength],
                     [(inputData[trainLength:trainLength + testLength],
                       outputData[trainLength:trainLength + testLength])],
                     printfreq=100,
                     verbose=2,
                     n_jobs=16)
    print("results:\r\n")
    print(results)
    print("")

    print("\r\nBest result (mse =  {0}):\r\n".format(gs._best_mse))
    print("best parameters {0}".format(gs._best_params))
Example #5
0
def mainFunction():
    global output_weights, frame_output_weights, last_states

    if (os.path.exists("../cache/raw/{0}_{1}.vh.dat.npy".format(ndata,
                                                                N)) == False):
        print("generating data...")
        data = generate_vh_data(ndata, 20000, 50,
                                Ngrid=N)  #20000 was 50000 ndata
        np.save("../cache/raw/{0}_{1}.vh.dat.npy".format(ndata, N), data)
        print("generating finished")
    else:
        print("loading data...")
        data = np.load("../cache/raw/{0}_{1}.vh.dat.npy".format(ndata, N))
        """
        #switch the entries for the u->v prediction
        tmp = data[0].copy()
        data[0] = data[1].copy()
        data[1] = tmp.copy()
        """

        print("loading finished")

    training_data = data[:, :ndata - 2000]
    test_data = data[:, ndata - 2000:]
    """
    esn = ESN(n_input = eff_sigma*eff_sigma, n_output = 1, n_reservoir = n_units,
            weight_generation = "advanced", leak_rate = 0.70, spectral_radius = 0.8,
            random_seed=42, noise_level=0.0001, sparseness=.1, regression_parameters=[3e-6], solver = "lsqr", output_input_scaling=0.01)
    """
    #0.2,  0.4, .6, .8, .95,
    if (id == 1):
        gs = GridSearchP(param_grid={
            "n_reservoir": [50],
            "spectral_radius": [0.5, .8, .9, .95],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    elif (id == 2):
        gs = GridSearchP(param_grid={
            "n_reservoir": [100],
            "spectral_radius": [0.5, .8, .9, .95],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    elif (id == 3):
        gs = GridSearchP(param_grid={
            "n_reservoir": [200],
            "spectral_radius": [0.5, .8, .9, .95],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    elif (id == 4):
        gs = GridSearchP(param_grid={
            "n_reservoir": [400],
            "spectral_radius": [0.5, .8, .9, .95],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    elif (id == 5):
        gs = GridSearchP(param_grid={
            "n_reservoir": [50],
            "spectral_radius": [0.1, 0.2, 0.3, 0.4],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    elif (id == 6):
        gs = GridSearchP(param_grid={
            "n_reservoir": [100],
            "spectral_radius": [0.1, 0.2, 0.3, 0.4],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    elif (id == 7):
        gs = GridSearchP(param_grid={
            "n_reservoir": [200],
            "spectral_radius": [0.1, 0.2, 0.3, 0.4],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    elif (id == 8):
        gs = GridSearchP(param_grid={
            "n_reservoir": [400],
            "spectral_radius": [0.1, 0.2, 0.3, 0.4],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    elif (id == 9):
        gs = GridSearchP(param_grid={
            "n_reservoir": [50],
            "spectral_radius": [1.1, 1.2, 1.3, 1.4, 1.5],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    elif (id == 10):
        gs = GridSearchP(param_grid={
            "n_reservoir": [100],
            "spectral_radius": [1.1, 1.2, 1.3, 1.4, 1.5],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    elif (id == 11):
        gs = GridSearchP(param_grid={
            "n_reservoir": [200],
            "spectral_radius": [1.1, 1.2, 1.3, 1.4, 1.5],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    elif (id == 12):
        gs = GridSearchP(param_grid={
            "n_reservoir": [400],
            "spectral_radius": [1.1, 1.2, 1.3, 1.4, 1.5],
            "leak_rate": [.05, .1, .2, .5, .7, .9, .95],
            "random_seed": [42, 41, 40, 39, 38, 37, 36],
            "sparseness": [.1, .2],
            "noise_level": [0.001, 0.0001, 0.00001],
            "regression_parameters": [[5e-2], [5e-3], [5e-4], [5e-5], [5e-6]]
        },
                         fixed_params={
                             "n_output": 1,
                             "n_input": 1,
                             "solver": "lsqr",
                             "weight_generation": "advanced"
                         },
                         esnType=ESN)
    print("start fitting...")

    sys.stdout.flush()
    results = gs.fit(training_data[1, :, N // 2, N // 2].reshape((-1, 1)),
                     training_data[0, :, N // 2, N // 2].reshape((-1, 1)),
                     [(test_data[1, :, N // 2, N // 2].reshape(
                         (-1, 1)), test_data[0, :, N // 2, N // 2].reshape(
                             (-1, 1)))],
                     printfreq=100,
                     verbose=2,
                     n_jobs=14)
    print("done:\r\n")
    print(results)

    print("\r\nBest result (mse =  {0}):\r\n".format(gs._best_mse))
    print(gs._best_params)