def calculate_ESN(name, rand_seed, nReservoir, spectralRadius, future, futureTotal): data = open(name + ".txt").read().split() data = np.array(data).astype("float64") n_resevoir = 500 sparsity = 0.2 rand_seed = 23 spectral_radius = 1.2 noise = 0.0005 future = 1 futureTotal = 7 esn = ESN( n_inputs=1, n_outputs=1, n_reservoir=nReservoir, sparsity=sparsity, random_state=rand_seed, spectral_radius=spectralRadius, noise=noise, ) trainlen = data.__len__() - futureTotal pred_tot = np.zeros(futureTotal) for i in range(0, futureTotal, future): pred_training = esn.fit(np.ones(trainlen), data[i : trainlen + i]) prediction = esn.predict(np.ones(future)) pred_tot[i : i + future] = prediction[:, 0] return pred_tot
def pso_esn_parameters_for_elasticnet(x): ic_s =x[0] ic_e =x[1] is_s = x[2] is_e = x[3] teacher_scaling = x[4] teacher_shift = x[5] alpha =x[6] l1_ratio=x[7] esn = ESN(n_inputs = 2, n_outputs = 1, n_reservoir = n_reservoir, spectral_radius = spectral_radius, sparsity = sparsity, noise = noise, input_shift = [is_s,is_e],#[0,0] input_scaling =[ic_s,ic_e],# [0.01, 3] teacher_scaling = teacher_scaling,#1.12, teacher_shift = teacher_shift,#-0.7, out_activation = np.tanh, inverse_out_activation = np.arctanh, random_state = rng, silent = False) esn.alpha = alpha esn.l1_ratio = l1_ratio internal_states,transient = esn.train_reservior(train_ctrl,train_output) pred_train = esn.train_readout_with_elasticnet(internal_states,train_output,transient) pred_test = esn.predict(test_ctrl) test_error_rate= np.sqrt(np.mean((pred_test - test_output)**2)) #get function name as title title = inspect.stack()[0][3] print "#### {} ## train_error:{},test_error:{}".format(title,esn.train_error_rate,test_error_rate) return test_error_rate
def test_freqgen(self): rng = np.random.RandomState(42) def frequency_generator(N,min_period,max_period,n_changepoints): """returns a random step function + a sine wave signal that changes its frequency at each such step.""" # vector of random indices < N, padded with 0 and N at the ends: changepoints = np.insert(np.sort(rng.randint(0,N,n_changepoints)),[0,n_changepoints],[0,N]) # list of interval boundaries between which the control sequence should be constant: const_intervals = zip(changepoints,np.roll(changepoints,-1))[:-1] # populate a control sequence frequency_control = np.zeros((N,1)) for (t0,t1) in const_intervals: frequency_control[t0:t1] = rng.rand() periods = frequency_control * (max_period - min_period) + max_period # run time through a sine, while changing the period length frequency_output = np.zeros((N,1)) z = 0 for i in range(N): z = z + 2 * np.pi / periods[i] frequency_output[i] = (np.sin(z) + 1)/2 return np.hstack([np.ones((N,1)),1-frequency_control]),frequency_output N = 15000 min_period = 2 max_period = 10 n_changepoints = N/200 frequency_control,frequency_output = frequency_generator(N,min_period,max_period,n_changepoints) traintest_cutoff = np.ceil(0.7*N) train_ctrl,train_output = frequency_control[:traintest_cutoff],frequency_output[:traintest_cutoff] test_ctrl, test_output = frequency_control[traintest_cutoff:],frequency_output[traintest_cutoff:] esn = ESN(n_inputs = 2, n_outputs = 1, n_reservoir = 200, spectral_radius = 0.25, sparsity = 0.95, noise = 0.001, input_shift = [0,0], input_scaling = [0.01, 3], teacher_scaling = 1.12, teacher_shift = -0.7, out_activation = np.tanh, inverse_out_activation = np.arctanh, random_state = rng, silent = True) pred_train = esn.fit(train_ctrl,train_output) #print "test error:" pred_test = esn.predict(test_ctrl) error = np.sqrt(np.mean((pred_test - test_output)**2)) self.assertAlmostEqual(error,0.39037112433756577)
def test_IODimensions(self): """try different combinations of input & output dimensionalities & teacher forcing""" tasks = [(1,1,100,True),(10,1,100,True),(1,10,100,True),(10,10,100,True), (1,1,100,False),(10,1,100,False),(1,10,100,False),(10,10,100,False)] for t in tasks: N_in ,N_out, N_samples, tf = t X = np.random.randn(N_samples,N_in) if N_in > 1 else np.random.randn(N_samples) y = np.random.randn(N_samples,N_out) if N_out > 1 else np.random.randn(N_samples) Xp = np.random.randn(N_samples,N_in) if N_in > 1 else np.random.randn(N_samples) esn = ESN(N_in,N_out,teacher_forcing=tf) prediction_tr = esn.fit(X,y) prediction_t = esn.predict(Xp) self.assertEqual(prediction_tr.shape,(N_samples,N_out)) self.assertEqual(prediction_t.shape,(N_samples,N_out))
def test_IODimensions(self): """try different combinations of input & output dimensionalities & teacher forcing""" tasks = [(1, 1, 100, True), (10, 1, 100, True), (1, 10, 100, True), (10, 10, 100, True), (1, 1, 100, False), (10, 1, 100, False), (1, 10, 100, False), (10, 10, 100, False)] for t in tasks: N_in, N_out, N_samples, tf = t X = np.random.randn( N_samples, N_in) if N_in > 1 else np.random.randn(N_samples) y = np.random.randn( N_samples, N_out) if N_out > 1 else np.random.randn(N_samples) Xp = np.random.randn( N_samples, N_in) if N_in > 1 else np.random.randn(N_samples) esn = ESN(N_in, N_out, teacher_forcing=tf) prediction_tr = esn.fit(X, y) prediction_t = esn.predict(Xp) self.assertEqual(prediction_tr.shape, (N_samples, N_out)) self.assertEqual(prediction_t.shape, (N_samples, N_out))
def test_mackey(self): try: data = np.load('mackey_glass_t17.npy') except IOError: self.skipTest("missing data") esn = ESN(n_inputs = 1, n_outputs = 1, n_reservoir = 500, spectral_radius = 1.5, random_state=42) trainlen = 2000 future = 2000 esn.fit(np.ones(trainlen),data[:trainlen]) prediction = esn.predict(np.ones(future)) error = np.sqrt(np.mean((prediction.flatten() - data[trainlen:trainlen+future])**2)) self.assertAlmostEqual(error,0.1396039098653574)
def test_mackey(self): try: data = np.load('mackey_glass_t17.npy') except IOError: self.skipTest("missing data") esn = ESN(n_inputs=1, n_outputs=1, n_reservoir=500, spectral_radius=1.5, random_state=42) trainlen = 2000 future = 2000 esn.fit(np.ones(trainlen), data[:trainlen]) prediction = esn.predict(np.ones(future)) error = np.sqrt( np.mean((prediction.flatten() - data[trainlen:trainlen + future])**2)) self.assertAlmostEqual(error, 0.1396039098653574)
def pso_esn_parameters_for_scad(x): # 0: tao, 1:c0, 2:IC_s,3:IC_e 4:IS_s,5:IS_e, ,6:teacher sacling,7:teacher shift # 0:IC_s,1:IC_e, 2:IS_s,3:IS_e, 4:teacher sacling,5:teacher shift,6: tao, 7:c0, ic_s =x[0] ic_e =x[1] is_s = x[2] is_e = x[3] teacher_scaling = x[4] teacher_shift = x[5] tao = x[6] c0 = x[7] esn = ESN(n_inputs = 2, n_outputs = 1, n_reservoir = n_reservoir, spectral_radius = spectral_radius, sparsity = sparsity, noise = noise, input_shift = [is_s,is_e],#[0,0] input_scaling =[ic_s,ic_e],# [0.01, 3] teacher_scaling = teacher_scaling,#1.12, teacher_shift = teacher_shift,#-0.7, out_activation = np.tanh, inverse_out_activation = np.arctanh, random_state = rng, silent = False) esn.penal_tao =tao esn.penal_c0 = c0 internal_states,transient = esn.train_reservior(train_ctrl,train_output) pred_train = esn.train_readout_with_scad(internal_states,train_output,transient) pred_test = esn.predict(test_ctrl) #test_error_rate= np.sqrt(np.mean((pred_test - test_output)**2)) test_error_rate= np.sqrt((pred_test - test_output)**2) #get function name as title title = inspect.stack()[0][3] print "#### {} ## train_error:{},test_error:{}".format(title,esn.train_error_rate,test_error_rate) return test_error_rate 这里画一个原始测试序列的图和一个test_output的图。
def mackey(reservoir_size, spectral_radius, train_len, future): # Load in mackey-glass numpy array data = np.load('mackey_glass_t17.npy' ) # http://minds.jacobs-university.de/mantas/code #Initialize ESN esn = ESN(n_inputs=1, n_outputs=1, n_reservoir=reservoir_size, spectral_radius=spectral_radius, random_state=42) # Fit the model pred_training = esn.fit(np.ones(train_len), data[:train_len]) # Predict and find the error prediction = esn.predict(np.ones(future)) error = np.sqrt( np.mean( (prediction.flatten() - data[train_len:train_len + future])**2)) return error, prediction, reservoir_size, spectral_radius, train_len, future
def henon(reservoir_size, spectral_radius, train_len, future): # Load in mackey-glass numpy array data_full = hn.henon() data = data_full[0] #Initialize ESN esn = ESN(n_inputs=1, n_outputs=1, n_reservoir=reservoir_size, spectral_radius=spectral_radius, random_state=42) # Fit the model pred_training = esn.fit(np.ones(train_len), data[:train_len]) # Predict and find the error prediction = esn.predict(np.ones(future)) error = np.sqrt( np.mean( (prediction.flatten() - data[train_len:train_len + future])**2)) return error, prediction, reservoir_size, spectral_radius, train_len, future
def test_inputshift(self): """input shift factors of different formats should be correctly interpreted or rejected""" esn = ESN(N_in,N_out,input_shift=1) self.assertTrue(np.all(1+self.X == esn._scale_inputs(self.X))) esn.fit(self.X,self.y) esn.predict(self.Xp) esn = ESN(N_in,N_out,input_shift=[1]*N_in) self.assertTrue(np.all(1+self.X == esn._scale_inputs(self.X))) esn.fit(self.X,self.y) esn.predict(self.Xp) esn = ESN(N_in,N_out,input_shift=np.array([1]*N_in)) self.assertTrue(np.all(1+self.X == esn._scale_inputs(self.X))) esn.fit(self.X,self.y) esn.predict(self.Xp) with self.assertRaises(ValueError): esn = ESN(N_in,N_out,input_shift=[1]*(N_in+1)) with self.assertRaises(ValueError): esn = ESN(N_in,N_out,input_shift=np.array([[1]*N_in]))
def test_inputshift(self): """input shift factors of different formats should be correctly interpreted or rejected""" esn = ESN(N_in, N_out, input_shift=1) self.assertTrue(np.all(1 + self.X == esn._scale_inputs(self.X))) esn.fit(self.X, self.y) esn.predict(self.Xp) esn = ESN(N_in, N_out, input_shift=[1] * N_in) self.assertTrue(np.all(1 + self.X == esn._scale_inputs(self.X))) esn.fit(self.X, self.y) esn.predict(self.Xp) esn = ESN(N_in, N_out, input_shift=np.array([1] * N_in)) self.assertTrue(np.all(1 + self.X == esn._scale_inputs(self.X))) esn.fit(self.X, self.y) esn.predict(self.Xp) with self.assertRaises(ValueError): esn = ESN(N_in, N_out, input_shift=[1] * (N_in + 1)) with self.assertRaises(ValueError): esn = ESN(N_in, N_out, input_shift=np.array([[1] * N_in]))
def ESN_train(data): #print(data) total_col = data.shape[1] opt_ang_col = data[:, total_col - 1] #print(opt_ang_col) max_angle = max(opt_ang_col) #print(max_angle) opt_ang_col = opt_ang_col / max_angle / 2 #print(opt_ang_col) opt_ang_col = np.transpose(opt_ang_col) #print(opt_ang_col) N = flytera_cfg.sim_tick rng = np.random.RandomState(42) traintest_cutoff = int(np.ceil(0.66 * N)) train_data, train_data_angle = data[: traintest_cutoff], opt_ang_col[: traintest_cutoff] test_data, test_data_angle = data[traintest_cutoff:], opt_ang_col[ traintest_cutoff:] #print('TEST DAtA ANGLE', test_data_angle) rows_zeros_ip_shift = np.zeros(total_col) #print('zeros', zeros) rows_ones_ip_scale = np.ones(total_col) esn = ESN( n_inputs=total_col, n_outputs=1, n_reservoir=50, spectral_radius=0.25, sparsity=0.25, noise=0.001, input_shift=rows_zeros_ip_shift, input_scaling=rows_ones_ip_scale, #input_scaling=[1, 1], teacher_scaling=0.4, #teacher_scaling=1, teacher_shift=0, #teacher_shift=0, # teacher_scaling = None, # teacher_shift = None, out_activation=np.tanh, inverse_out_activation=np.arctanh, random_state=rng, silent=False) print('n_inputs=', esn.n_inputs) print('n_outputs=', esn.n_outputs) print('n_reservoir=', esn.n_reservoir) print('spectral radius=', esn.spectral_radius) print('sparsity=', esn.sparsity) print('noise=', esn.noise) print('input_shift=', esn.input_shift) print('input_scaling=', esn.input_scaling) print('teacher_forcing=', esn.teacher_forcing) # print ('feedback_scaling=' , esn1.feedback_scaling) print('teacher_scaling=', esn.teacher_scaling) print('teacher_shift=', esn.teacher_shift) print('out_activation=', esn.out_activation) print('inverse_out=', esn.inverse_out_activation) print('random_state=', esn.random_state) print('silent=', esn.silent) pred_train = esn.fit(train_data, train_data_angle) #print(pred_train) pred_test = esn.predict(test_data) #print('PREDICTED TEST', pred_test) pred_max_angle = max(pred_test) * 2 * max_angle return_angles = np.ceil(pred_test * 2 * max_angle) pred_max_angle = int(np.ceil(pred_max_angle)) #print(pred_max_angle) #exit() pp.figure(1) font = {'family': 'sans', 'size': 14} pp.rc('font', **font) pp.xlim(0, 100) pp.ylim(0, 20) pp.xlabel('Network Run Time (slot)') pp.ylabel('Directivity Angle (degree)') #print() #print() a = (test_data_angle * 2 * max_angle)[0] b = (pred_test * 2 * max_angle)[0] #print(a) #print(b) acc = 100 - ((a - b) / a) * 100 #print(acc) #exit() #print(test_data_angle*2*max_angle) #print(pred_test*2*max_angle) before = sum(test_data_angle * 2 * max_angle) after = sum(pred_test * 2 * max_angle) #print(before) #print(after) error = (abs(after - before) / after) * 100 #print(accuracy1) #accuracy2 = (after/before)*100 #print(accuracy2) pp.plot(range(len(test_data_angle)), test_data_angle * 2 * max_angle, c='r', marker='o', label='Optimal Directivity Angle') pp.plot(range(len(test_data_angle)), pred_test * 2 * max_angle, c='k', linestyle='--', label='Predicted Directivity Angle') pp.legend() pp.grid(True) pp.show() exit() list_angles = [(pred_max_angle), (pred_max_angle) * 2, (pred_max_angle) * 4, (pred_max_angle) * 6, (pred_max_angle) * 8] #print(list_angles) return return_angles
def esn2_training(data, index2): #print('a',data) N = np.size(data, 0) #print(N) rng = np.random.RandomState(42) traintest_cutoff = int(np.ceil(0.8 * N)) data_index = data[:, 3 * netcfg.num_lte_bs_mobile + 1] #print(np.transpose(np.array([data_index]))) #print('a',data_index) data_index_max = max(data_index) data_index = data_index / data_index_max / 2 data_index = np.transpose(np.array([data_index])) #print(data_index) train_data, train_data_index = data[: traintest_cutoff], data_index[: traintest_cutoff] test_data, test_data_index = data[traintest_cutoff:], data_index[ traintest_cutoff:] #print(test_data) col_mbs = 1 + 3 * netcfg.num_lte_bs_mobile + 1 zeros = np.zeros(col_mbs) #print('zeros', zeros) ones = np.ones(col_mbs) esn = ESN( n_inputs=col_mbs, n_outputs=1, n_reservoir=14, spectral_radius=0.25, sparsity=0.25, noise=0.001, input_shift=zeros, input_scaling=ones, #input_scaling=[1, 1], teacher_scaling=0.5, #teacher_scaling=1, teacher_shift=-0, #teacher_shift=0, # teacher_scaling = None, # teacher_shift = None, out_activation=np.tanh, inverse_out_activation=np.arctanh, random_state=rng, silent=False) print('----------------------------------------------------------------') print('esn {}'.format(index2 + 1)) print('----------------------------------------------------------------') print('n_inputs=', esn.n_inputs) print('n_outputs=', esn.n_outputs) print('n_reservoir=', esn.n_reservoir) print('spectral radius=', esn.spectral_radius) print('sparsity=', esn.sparsity) print('noise=', esn.noise) print('input_shift=', esn.input_shift) print('input_scaling=', esn.input_scaling) print('teacher_forcing=', esn.teacher_forcing) # print ('feedback_scaling=' , esn1.feedback_scaling) print('teacher_scaling=', esn.teacher_scaling) print('teacher_shift=', esn.teacher_shift) print('out_activation=', esn.out_activation) print('inverse_out=', esn.inverse_out_activation) print('random_state=', esn.random_state) print('silent=', esn.silent) pred_train = esn.fit(train_data, train_data_index) pred_test = esn.predict(test_data) #print('b',pred_test*2*data_index_max) predicted_index = pred_test * 2 * data_index_max #print(predicted_index) pp.figure(2) axis = (netcfg.num_lte_bs_mobile * 100) + 10 + (index2 + 1) pp.subplot(axis) #pp.title('MBS {}'.format (index2+1)) #pp.title('ESN Performance for Predicting Next Best Location Index') pp.xlabel('Number of iterations') pp.ylabel('Next Location Index Value') pp.ylim(0, 400) #pp.plot(range(len(test_data)), test_data_index*2*data_index_max, range(len(pred_test)), pred_test*2*data_index_max) pp.plot(range(len(test_data_index)), test_data_index * 2 * data_index_max, c='r', marker='o', label='Exhaustive Search') pp.plot(range(len(test_data_index)), predicted_index, c='g', linestyle='--', label='ESN-Opt') pp.xlabel('Network Run Time (slot)') pp.ylabel('Location Index') pp.grid(True) pp.legend()
random_state = rng, silent = False) print('fitting') pred_train = esn.fit(u, y) #Assess Training Error vec_bounder = np.vectorize(bounder) bounded = vec_bounder(pred_train) #print(bounded) #print('\n', y.reshape(lengthTrain, parity-1)) boundedErrors = np.abs(bounded - y.reshape(lengthTrain, parity-1)) numWrong = sum([1 if xi != 0 else 0 for xi in np.sum(boundedErrors, axis = 1)]) print('Number Wrong (Training):', numWrong, 'Out of', lengthTrain, '\n') #Testing (uTest,yTest) = generateParity(lengthTest, parity) pred_test = esn.predict(uTest) print('Testing MSE:',np.sqrt(np.mean((pred_test - yTest)**2))) boundedTest = vec_bounder(pred_test) boundedTestErrors = np.abs(boundedTest - yTest.reshape(lengthTest, parity-1)) numWrongTest = sum([1 if xi != 0 else 0 for xi in np.sum(boundedTestErrors, axis = 1)]) #print(boundedTest) #print('\n', yTest.reshape(lengthTest, parity-1)) print('Number Wrong (Testing):', numWrongTest, 'Out of', lengthTest, '\n')
def esn_training(index, data, ntwk_obj): '''ESN Training for each MBS''' #print(index) num = netcfg.num_lte_bs_mobile - netcfg.num_lte_bs #print(num) index = index - (netcfg.num_lte_bs_mobile - num) #print(index) col_mbs = 3 * netcfg.num_lte_bs_mobile + 1 + index #print('a',col_mbs) #print('all data',data) data_rate = data[:, col_mbs:col_mbs + 1] #print('data_rate',data_rate) data = data[:, 0:col_mbs - index] #print('intermediate',data) data = np.hstack((data, data_rate)) #print('data',data) coord_x = ntwk_obj.grid_x_coord #print(coord_x,'\n') coord_y = ntwk_obj.grid_y_coord #print(coord_y,'\n') #data_rate = np.transpose(data_rate) #print(data_rate) #print('data',data) data_rate = data[:, col_mbs - index] #print(data_rate) data_rate_max = max(data_rate) #print('max',data_rate_max) #exit(0) ntwk_obj.max_data = data_rate_max #print(ntwk_obj.max_data) data_rate = data_rate / data_rate_max / 2 #print(data_rate) data_rate = np.transpose(np.array([data_rate])) #print('data rate',data_rate) N = netcfg.total_tick rng = np.random.RandomState(42) traintest_cutoff = int(np.ceil(0.8 * N)) train_data, train_data_rate = data[: traintest_cutoff], data_rate[: traintest_cutoff] #print('train data',train_data) #print('train data rate',train_data_rate) #print('train data',train_data.shape) #print('train data rate',train_data_rate.shape) test_data, test_data_rate = data[traintest_cutoff:], data_rate[ traintest_cutoff:] #print('test data',test_data) #print('test data rate',test_data_rate) p1 = sum(test_data_rate) print('p1', p1) #print('test data',test_data.shape) #print('test data rate',test_data_rate.shape) col_mbs = col_mbs + 1 - index zeros_row = np.zeros(col_mbs) #print('zeros', zeros) ones_row = np.ones(col_mbs) #print('ones', ones) #print(col_mbs) # create an echo state network esn = ESN( n_inputs=col_mbs, n_outputs=1, n_reservoir=21, spectral_radius=0.25, sparsity=0.25, noise=0.001, input_shift=zeros_row, input_scaling=ones_row, #input_scaling=[1, 1], teacher_scaling=0.5, #teacher_scaling=1, teacher_shift=-0, #teacher_shift=0, # teacher_scaling = None, # teacher_shift = None, out_activation=np.tanh, inverse_out_activation=np.arctanh, random_state=rng, silent=False) # print('----------------------------------------------------------------') # print ('esn {}'.format(index+1)) # print('----------------------------------------------------------------') # print ('n_inputs=', esn.n_inputs) # print ('n_outputs=', esn.n_outputs) # print ('n_reservoir=', esn.n_reservoir) # print ('spectral radius=', esn.spectral_radius) # print ('sparsity=', esn.sparsity) # print ('noise=', esn.noise) # print ('input_shift=', esn.input_shift) # print ('input_scaling=', esn.input_scaling) # print ('teacher_forcing=', esn.teacher_forcing) # # print ('feedback_scaling=' , esn1.feedback_scaling) # print ('teacher_scaling=', esn.teacher_scaling) # print ('teacher_shift=', esn.teacher_shift) # print ('out_activation=', esn.out_activation) # print ('inverse_out=', esn.inverse_out_activation) # print ('random_state=', esn.random_state) # print ('silent=', esn.silent) #print('1') # fitting pred_train = esn.fit(train_data, train_data_rate) #print(pred_train) #print('2') # prediction #print(data.shape) pred_test = esn.predict(test_data) # print('data rate',data_rate) # print('pred test',pred_test,'\n') # print('3') pp.figure(1) font = {'family': 'sans', 'size': 16} pp.rc('font', **font) axis = (netcfg.num_lte_bs_mobile * 100) + 10 + (index + 1) pp.subplot(axis) #pp.title('MBS {}'.format (index+1)) #pp.title('ESN Performance for Rate Prediction') pp.xlabel('Network Run Time (slot)') pp.ylabel('FBS Rate (Mbps)') # print(data_rate) # print(pred_test) pp.plot(range(len(test_data_rate)), test_data_rate * 2 * data_rate_max, c='r', marker='o', label='Measured Rate') pp.plot(range(len(test_data_rate)), pred_test * 2 * data_rate_max, c='g', linestyle='--', label='ESN-Pdt Prediction') pp.legend() pp.grid(True) return esn
n_outputs= n_features, #Número de saídas (produtos) utilizadas na rede. Neste caso, são iguais. n_reservoir=n_reservoir, sparsity=sparsity, random_state=1, spectral_radius=spectral_radius, noise=noise) ## #Primeira predição: trainlen = 130 #Quantidade de pontos (dias) a serem usados para treino future = 30 #Quantidade de pontos (dias) a serem previstos. pred_training0 = esn.fit(np.ones((trainlen, n_features)), data[:trainlen, :trainlen]) prediction0 = esn.predict(np.ones((future, n_features))) errorlist0, errormean0 = results( data, prediction0, trainlen, future, plotfigures=False ) #Se desejar ver os erros e plotar gráficos para comparar predições, setar plotfigures = True. # #Início de otimização de parâmetros. São quatro variáveis: n_reservoir, sparsity, spectral_radius e noise. #Primeiramente são buscados o melhor par de spectral_radius e noise, onde melhor significa ter menor RMSE. #Serão mantidos fixos n_reservoir e sparsity. n_reservoir = 1000 #Atenção: aumentar n_reservoir aumenta muito o tempo computacional. sparsity = 0.3 #Conjunto de spectral_radius linearmente espaçado entre 1 e 1.8: radiusset = [ 1, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55, 1.6, 1.65, 1.7, 1.75, 1.8
from pyESN import ESN from matplotlib import pyplot as plt data = np.load( 'mackey_glass_t17.npy') # http://minds.jacobs-university.de/mantas/code esn = ESN(n_inputs=1, n_outputs=1, n_reservoir=500, spectral_radius=1.5, random_state=42) trainlen = 2000 future = 2000 pred_training = esn.fit(np.ones(trainlen), data[:trainlen]) prediction = esn.predict(np.ones(future)) print("test error: \n" + str( np.sqrt( np.mean((prediction.flatten() - data[trainlen:trainlen + future])**2)))) plt.figure(figsize=(11, 1.5)) plt.plot(range(0, trainlen + future), data[0:trainlen + future], 'k', label="target system") plt.plot(range(trainlen, trainlen + future), prediction, 'r', label="free running ESN") lo, hi = plt.ylim()
def esn_training(index, data, ntwk_obj): '''ESN Training for each MBS''' #print(index) num = netcfg.num_lte_bs_mobile - netcfg.num_lte_bs #print(num) index = index - (netcfg.num_lte_bs_mobile - num) #print(index) col_mbs = 3 * netcfg.num_lte_bs_mobile + 1 + index #print('a',col_mbs) #print('all data',data) data_rate = data[:, col_mbs:col_mbs + 1] #print('data_rate',data_rate) data = data[:, 0:col_mbs - index] #print('intermediate',data) data = np.hstack((data, data_rate)) #print('data',data) coord_x = ntwk_obj.grid_x_coord #print(coord_x,'\n') coord_y = ntwk_obj.grid_y_coord #print(coord_y,'\n') length = len(coord_x) #print(length) length1 = len(coord_y) #print(length) #print(length1) #xyz_coord = np.array([1] xyz_data = np.array([]) z = 100 utility = 0 for i in range(length): xyz_coord = np.array([1]) for num in range(netcfg.num_lte_bs_mobile): #print(i) #print(length) j = i + num #print(j) if j < length: #print(j) x = coord_x[j] y = coord_y[j] xy = np.hstack((x, y)) xyz = np.hstack((xy, z)) #xyz_coord = np.hstack((xyz_coord,xyz)) #print('a',xyz_coord) else: #print('j',j) j = j - length #print('j',j) x = coord_x[j] y = coord_y[j] xy = np.hstack((x, y)) xyz = np.hstack((xy, z)) #xyz_coord = np.hstack((xyz_coord,xyz)) #print('b',xyz_coord) #print(xyz) if num == 0: #print(xyz_coord) xyz = np.hstack((1, xyz)) data_xyz = xyz #print(data_xyz) else: data_xyz = np.hstack((data_xyz, xyz)) #print(data_xyz) data_xyz = np.hstack((data_xyz, utility)) #print(data_xyz) #print(i) if i == 0: xyz_coord_data = data_xyz #print(xyz_coord_data) else: xyz_coord_data = np.vstack((xyz_coord_data, data_xyz)) #print(xyz_coord_data) #print(data) print(xyz_coord_data) data = np.vstack((data, xyz_coord_data)) #print(data) #data_rate = np.transpose(data_rate) #print(data_rate) #print('data',data) data_rate = data[:, col_mbs - index] #print(data_rate) data_rate_max = max(data_rate) #print('max',data_rate_max) #exit(0) data_rate = data_rate / data_rate_max / 2 #print(data_rate) data_rate = np.transpose(np.array([data_rate])) #print('data rate',data_rate) N = netcfg.total_tick rng = np.random.RandomState(42) traintest_cutoff = int(np.ceil(N)) train_data, train_data_rate = data[:N], data_rate[:N] #print('train data',train_data) #print('train data rate',train_data_rate) #print('train data',train_data.shape) #print('train data rate',train_data_rate.shape) test_data, test_data_rate = data[N:], data_rate[N:] #print('test data',test_data) #print('test data rate',test_data_rate) #print('test data',test_data.shape) #print('test data rate',test_data_rate.shape) col_mbs = col_mbs + 1 - index zeros = np.zeros(col_mbs) #print('zeros', zeros) ones = np.ones(col_mbs) #print('ones', ones) # create an echo state network esn = ESN( n_inputs=col_mbs, n_outputs=1, n_reservoir=475, spectral_radius=0.25, sparsity=0.25, noise=0.001, input_shift=zeros, input_scaling=ones, #input_scaling=[1, 1], teacher_scaling=1.50, #teacher_scaling=1, teacher_shift=-0.7, #teacher_shift=0, # teacher_scaling = None, # teacher_shift = None, out_activation=np.tanh, inverse_out_activation=np.arctanh, random_state=rng, silent=False) print('----------------------------------------------------------------') print('esn {}'.format(index + 1)) print('----------------------------------------------------------------') print('n_inputs=', esn.n_inputs) print('n_outputs=', esn.n_outputs) print('n_reservoir=', esn.n_reservoir) print('spectral radius=', esn.spectral_radius) print('sparsity=', esn.sparsity) print('noise=', esn.noise) print('input_shift=', esn.input_shift) print('input_scaling=', esn.input_scaling) print('teacher_forcing=', esn.teacher_forcing) # print ('feedback_scaling=' , esn1.feedback_scaling) print('teacher_scaling=', esn.teacher_scaling) print('teacher_shift=', esn.teacher_shift) print('out_activation=', esn.out_activation) print('inverse_out=', esn.inverse_out_activation) print('random_state=', esn.random_state) print('silent=', esn.silent) #print('1') # fitting pred_train = esn.fit(train_data, train_data_rate) #print('2') # prediction pred_test = esn.predict(data) #print(pred_test) #print('data rate',data_rate) #print('pred test',pred_test,'\n') #print('3') #pp.figure(1) axis = (netcfg.num_lte_bs_mobile * 100) + 10 + (index + 1) pp.subplot(axis) pp.title('MBS {}'.format(index + 1)) #print(data_rate) #print(pred_test) pp.plot(range(len(data)), data_rate, range(len(pred_test)), pred_test)
def test_freqgen(self): rng = np.random.RandomState(42) def frequency_generator(N, min_period, max_period, n_changepoints): """returns a random step function + a sine wave signal that changes its frequency at each such step.""" # vector of random indices < N, padded with 0 and N at the ends: changepoints = np.insert( np.sort(rng.randint(0, N, n_changepoints)), [0, n_changepoints], [0, N]) # list of interval boundaries between which the control sequence # should be constant: const_intervals = list(zip(changepoints, np.roll(changepoints, -1)))[:-1] # populate a control sequence frequency_control = np.zeros((N, 1)) for (t0, t1) in const_intervals: frequency_control[t0:t1] = rng.rand() periods = frequency_control * \ (max_period - min_period) + max_period # run time through a sine, while changing the period length frequency_output = np.zeros((N, 1)) z = 0 for i in range(N): z = z + 2 * np.pi / periods[i] frequency_output[i] = (np.sin(z) + 1) / 2 return np.hstack([np.ones((N, 1)), 1 - frequency_control]), frequency_output N = 15000 min_period = 2 max_period = 10 n_changepoints = int(N / 200) frequency_control, frequency_output = frequency_generator( N, min_period, max_period, n_changepoints) traintest_cutoff = int(np.ceil(0.7 * N)) train_ctrl, train_output = frequency_control[: traintest_cutoff], frequency_output[: traintest_cutoff] test_ctrl, test_output = frequency_control[ traintest_cutoff:], frequency_output[traintest_cutoff:] esn = ESN(n_inputs=2, n_outputs=1, n_reservoir=200, spectral_radius=0.25, sparsity=0.95, noise=0.001, input_shift=[0, 0], input_scaling=[0.01, 3], teacher_scaling=1.12, teacher_shift=-0.7, out_activation=np.tanh, inverse_out_activation=np.arctanh, random_state=rng, silent=True) pred_train = esn.fit(train_ctrl, train_output) # print "test error:" pred_test = esn.predict(test_ctrl) error = np.sqrt(np.mean((pred_test - test_output)**2)) self.assertAlmostEqual(error, 0.30519018985725715)
def esn_training(index, data_mbs): '''ESN Training for each MBS''' col_mbs = 3 * netcfg.num_lte_bs_mobile + index data_mbs = data_mbs[:, col_mbs] #print(data_rate,'\n') data_mbs_max = max(data_mbs) #print(data_mbs_1_rate_max,'\n') data_mbs = data_mbs / data_mbs_max / 2 #print(data_mbs_1_rate,'\n') data_mbs = np.transpose(np.array([data_mbs])) #print(data_mbs_1_rate,'\n') N = 10000 rng = np.random.RandomState(42) traintest_cutoff = int(np.ceil(0.6 * N)) train_data, train_data_mbs = data[: traintest_cutoff], data_mbs[: traintest_cutoff] test_data, test_data_mbs = data[traintest_cutoff:], data_mbs[ traintest_cutoff:] col_mbs = col_mbs + 1 #print('n = ', col_mbs) zeros = np.zeros(col_mbs) #print('zeros', zeros) ones = np.ones(col_mbs) #print('ones', ones) #exit(0) # create an echo state network esn = ESN( n_inputs=col_mbs, n_outputs=1, n_reservoir=200, spectral_radius=0.25, sparsity=0.25, noise=0.001, input_shift=zeros, input_scaling=ones, #input_scaling=[1, 1], teacher_scaling=2.0, #teacher_scaling=1, teacher_shift=-0.9, #teacher_shift=0, # teacher_scaling = None, # teacher_shift = None, out_activation=np.tanh, inverse_out_activation=np.arctanh, random_state=rng, silent=False) print('----------------------------------------------------------------') print('esn {}'.format(i + 1)) print('----------------------------------------------------------------') print('n_inputs=', esn.n_inputs) print('n_outputs=', esn.n_outputs) print('n_reservoir=', esn.n_reservoir) print('spectral radius=', esn.spectral_radius) print('sparsity=', esn.sparsity) print('noise=', esn.noise) print('input_shift=', esn.input_shift) print('input_scaling=', esn.input_scaling) print('teacher_forcing=', esn.teacher_forcing) # print ('feedback_scaling=' , esn1.feedback_scaling) print('teacher_scaling=', esn.teacher_scaling) print('teacher_shift=', esn.teacher_shift) print('out_activation=', esn.out_activation) print('inverse_out=', esn.inverse_out_activation) print('random_state=', esn.random_state) print('silent=', esn.silent) #print('1') # fitting pred_train = esn.fit(train_data, train_data_mbs) #print('2') # prediction pred_test = esn.predict(data) #print('3') pp.plot(range(len(data)), data_mbs, range(len(pred_test)), pred_test) #print('4') pp.title('MBS no. {}'.format(i + 1)) #print('5') pp.show()
spectral_radius = 0.25, sparsity = 0.95, noise = 0.001, input_shift = [0,0], input_scaling = [0.01, 3], teacher_scaling = 1.12, teacher_shift = -0.7, out_activation = np.tanh, inverse_out_activation = np.arctanh, random_state = rng, silent = False) pred_train = esn.fit(train_ctrl,train_output) print("test error:") pred_test = esn.predict(test_ctrl) print(np.sqrt(np.mean((pred_test - test_output)**2))) window_tr = range(int(len(train_output)/4),int(len(train_output)/4+2000)) plt.figure(figsize=(10,1.5)) plt.plot(train_ctrl[window_tr,1],label='control') plt.plot(train_output[window_tr],label='target') plt.plot(pred_train[window_tr],label='model') plt.legend(fontsize='x-small') plt.title('training (excerpt)') plt.ylim([-0.1,1.1]) window_test = range(2000) plt.figure(figsize=(10,1.5)) plt.plot(test_ctrl[window_test,1],label='control') plt.plot(test_output[window_test],label='target')
Y = np.asarray(Y) trainX = X[:half_range] trainY = Y[:half_range] testX = X[half_range:] testY = Y[half_range:] ne_list = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] for ne in ne_list: print("neurons num: %d" % (ne)) esn = ESN(n_inputs=5, n_outputs=1, n_reservoir=ne, spectral_radius=1.5, random_state=42) esn_train = esn.fit(trainX, trainY) esn_pred = esn.predict(testX) print("esn error: \n" + str(np.sqrt(np.mean((esn_pred.flatten() - testY)**2)))) mlp = MLPRegressor(alpha=0.1, hidden_layer_sizes=(ne, ), max_iter=1000, activation='logistic', learning_rate='constant', learning_rate_init=0.01) mlp_train = mlp.fit(trainX, trainY) mlp_pred = mlp.predict(testX) print("mlp error: \n" + str(np.sqrt(np.mean((mlp_pred.flatten() - testY)**2)))) sgd = MLPRegressor(alpha=0.1,