-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_Multi_input.py
139 lines (91 loc) · 3.22 KB
/
train_Multi_input.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 7 11:00:03 2018
@author: acerr
"""
import random
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import scipy
import scipy.linalg
from math import *
import image
from mpl_toolkits.mplot3d import Axes3D
from function import *
from ESN import *
#%% <without regularization>
timeshift=0
timeseries=np.load('timeseries generated by Reservoir.npy')
n_inputs=np.shape(timeseries[:30,:])[0]
# using 8000 of the timeseries to train , 2000 of the timeseries to test
input_train=timeseries[:30,12:8000-timeshift]
mutiEsn=ESN(n_inputs=n_inputs, n_outputs=1, ifplot=1, n_reservoir=200, sparsity=0.9, sparsity_in=0.1)
output_target=timeseries[22,12+timeshift:8000]
input_valid=timeseries[:30,8000-timeshift:9000]
output_valid_true=timeseries[22,8000:9000+timeshift]
mutiEsn.initweights()
mutiEsn.update(input_train,1)
mutiEsn.allstate=discard(mutiEsn.allstate) # optimal!!!
output_target=discard(output_target)
mutiEsn.fit(input_train,output_target,0)
mutiEsn.predict(0)
plt.plot(mutiEsn.outputs,label='trainOut')
plt.plot(output_target,label='target')
mutiEsn.mydel(0)
mutiEsn.update(input_valid,0)
mutiEsn.allstate=discard(mutiEsn.allstate)
input_valid=discard(input_valid)
output_valid_true=discard(output_valid_true)
mutiEsn.predict(1)
plt.figure()
plt.plot(mutiEsn.outputs,label='outputs')
plt.plot(output_valid_true,label='true')
plt.legend()
plt.show()
"""
n_reservoir=100, n_inputs=2, perform terrible
n_reservoir=400, n_inputs=2, perform good
"""
#%% <regularization>
timeshift=1
timeseries=np.load('timeseries generated by Reservoir.npy')
n_inputs=np.shape(timeseries[:13,:])[0]
# using 8000 of the timeseries to train , 2000 of the timeseries to test
input_train=timeseries[:13,12:8000-timeshift]
mutiEsn=ESN(n_inputs=n_inputs, n_outputs=1, ifplot=1, n_reservoir=1500, spectral_radius=0.8, sparsity=0.01, sparsity_in=0.01)
output_target=timeseries[4,12+timeshift:8000]
input_valid=timeseries[:13,8000-timeshift:9000]
output_valid_true=timeseries[4,8000:9000+timeshift]
err=[]
para=[]
x=np.linspace(-6, 2.5, num=50)
mutiEsn.initweights()
output_target=discard(output_target)
output_valid_true=discard(output_valid_true)
error,lamda=mutiEsn.choose(timeshift,input_train,output_target,input_valid,output_valid_true,x,1)
err.append(error)
para.append(lamda)
timeseries=np.load('timeseries generated by Reservoir.npy')
n_inputs=np.shape(timeseries[:13,:])[0]
#%%
timeshift=1
timeseries=np.load('timeseries generated by Reservoir.npy')
n_inputs=np.shape(timeseries[:13,:])[0]
# using 8000 of the timeseries to train , 2000 of the timeseries to test
input_train=timeseries[:13,12:8000-timeshift]
output_target=timeseries[4,12+timeshift:8000]
input_valid=timeseries[:13,8000-timeshift:9000]
output_valid_true=timeseries[4,8000:9000+timeshift]
err=[]
para=[]
#x=np.linspace(-8, 2, num=100)s
# spectral radius
mutiEsn=ESN(n_inputs=n_inputs, n_outputs=1, ifplot=1, n_reservoir=1500, spectral_radius=0.8, sparsity=0.1, sparsity_in=0.1)
mutiEsn.initweights()
output_target=discard(output_target)
output_valid_true=discard(output_valid_true)
error,lamda=mutiEsn.choose(timeshift,input_train,output_target,input_valid,output_valid_true,1)
print("")
err.append(error)
para.append(lamda)