Example #1
0
def test_RBM(train_X, train_Y, test_X, test_Y):
    # Create and train RBM
    rbm = RBM(28 * 28, 500)
    rbm.fit(train_X)

    rbm.get_filters()

    train_X = np.array([rbm.inference(x) for x in train_X])
    test_X = np.array([rbm.inference(x) for x in test_X])

    logreg = LogisticRegression(max_iter=10)
    logreg.fit(train_X, train_Y)

    predict_Y = logreg.predict(train_X)
    print("Accuracy on training data")
    print(accuracy_score(train_Y, predict_Y))
    predict_Y = logreg.predict(test_X)
    print("Accuracy on test data")
    print(accuracy_score(test_Y, predict_Y))
        print("Epoch: {}, Reconstruction Error: {}".format(num, ReconErr))

    for num in range(Epoch):
        batch_xs = rbm1.passThrough(MA_data)
        new_w, new_hb, new_vb, ReconErr = rbm2.train(batch_xs)
        print("Epoch: {}, Reconstruction Error: {}".format(num, ReconErr))

    for num in range(Epoch):
        batch_xs = rbm1.passThrough(MA_data)
        batch_xs1 = rbm2.passThrough(batch_xs)
        new_w, new_hb, new_vb, ReconErr = rbm3.train(batch_xs1)
        print("Epoch: {}, Reconstruction Error: {}".format(num, ReconErr))

    temp1 = rbm1.passThrough(MA_data)
    temp2 = rbm2.passThrough(temp1)
    temp3 = rbm3.inference(temp2)
    temp4 = rbm2.passBack(temp3)
    REC = rbm1.passBack(temp4)

    print(np.shape(REC))
    RMSE = np.sqrt(
        np.mean(np.square(MA_data - REC), axis=0) /
        float(np.shape(MA_data)[0]))
    print("Shape of RMSE: {}, RMSE {}".format(np.shape(RMSE), RMSE))
    ReconRank = rankdata(RMSE)

    with open('FeatureRank.csv', 'wb') as csvfile:
        spamwriter = csv.writer(csvfile,
                                delimiter=',',
                                quotechar='|',
                                quoting=csv.QUOTE_MINIMAL)