зеркало из https://github.com/microsoft/EdgeML.git
Fixed the naming convention for stdDev earlier called variance and added more fucntionalities
This commit is contained in:
Родитель
bece1c1f07
Коммит
80d9135206
|
@ -14,7 +14,7 @@ BONSAI_DIR=$(SOURCE_DIR)/Bonsai
|
|||
IFLAGS = -I eigen/ -I$(MKL_ROOT)/include \
|
||||
-I$(COMMON_DIR) -I$(PROTONN_DIR) -I$(BONSAI_DIR)
|
||||
|
||||
all: ProtoNNTrain ProtoNNPredict BonsaiTrain BonsaiPredict #ProtoNNIngestTest BonsaiIngestTest
|
||||
all: ProtoNNTrain ProtoNNPredict BonsaiTrain BonsaiPredict Bonsai #ProtoNNIngestTest BonsaiIngestTest
|
||||
|
||||
libcommon.so: $(COMMON_INCLUDES)
|
||||
$(MAKE) -C $(SOURCE_DIR)/common
|
||||
|
@ -31,6 +31,9 @@ ProtoNNTrainDriver.o:
|
|||
ProtoNNPredictDriver.o:
|
||||
$(MAKE) -C $(DRIVER_DIR)/ProtoNN/predictor
|
||||
|
||||
BonsaiLocalDriver.o:
|
||||
$(MAKE) -C $(DRIVER_DIR)/Bonsai/local
|
||||
|
||||
BonsaiTrainDriver.o:
|
||||
$(MAKE) -C $(DRIVER_DIR)/Bonsai/trainer
|
||||
|
||||
|
@ -48,8 +51,8 @@ ProtoNNPredict: ProtoNNPredictDriver.o libcommon.so libProtoNN.so
|
|||
#ProtoNNIngestTest: ProtoNNIngestTest.o libcommon.so libProtoNN.so
|
||||
# $(CC) -o $@ $^ $(CFLAGS) $(MKL_PAR_LDFLAGS) $(CILK_LDFLAGS)
|
||||
|
||||
#Bonsai: BonsaiLocalDriver.o libcommon.so libBonsai.so
|
||||
# $(CC) -o $@ $^ $(CFLAGS) $(MKL_SEQ_LDFLAGS) $(CILK_LDFLAGS)
|
||||
Bonsai: BonsaiLocalDriver.o libcommon.so libBonsai.so
|
||||
$(CC) -o $@ $^ $(CFLAGS) $(MKL_SEQ_LDFLAGS) $(CILK_LDFLAGS)
|
||||
|
||||
BonsaiTrain: BonsaiTrainDriver.o libcommon.so libBonsai.so
|
||||
$(CC) -o $@ $^ $(CFLAGS) $(MKL_SEQ_LDFLAGS) $(CILK_LDFLAGS)
|
||||
|
|
|
@ -77,7 +77,7 @@ BonsaiPredict:
|
|||
-f : [Required] Input format. Takes two values [0 and 1]. 0 is for libsvmFormat(default), 1 is for tab/space separated input.
|
||||
-N : [Required] Number of data points in the test data.
|
||||
-D : [Required] Directory of data with test.txt present in it.
|
||||
-M : [Required] Directory of the Model (loadableModel and loadableMeanVar).
|
||||
-M : [Required] Directory of the Model (loadableModel and loadableMeanStd).
|
||||
|
||||
## Data Format
|
||||
|
||||
|
@ -107,11 +107,11 @@ The DataFolder will have a new forlder named "BonsaiResults" with the following
|
|||
|
||||
(a) A directory for each run with the signature hrs_min_sec_day_month with the following in it:
|
||||
(1) loadableModel - Char file which can be directly loaded using the inbuilt load model functions
|
||||
(2) loadableMeanVar - Char file which can be directly loaded using inbuilt load mean-var functions
|
||||
(2) loadableMeanStd - Char file which can be directly loaded using inbuilt load mean-var functions
|
||||
(3) predClassAndScore - File with Prediction Score and Predicted Class for each Data point in the test set
|
||||
(4) runInfo - File with the hyperparameters for that run of Bonsai along with Test Accuracy and Total NonZeros in the model
|
||||
(5) timerLog - Created on using the `TIMER` flag. This file stores proc time and wall time taken to execute various function calls in the code. Indicates the degree of parallelization and is useful for identifying bottlenecks to optimize the code. On specifying the `CONCISE` flag, timing information will only be printed if running time is higher than a threshold specified in `src/common/timer.cpp`
|
||||
(6) Params - A directory with readable files with Z, W, V, Theta, Mean and Variance
|
||||
(6) Params - A directory with readable files with Z, W, V, Theta, Mean and Std
|
||||
(b) A file resultDump which has consolidated results and map to the respective run directory
|
||||
|
||||
## Notes
|
||||
|
|
|
@ -5,4 +5,5 @@
|
|||
add_subdirectory(trainer)
|
||||
add_subdirectory(predictor)
|
||||
#add_subdirectory(ingestTest)
|
||||
#add_subdirectory(local)
|
||||
|
||||
|
|
|
@ -95,19 +95,19 @@ int main()
|
|||
auto modelBytes = trainer.getSparseModelSize();
|
||||
auto model = new char[modelBytes];
|
||||
|
||||
auto meanVarBytes = trainer.getMeanVarSize();
|
||||
auto meanVar = new char[meanVarBytes];
|
||||
auto meanStdBytes = trainer.getMeanStdSize();
|
||||
auto meanStd = new char[meanStdBytes];
|
||||
|
||||
// trainer.exportModel(modelBytes, model);
|
||||
trainer.exportSparseModel(modelBytes, model);
|
||||
|
||||
trainer.exportMeanVar(meanVarBytes, meanVar);
|
||||
trainer.exportMeanStd(meanStdBytes, meanStd);
|
||||
|
||||
// BonsaiPredictor predictor(modelBytes, model);
|
||||
|
||||
BonsaiPredictor predictor(modelBytes, model, false);
|
||||
|
||||
predictor.importMeanVar(meanVarBytes, meanVar);
|
||||
predictor.importMeanStd(meanStdBytes, meanStd);
|
||||
|
||||
FP_TYPE *scoreArray = new FP_TYPE[hyperParam.numClasses];
|
||||
|
||||
|
@ -188,21 +188,21 @@ int main()
|
|||
// trainer->finalizeData();
|
||||
|
||||
// // std::cout<<trainer->mean<<std::endl<<std::endl;
|
||||
// // std::cout<<trainer->variance<<std::endl<<std::endl;
|
||||
// // std::cout<<trainer->stdDev<<std::endl<<std::endl;
|
||||
|
||||
// trainer->train();
|
||||
|
||||
// auto modelBytes = trainer->getModelSize();
|
||||
// auto model = new char[modelBytes];
|
||||
|
||||
// auto meanVarBytes = trainer->getMeanVarSize();
|
||||
// auto meanVar = new char[meanVarBytes];
|
||||
// auto meanStdBytes = trainer->getMeanStdSize();
|
||||
// auto meanStd = new char[meanStdBytes];
|
||||
|
||||
// trainer->exportModel(modelBytes, model);
|
||||
// trainer->exportMeanVar(meanVarBytes, meanVar);
|
||||
// trainer->exportMeanStd(meanStdBytes, meanStd);
|
||||
|
||||
// auto predictor = new BonsaiPredictor(modelBytes, model);
|
||||
// predictor->importMeanVar(meanVarBytes, meanVar);
|
||||
// predictor->importMeanStd(meanStdBytes, meanStd);
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -27,20 +27,20 @@ int main(int argc, char **argv)
|
|||
|
||||
auto modelBytes = trainer.getModelSize(); // This can be changed to getSparseModelSize() if you need to export sparse model
|
||||
auto model = new char[modelBytes];
|
||||
auto meanVarBytes = trainer.getMeanVarSize();
|
||||
auto meanVar = new char[meanVarBytes];
|
||||
auto meanStdBytes = trainer.getMeanStdSize();
|
||||
auto meanStd = new char[meanStdBytes];
|
||||
|
||||
trainer.exportModel(modelBytes, model, currResultsPath); // use exportSparseModel(...) if you need sparse model
|
||||
trainer.exportMeanVar(meanVarBytes, meanVar, currResultsPath);
|
||||
trainer.exportMeanStd(meanStdBytes, meanStd, currResultsPath);
|
||||
|
||||
trainer.dumpModelMeanVar(currResultsPath);
|
||||
trainer.dumpModelMeanStd(currResultsPath);
|
||||
|
||||
BonsaiPredictor predictor(modelBytes, model); // use the constructor predictor(modelBytes, model, false) for loading a sparse model.
|
||||
predictor.importMeanVar(meanVarBytes, meanVar);
|
||||
predictor.importMeanStd(meanStdBytes, meanStd);
|
||||
|
||||
predictor.batchEvaluate(trainer.data.Xvalidation, trainer.data.Yvalidation, dataDir, currResultsPath);
|
||||
|
||||
delete[] model, meanVar;
|
||||
delete[] model, meanStd;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
set (tool_name Bonsai)
|
||||
|
||||
set (src BonsaiLocalDriver.cpp)
|
||||
|
||||
source_group("src" FILES ${src})
|
||||
|
||||
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR})
|
||||
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR})
|
||||
|
||||
add_executable(${tool_name} ${src} ${include})
|
||||
target_include_directories(${tool_name} PRIVATE ${CMAKE_SOURCE_DIR}/src/common ${CMAKE_SOURCE_DIR}/src/Bonsai)
|
||||
|
||||
IF(CMAKE_COMPILER_IS_GNUCC)
|
||||
target_link_libraries(${tool_name} common Bonsai mkl_intel_ilp64 mkl_core mkl_sequential cilkrts)
|
||||
ENDIF (CMAKE_COMPILER_IS_GNUCC)
|
||||
|
||||
IF(NOT CMAKE_COMPILER_IS_GNUCC)
|
||||
target_link_libraries(${tool_name} common Bonsai mkl_intel_ilp64 mkl_core mkl_sequential)
|
||||
ENDIF (NOT CMAKE_COMPILER_IS_GNUCC)
|
||||
|
||||
set_property(TARGET ${tool_name} PROPERTY FOLDER "drivers/Bonsai")
|
|
@ -0,0 +1,24 @@
|
|||
# Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
# Licensed under the MIT license.
|
||||
|
||||
include ../../../config.mk
|
||||
|
||||
SOURCE_DIR=../../../src
|
||||
|
||||
COMMON_DIR=$(SOURCE_DIR)/common
|
||||
PROTONN_DIR=$(SOURCE_DIR)/Bonsai
|
||||
IFLAGS = -I ../../../eigen -I$(MKL_ROOT)/include \
|
||||
-I$(COMMON_DIR) -I$(PROTONN_DIR)
|
||||
|
||||
all: ../../../BonsaiLocalDriver.o
|
||||
|
||||
../../../BonsaiLocalDriver.o: BonsaiLocalDriver.cpp
|
||||
$(CC) -c -o $@ $(IFLAGS) $(CFLAGS) $<
|
||||
|
||||
.PHONY: clean cleanest
|
||||
|
||||
clean:
|
||||
rm -f ../../../BonsaiLocalDriver.o
|
||||
|
||||
cleanest: clean
|
||||
rm *~
|
|
@ -27,15 +27,15 @@ int main(int argc, char **argv)
|
|||
|
||||
auto modelBytes = trainer.getModelSize(); // This can be changed to getSparseModelSize() if you need to export sparse model
|
||||
auto model = new char[modelBytes];
|
||||
auto meanVarBytes = trainer.getMeanVarSize();
|
||||
auto meanVar = new char[meanVarBytes];
|
||||
auto meanStdBytes = trainer.getMeanStdSize();
|
||||
auto meanStd = new char[meanStdBytes];
|
||||
|
||||
trainer.exportModel(modelBytes, model); // use exportSparseModel(...) if you need sparse model
|
||||
trainer.exportMeanVar(meanVarBytes, meanVar);
|
||||
trainer.getLoadableModelMeanVar(model, modelBytes, meanVar, meanVarBytes, currResultsPath);
|
||||
trainer.dumpModelMeanVar(currResultsPath);
|
||||
trainer.exportMeanStd(meanStdBytes, meanStd);
|
||||
trainer.getLoadableModelMeanStd(model, modelBytes, meanStd, meanStdBytes, currResultsPath);
|
||||
trainer.dumpModelMeanStd(currResultsPath);
|
||||
|
||||
delete[] model, meanVar;
|
||||
delete[] model, meanStd;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -273,7 +273,7 @@ namespace EdgeML
|
|||
featureCount_t* feedDataFeatureBuffer; ///< Buffer to hold incoming Label values
|
||||
|
||||
MatrixXuf mean; ///< Object to hold the mean of the train data
|
||||
MatrixXuf variance; ///< Object to hold variance of the train data
|
||||
MatrixXuf stdDev; ///< Object to hold stdDev of the train data
|
||||
|
||||
///
|
||||
/// Function to support various normalisations
|
||||
|
@ -486,15 +486,15 @@ namespace EdgeML
|
|||
size_t getModelSize();
|
||||
size_t getSparseModelSize();
|
||||
|
||||
void exportMeanVar(
|
||||
const size_t& meanVarSize,
|
||||
void exportMeanStd(
|
||||
const size_t& meanStdSize,
|
||||
char *const buffer);
|
||||
void exportMeanVar(
|
||||
const size_t& meanVarSize,
|
||||
void exportMeanStd(
|
||||
const size_t& meanStdSize,
|
||||
char *const buffer,
|
||||
const std::string& currResultsPath);
|
||||
|
||||
size_t getMeanVarSize();
|
||||
size_t getMeanStdSize();
|
||||
|
||||
///
|
||||
/// Gives reloadablity for a pretrained model stored after training in the results directory
|
||||
|
@ -531,14 +531,14 @@ namespace EdgeML
|
|||
void exportThetaDense(int bufferSize, char *const buf);
|
||||
|
||||
///
|
||||
/// Function to Dump Readable Mean, Variance and Model
|
||||
/// Function to Dump Readable Mean, StdDev and Model
|
||||
///
|
||||
void dumpModelMeanVar(const std::string& currResultsPath);
|
||||
void dumpModelMeanStd(const std::string& currResultsPath);
|
||||
|
||||
///
|
||||
/// Function to Dump Loadable Mean, Variance and Model
|
||||
/// Function to Dump Loadable Mean, StdDev and Model
|
||||
///
|
||||
void getLoadableModelMeanVar(char *const modelBuffer, const size_t& modelBytes, char *const meanVarBuffer, const size_t& meanVarBytes, const std::string& currResultsPath);
|
||||
void getLoadableModelMeanStd(char *const modelBuffer, const size_t& modelBytes, char *const meanStdBuffer, const size_t& meanStdBytes, const std::string& currResultsPath);
|
||||
|
||||
size_t totalNonZeros();
|
||||
};
|
||||
|
@ -553,7 +553,7 @@ namespace EdgeML
|
|||
featureCount_t* feedDataFeatureBuffer; ///< Buffer to hold incoming Label values
|
||||
|
||||
MatrixXuf mean; ///< Object to hold the mean of the train data from imported model
|
||||
MatrixXuf variance; ///< Object to hold variance of the train data from imported model
|
||||
MatrixXuf stdDev; ///< Object to hold stdDev of the train data from imported model
|
||||
|
||||
BonsaiModel model; ///< Object to hold the imported model
|
||||
Data testData;
|
||||
|
@ -583,15 +583,15 @@ namespace EdgeML
|
|||
~BonsaiPredictor();
|
||||
|
||||
///
|
||||
/// Function to import stored Mean and Variance
|
||||
/// Function to import stored Mean and stdDev
|
||||
///
|
||||
void importMeanVar(const size_t numBytes,
|
||||
void importMeanStd(const size_t numBytes,
|
||||
const char *const fromBuffer);
|
||||
|
||||
///
|
||||
/// Function to import stored Mean and Variance
|
||||
/// Function to import stored Mean and stdDev
|
||||
///
|
||||
void importMeanVar(std::string meanVarFile);
|
||||
void importMeanStd(std::string meanStdFile);
|
||||
|
||||
///
|
||||
/// Function to Score an incoming Dense Data Point.Not thread safe
|
||||
|
|
|
@ -93,19 +93,19 @@ int main()
|
|||
auto modelBytes = trainer.getSparseModelSize();
|
||||
auto model = new char[modelBytes];
|
||||
|
||||
auto meanVarBytes = trainer.getMeanVarSize();
|
||||
auto meanVar = new char[meanVarBytes];
|
||||
auto meanStdBytes = trainer.getMeanStdSize();
|
||||
auto meanStd = new char[meanStdBytes];
|
||||
|
||||
// trainer.exportModel(modelBytes, model);
|
||||
trainer.exportSparseModel(modelBytes, model);
|
||||
|
||||
trainer.exportMeanVar(meanVarBytes, meanVar);
|
||||
trainer.exportMeanStd(meanStdBytes, meanStd);
|
||||
|
||||
// BonsaiPredictor predictor(modelBytes, model);
|
||||
|
||||
BonsaiPredictor predictor(modelBytes, model, false);
|
||||
|
||||
predictor.importMeanVar(meanVarBytes, meanVar);
|
||||
predictor.importMeanStd(meanStdBytes, meanStd);
|
||||
|
||||
FP_TYPE *scoreArray = new FP_TYPE[hyperParam.numClasses];
|
||||
|
||||
|
@ -186,21 +186,21 @@ int main()
|
|||
// trainer->finalizeData();
|
||||
|
||||
// // std::cout<<trainer->mean<<std::endl<<std::endl;
|
||||
// // std::cout<<trainer->variance<<std::endl<<std::endl;
|
||||
// // std::cout<<trainer->stdDev<<std::endl<<std::endl;
|
||||
|
||||
// trainer->train();
|
||||
|
||||
// auto modelBytes = trainer->getModelSize();
|
||||
// auto model = new char[modelBytes];
|
||||
|
||||
// auto meanVarBytes = trainer->getMeanVarSize();
|
||||
// auto meanVar = new char[meanVarBytes];
|
||||
// auto meanStdBytes = trainer->getMeanStdSize();
|
||||
// auto meanStd = new char[meanStdBytes];
|
||||
|
||||
// trainer->exportModel(modelBytes, model);
|
||||
// trainer->exportMeanVar(meanVarBytes, meanVar);
|
||||
// trainer->exportMeanStd(meanStdBytes, meanStd);
|
||||
|
||||
// auto predictor = new BonsaiPredictor(modelBytes, model);
|
||||
// predictor->importMeanVar(meanVarBytes, meanVar);
|
||||
// predictor->importMeanStd(meanStdBytes, meanStd);
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ void BonsaiPredictor::exitWithHelp()
|
|||
LOG_INFO("-f : [Required] Input format. Takes two values [0 and 1]. 0 is for libsvmFormat(default), 1 is for tab/space separated input.");
|
||||
LOG_INFO("-N : [Required] Number of data points in the test data.");
|
||||
LOG_INFO("-D : [Required] Directory of data with test.txt present in it.");
|
||||
LOG_INFO("-M : [Required] Directory of the Model (loadableModel and loadableMeanVar).");
|
||||
LOG_INFO("-M : [Required] Directory of the Model (loadableModel and loadableMeanStd).");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -73,11 +73,11 @@ BonsaiPredictor::BonsaiPredictor(
|
|||
feedDataFeatureBuffer = new labelCount_t[model.hyperParams.dataDimension];
|
||||
|
||||
mean = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
variance = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
stdDev = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
|
||||
std::string meanVarFile = modelDir + "/loadableMeanVar";
|
||||
std::string meanStdFile = modelDir + "/loadableMeanStd";
|
||||
|
||||
importMeanVar(meanVarFile);
|
||||
importMeanStd(meanStdFile);
|
||||
|
||||
testData = Data(FileIngest,
|
||||
DataFormatParams{0, 0, numTest, model.hyperParams.numClasses, model.hyperParams.dataDimension});
|
||||
|
@ -99,41 +99,41 @@ BonsaiPredictor::BonsaiPredictor(
|
|||
feedDataFeatureBuffer = new labelCount_t[model.hyperParams.dataDimension];
|
||||
|
||||
mean = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
variance = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
stdDev = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
}
|
||||
|
||||
void BonsaiPredictor::importMeanVar(
|
||||
std::string meanVarFile)
|
||||
void BonsaiPredictor::importMeanStd(
|
||||
std::string meanStdFile)
|
||||
{
|
||||
size_t meanVarSize;
|
||||
std::ifstream infileMeanVar(meanVarFile, std::ios::in|std::ios::binary);
|
||||
assert(infileMeanVar.is_open());
|
||||
size_t meanStdSize;
|
||||
std::ifstream infileMeanStd(meanStdFile, std::ios::in|std::ios::binary);
|
||||
assert(infileMeanStd.is_open());
|
||||
|
||||
infileMeanVar.read((char*)&meanVarSize, sizeof(meanVarSize));
|
||||
infileMeanVar.close();
|
||||
infileMeanStd.read((char*)&meanStdSize, sizeof(meanStdSize));
|
||||
infileMeanStd.close();
|
||||
|
||||
infileMeanVar.open(meanVarFile, std::ios::in|std::ios::binary);
|
||||
infileMeanStd.open(meanStdFile, std::ios::in|std::ios::binary);
|
||||
|
||||
char* meanVarBuff = new char[meanVarSize];
|
||||
infileMeanVar.read((char*)meanVarBuff, meanVarSize);
|
||||
infileMeanVar.close();
|
||||
char* meanStdBuff = new char[meanStdSize];
|
||||
infileMeanStd.read((char*)meanStdBuff, meanStdSize);
|
||||
infileMeanStd.close();
|
||||
|
||||
importMeanVar(meanVarSize, meanVarBuff);
|
||||
importMeanStd(meanStdSize, meanStdBuff);
|
||||
}
|
||||
|
||||
void BonsaiPredictor::importMeanVar(
|
||||
void BonsaiPredictor::importMeanStd(
|
||||
const size_t numBytes,
|
||||
const char *const fromBuffer)
|
||||
{
|
||||
size_t offset = 0;
|
||||
|
||||
size_t meanVarSize;
|
||||
memcpy((void *)&meanVarSize, fromBuffer + offset, sizeof(meanVarSize));
|
||||
offset += sizeof(meanVarSize);
|
||||
size_t meanStdSize;
|
||||
memcpy((void *)&meanStdSize, fromBuffer + offset, sizeof(meanStdSize));
|
||||
offset += sizeof(meanStdSize);
|
||||
memcpy(mean.data(), fromBuffer + offset, sizeof(FP_TYPE) * mean.rows() * mean.cols());
|
||||
offset += sizeof(FP_TYPE) * mean.rows() * mean.cols();
|
||||
memcpy(variance.data(), fromBuffer + offset, sizeof(FP_TYPE) * variance.rows() * variance.cols());
|
||||
offset += sizeof(FP_TYPE) * variance.rows() * variance.cols();
|
||||
memcpy(stdDev.data(), fromBuffer + offset, sizeof(FP_TYPE) * stdDev.rows() * stdDev.cols());
|
||||
offset += sizeof(FP_TYPE) * stdDev.rows() * stdDev.cols();
|
||||
|
||||
assert(numBytes == offset);
|
||||
}
|
||||
|
@ -223,9 +223,9 @@ void BonsaiPredictor::scoreSparseDataPoint(
|
|||
|
||||
dataPoint -= mean;
|
||||
|
||||
//vDiv(model.hyperParams.dataDimension, dataPoint.data(), variance.data(), dataPoint.data());
|
||||
//vDiv(model.hyperParams.dataDimension, dataPoint.data(), stdDev.data(), dataPoint.data());
|
||||
for (featureCount_t f = 0; f < model.hyperParams.dataDimension; f++) {
|
||||
dataPoint(f, 0) /= variance(f, 0);
|
||||
dataPoint(f, 0) /= stdDev(f, 0);
|
||||
}
|
||||
|
||||
dataPoint(model.hyperParams.dataDimension - 1, 0) = (FP_TYPE)1.0;
|
||||
|
@ -245,10 +245,10 @@ void BonsaiPredictor::scoreDenseDataPoint(
|
|||
|
||||
dataPoint = dataPoint - mean;
|
||||
|
||||
//vDiv(model.hyperParams.dataDimension, dataPoint.data(), variance.data(), dataPoint.data());
|
||||
//vDiv(model.hyperParams.dataDimension, dataPoint.data(), stdDev.data(), dataPoint.data());
|
||||
|
||||
for (featureCount_t f = 0; f < model.hyperParams.dataDimension; f++) {
|
||||
dataPoint(f, 0) /= variance(f, 0);
|
||||
dataPoint(f, 0) /= stdDev(f, 0);
|
||||
}
|
||||
|
||||
dataPoint(model.hyperParams.dataDimension - 1, 0) = (FP_TYPE)1.0;
|
||||
|
|
|
@ -46,7 +46,7 @@ BonsaiTrainer::BonsaiTrainer(
|
|||
feedDataFeatureBuffer = new featureCount_t[5];
|
||||
|
||||
mean = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
variance = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
stdDev = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
|
||||
data.loadDataFromFile(model.hyperParams.dataformatType, dataDir + "/train.txt", dataDir + "/test.txt", "");
|
||||
finalizeData();
|
||||
|
@ -81,7 +81,7 @@ BonsaiTrainer::BonsaiTrainer(
|
|||
feedDataFeatureBuffer = new featureCount_t[5];
|
||||
|
||||
mean = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
variance = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
stdDev = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
|
||||
data.loadDataFromFile(model.hyperParams.dataformatType, dataDir + "/train.txt", dataDir + "/test.txt", "");
|
||||
finalizeData();
|
||||
|
@ -110,7 +110,7 @@ BonsaiTrainer::BonsaiTrainer(
|
|||
feedDataFeatureBuffer = new featureCount_t[model.hyperParams.dataDimension + 5];
|
||||
|
||||
mean = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
variance = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
stdDev = MatrixXuf::Zero(model.hyperParams.dataDimension, 1);
|
||||
|
||||
initializeModel();
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ BonsaiTrainer::BonsaiTrainer(
|
|||
BonsaiTrainer::~BonsaiTrainer()
|
||||
{
|
||||
mean.resize(0, 0);
|
||||
variance.resize(0, 0);
|
||||
stdDev.resize(0, 0);
|
||||
delete[] feedDataValBuffer;
|
||||
delete[] feedDataFeatureBuffer;
|
||||
}
|
||||
|
@ -171,7 +171,7 @@ void BonsaiTrainer::finalizeData()
|
|||
|
||||
initializeTrainVariables(data.Ytrain);
|
||||
|
||||
meanVarNormalize(data.Xtrain, mean, variance);
|
||||
meanVarNormalize(data.Xtrain, mean, stdDev);
|
||||
}
|
||||
|
||||
FP_TYPE BonsaiTrainer::computeObjective(const MatrixXuf& ZX, const LabelMatType& Y)
|
||||
|
@ -285,19 +285,19 @@ void BonsaiTrainer::exportModel(
|
|||
modelExporter.close();
|
||||
}
|
||||
|
||||
void BonsaiTrainer::getLoadableModelMeanVar(
|
||||
void BonsaiTrainer::getLoadableModelMeanStd(
|
||||
char *const modelBuffer,
|
||||
const size_t& modelBytes,
|
||||
char *const meanVarBuffer,
|
||||
const size_t& meanVarBytes,
|
||||
char *const meanStdBuffer,
|
||||
const size_t& meanStdBytes,
|
||||
const std::string& currResultsPath)
|
||||
{
|
||||
std::ofstream modelExporter(currResultsPath + "/loadableModel", std::ios::out | std::ios::binary);
|
||||
modelExporter.write(modelBuffer, modelBytes);
|
||||
modelExporter.close();
|
||||
std::ofstream meanVarExporter(currResultsPath + "/loadableMeanVar", std::ios::out | std::ios::binary);
|
||||
meanVarExporter.write(meanVarBuffer, meanVarBytes);
|
||||
meanVarExporter.close();
|
||||
std::ofstream meanStdExporter(currResultsPath + "/loadableMeanStd", std::ios::out | std::ios::binary);
|
||||
meanStdExporter.write(meanStdBuffer, meanStdBytes);
|
||||
meanStdExporter.close();
|
||||
}
|
||||
|
||||
void BonsaiTrainer::exportSparseModel(
|
||||
|
@ -313,48 +313,48 @@ void BonsaiTrainer::exportSparseModel(
|
|||
modelExporter.close();
|
||||
}
|
||||
|
||||
size_t BonsaiTrainer::getMeanVarSize()
|
||||
size_t BonsaiTrainer::getMeanStdSize()
|
||||
{
|
||||
size_t offset = 0;
|
||||
|
||||
offset += sizeof(size_t);
|
||||
offset += sizeof(FP_TYPE) * mean.rows() * mean.cols();
|
||||
offset += sizeof(FP_TYPE) * variance.rows() * variance.cols();
|
||||
offset += sizeof(FP_TYPE) * stdDev.rows() * stdDev.cols();
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
void BonsaiTrainer::exportMeanVar(
|
||||
const size_t& meanVarSize,
|
||||
void BonsaiTrainer::exportMeanStd(
|
||||
const size_t& meanStdSize,
|
||||
char *const buffer)
|
||||
{
|
||||
assert(meanVarSize == getMeanVarSize());
|
||||
assert(meanStdSize == getMeanStdSize());
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
memcpy(buffer + offset, (void *)&meanVarSize, sizeof(meanVarSize));
|
||||
offset += sizeof(meanVarSize);
|
||||
memcpy(buffer + offset, (void *)&meanStdSize, sizeof(meanStdSize));
|
||||
offset += sizeof(meanStdSize);
|
||||
|
||||
memcpy(buffer + offset, mean.data(), sizeof(FP_TYPE) * mean.rows() * mean.cols());
|
||||
offset += sizeof(FP_TYPE) * mean.rows() * mean.cols();
|
||||
|
||||
memcpy(buffer + offset, variance.data(), sizeof(FP_TYPE) * variance.rows() * variance.cols());
|
||||
offset += sizeof(FP_TYPE) * variance.rows() * variance.cols();
|
||||
memcpy(buffer + offset, stdDev.data(), sizeof(FP_TYPE) * stdDev.rows() * stdDev.cols());
|
||||
offset += sizeof(FP_TYPE) * stdDev.rows() * stdDev.cols();
|
||||
|
||||
assert(offset < (size_t)(1 << 31)); // Because we make this promise to TLC.
|
||||
}
|
||||
|
||||
void BonsaiTrainer::exportMeanVar(
|
||||
const size_t& meanVarSize,
|
||||
void BonsaiTrainer::exportMeanStd(
|
||||
const size_t& meanStdSize,
|
||||
char *const buffer,
|
||||
const std::string& currResultsPath)
|
||||
{
|
||||
std::string loadableMeanvarPath = currResultsPath + "/loadableMeanVar";
|
||||
exportMeanVar(meanVarSize, buffer);
|
||||
std::string loadableMeanStdPath = currResultsPath + "/loadableMeanStd";
|
||||
exportMeanStd(meanStdSize, buffer);
|
||||
|
||||
std::ofstream meanVarExporter(loadableMeanvarPath);
|
||||
meanVarExporter.write(buffer, meanVarSize);
|
||||
meanVarExporter.close();
|
||||
std::ofstream meanStdExporter(loadableMeanStdPath);
|
||||
meanStdExporter.write(buffer, meanStdSize);
|
||||
meanStdExporter.close();
|
||||
}
|
||||
|
||||
void BonsaiTrainer::normalize()
|
||||
|
@ -635,7 +635,7 @@ void BonsaiTrainer::exportThetaDense(int bufferSize, char *const buf)
|
|||
exportDenseMatrix(model.params.Theta, bufferSize, buf);
|
||||
}
|
||||
|
||||
void BonsaiTrainer::dumpModelMeanVar(const std::string& currResultsPath)
|
||||
void BonsaiTrainer::dumpModelMeanStd(const std::string& currResultsPath)
|
||||
{
|
||||
std::string params_path = currResultsPath + "/Params";
|
||||
writeMatrixInASCII(MatrixXuf(model.params.Z), params_path, "Z");
|
||||
|
@ -644,7 +644,7 @@ void BonsaiTrainer::dumpModelMeanVar(const std::string& currResultsPath)
|
|||
writeMatrixInASCII(MatrixXuf(model.params.Theta), params_path, "Theta");
|
||||
|
||||
writeMatrixInASCII(mean, params_path, "Mean");
|
||||
writeMatrixInASCII(variance, params_path, "Variance");
|
||||
writeMatrixInASCII(stdDev, params_path, "Std");
|
||||
LOG_INFO(currResultsPath);
|
||||
}
|
||||
|
||||
|
|
|
@ -165,26 +165,26 @@ namespace EdgeML
|
|||
|
||||
//
|
||||
// Size of the mean-var normalization values to be exported.
|
||||
// Call before exportMeanVar
|
||||
// Call before exportMeanStd
|
||||
//
|
||||
EXPORT_API(int) GetMeanVarSize(
|
||||
EXPORT_API(int) GetMeanStdSize(
|
||||
BonsaiTrainer* trainer)
|
||||
{
|
||||
size_t meanVarSize = trainer->getMeanVarSize();
|
||||
assert(meanVarSize < (size_t)(1 << 31)); // Because this is expected in TLC
|
||||
return (int)meanVarSize;
|
||||
size_t meanStdSize = trainer->getMeanStdSize();
|
||||
assert(meanStdSize < (size_t)(1 << 31)); // Because this is expected in TLC
|
||||
return (int)meanStdSize;
|
||||
}
|
||||
|
||||
//
|
||||
// Preallocate to size mean-var normalization
|
||||
//
|
||||
EXPORT_API(void) ExportMeanVar(
|
||||
EXPORT_API(void) ExportMeanStd(
|
||||
BonsaiTrainer* trainer,
|
||||
const int meanVarSize,
|
||||
const int meanStdSize,
|
||||
char *const buffer)
|
||||
{
|
||||
assert(meanVarSize == trainer->getMeanVarSize());
|
||||
trainer->exportMeanVar(meanVarSize, buffer);
|
||||
assert(meanStdSize == trainer->getMeanStdSize());
|
||||
trainer->exportMeanStd(meanStdSize, buffer);
|
||||
}
|
||||
|
||||
|
||||
|
@ -277,11 +277,11 @@ namespace EdgeML
|
|||
EXPORT_API(BonsaiPredictor*) CreatePredictor(
|
||||
const int numBytes,
|
||||
const char *const trainedModel,
|
||||
const int meanVarSize = 0,
|
||||
const int meanStdSize = 0,
|
||||
const char *const buffer = NULL)
|
||||
{
|
||||
BonsaiPredictor* predictor = new BonsaiPredictor(numBytes, trainedModel);
|
||||
predictor->importMeanVar(meanVarSize, buffer);
|
||||
predictor->importMeanStd(meanStdSize, buffer);
|
||||
return predictor;
|
||||
}
|
||||
|
||||
|
|
|
@ -426,7 +426,7 @@ void EdgeML::l2Normalize(SparseMatrixuf& dataMatrix)
|
|||
void EdgeML::meanVarNormalize(
|
||||
SparseMatrixuf& dataMatrix, //<
|
||||
MatrixXuf& mean, //< Initialize to vector of size numFeatures
|
||||
MatrixXuf& variance) //< Initialize to vector of size numFeatures
|
||||
MatrixXuf& stdDev) //< Initialize to vector of size numFeatures
|
||||
{
|
||||
MatrixXuf denseDataMatrix = MatrixXuf(dataMatrix);
|
||||
const Eigen::Index numDataPoints = denseDataMatrix.cols();
|
||||
|
@ -442,15 +442,15 @@ void EdgeML::meanVarNormalize(
|
|||
denseDataMatrix.transposeInPlace();
|
||||
|
||||
for (Eigen::Index f = 0; f < numFeatures; f++) {
|
||||
variance(f, 0) = (FP_TYPE)std::sqrt(dot(numDataPoints, denseDataMatrix.data() + f*numDataPoints, 1,
|
||||
stdDev(f, 0) = (FP_TYPE)std::sqrt(dot(numDataPoints, denseDataMatrix.data() + f*numDataPoints, 1,
|
||||
denseDataMatrix.data() + f*numDataPoints, 1)
|
||||
/ numDataPoints);
|
||||
|
||||
if (!(fabs(variance(f, 0)) < (FP_TYPE)1e-7)) {
|
||||
scal(numDataPoints, (FP_TYPE)1.0 / variance(f, 0), denseDataMatrix.data() + f*numDataPoints, 1);
|
||||
if (!(fabs(stdDev(f, 0)) < (FP_TYPE)1e-7)) {
|
||||
scal(numDataPoints, (FP_TYPE)1.0 / stdDev(f, 0), denseDataMatrix.data() + f*numDataPoints, 1);
|
||||
}
|
||||
else {
|
||||
variance(f, 0) = (FP_TYPE)1.0;
|
||||
stdDev(f, 0) = (FP_TYPE)1.0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -521,14 +521,14 @@ void EdgeML::loadMinMax(
|
|||
// void EdgeML::meanVarNormalize(
|
||||
// MatrixXuf& dataMatrix, //<
|
||||
// MatrixXuf& mean, //< Initialize to vector of size numFeatures
|
||||
// MatrixXuf& variance) //< Initialize to vector of size numFeatures
|
||||
// MatrixXuf& stdDev) //< Initialize to vector of size numFeatures
|
||||
// {
|
||||
|
||||
// const Eigen::Index numDataPoints = dataMatrix.cols();
|
||||
// const Eigen::Index numFeatures = dataMatrix.rows();
|
||||
|
||||
// assert(mean.rows() == numFeatures);
|
||||
// assert(variance.rows() == numFeatures);
|
||||
// assert(stdDev.rows() == numFeatures);
|
||||
|
||||
// const MatrixXuf onesVec = MatrixXuf::Ones(numDataPoints, 1);
|
||||
|
||||
|
@ -539,15 +539,15 @@ void EdgeML::loadMinMax(
|
|||
// dataMatrix.transposeInPlace();
|
||||
|
||||
// for(featureCount_t f = 0; f < numFeatures; f++) {
|
||||
// variance(f,0) = (FP_TYPE)std::sqrt(dot(numDataPoints, dataMatrix.data() + f*numDataPoints, 1,
|
||||
// stdDev(f,0) = (FP_TYPE)std::sqrt(dot(numDataPoints, dataMatrix.data() + f*numDataPoints, 1,
|
||||
// dataMatrix.data() + f*numDataPoints, 1)
|
||||
// / numDataPoints);
|
||||
|
||||
// if(!(fabs(variance(f, 0)) < (FP_TYPE)1e-7)) {
|
||||
// scal(numDataPoints, (FP_TYPE)1.0/variance(f,0), dataMatrix.data() + f*numDataPoints, 1);
|
||||
// if(!(fabs(stdDev(f, 0)) < (FP_TYPE)1e-7)) {
|
||||
// scal(numDataPoints, (FP_TYPE)1.0/stdDev(f,0), dataMatrix.data() + f*numDataPoints, 1);
|
||||
// }
|
||||
// else {
|
||||
// variance(f, 0) = (FP_TYPE)1.0;
|
||||
// stdDev(f, 0) = (FP_TYPE)1.0;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
|
|
@ -82,7 +82,7 @@ namespace EdgeML
|
|||
SparseMatrixuf Xvalidation, Yvalidation;
|
||||
SparseMatrixuf Xtest, Ytest;
|
||||
|
||||
MatrixXuf mean, variance;
|
||||
MatrixXuf mean, stdDev;
|
||||
MatrixXuf min, max;
|
||||
|
||||
void loadDataFromFile(
|
||||
|
@ -108,7 +108,7 @@ namespace EdgeML
|
|||
void computeMinMax(const SparseMatrixuf& dataMatrix, MatrixXuf& min, MatrixXuf& max);
|
||||
void minMaxNormalize(SparseMatrixuf& dataMatrix, const MatrixXuf& min, const MatrixXuf& max);
|
||||
void l2Normalize(SparseMatrixuf& dataMatrix);
|
||||
void meanVarNormalize(SparseMatrixuf& dataMatrix, MatrixXuf& mean, MatrixXuf& variance);
|
||||
void meanVarNormalize(SparseMatrixuf& dataMatrix, MatrixXuf& mean, MatrixXuf& stdDev);
|
||||
void saveMinMax(const MatrixXuf& min, const MatrixXuf& max, std::string fileName);
|
||||
void loadMinMax(MatrixXuf& min, MatrixXuf& max, int dim, std::string fileName);
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче