Пример #1
0
def model_fit(param):
    print "Training %s" % param[0]
    neural_shape = [param[0], 15, 1]
    X_train = param[1][0]
    y_train = param[1][1]
    X_test = param[1][2]
    y_test = param[1][3]
    fit_param = {"neural_shape": neural_shape}
    neuralNet = NeuralFlowRegressor()
    neuralNet.fit(X_train, y_train, **fit_param)
    return param[0], np.sqrt(neuralNet.score(X_test, y_test))
Пример #2
0
def model_fit(param):
    print "Training %s"%param[0]
    neural_shape = [param[0],15,1]
    X_train = param[1][0]
    y_train = param[1][1]
    X_test = param[1][2]
    y_test = param[1][3]
    fit_param = {
        "neural_shape":neural_shape
    }
    neuralNet = NeuralFlowRegressor()
    neuralNet.fit(X_train,y_train,**fit_param)
    return param[0],np.sqrt(neuralNet.score(X_test,y_test))
Пример #3
0
def model_fit(param):
    print "Training %s" % param[0]
    X_train = param[1][0]
    y_train = param[1][1]
    X_test = param[1][2]
    y_test = param[1][3]
    neural_shape = [y_train.shape[1] * param[0], 10, y_train.shape[1]]
    acoNet = GAEstimator(cross_rate=0.65, mutation_rate=0.01, pop_size=50)
    fit_param = {"neural_shape": neural_shape}
    acoNet.fit(X_train, y_train, **fit_param)
    fit_param["weights_matrix"] = acoNet.best_archive
    neuralNet = NeuralFlowRegressor()
    neuralNet.fit(X_train, y_train, **fit_param)
    return param[0], np.sqrt(neuralNet.score(X_test, y_test))
def model_fit(param):
    print "Training %s" % param[0]
    neural_shape = [param[0], 15, 1]
    X_train = param[1][0]
    y_train = param[1][1]
    X_test = param[1][2]
    y_test = param[1][3]
    acoNet = ACOEstimator(Q=0.08, epsilon=0.55)
    fit_param = {"neural_shape": neural_shape}
    acoNet.fit(X_train, y_train, **fit_param)
    fit_param["weights_matrix"] = acoNet.best_archive
    neuralNet = NeuralFlowRegressor()
    neuralNet.fit(X_train, y_train, **fit_param)
    return param[0], np.sqrt(neuralNet.score(X_test, y_test))
Пример #5
0
def model_fit(param):
    print "Training %s"%param[0]
    neural_shape = [param[0],15,1]
    X_train = param[1][0]
    y_train = param[1][1]
    X_test = param[1][2]
    y_test = param[1][3]
    acoNet = GAEstimator(cross_rate=0.65,mutation_rate=0.01,pop_size=50)
    fit_param = {
        "neural_shape":neural_shape
    }
    acoNet.fit(X_train,y_train,**fit_param)
    fit_param["weights_matrix"] = acoNet.best_archive
    neuralNet = NeuralFlowRegressor()
    neuralNet.fit(X_train,y_train,**fit_param)
    return param[0],np.sqrt(neuralNet.score(X_test,y_test))
Пример #6
0
def model_fit(param):
    print "Training %s"%param[0]
    X_train = param[1][0]
    y_train = param[1][1]
    X_test = param[1][2]
    y_test = param[1][3]
    neural_shape = [y_train.shape[1]*param[0],10,y_train.shape[1]]
    acoNet = ACOEstimator(Q=0.08,epsilon=0.55)
    fit_param = {
        "neural_shape":neural_shape
    }
    acoNet.fit(X_train,y_train,**fit_param)
    fit_param["weights_matrix"] = acoNet.best_archive
    neuralNet = NeuralFlowRegressor()
    neuralNet.fit(X_train,y_train,**fit_param)
    return param[0],np.sqrt(neuralNet.score(X_test,y_test))
def estimator(n_windows, n_hidden_nodes):
    data = data_train[n_windows]
    X_train = data[0]
    y_train = data[1]
    X_test = data[2]
    y_test = data[3]

    fit_param = {'neural_shape': [2 * n_windows, n_hidden_nodes, 2]}
    neuralNet = NeuralFlowRegressor()
    kfold = KFold(X_train.shape[0], 5)
    score_lst = np.zeros(len(kfold), dtype=np.float32)
    for k, (train, test) in enumerate(kfold):
        neuralNet.fit(X_train[train], y_train[train], **fit_param)
    nn_shape = "%s-%s" % (2 * n_windows, n_hidden_nodes)
    score = neuralNet.score(X_test, y_test)
    neuralNet.save("tmp/score_%s" % score)
    return nn_shape, score
 def fit(self, data=None):
     result = []
     for train_item in self.train_bucket:
         X_train, y_train, X_test, y_test = train_item.getitems()
         n_hidden = np.array([55])
         param_dict = {
             "cross_rate": [0.65, 0.7],
             "pop_size": [45, 50, 60],
             "mutation_rate": np.arange(0.01, 0.04, step=0.01)
         }
         estimator = GAEstimator(cross_rate=0.7,
                                 mutation_rate=0.04,
                                 pop_size=60,
                                 gen_size=100)
         neuralNet = NeuralFlowRegressor(learning_rate=1E-03,
                                         hidden_nodes=n_hidden)
         neural_shape = [X_train.shape[1], n_hidden[0], y_train.shape[1]]
         fit_param = {'neural_shape': neural_shape}
         print "Preparing for grid search"
         gridSearch = GridSearchCV(estimator,
                                   param_dict,
                                   n_jobs=-1,
                                   fit_params=fit_param,
                                   scoring='mean_squared_error',
                                   verbose=1)
         gridSearch.fit(X_train, y_train)
         optimizer = OptimizerNNEstimator(gridSearch.best_estimator_,
                                          neuralNet)
         optimizer.fit(X_train, y_train)
         X_test_f = self.data_source[self.train_len + 1:self.train_len +
                                     self.test_len + 1]
         y_pred_f = optimizer.predict(X_test)
         y_pred = self.fuzzy_transform.defuzzy(X_test_f, y_pred_f)
         score_nn = np.sqrt(mean_squared_error(y_test[1:], y_pred))
         tmp = {
             'score': score_nn,
             'n_sliding': train_item.metadata['sliding_windows'],
             'best_estimator': '%s' % gridSearch.best_estimator_
         }
         result.append(tmp)
     np.savez('model_saved/%s' % score_nn, y_pred=y_pred, y_test=y_test[1:])
     optimizer.save('model_saved/%s_model' % score_nn)
     pd.DataFrame(result).to_csv('score_grid_exhaust_ga.csv', index=None)
def estimator(n_windows,n_hidden_nodes):
    data = data_train[n_windows]
    X_train = data[0]
    y_train = data[1]
    X_test = data[2]
    y_test = data[3]

    fit_param = {
                'neural_shape':[2*n_windows,n_hidden_nodes,2]
            }
    neuralNet = NeuralFlowRegressor()
    kfold = KFold(X_train.shape[0],5)
    score_lst = np.zeros(len(kfold),dtype=np.float32)
    for k,(train,test) in enumerate(kfold):
        neuralNet.fit(X_train[train],y_train[train],**fit_param)
    nn_shape = "%s-%s"%(2*n_windows,n_hidden_nodes)
    score = neuralNet.score(X_test,y_test)
    neuralNet.save("tmp/score_%s"%score)
    return nn_shape,score
Пример #10
0
X_train = X_dat[:training_set_size]
X_test = X_dat[training_set_size:training_set_size+testing_set_size]
y_train = fuzzy_train[1:training_set_size+1]
y_test = data[training_set_size+1:training_set_size+testing_set_size+1]
#
# # Setup paramters of model
# n_hidden = np.array([80])
# # neural_shape = [X_train.shape[1],n_hidden,y_train.shape[1]]
# # Initialize ACO Estimator
# estimator = ACOEstimator(Q=0.65,epsilon=0.1,number_of_solutions=130,hidden_nodes=n_hidden)
# # estimator = GAEstimator()
# # fit_param = {'neural_shape':neural_shape}
score_nodes = {}
print "Initialize estimator"
for n_hidden in np.arange(28,80):
	neuralNet = NeuralFlowRegressor(learning_rate=1E-03,hidden_nodes=np.array([n_hidden]))
	neuralNet.fit(X_train,y_train)
	print "Score hidden nodes = %s : %s"%(n_hidden,neuralNet.score(X_train,y_train))
	score_nodes["%s"%n_hidden]=neuralNet.score(X_train,y_train)
print score_nodes
score_df = pd.DataFrame(score_nodes.items(),columns=['Nodes','Score'])
score_df.to_csv('score_lst.csv',index=None)
#gridSearch = GridSearchCV(neuralNet,{'hidden_nodes':[np.arange(28,50).reshape(-1,1)]},n_jobs=-1)
#gridSearch.fit(X_train,y_train)
# print gridSearch.score(X_test,y_test)
#print gridSearch.grid_scores_
#print "Best model %s"%gridSearch.best_estimator_
# optimizer = OptimizerNNEstimator(estimator,neuralNet)
# optimizer.fit(X_train,y_train)
#
# # <codecell>
Пример #11
0
                                     fuzzy_transform=FuzzyProcessor(
                                         automf=True, fuzzy_distance=0.01))
bruteGrid.transform(data)
result = []
for train_item in bruteGrid.train_bucket:
    X_train, y_train, X_test, y_test = train_item.getitems()
    hidden_assume = X_train.shape[1] + y_train.shape[1]
    n_hidden_range = np.arange(hidden_assume, hidden_assume + 2)
    for hidden_node in n_hidden_range:
        n_hidden = np.array([hidden_node])
        estimator = GAEstimator(cross_rate=0.65,
                                mutation_rate=0.01,
                                pop_size=45,
                                gen_size=100)
        neuralNet = NeuralFlowRegressor(learning_rate=1E-03,
                                        steps=6000,
                                        hidden_nodes=n_hidden)
        neural_shape = [X_train.shape[1], n_hidden[0], y_train.shape[1]]
        fit_param = {'neural_shape': neural_shape}
        optimizer = OptimizerNNEstimator(estimator, neuralNet)
        optimizer.fit(X_train, y_train, **fit_param)
        X_test_f = bruteGrid.data_source[bruteGrid.train_len +
                                         1:bruteGrid.train_len +
                                         bruteGrid.test_len + 1]
        # y_pred_f = neuralNet.predict(X_test)
        y_pred_f = optimizer.predict(X_train)
        # y_pred = bruteGrid.fuzzy_transform.defuzzy(X_test_f, y_pred_f)
        score_nn = np.sqrt(mean_squared_error(y_pred=y_pred_f, y_true=y_train))
        tmp = {
            'score': score_nn,
            'n_sliding': train_item.metadata['sliding_windows'],
Пример #12
0
score_list = {}
for n_hidden in np.arange(10, 30, step=1):
    # n_hidden = 80
    # Define neural shape
    # Input layer: [n_sample*n_size]
    # Hidden layer:
    # Output layer: regression
    neural_shape = [dataFeeder.input_size, n_hidden, dataFeeder.output_size]
    # Initialize GA Estimator
    # estimator = GAEstimator(cross_rate=0.7,mutation_rate=0.04,pop_size=60,gen_size=100)

    fit_param = {'neural_shape': neural_shape}

    # Initialize neural network model for regression
    neuralNet = NeuralFlowRegressor(learning_rate=1E-03,
                                    optimize='SGD',
                                    activation='sigmoid')

    # There are many techniques for combining GA with NN. One of this, the optimizer solution of GA will be weights initialized of NN
    # optimizer = OptimizerNNEstimator(estimator,neuralNet)
    optimizer = neuralNet
    optimizer.fit(X_train, y_train, **fit_param)
    score = optimizer.score(X_test, y_test)
    print score
    score_list[n_hidden] = score
    optimizer.save("params/model_full_metric_%s" % score)
# if score < 0.01:
# y_pred = optimizer.predict(X_test)
score_list = pd.Series(score_list)
print "Optimal hidden nodes: %s, with score = %s" % (score_list.argmin(),
                                                     score_list.min())
Пример #13
0
if __name__ == '__main__':
    best_estimator = None
    best_score = np.Inf
    for loop in np.arange(1, 20):
        n_input = 4
        n_periodic = 1
        n_hidden = 15
        neural_shape = [n_input + n_periodic, n_hidden, 1]
        Q = 0.09
        epsilon = 0.55

        dataFeeder = TrafficFeeder()
        X_train, y_train = dataFeeder.fetch_traffic_training(
            n_input, 1, (40, 46))
        X_test, y_test = dataFeeder.fetch_traffic_test(n_input, 1, (46, 48))
        # retrieve = [n_input+1,(X_train,y_train,X_test,y_test)]
        acoNet = ACOEstimator(Q=Q, epsilon=epsilon)
        fit_param = {"neural_shape": neural_shape}
        acoNet.fit(X_train, y_train, **fit_param)
        fit_param["weights_matrix"] = acoNet.best_archive
        neuralNet = NeuralFlowRegressor()
        neuralNet.fit(X_train, y_train, **fit_param)
        y_pred = dataFeeder.convert(neuralNet.predict(X_test))
        score = np.sqrt(mean_squared_error(y_pred, y_test))
        if (score < best_score):
            best_estimator = acoNet
            print score
        # plot_fig(y_pred,y_test)
    print best_score, best_estimator
Пример #14
0
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from utils.SlidingWindowUtil import SlidingWindow
from io_utils.GFeeder import GFeeder
from estimators.NeuralFlow import NeuralFlowRegressor
from utils.GraphUtil import *
from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler()
dat = pd.read_csv('../sample_610_10min.csv', index_col=0, parse_dates=True)
training_size = 3000
test_size = 600
gFeeder = GFeeder()
dat.cpu_rate = np.array(scaler.fit_transform(dat.cpu_rate))
X_dat = np.array(list(SlidingWindow(dat.cpu_rate, sliding_number=4)))

X_train = X_dat[:training_size]
y_train = np.array(dat.cpu_rate[:training_size].tolist()).reshape(-1, 1)
X_test = X_dat[training_size + 1:training_size + 1 + test_size]
y_test = np.array(dat.cpu_rate[training_size + 1:training_size + test_size +
                               1].tolist()).reshape(-1, 1)

nn = NeuralFlowRegressor(learning_rate=1E-03, hidden_nodes=np.array([55]))
nn.fit(X_train, y_train)
y_pred = nn.predict(X_test)
plot_figure(y_pred=y_pred, y_true=y_test, title="Neural Flow sliding window 4")
Пример #15
0
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from utils.SlidingWindowUtil import SlidingWindow
from io_utils.GFeeder import GFeeder
from estimators.NeuralFlow import NeuralFlowRegressor
from utils.GraphUtil import *
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
dat = pd.read_csv('../sample_610_10min.csv',index_col=0,parse_dates=True)
training_size = 3000
test_size = 600
gFeeder = GFeeder()
dat.cpu_rate = np.array(scaler.fit_transform(dat.cpu_rate))
X_dat = np.array(list(SlidingWindow(dat.cpu_rate,sliding_number=4)))

X_train = X_dat[:training_size]
y_train = np.array(dat.cpu_rate[:training_size].tolist()).reshape(-1,1)
X_test = X_dat[training_size + 1:training_size + 1 + test_size]
y_test = np.array(dat.cpu_rate[training_size + 1:training_size + test_size + 1].tolist()).reshape(-1,1)

nn = NeuralFlowRegressor(learning_rate=1E-03,hidden_nodes=np.array([55]))
nn.fit(X_train,y_train)
y_pred = nn.predict(X_test)
plot_figure(y_pred=y_pred,y_true=y_test,title="Neural Flow sliding window 4")
Пример #16
0
# Initialize ACO Estimator
estimator = ACOEstimator(Q=0.65, epsilon=0.2, number_of_solutions=130)

fit_param = {'neural_shape': neural_shape}

# fuzzy before training
# fuzzy step:
X_train_f = fuzzy(X_train)
y_train_f = fuzzy(y_train)
X_test_f = fuzzy(X_test)

sliding_number = 2
X_train = list(SlidingWindow(X_train_f, sliding_number))
# # Initialize neural network model for regression
neuralNet = NeuralFlowRegressor()
X_test_f = list(SlidingWindow(X_test_f, sliding_number))
# There are many techniques for combining GA with NN. One of this, the optimizer solution of GA will be weights initialized of NN
optimizer = OptimizerNNEstimator(estimator, neuralNet)
# optimizer = neuralNet
optimizer.fit(X_train, y_train_f[1:], **fit_param)
print optimizer.score(X_train_f, y_train_f[1:])

y_pred = optimizer.predict(X_test_f)

#defuzzy step:
ft = defuzzy(X_test, y_pred)
#mean_squared_error
print mean_squared_error(y_test, ft)
plot_figure(ft, y_test, color=['blue', 'red'], title='FGANN')
Пример #17
0
from estimators.NeuralFlow import NeuralFlowRegressor
from utils.GraphUtil import *

if __name__ == '__main__':
    n_input = 4
    n_periodic = 1
    n_hidden = 15
    neural_shape = [n_input+n_periodic,n_hidden,1]

    cross_rate = 0.6
    mutation_rate = 0.04
    pop_size = 50

    dataFeeder = TrafficFeeder()
    X_train,y_train = dataFeeder.fetch_traffic_training(n_input,1,(40,46))
    X_test,y_test = dataFeeder.fetch_traffic_test(n_input,1,(46,48))
    # retrieve = [n_input+1,(X_train,y_train,X_test,y_test)]
    gaEstimator = GAEstimator(cross_rate=cross_rate,mutation_rate=mutation_rate,pop_size=pop_size)
    fit_param = {
        "neural_shape":neural_shape
    }
    gaEstimator.fit(X_train,y_train,**fit_param)
    fit_param["weights_matrix"] = gaEstimator.best_archive
    neuralNet = NeuralFlowRegressor()
    neuralNet.fit(X_train,y_train,**fit_param)
    y_pred = dataFeeder.convert(neuralNet.predict(X_test))
    print sqrt(mean_squared_error(y_pred,y_test))
    plot_figure(y_pred,y_test)


Пример #18
0
# Initialize ACO Estimator 130
estimator = ACOEstimator(Q=0.65,
                         epsilon=0.2,
                         number_of_solutions=10,
                         hidden_nodes=n_hidden)

# fit_param = {'neural_shape':neural_shape}

# fuzzy before training
# fuzzy step:
X_train_f = fuzzy(X_train)
y_train_f = fuzzy(y_train)
X_test_f = fuzzy(X_test)

# # # Initialize neural network model for regression
neuralNet = NeuralFlowRegressor(learning_rate=1E-04, hidden_nodes=n_hidden)
# # There are many techniques for combining GA with NN. One of this, the optimizer solution of GA will be weights initialized of NN
optimizer = OptimizerNNEstimator(estimator, neuralNet)
# # optimizer = neuralNet
optimizer.fit(X_train_f, y_train_f)
# print  optimizer.score(X_train_f,y_train_f)
#
y_pred = optimizer.predict(X_test_f)

#defuzzy step:
ft = defuzzy(X_test, y_pred)
#mean_squared_error
print mean_squared_error(y_test, ft)
plot_figure(ft, y_test, color=['blue', 'red'], title='FGANN')