def fit_and_predict(self, sensor_names, training_samples, training_RULs,
                     configuration_samples, configuration_RULs,
                     validation_samples, validation_RULs, testing_samples,
                     testing_RULs, testing_time):
     self.configuration_samples = configuration_samples
     self.configuration_RULs = configuration_RULs
     self.validation_samples = validation_samples
     self.validation_RULs = validation_RULs
     self.testing_time = testing_time
     x = self.optimize_network()
     self.x = x
     n_reservoir, spectral_radius, sparsity, input_scaling, input_shifting, output_scaling, output_shifting, state_noise = \
         x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]
     self.esnet = ESN.ESN(n_inputs=len(sensor_names),
                          n_outputs=1,
                          n_reservoir=int(n_reservoir),
                          spectral_radius=spectral_radius,
                          sparsity=sparsity,
                          random_state=42,
                          input_shift=input_shifting,
                          input_scaling=input_scaling,
                          teacher_scaling=output_scaling,
                          teacher_forcing=True,
                          teacher_shift=output_shifting,
                          noise=state_noise)
     self.esnet.fit(np.array(training_samples), np.array(training_RULs))
     predicted_RULs = self.esnet.predict(np.array(testing_samples), True)
     self.predicted_RULs = [
         item for sublist in predicted_RULs for item in sublist
     ]
     self.testing_RULs = testing_RULs
    def check_accuracy(self, x):
        n_reservoir, spectral_radius, sparsity, input_scaling, input_shifting, output_scaling, output_shifting, state_noise = \
        x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]
        esn = ESN.ESN(n_inputs=len(sensor_names),
                      n_outputs=1,
                      n_reservoir=int(n_reservoir),
                      spectral_radius=spectral_radius,
                      sparsity=sparsity,
                      random_state=42,
                      input_shift=input_shifting,
                      input_scaling=input_scaling,
                      teacher_scaling=output_scaling,
                      teacher_forcing=True,
                      teacher_shift=output_shifting,
                      noise=state_noise)
        esn.fit(np.array(self.configuration_samples),
                np.array(self.configuration_RULs))
        predictions = esn.predict(np.array(self.validation_samples))
        predicted_RULs = [item for sublist in predictions for item in sublist]

        errors = [
            abs(a - b) * 400.0
            for a, b in zip(predicted_RULs, self.validation_RULs)
        ]
        return np.mean(errors)
    def __init__(self, num_params, num_inputs, num_actions):

        self.num_units = num_params//num_actions
        self.num_inputs = num_inputs
        self.num_actions = num_actions
        
        self.echo = ESN(
            N       = self.num_units,
            dt      = 1.0,
            tau     = 5.0,
            alpha   = 0.1,
            beta    = 0.9,
            epsilon = 1.0e-10)
        
        self.input_weights = np.random.randn(self.num_inputs, self.num_units)
        self.out_weights = np.random.randn(self.num_units, self.num_actions)
예제 #4
0
파일: main.py 프로젝트: LindaPetrini/ESN
import os
import pickle

data_dir = "./output/"
output_dir = "./nets/"
filenames = os.listdir(data_dir)
filenames.sort()
hidden_size = 10
input_size = 11
output_size = 2
steer_out = 1

#file_ind = find_ind("forza_1", filenames)
for index in range(0, len(filenames) - 3, 6):

    esn = ESN(input_size, output_size, hidden_size, bias=False)
    for file_index in range(index, index + 6):

        input_train, target_train, N_max = read_file(
            os.path.join(data_dir, filenames[file_index]), steer_out)

        epochs = 2

        #train
        for epoch in range(epochs):
            for ind in range(N_max - 1):
                inp = input_train[ind].reshape(-1, 1)
                desired_target = target_train[ind + 1].reshape(-1, 1)

                if ind >= esn.washout:
                    out = esn.online_train(inp, desired_target)
예제 #5
0
from esn import ESN

# Prepare a synthetic dataset
dataset = torch.Tensor( [ math.sin(x*0.5) + 2 * round(math.cos(x*0.5)) for x in range(2000) ] )
dataset = dataset / dataset.abs().max()

# Washout length
washout = 200

# Split training set and test set
training_x = dataset[0:1200].view(-1,1)
training_y = dataset[1:1201]
test_x = dataset[1200:-1].view(-1,1)
test_y = dataset[1201:]

model = ESN(1, reservoir_size=50, contractivity_coeff=1.2, density=0.9)

# Collect states for training set
X = model(training_x)

# Washout
X = X[washout:]
Y = training_y[washout:]

# Train the model by Moore-Penrose pseudoinversion.
W = X.pinverse() @ Y

# Evaluate the model on the test set
# We pass the latest training state in order to avoid the need for another washout
X_test = model(test_x, X[-1])
predicted_test = X_test @ W
예제 #6
0
data = readBinary(path,
                  precision=4,
                  nsteps=total_length * time_thinning_step,
                  npoints=output_size * space_thinning_step)
data = data.getData(step=time_thinning_step).T[::space_thinning_step, :]

train_data = data[::obs_thinning_step, :train_length]
target_data = data[:, 1:train_length + 1]
test_observed = data[::obs_thinning_step,
                     train_length:train_length + test_length]
test_target = data[:, train_length + 1:train_length + test_length + 1]

model = ESN(input_size=input_size,
            output_size=output_size,
            reservoir_size=reservoir_size,
            adjacency_density=0.0006,
            spectral_radius=0.1,
            input_scale=0.5)

W_out, reservoir = model.train(train_data,
                               target_data=target_data,
                               washout=washout,
                               ridge_param=ridge_param)

predict = model.predict(reservoir,
                        test_observed,
                        ptb_func=None,
                        ptb_scale=1.0,
                        nexttime=nexttime,
                        extended_interval=1000)
예제 #7
0
파일: gen.py 프로젝트: minbin/esn
args = parser.parse_args()

if not args.i:
    print('[ERROR] use -i FILE_NAME to set an input file')
    exit()

data = np.load(args.i)
print('loaded %d points' % len(data))

if args.t == 'dnf':
    data = np.log(data)
elif args.t == 'ig':
    data = data[0::2] / 10000 - 1.1

esn = ESN(n_in=1, n_fb=1, n_units=args.u, spectral_radius=args.s)

trainlen = args.trainlen
predlen = args.predlen
print('using: %d, predicting: %d' % (trainlen, predlen))

print('fitting...')
fit = esn.fit(np.ones(trainlen), data[:trainlen])
print('predicting...')
pred = esn.predict(np.ones(predlen), cont=True)

if args.t == 'dnf':
    data = np.exp(data)
    pred = np.exp(pred)
elif args.t == 'ig':
    data = data + 1.1 * 10000
예제 #8
0
BIAS = os.getenv("BIAS", "true").lower() in (True, "true")
ACTIVATION = os.getenv("ACTIVATION", "tanh")
MODEL_PATH = os.getenv("MODEL_PATH", "models/esn")
SAVE_STEPS = int(os.getenv("SAVE_STEPS", 10000))

esn = None
try:
    print("Loading", MODEL_PATH)
    esn = ESN.load(MODEL_PATH)
    print(MODEL_PATH, "loaded")
except FileNotFoundError as e:
    print(e)
    print('"{}" not found. Creating new model.'.format(MODEL_PATH))
    esn = ESN(SIZE,
              density=DENSITY,
              spectral_radius=SPECTRAL_RADIUS,
              bias=BIAS,
              activation=ACTIVATION)
    esn.save(MODEL_PATH)

app = Flask(__name__)
api = Api(app)

step_counter = 0


class ReadResource(Resource):
    def post(self, key, shape):
        #get shape from url path /dim0/dim1/dim2 etc
        shape = shape.split("/")
        shape = [int(x) for x in shape]