daal4py 随机森林模型训练mnist并保存模型给C++ daal predict使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | # daal4py Decision Forest Classification Training example Serialization import daal4py as d4p import numpy as np import pickle from sklearn.datasets import fetch_mldata from sklearn.model_selection import train_test_split def get_mnist(): mnist = fetch_mldata( 'MNIST original' ) X_train, X_test, y_train, y_test = train_test_split(mnist.data, mnist.target, train_size=60000, test_size=10000) data = np.ascontiguousarray(X_train, dtype=np.float32) labels = np.ascontiguousarray(y_train, dtype=np.float32).reshape(y_train.shape[0],1) return data, labels # serialized model can be used only by daal4py with pickle def pickle_serialization(result, file= 'df_result.pkl' ): with open(file, 'wb' ) as out: pickle.dump(result, out) # universal naitive DAAL model serializtion. Can be used in all DAAL interfaces C++/Java/pydaal/daal4py def native_serialization(result, file= 'native_result.txt' ): daal_buff = result.__getstate__() File = open(file, "wb" ) File.write(daal_buff) if __name__ == "__main__" : data, labels = get_mnist() # 'fptype' parameter should be the same type as input numpy arrays to archive the best performance # (no data conversation in this case) train = d4p.decision_forest_classification_training(10, fptype= 'float' , nTrees=100, minObservationsInLeafNode=1, engine = d4p.engines_mt19937(seed=777),bootstrap=True) result = train.compute(data, labels) # serialize model to file pickle_serialization(result) native_serialization(result) |
python预测
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import daal4py as d4p import numpy as np import pickle from sklearn.datasets import fetch_mldata from sklearn.model_selection import train_test_split def get_mnist_test(): mnist = fetch_mldata( 'MNIST original' ) X_train, X_test, y_train, y_test = train_test_split(mnist.data, mnist.target, train_size = 60000 , test_size = 10000 ) pdata = np.ascontiguousarray(X_test, dtype = np.float32) plabels = np.ascontiguousarray(y_test, dtype = np.float32).reshape(y_test.shape[ 0 ], 1 ) return pdata, plabels def checkAccuracy(plabels, prediction): t = 0 count = 0 for i in plabels: if i ! = prediction[t]: count = count + 1 t = t + 1 return ( 1 - count / t) def pickle_deserialization( file = 'df_result.pkl' ): with open ( file , 'rb' ) as inp: return pickle.load(inp) def native_deserialization( file = 'native_result.txt' ): daal_result = d4p.decision_forest_classification_training_result() File = open ( file , "rb" ) daal_buff = File .read() daal_result.__setstate__(daal_buff) return daal_result if __name__ = = "__main__" : nClasses = 10 pdata, plabels = get_mnist_test() #deserialize model deserialized_result_pickle = pickle_deserialization() deserialized_result_naitive = native_deserialization() # now predict using the deserialized model from the training above, fptype is float as input data predict_algo = d4p.decision_forest_classification_prediction(nClasses, fptype = 'float' ) # just set pickle-obtained model into compute predict_result = predict_algo.compute(pdata, deserialized_result_pickle.model) print ( "\nAccuracy:" , checkAccuracy(plabels, predict_result.prediction)) # the same result as above. just set native-obtained model into compute predict_result = predict_algo.compute(pdata, deserialized_result_naitive.model) print ( "\nAccuracy:" , checkAccuracy(plabels, predict_result.prediction)) |
c++使用该daal4py的模型:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | /** * <a name="DAAL-EXAMPLE-CPP-DF_CLS_DENSE_BATCH"></a> * \example df_cls_dense_batch.cpp */ #include "daal.h" #include "service.h" #include "stdio.h" using namespace std; using namespace daal; using namespace daal::algorithms; using namespace daal::algorithms::decision_forest::classification; /* Input data set parameters */ const string testDatasetFileName = "../data/batch/mnist_test_data.csv" ; const string labels = "../data/batch/mnist_test_labels.csv" ; const size_t nFeatures = 784; /* Number of features in training and testing data sets */ const size_t nClasses = 10; /* Number of classes */ void testModel(); void loadData( const std::string& dataFileName, const std::string& labelsFileName, NumericTablePtr& pData, NumericTablePtr& pDependentVar); void check_accuracy(NumericTablePtr prediction, NumericTablePtr testGroundTruth); int main( int argc, char *argv[]) { checkArguments(argc, argv, 2, &labels, &testDatasetFileName); /* Deserialization */ size_t size = 0; byte * buffer = NULL; FILE * pFile; size_t result; pFile = fopen ( "../data/batch/native_result.txt" , "rb" ); if (pFile==NULL) { fputs ( "File error" ,stderr); exit (1); } // obtain file size: fseek (pFile , 0 , SEEK_END); size = ftell (pFile); std::cout << "size: " << size << "\n" ; rewind (pFile); // allocate memory to contain the whole file: buffer = (byte*) malloc ( sizeof (byte)*size); if (buffer == NULL) { fputs ( "Memory error" ,stderr); exit (2); } // copy the file into the buffer: result = fread (buffer,1,size,pFile); if (result != size) { fputs ( "Reading error" ,stderr); exit (3); } /* the result buffer is now loaded in the buffer. */ /* Create a data archive to deserialize the numeric table */ OutputDataArchive out_dataArch(buffer, size); free (buffer); fclose (pFile); /* needed for result allocation */ training::Batch<> train(nClasses); train.getResult()->deserialize(out_dataArch); /* Create Numeric Tables for testing data and ground truth values */ NumericTablePtr testData; NumericTablePtr testGroundTruth; loadData(testDatasetFileName, labels, testData, testGroundTruth); /* Create an algorithm object to predict values of decision forest classification */ prediction::Batch<> algorithm(nClasses); /* Pass a testing data set and the trained model to the algorithm */ algorithm.input.set(classifier::prediction::data, testData); /* set deserialized model */ algorithm.input.set(classifier::prediction::model, train.getResult()->get(classifier::training::model)); /* Predict values of decision forest classification */ algorithm.compute(); /* Retrieve the algorithm results */ NumericTablePtr prediction = algorithm.getResult()->get(classifier::prediction::prediction); printNumericTable(prediction, "Prediction results (first 10 rows):" , 10); printNumericTable(testGroundTruth, "Ground truth (first 10 rows):" , 10); check_accuracy(prediction, testGroundTruth); return 0; } void check_accuracy(NumericTablePtr prediction, NumericTablePtr testGroundTruth) { /* check accuracy */ BlockDescriptor< double > blockPr; prediction->getBlockOfRows(0, prediction->getNumberOfRows(), readOnly, blockPr); double * valueP = (blockPr.getBlockPtr()); BlockDescriptor< double > blockGT; testGroundTruth->getBlockOfRows(0, testGroundTruth->getNumberOfRows(), readOnly, blockGT); double * valueG = (blockGT.getBlockPtr()); size_t count = 0; for ( size_t i = 0; i < testGroundTruth->getNumberOfRows(); i++) { if (valueG[i] != valueP[i]) count++; } testGroundTruth->releaseBlockOfRows(blockGT); prediction->releaseBlockOfRows(blockPr); cout << "accuracy: " << 1- double (count)/ double (testGroundTruth->getNumberOfRows()) << "\n" ; } void loadData( const std::string& dataFileName, const std::string& labelsFileName, NumericTablePtr& pData, NumericTablePtr& pDependentVar) { /* Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file */ FileDataSource<CSVFeatureManager> trainDataSource(dataFileName, DataSource::notAllocateNumericTable, DataSource::doDictionaryFromContext); FileDataSource<CSVFeatureManager> trainLabels(labelsFileName, DataSource::notAllocateNumericTable, DataSource::doDictionaryFromContext); /* Create Numeric Tables for training data and dependent variables */ pData.reset( new HomogenNumericTable<>(nFeatures, 0, NumericTable::notAllocate)); pDependentVar.reset( new HomogenNumericTable<>(1, 0, NumericTable::notAllocate)); /* Retrieve the data from input file */ trainDataSource.loadDataBlock(pData.get()); trainLabels.loadDataBlock(pDependentVar.get()); NumericTableDictionaryPtr pDictionary = pData->getDictionarySharedPtr(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2017-11-30 Batch Normalization的算法本质是在网络每一层的输入前增加一层BN层(也即归一化层),对数据进行归一化处理,然后再进入网络下一层,但是BN并不是简单的对数据进行求归一化,而是引入了两个参数λ和β去进行数据重构
2017-11-30 终端安全工具 gartner 排名
2017-11-30 When Cyber Security Meets Machine Learning 机器学习 安全分析 对于安全领域的总结很有用 看未来演进方向
2017-11-30 DNS隧道之DNS2TCP实现——dns2tcpc必须带server IP才可以,此外ssh可以穿过墙的,设置代理上网
2017-11-30 DNS隧道之DNS2TCP使用心得教程——是可以用来穿透qiang的,ubuntu下直接apt install dns2tcp
2017-11-30 DNS隧道工具汇总——补充,还有IP over DNS的工具NSTX、Iodine、DNSCat