kalekiu / easyesn

Python library for Reservoir Computing using Echo State Networks
129 stars 37 forks source link

Example of generative task with many features #4

Closed thild closed 5 years ago

thild commented 5 years ago

Hi,

First of all, thanks for creating this great library.

I'm trying to predict a temporal series based on a chaotic system using generative task. My system have many features but I'm unable to generate the output with more than one feature.

The data was loaded with pandas. inputDataTraining and inputDataValidation are pandas DataFrames.

Could you create a example o generating task with more than one feature?

Thanks

esn = PredictionESN(n_input=0, n_output=2, n_reservoir=50, spectralRadius=0.9, leakingRate=0.2, regressionParameters=[1e-1], solver="pinv", feedback=True)

esn.fit(None, inputDataTraining, transientTime=2500, verbose=1)

generation = esn.generate(n=len(inputDataValidation), inputData=None, initialOutputData=inputDataTraining[-1])
Traceback (most recent call last):
  File "echo2.py", line 50, in <module>
    generation = esn.generate(n=len(inputDataValidation), inputData=None, initialOutputData=inputDataTraining)
  File "/home/tony/.local/lib/python3.6/site-packages/easyesn/PredictionESN.py", line 223, in generate
    _, Y = self.propagate(inputData, None, verbose=verbose, steps=n, previousOutputData=initialOutputData)
  File "/home/tony/.local/lib/python3.6/site-packages/easyesn/BaseESN.py", line 125, in propagate
    self.update(None, previousOutputData, x=x)
  File "/home/tony/.local/lib/python3.6/site-packages/easyesn/BaseESN.py", line 326, in update
    outputData = outputData.reshape(self.n_output, 1)
  File "/home/tony/.local/lib/python3.6/site-packages/pandas/core/generic.py", line 4376, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'reshape'

If I change inputDataTraining[-1] to inputDataTraining.values[-1] I get:

Traceback (most recent call last):
  File "echo2.py", line 46, in <module>
    generation = esn.generate(n=len(inputDataValidation), inputData=None, initialOutputData=inputDataTraining.values[-1])
  File "/home/tony/.local/lib/python3.6/site-packages/easyesn/PredictionESN.py", line 223, in generate
    _, Y = self.propagate(inputData, None, verbose=verbose, steps=n, previousOutputData=initialOutputData)
  File "/home/tony/.local/lib/python3.6/site-packages/easyesn/BaseESN.py", line 136, in propagate
    Y[t-transientTime, :] = previousOutputData
ValueError: could not broadcast input array from shape (2,1) into shape (2)
zimmerrol commented 5 years ago

Hello, please make sure to flatten/reshape the initial data, i.e. inputDataTraining.values[-1].reshape(-1). Let me know if that solves your problem.

thild commented 5 years ago

Hi @FlashTek

I tried to reshape as you said, but with no luck.

I'm trying to predict a Lorenz attractor using x and y variables. So I have two inputs and two outputs. I started to study ESN and I'm using this problem to understand the concepts.

Can your library handle multi features? What I'm doing wrong? Any help wold be appreciated.

Best regards

import numpy as np
import pandas as pd

from easyesn.optimizers import GradientOptimizer
from easyesn import PredictionESN
from easyesn.optimizers import GridSearchOptimizer
from easyesn import helper as hlp
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

np.random.seed(42)

# feature_cols = ["x", "y", "z", "vet_norm_x1", "vet_norm_x2", "vet_norm_y1", "vet_norm_y2", "vet_norm_z1", "vet_norm_z2"]
feature_cols = ["x", "y"]

data = pd.read_csv("./data/Lorenz-Bred.dat", 
              sep=r' ', 
              skipinitialspace=True, usecols=range(1,10))

X = data.loc[:,feature_cols]

X_train, X_test, y_train, y_test = train_test_split(X, X, test_size=0.33, shuffle=False)

inputDataTraining = np.array(list(zip(X_train["x"],X_train["y"])))
inputDataValidation = np.array(list(zip(X_test["x"],X_test["y"])))
# inputDataTraining = X_train.values.reshape(-1)
# inputDataValidation = X_test.values.reshape(-1)

esn = PredictionESN(n_input=0, n_output=2, n_reservoir=100, spectralRadius=0.9, leakingRate=0.2, regressionParameters=[1e-1], solver="pinv", feedback=True)

esn.fit(None, inputDataTraining, transientTime=2500, verbose=1)

generation = esn.generate(n=len(inputDataValidation), inputData=None, initialOutputData=inputDataTraining[-1].reshape(-1))

plt.plot(generation[0, :])
plt.plot(inputDataValidation)
plt.savefig("plot.png")
plt.show()
jeyojey commented 2 years ago

I have the same problem. I'm using the feedback=True in PredictionESN. I guess when setting the number of outputs to more than 1 brings this error, as the previous outputs with a different shape cannot be properly stored in Y[t, :] = previousOutputData[:].