mlp_clf_mnist_test

import os
os.environ['CUDA_VISIBLE_DEVICES'] = "0"
from mlp_clf import MLPClassifier
import numpy as np
import tensorflow as tf
from sklearn.datasets import load_svmlight_file

#from scipy.sparse import csr_matrix
def mean(numbers):         #计算平均值
    s = 0.0
    for num in numbers:
        s = s + num
    return s/len(numbers)
def dev(numbers, mean):    #计算方差
    sdev = 0.0
    for num in numbers:
        sdev = sdev + (num - mean)**2
    return pow(sdev / (len(numbers)-1), 0.5)


if __name__ == '__main__':
    mze_array=[]
    mae_array=[]
    mse_array=[]

    trainfile = "test_housing.0"
    testfile  = "train_housing.0"

    X_train0, y_train = load_svmlight_file(trainfile)
    X_test0 , y_test  = load_svmlight_file(testfile)
    X_train = X_train0.todense()
    X_test = X_test0.todense()
    attribute = np.shape(X_train)[-1]#tf.shape(X_train)
    n_class = tf.reduce_max(y_train)
    print("n_class:", n_class)

    y_train = y_train - 1
    y_test = y_test - 1



    clf = MLPClassifier(attribute, 5, [100]*3)
    log = clf.fit(X_train, y_train, n_epoch=1000, val_data=(X_test, y_test))
    Y_pred = clf.predict(X_test)
    mze = 1- (Y_pred == y_test).mean()
    mae = np.abs(Y_pred.ravel()-y_test.ravel()).astype(float).mean()
    mse = np.power(Y_pred.ravel()-y_test.ravel(),2).astype(float).mean()

    print("mze: %.4f" % mze)
    print("mae:", mae)
    print("mse:", mse)

 

上一篇:Keras之DNN::基于Keras(sigmoid+binary_crossentropy+predict_classes)利用DNN实现二分类——DIY二分类数据集&预测新数据点


下一篇:Keras之ML~P:基于Keras中建立的简单的二分类问题的神经网络模型(根据200个数据样本预测新的5+1个样本)——类别预测