Beispiel #1
0
def train_model(learning_rate_rbm, learning_rate, batch_size, x_train, y_train, x_test, message_queue):
    path_DBN = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), "models"), "deep-belief-network")
    sys.path.append(path_DBN)
    from dbn.tensorflow import SupervisedDBNRegression

    regressor_DBN = SupervisedDBNRegression(hidden_layers_structure=[100],
                                            learning_rate_rbm=learning_rate_rbm,
                                            learning_rate=learning_rate,
                                            n_epochs_rbm=20,
                                            n_iter_backprop=200,
                                            batch_size=batch_size,
                                            activation_function='sigmoid',
                                            verbose=False)
    regressor_DBN.fit(x_train, y_train)
    pred = regressor_DBN.predict(x_test)
    message_queue.put(pred)
    return
Beispiel #2
0
#How to use pd.concat:https://blog.csdn.net/stevenkwong/article/details/52528616

data_rs_x_2013_mm = MinMaxScaler().fit_transform(data_rs_x_2013)
data_rs_x_2014_mm = MinMaxScaler().fit_transform(data_rs_x_2014)
data_rs_x_2015_mm = MinMaxScaler().fit_transform(data_rs_x_2015)
data_rs_x_2016_mm = MinMaxScaler().fit_transform(data_rs_x_2016)

# Training
regressor = SupervisedDBNRegression(hidden_layers_structure=[100],
                                    learning_rate_rbm=0.01,
                                    learning_rate=0.01,
                                    n_epochs_rbm=20,
                                    n_iter_backprop=300,
                                    batch_size=16,
                                    activation_function='relu')
regressor.fit(x_train, y_train)

# Test
#X_test = min_max_scaler.transform(X_test)
y_pred = regressor.predict(x_test)
print('Done.\nR-squared: %f\nMSE: %f' % (r2_score(y_test, y_pred), mean_squared_error(y_test, y_pred)))

r, p = pearsonr(y_test.flatten(), y_pred.flatten())
r2 = r2_score(y_test.flatten(), y_pred.flatten())
MAE = mean_absolute_error(y_test.flatten(), y_pred.flatten())
MSE = mean_squared_error(y_test.flatten(), y_pred.flatten())

min_vl, max_vl = -2, 8
plt.figure()
plt.title('y_test vs y_pred')
plt.xlim(min_vl,max_vl)
Beispiel #3
0
X = min_max_scaler.fit_transform(X)

#print('x_train number: ', X.shape[0])
#print('x_test number: ', X_test.shape[0])
#print('Y_train number: ', Y.shape[0])
#print('y_test number: ', Y_test)

regressor = SupervisedDBNRegression(hidden_layers_structure=[100],
                                    learning_rate_rbm=0.01,
                                    learning_rate=0.01,
                                    n_epochs_rbm=10,
                                    n_iter_backprop=100,
                                    batch_size=16,
                                    activation_function='relu')

regressor.fit(X, Y)

# Save the model
regressor.save('models/abalone_3.pkl')

# Restore it
#regressor = SupervisedDBNRegression.load('models/abalone_2.pkl')

# Test
data1 = pd.read_csv("abalone_test.csv")

data1['age'] = data1['rings'] + 1.5
data1.drop('rings', axis=1, inplace=True)

X1 = data1.drop(['age', 'sex'], axis=1)
Y1 = data1['age']


# Data scaling
min_max_scaler = MinMaxScaler()
X_train = min_max_scaler.fit_transform(X_train)

# Training
regressor = SupervisedDBNRegression(hidden_layers_structure=[7],
                                    learning_rate_rbm=0.01,
                                    learning_rate=0.0001,
                                    n_epochs_rbm=500,  
                                    n_iter_backprop=7409, # run more iter
                                    batch_size=40,
                                    activation_function='relu')
regressor.fit(X_train, Y_train)


# Test
X_test = min_max_scaler.transform(X_test)
Y_pred = regressor.predict(X_test)


mse = mean_squared_error(Y_test, Y_pred)
print('Done.\nR-squared: %f\nMSE: %f' % (r2_score(Y_test, Y_pred), mse))

def mean_absolute_percentage_error(y_test, y_pred): 
    y_test, y_pred = np.array(y_test), np.array(y_pred)
    #print('y_pred',y_pred)
    return np.mean(np.abs(y_test - y_pred)/y_test) * 100
from sklearn.preprocessing import MinMaxScaler

from dbn.tensorflow import SupervisedDBNRegression


# Loading dataset
boston = load_boston()
X, Y = boston.data, boston.target

# Splitting data
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)

# Data scaling
min_max_scaler = MinMaxScaler()
X_train = min_max_scaler.fit_transform(X_train)

# Training
regressor = SupervisedDBNRegression(hidden_layers_structure=[100],
                                    learning_rate_rbm=0.01,
                                    learning_rate=0.01,
                                    n_epochs_rbm=20,
                                    n_iter_backprop=200,
                                    batch_size=16,
                                    activation_function='relu')
regressor.fit(X_train, Y_train)

# Test
X_test = min_max_scaler.transform(X_test)
Y_pred = regressor.predict(X_test)
print 'Done.\nR-squared: %f\nMSE: %f' % (r2_score(Y_test, Y_pred), mean_squared_error(Y_test, Y_pred))
Beispiel #6
0
def train_model(learning_rate, periods, batch_size, feature, label,
                path_out_png):
    path_DBN = os.path.join(
        os.path.join(os.path.dirname(os.path.abspath(__file__)), "models"),
        "deep-belief-network")
    sys.path.append(path_DBN)
    from dbn.tensorflow import SupervisedDBNRegression

    X_train, X_test, Y_train, Y_test = train_test_split(feature,
                                                        label,
                                                        test_size=0.2,
                                                        shuffle=False)

    train_steps_per_period = X_train.shape[0] // periods  # floor
    test_steps_per_period = X_test.shape[0] // periods
    # print(X_train)
    # print(Y_train)
    '''regressor_DBN = SupervisedDBNRegression(hidden_layers_structure=[100],
                                            learning_rate_rbm=0.01,
                                            learning_rate=learning_rate,
                                            n_epochs_rbm=20,
                                            n_iter_backprop=200,
                                            batch_size=batch_size,
                                            activation_function='sigmoid',
                                            verbose=False)'''
    regressor_DBN = SupervisedDBNRegression(learning_rate=learning_rate,
                                            batch_size=batch_size,
                                            verbose=False)

    print("Training model...")
    print("RMSE (on training data):")
    root_mean_squared_errors = []
    for period in range(0, periods):
        x_train = np.array(
            X_train[period * train_steps_per_period:(period + 1) *
                    train_steps_per_period])
        # x_train=x_train.reshape(x_train.size,1)
        y_trian = np.array(
            Y_train[period * train_steps_per_period:(period + 1) *
                    train_steps_per_period])
        # y_trian=y_trian.reshape(y_trian.size,1,1)
        # print(x_train)
        # print(y_trian)
        regressor_DBN.fit(x_train, y_trian)

        x_test = X_test[period * test_steps_per_period:(period + 1) *
                        test_steps_per_period]
        y_test = Y_test[period * test_steps_per_period:(period + 1) *
                        test_steps_per_period]
        predictions = regressor_DBN.predict(x_test)
        # predictions = np.array([predictions])
        # print(predictions.shape)
        # print(y_test.shape)

        root_mean_squared_error = math.sqrt(
            mean_squared_error(y_test, predictions))

        print("  period %02d : %0.2f" % (period, root_mean_squared_error))

        root_mean_squared_errors.append(root_mean_squared_error)

    print("Model training finished.")

    # Output a graph of loss metrics over periods.
    plt.subplot(1, 2, 2)
    plt.ylabel('RMSE')
    plt.xlabel('Periods')
    plt.title("Root Mean Squared Error vs. Periods")
    plt.tight_layout()
    plt.plot(root_mean_squared_errors)
    plt.savefig(path_out_png)

    print("Final RMSE (on training data): %0.2f" % root_mean_squared_error)
Beispiel #7
0
    # training
    if i > train_start:
        if use_min_max_scaler:
            if use_deep:
                tmp_data = min_max_scaler.fit_transform(Data[i - train_deep:i])
            else:
                tmp_data = min_max_scaler.fit_transform(Data)
        else:
            if use_deep:
                tmp_data = Data[i - train_deep:i]
            else:
                tmp_data = Data
        logging.debug("Data:%s", Data.shape)
        logging.debug("tmp_data:%s", tmp_data.shape)
        if use_deep:
            regressor_DBN.fit(tmp_data, Target[i - train_deep:i, 0])
            regressor_DBNAdaBoost.fit(tmp_data, Target[i - train_deep:i, 0])
            regressor_AdaBoost.fit(tmp_data, Target[i - train_deep:i, 0])
            regressor_SVM.fit(tmp_data, Target[i - train_deep:i, 0])
        else:
            regressor_DBN.fit(tmp_data, Target[:, 0])
            regressor_DBNAdaBoost.fit(tmp_data, Target[:, 0])
            regressor_AdaBoost.fit(tmp_data, Target[:, 0])
            regressor_SVM.fit(tmp_data, Target[:, 0])

logging.info(
    'Done.\nDBN:\tR-squared: %f\nMSE: %f' %
    (r2_score(correct, predict_DBN), mean_squared_error(correct, predict_DBN)))
logging.info('DBNAdaBoost:\tR-squared: %f\nMSE: %f' %
             (r2_score(correct, predict_DBNAdaBoost),
              mean_squared_error(correct, predict_DBNAdaBoost)))
def DBN_Run(hidden_layers_struc, learnin_rate_rbm, learnin_rate,
            num_epochs_rbm, num_iter_backprop, batchSize, activation_func):
    # This is a parameter for the start date of the model, it must be entered in the format Month/Day/Year like 2/6/2019
    # The program will find the next closest date to the one listed by iterating forware in the calendar (no leap years)
    # Enter post 1/3/1950 for Volume data to be there
    startDate = "6/26/2000"

    # defines a start time for the project job
    startTime = time.time()
    # Creates the output file director if it is not already created
    if not os.path.isdir("benchmark-output"):
        os.mkdir("benchmark-output")

    # Loading dataset
    SP_Data_Full = pd.read_csv("data-files/S&P_Movement.csv", sep=',')

    # Change start index based on the given start date and read in new dataframe
    startIndex = SP_Data_Full.index[SP_Data_Full["Date"] == startDate].values
    SP_Data = SP_Data_Full.iloc[startIndex[0]:-1]
    SP_Data.reset_index(inplace=True)

    # Shift the data set so that the model is reading in the previous day's information on the High, Low, Close, Volume
    # and Movement
    SP_Data["PrevHigh"] = SP_Data["High"].shift(-1)
    SP_Data["PrevLow"] = SP_Data["Low"].shift(-1)
    SP_Data["PrevClose"] = SP_Data["Close"].shift(-1)
    SP_Data["PrevVolume"] = SP_Data["Volume"].shift(-1)
    SP_Data["PrevMovement"] = SP_Data["Movement"].shift(-1)

    # split the new dataframe into features and targets
    target = SP_Data[["Close"], ["Movement"]]
    features = SP_Data[[
        "Date", "Open", "PrevHigh", "PrevLow", "PrevVolume", "PrevMovement"
    ]]

    features = format_year(features)

    # format the dataframe as a numpy array for the tensorflow functions
    target_array = target.values
    features_array = features.values
    X, Y = features_array, target_array

    # Splitting data into test and train
    X_train, X_test, Y_train, Y_test = train_test_split(X,
                                                        Y,
                                                        test_size=0.2,
                                                        random_state=1337)

    # Data scaling
    min_max_scaler = MinMaxScaler()
    X_train = min_max_scaler.fit_transform(X_train)

    # Training
    regressor = SupervisedDBNRegression(
        hidden_layers_structure=hidden_layers_struc,
        learning_rate_rbm=learnin_rate_rbm,
        learning_rate=learnin_rate,
        n_epochs_rbm=num_epochs_rbm,
        n_iter_backprop=num_iter_backprop,
        batch_size=batchSize,
        activation_function=activation_func)
    regressor.fit(X_train, Y_train)

    trainingTime = time.time() - startTime
    # Test
    X_test = min_max_scaler.transform(X_test)
    Y_pred = regressor.predict(X_test)
    r2 = r2_score(Y_test, Y_pred)
    mse = mean_squared_error(Y_test, Y_pred)
    testingTime = time.time() - startTime - trainingTime

    totalRunTime = time.time() - startTime
    # Text output file for more user friendly reading
    # formated based on the input into this function as the hyperparameters in order delimited by the _
    file = open(
        "benchmark-output/result_" + str(hidden_layers_struc) + "_" +
        str(learnin_rate_rbm) + "_" + str(learnin_rate) + "_" +
        str(num_epochs_rbm) + "_" + str(num_iter_backprop) + "_" +
        str(batchSize) + "_" + str(activation_func) + ".txt", "w+")
    file.write('Done.\nR-squared: %f\nMSE: %f' % (r2, mse) + "\n")
    file.write("Training Time: " + str(trainingTime) + "\n")
    file.write("Testing Time: " + str(testingTime) + "\n")
    file.write("Total Run Time: " + str(totalRunTime) + "\n\n")
    file.write("Network Information:")
    file.write("Hidden Layer Structure: " + str(hidden_layers_struc) + "\n")
    file.write("Learning Rate RBM: " + str(learnin_rate_rbm) + "\n")
    file.write("Learning Rate: " + str(learnin_rate) + "\n")
    file.write("Number of Epochs: " + str(num_epochs_rbm) + "\n")
    file.write("Number of Iterative Backpropogations: " +
               str(num_iter_backprop) + "\n")
    file.write("Batch Size: " + str(batchSize) + "\n")
    file.write("Activation Function: " + str(activation_func) + "\n")
    file.close()
    # CSV file output for use in data visualization
    hiddenlayerNumNodes = hidden_layers_struc[0]
    hiddenlayerNum = hidden_layers_struc[1]
    file = open(
        "benchmark-output/result_" + str(hidden_layers_struc) + "_" +
        str(learnin_rate_rbm) + "_" + str(learnin_rate) + "_" +
        str(num_epochs_rbm) + "_" + str(num_iter_backprop) + "_" +
        str(batchSize) + "_" + str(activation_func) + ".csv", "w+")
    file.write(
        "R-squared,MSE,trainingTime,testingTime,totalRunTime,hiddenlayerNumNodes,hiddenlayerNum,learnin_rate_rbm"
        ",learnin_rate,num_epochs_rbm,num_iter_backprop,batchSize,activation_func\n"
    )
    file.write(
        str(r2) + "," + str(mse) + "," + str(trainingTime) + "," +
        str(testingTime) + "," + str(totalRunTime) + "," +
        str(hiddenlayerNumNodes) + "," + str(hiddenlayerNum) + "," +
        str(learnin_rate_rbm) + "," + str(learnin_rate) + "," +
        str(num_epochs_rbm) + "," + str(num_iter_backprop) + "," +
        str(batchSize) + "," + str(activation_func))
    file.close()