Beispiel #1
0
def run_dbn():
    X_train, Y_train, X_test, Y_test = normalize_data()
    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')
    # Y_train = np.reshape(Y_train, (79, 2))
    print(X_train.shape, Y_train.shape)

    regressor.fit(X_train, Y_train)

    # Test
    Y_pred = regressor.predict(X_test)
    Y_pred = np.delete(Y_pred, np.s_[-1:], axis=1)
    Y_test = np.delete(Y_test, np.s_[-1:], axis=1)
    print("Y_pred", Y_pred)
    print("Y_test", Y_test)
    print('Done.\nR-squared: %f\nMSE: %f' %
          (r2_score(Y_test, Y_pred), mean_squared_error(Y_test, Y_pred)))
    # print("mean_absolute_error", mean_absolute_error(Y_test, Y_pred) / Y_test.shape[0])
    # print("len", Y_test.shape[0])
    abs_sum = 0
    for i in range(0, 13):
        v1 = Y_pred[i][0]
        v2 = Y_test[i][0]
        print(v1, v2)
        abs_sum += abs(v1 - v2)
    print("final: ", abs_sum, abs_sum / 13)
Beispiel #2
0
def train_model(learning_rate_rbm, learning_rate, batch_size, x_train, y_trian, x_test):
    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(learning_rate_rbm=learning_rate_rbm, learning_rate=learning_rate,
                                            batch_size=batch_size, verbose=False)
    regressor_DBN.fit(x_train, y_trian)
    pred = regressor_DBN.predict(x_test)
    return pred
Beispiel #3
0
    def update_mse(tmp_input_element, tmp_list):
        data_train, label_train, data_test, label_test = \
            HSMemory.create_train_and_test_data(tmp_list, tmp_input_element.number_visible_input)
        tmp_regression = SupervisedDBNRegression(
            hidden_layers_structure=[
                tmp_input_element.number_visible_input,
                tmp_input_element.number_hidden_input
            ],
            learning_rate_rbm=tmp_input_element.learning_rate_rbm,
            learning_rate=tmp_input_element.learning_rate,
            n_epochs_rbm=tmp_input_element.n_epochs_rbm,
            n_iter_backprop=tmp_input_element.n_iter_back_prop,
            contrastive_divergence_iter=tmp_input_element.
            contrastive_divergence_iter,
            batch_size=tmp_input_element.batch_size,
            activation_function=tmp_input_element.activation_function,
            n_hidden_layers_mlp=tmp_input_element.n_hidden_layers_mlp,
            cost_function_name=tmp_input_element.cost_function_name)

        tmp_regression.fit(data_train, label_train)  # train data
        tmp_input_element.train_mse = sum(
            tmp_regression.train_loss) / HSElement.config_n_iter_back_prop

        y_pred_test = tmp_regression.predict(data_test)
        check_nan = np.isnan(y_pred_test).any()

        if check_nan:
            tmp_input_element.test_mse = 1000
        else:
            tmp_input_element.test_mse = mean_squared_error(
                label_test, y_pred_test)
        if np.isnan(tmp_input_element.train_mse) or np.isinf(
                tmp_input_element.train_mse):
            tmp_input_element.train_mse = 1000

        # add to export result
        tmp_result_data = [
            tmp_input_element.learning_rate_rbm,
            tmp_input_element.learning_rate,
            tmp_input_element.number_visible_input,
            tmp_input_element.number_hidden_input, tmp_input_element.train_mse,
            tmp_input_element.test_mse, '', '', '', '', '', '', '', ''
        ]
        TensorGlobal.followHs.append(tmp_result_data)

        TensorGlobal.sessFlg = True
        tf.reset_default_graph()
        del tmp_regression
        return tmp_input_element
Beispiel #4
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 #5
0
X_train = min_max_scaler.fit_transform(X_train)

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

# 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)

# Save the model
#regressor.save('model_regression_128.pkl')

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

# 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)))

#print(Y_pred)
Beispiel #6
0
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)
plt.ylim(min_vl,max_vl)
plt.scatter(y_test,y_pred)
plt.text(0,4,'r={:.3f}'.format(r))
plt.text(0,5,'r2={:.3f}'.format(r2))
Beispiel #7
0
# 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']

X1 = min_max_scaler.fit_transform(X1)

Y_pred1 = regressor.predict(X1)
print('Done.\nR-squared: %f\nMSE: %f' %
      (r2_score(Y1, Y_pred1), mean_squared_error(Y1, Y_pred1)))

#print(Y1, Y_pred1)
'''
#save to csv file
submission = pd.DataFrame({
        "Age": Y1
    })

submission.to_csv("PredictedAge2.csv", index=False)
print('saved')


#for x in range(1,20):
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 #9
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 #10
0
    Target = np.row_stack((Target, pm25[i]))

    # predicting
    if i > predict_start:
        if use_min_max_scaler:
            train_data_last = train_data_last.reshape(
                (1, train_data_last.size))
            tmp_test = min_max_scaler.transform(train_data_last)
        else:
            train_data_last = train_data_last.reshape(
                (1, train_data_last.size))
            tmp_test = train_data_last

        logging.debug("train_data_last:%s", train_data_last)
        logging.debug("tmp_test:%s", tmp_test)
        tmp_pred_DBN = regressor_DBN.predict(tmp_test)[0][0]
        tmp_pred_DBNAdaBoost = regressor_DBNAdaBoost.predict(
            tmp_test)[0][0][0][0]
        tmp_pred_AdaBoost = regressor_AdaBoost.predict(tmp_test)[0]
        tmp_pred_SVM = regressor_SVM.predict(tmp_test)[0]
        logging.info("==========================================")
        logging.info("pred_DBN:%s", tmp_pred_DBN)
        logging.info("pred_DBNAdaBoost:%s", tmp_pred_DBNAdaBoost)
        logging.info("pred_AdaBoost:%s", tmp_pred_AdaBoost)
        logging.info("pred_SVM:%s", tmp_pred_SVM)
        logging.info("correct:%s", pm25[i])
        logging.info("==========================================")

        predict_DBN.append(tmp_pred_DBN)
        predict_DBNAdaBoost.append(tmp_pred_DBNAdaBoost)
        predict_AdaBoost.append(tmp_pred_AdaBoost)
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()