def model_test_for_esn_base(Base, esn_param, X_train, X_test, Y_train, Y_test, n_estimators=500, learning_rate=0.01, Score=MLE, Dist=Normal, verbose=True, verbose_eval=100, plot_predict=True, return_y_pred=False, return_y_dists=False, return_mse=False): ESN = SimpleESN(n_readout=esn_param['n_readout'], n_components=esn_param['n_components'], damping=esn_param['damping'], weight_scaling=esn_param['weight_scaling'], discard_steps=0, random_state=None) X_train = ESN.fit_transform(X_train) X_test = ESN.fit_transform(X_test) ngb = NGBRegressor(Base=Base, n_estimators=n_estimators, verbose=verbose, verbose_eval=verbose_eval, learning_rate=learning_rate, Dist=Dist, Score=Score) print(ESN, '\n') print(ngb, '\n') ngb.fit(X_train, Y_train) Y_preds = ngb.predict(X_test) Y_dists = ngb.pred_dist(X_test) # return norm method: mean std # test Mean Squared Error test_MSE = mean_squared_error(Y_preds, Y_test) print('\nTest MSE', test_MSE) # test Negative Log Likelihood test_NLL = -Y_dists.logpdf(Y_test).mean() print('Test NLL', test_NLL) if plot_predict: df = pd.concat([Y_test, pd.Series(Y_preds, index=Y_test.index)], axis=1) df.columns = ['test', 'pred'] df.plot(figsize=(10, 4), title='MSE:{} NLL:{}'.format(round(test_MSE, 4), round(test_NLL, 4))) if (return_y_pred) & (not (return_y_dists)): return pd.Series(Y_preds, index=Y_test.index) if (not (return_y_pred)) & (return_y_dists): return Y_dists if (return_y_pred) & (return_y_dists): return pd.Series(Y_preds, index=Y_test.index), Y_dists if return_mse: return test_MSE
def __init__(self, n_readout=1000, n_components=100, damping=0.5, weight_scaling=0.9, discard_steps=0, random_state=None, alpha=1, kernel="poly", degree=3): self.n_readout = n_readout self.n_components = n_components self.damping = damping self.weight_scaling = weight_scaling self.discard_steps = discard_steps self.random_state = random_state self.alpha = alpha self.kernel = kernel self.degree = degree self.ESN = SimpleESN(n_readout=self.n_readout, n_components=self.n_components, damping=self.damping, weight_scaling=self.weight_scaling, discard_steps=self.discard_steps, random_state=check_random_state( self.random_state)) self.Kernel_Ridge = KernelRidge(alpha=self.alpha, kernel=self.kernel, gamma=None, degree=self.degree, coef0=1, kernel_params=None)
class ESN_Ridge_learner(): def __init__(self, n_readout=1000, n_components=100, damping=0.5, weight_scaling=0.9, discard_steps=0, random_state=None, alpha=0.01): self.n_readout = n_readout self.n_components = n_components self.damping = damping self.weight_scaling = weight_scaling self.discard_steps = discard_steps self.random_state = random_state self.alpha = alpha self.ESN = SimpleESN(n_readout=self.n_readout, n_components=self.n_components, damping=self.damping, weight_scaling=self.weight_scaling, discard_steps=self.discard_steps, random_state=check_random_state( self.random_state)) self.Ridge = Ridge(alpha=self.alpha) # change y to np.array(y).reshape(-1,) def fit(self, X, y): self.ESN.fit(X) self.Ridge.fit(self.ESN.transform(X), np.array(y).reshape(-1, )) return self def predict(self, X): return self.Ridge.predict(self.ESN.transform(X)) def get_params(self, deep=True): if deep: params = { 'n_readout': self.n_readout, 'n_components': self.n_components, 'damping': self.damping, 'weight_scaling': self.weight_scaling, 'discard_steps': self.discard_steps, 'random_state': self.random_state, 'alpha': self.alpha } return params else: params = { 'n_readout': self.n_readout, 'n_components': self.n_components, 'damping': self.damping, 'weight_scaling': self.weight_scaling } return params
def __init__(self, n_readout=1000, n_components=100, damping=0.5, weight_scaling=0.9, discard_steps=0, random_state=None, alpha=0.01): self.Ridge = Ridge(alpha=alpha) self.ESN = SimpleESN(n_readout=n_readout, n_components=n_components, damping=damping, weight_scaling=weight_scaling, discard_steps=discard_steps, random_state=check_random_state(random_state))
def __init__(self, n_readout=1000, n_components=100, damping=0.5, weight_scaling=0.9, discard_steps=0, random_state=None, criterion="mse", max_depth=None): self.n_readout = n_readout self.n_components = n_components self.damping = damping self.weight_scaling = weight_scaling self.discard_steps = discard_steps self.random_state = random_state self.criterion = criterion self.max_depth = max_depth self.ESN = SimpleESN(n_readout=self.n_readout, n_components=self.n_components, damping=self.damping, weight_scaling=self.weight_scaling, discard_steps=self.discard_steps, random_state=check_random_state( self.random_state)) self.Decision_Tree = DecisionTreeRegressor(criterion=self.criterion, splitter="best", max_depth=self.max_depth, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_features=None, random_state=None, max_leaf_nodes=None, min_impurity_decrease=0., min_impurity_split=None, presort=False)
def __init__(self, n_readout=1000, n_components=100, damping=0.5, weight_scaling=0.9, discard_steps=0, random_state=None, epsilon=0.0, C=1.0, max_iter=1000): self.n_readout = n_readout self.n_components = n_components self.damping = damping self.weight_scaling = weight_scaling self.discard_steps = discard_steps self.random_state = random_state self.epsilon = epsilon self.C = C self.max_iter = max_iter self.ESN = SimpleESN(n_readout=self.n_readout, n_components=self.n_components, damping=self.damping, weight_scaling=self.weight_scaling, discard_steps=self.discard_steps, random_state=check_random_state( self.random_state)) self.Linear_SVR = LinearSVR(epsilon=self.epsilon, tol=1e-4, C=self.C, loss='epsilon_insensitive', fit_intercept=True, intercept_scaling=1., dual=True, verbose=0, random_state=None, max_iter=self.max_iter)
def __init__(self, n_readout=1000, n_components=100, damping=0.5, weight_scaling=0.9, discard_steps=0, random_state=None, alpha=0.01): self.n_readout = n_readout self.n_components = n_components self.damping = damping self.weight_scaling = weight_scaling self.discard_steps = discard_steps self.random_state = random_state self.alpha = alpha self.ESN = SimpleESN(n_readout=self.n_readout, n_components=self.n_components, damping=self.damping, weight_scaling=self.weight_scaling, discard_steps=self.discard_steps, random_state=check_random_state( self.random_state)) self.Lasso = Lasso(alpha=self.alpha)
class ESN_Ridge_learner(): def __init__(self, n_readout=1000, n_components=100, damping=0.5, weight_scaling=0.9, discard_steps=0, random_state=None, alpha=0.01): self.Ridge = Ridge(alpha=alpha) self.ESN = SimpleESN(n_readout=n_readout, n_components=n_components, damping=damping, weight_scaling=weight_scaling, discard_steps=discard_steps, random_state=check_random_state(random_state)) def fit(self, X, y): self.ESN.fit(X) self.Ridge.fit(self.ESN.transform(X), y) return self def predict(self, X): return self.Ridge.predict(self.ESN.transform(X))
def __init__(self, args, esn_param=None, box_cox=False): X_train, X_test, Y_train, Y_test = get_data2(hour_num=1, transform='sin+cos', train_index=[3001, 7002], test_index=[2000, 3001], return_y_scaler=False, drop_else=True, box_cox=box_cox) if esn_param is not None: ESN = SimpleESN(n_readout=esn_param['n_readout'], n_components=esn_param['n_components'], damping=esn_param['damping'], weight_scaling=esn_param['weight_scaling'], discard_steps=esn_param['discard_steps'], random_state=check_random_state( esn_param['random_state'])) ESN.fit(X_train) X_train = ESN.transform(X_train) X_test = ESN.transform(X_test) print('ESN param:', esn_param) self.xs = np.array(X_train, dtype=np.float32) self.ys = np.array(Y_train, dtype=np.float32).reshape(len(Y_train), 1) self.test_xs = np.array(X_test, dtype=np.float32) self.test_ys = np.array(Y_test, dtype=np.float32).reshape(len(Y_test), 1) # Standardize input features self.input_mean = np.mean(self.xs, 0) self.input_std = np.std(self.xs, 0) # self.xs = (self.xs - self.input_mean)/self.input_std # Target mean and std self.target_mean = np.mean(self.ys, 0)[0] self.target_std = np.std(self.ys, 0)[0] self.batch_size = args.batch_size
from sklearn.pipeline import Pipeline if __name__ == '__main__': X = loadtxt('MackeyGlass_t17.txt') X = atleast_2d(X).T train_length = 2000 test_length = 2000 X_train = X[:train_length] y_train = X[1:train_length+1] X_test = X[train_length:train_length+test_length] y_test = X[train_length+1:train_length+test_length+1] # Simple training my_esn = SimpleESN(n_readout=1000, n_components=1000, damping=0.3, weight_scaling=1.25) echo_train = my_esn.fit_transform(X_train) regr = Ridge(alpha=0.01) regr.fit(echo_train, y_train) echo_test = my_esn.transform(X_test) y_true, y_pred = y_test, regr.predict(echo_test) err = mean_squared_error(y_true, y_pred) fp = plt.figure(figsize=(12, 4)) trainplot = fp.add_subplot(1, 3, 1) trainplot.plot(X_train[100:600], 'b') trainplot.set_title('Some training signal') echoplot = fp.add_subplot(1, 3, 2) echoplot.plot(echo_train[100:600, :20]) echoplot.set_title('Some reservoir activation') testplot = fp.add_subplot(1, 3, 3)
class ESN_decision_tree_learner(): def __init__(self, n_readout=1000, n_components=100, damping=0.5, weight_scaling=0.9, discard_steps=0, random_state=None, criterion="mse", max_depth=None): self.n_readout = n_readout self.n_components = n_components self.damping = damping self.weight_scaling = weight_scaling self.discard_steps = discard_steps self.random_state = random_state self.criterion = criterion self.max_depth = max_depth self.ESN = SimpleESN(n_readout=self.n_readout, n_components=self.n_components, damping=self.damping, weight_scaling=self.weight_scaling, discard_steps=self.discard_steps, random_state=check_random_state( self.random_state)) self.Decision_Tree = DecisionTreeRegressor(criterion=self.criterion, splitter="best", max_depth=self.max_depth, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_features=None, random_state=None, max_leaf_nodes=None, min_impurity_decrease=0., min_impurity_split=None, presort=False) def fit(self, X, y): self.ESN.fit(X) self.Decision_Tree.fit(self.ESN.transform(X), y) return self def predict(self, X): return self.Decision_Tree.predict(self.ESN.transform(X)) def get_params(self, deep=True): if deep: params = { 'n_readout': self.n_readout, 'n_components': self.n_components, 'damping': self.damping, 'weight_scaling': self.weight_scaling, 'discard_steps': self.discard_steps, 'random_state': self.random_state, 'criterion': self.criterion, 'max_depth': self.max_depth } return params else: params = { 'n_readout': self.n_readout, 'n_components': self.n_components, 'damping': self.damping, 'weight_scaling': self.weight_scaling } return params def set_params(self, **parameters): for parameter, value in parameters.items(): setattr(self, parameter, value) return self
class ESN_linear_svr_learner(): def __init__(self, n_readout=1000, n_components=100, damping=0.5, weight_scaling=0.9, discard_steps=0, random_state=None, epsilon=0.0, C=1.0, max_iter=1000): self.n_readout = n_readout self.n_components = n_components self.damping = damping self.weight_scaling = weight_scaling self.discard_steps = discard_steps self.random_state = random_state self.epsilon = epsilon self.C = C self.max_iter = max_iter self.ESN = SimpleESN(n_readout=self.n_readout, n_components=self.n_components, damping=self.damping, weight_scaling=self.weight_scaling, discard_steps=self.discard_steps, random_state=check_random_state( self.random_state)) self.Linear_SVR = LinearSVR(epsilon=self.epsilon, tol=1e-4, C=self.C, loss='epsilon_insensitive', fit_intercept=True, intercept_scaling=1., dual=True, verbose=0, random_state=None, max_iter=self.max_iter) def fit(self, X, y): self.ESN.fit(X) self.Linear_SVR.fit(self.ESN.transform(X), y) return self def predict(self, X): return self.Linear_SVR.predict(self.ESN.transform(X)) def get_params(self, deep=True): if deep: params = { 'n_readout': self.n_readout, 'n_components': self.n_components, 'damping': self.damping, 'weight_scaling': self.weight_scaling, 'discard_steps': self.discard_steps, 'random_state': self.random_state, 'epsilon': self.epsilon, 'C': self.C, 'max_iter': self.max_iter } return params else: params = { 'n_readout': self.n_readout, 'n_components': self.n_components, 'damping': self.damping, 'weight_scaling': self.weight_scaling } return params
class ESN_kernel_ridge_learner(): def __init__(self, n_readout=1000, n_components=100, damping=0.5, weight_scaling=0.9, discard_steps=0, random_state=None, alpha=1, kernel="poly", degree=3): self.n_readout = n_readout self.n_components = n_components self.damping = damping self.weight_scaling = weight_scaling self.discard_steps = discard_steps self.random_state = random_state self.alpha = alpha self.kernel = kernel self.degree = degree self.ESN = SimpleESN(n_readout=self.n_readout, n_components=self.n_components, damping=self.damping, weight_scaling=self.weight_scaling, discard_steps=self.discard_steps, random_state=check_random_state( self.random_state)) self.Kernel_Ridge = KernelRidge(alpha=self.alpha, kernel=self.kernel, gamma=None, degree=self.degree, coef0=1, kernel_params=None) def fit(self, X, y): self.ESN.fit(X) self.Kernel_Ridge.fit(self.ESN.transform(X), y) return self def predict(self, X): return self.Kernel_Ridge.predict(self.ESN.transform(X)) def get_params(self, deep=True): if deep: params = { 'n_readout': self.n_readout, 'n_components': self.n_components, 'damping': self.damping, 'weight_scaling': self.weight_scaling, 'discard_steps': self.discard_steps, 'random_state': self.random_state, 'alpha': self.alpha, 'kernel': self.kernel, 'degree': self.degree } return params else: params = { 'n_readout': self.n_readout, 'n_components': self.n_components, 'damping': self.damping, 'weight_scaling': self.weight_scaling } return params