Cranium
1.0.0
matrix.h
中第 7 行的註解即可啟用 BLAS sgemm
函數以進行快速矩陣乘法。 由於 Cranium 僅包含頭文件,因此只需將src
目錄複製到您的專案中,然後#include "src/cranium.h"
即可開始使用它。
它唯一需要的編譯器依賴項來自
標頭,因此使用-lm
進行編譯。
如果您使用 CBLAS,您還需要使用-lcblas
進行編譯,並透過-I
包含特定機器的 BLAS 實現所在的路徑。常見的有 OpenBLAS 和 ATLAS。
它已經過測試,可以與任何級別的 gcc 優化完美配合,因此請隨意使用它們。
#include "cranium.h"
/*
This basic example program is the skeleton of a classification problem.
The training data should be in matrix form, where each row is a data point, and
each column is a feature.
The training classes should be in matrix form, where the ith row corresponds to
the ith training example, and each column is a 1 if it is of that class, and
0 otherwise. Each example may only be of 1 class.
*/
// create training data and target values (data collection not shown)
int rows , features , classes ;
float * * training ;
float * * classes ;
// create datasets to hold the data
DataSet * trainingData = createDataSet ( rows , features , training );
DataSet * trainingClasses = createDataSet ( rows , classes , classes );
// create network with 2 input neurons, 1 hidden layer with sigmoid
// activation function and 5 neurons, and 2 output neurons with softmax
// activation function
srand ( time ( NULL ));
size_t hiddenSize [] = { 5 };
Activation hiddenActivation [] = { sigmoid };
Network * net = createNetwork ( 2 , 1 , hiddenSize , hiddenActivation , 2 , softmax );
// train network with cross-entropy loss using Mini-Batch SGD
ParameterSet params ;
params . network = net ;
params . data = trainingData ;
params . classes = trainingClasses ;
params . lossFunction = CROSS_ENTROPY_LOSS ;
params . batchSize = 20 ;
params . learningRate = .01 ;
params . searchTime = 5000 ;
params . regularizationStrength = .001 ;
params . momentumFactor = .9 ;
params . maxIters = 10000 ;
params . shuffle = 1 ;
params . verbose = 1 ;
optimize ( params );
// test accuracy of network after training
printf ( "Accuracy is %fn" , accuracy ( net , trainingData , trainingClasses ));
// get network's predictions on input data after training
forwardPass ( net , trainingData );
int * predictions = predict ( net );
free ( predictions );
// save network to a file
saveNetwork ( net , "network" );
// free network and data
destroyNetwork ( net );
destroyDataSet ( trainingData );
destroyDataSet ( trainingClasses );
// load previous network from file
Network * previousNet = readNetwork ( "network" );
destroyNetwork ( previousNet );
若要執行測試,請查看tests
資料夾。
Makefile
包含用於執行每批單元測試或同時執行所有單元測試的命令。
如果您想新增任何功能或發現錯誤,請隨時發送拉取請求。
檢查問題標籤以了解一些可能要做的事情。