Updated logistic regression example to also output the loss

This commit is contained in:
Alejandro Saucedo 2020-09-12 17:15:46 +01:00
parent 9f8508075a
commit ad7321eb87
4 changed files with 409 additions and 389 deletions

View file

@ -7,26 +7,24 @@
TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegression) {
uint32_t ITERATIONS = 100;
std::vector<float> wInVec = { 0.001, 0.001 };
std::vector<float> bInVec = { 0 };
float learningRate = 0.1;
std::shared_ptr<kp::Tensor> xI{ new kp::Tensor({ 0, 1, 1, 1, 1 })};
std::shared_ptr<kp::Tensor> xJ{ new kp::Tensor({ 0, 0, 0, 1, 1 })};
std::shared_ptr<kp::Tensor> y{ new kp::Tensor({ 0, 0, 0, 1, 1 })};
std::shared_ptr<kp::Tensor> wIn{
new kp::Tensor(wInVec)};
std::shared_ptr<kp::Tensor> wIn{ new kp::Tensor({ 0.001, 0.001 })};
std::shared_ptr<kp::Tensor> wOutI{ new kp::Tensor({ 0, 0, 0, 0, 0 })};
std::shared_ptr<kp::Tensor> wOutJ{ new kp::Tensor({ 0, 0, 0, 0, 0 })};
std::shared_ptr<kp::Tensor> bIn{
new kp::Tensor(bInVec)};
std::shared_ptr<kp::Tensor> bIn{ new kp::Tensor({ 0 })};
std::shared_ptr<kp::Tensor> bOut{ new kp::Tensor({ 0, 0, 0, 0, 0 })};
std::shared_ptr<kp::Tensor> lOut{ new kp::Tensor({ 0, 0, 0, 0, 0 })};
std::vector<std::shared_ptr<kp::Tensor>> params =
{xI, xJ, y, wIn, wOutI, wOutJ, bIn, bOut};
{xI, xJ, y, wIn, wOutI, wOutJ, bIn, bOut, lOut};
{
kp::Manager mgr;
@ -50,7 +48,7 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegression) {
params,
"test/shaders/glsl/test_logistic_regression.comp");
sq->record<kp::OpTensorSyncLocal>({wOutI, wOutJ, bOut});
sq->record<kp::OpTensorSyncLocal>({wOutI, wOutJ, bOut, lOut});
sq->end();
@ -60,9 +58,9 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegression) {
sq->eval();
for(size_t j = 0; j < bOut->size(); j++) {
wIn->data()[0] -= wOutI->data()[j];
wIn->data()[1] -= wOutJ->data()[j];
bIn->data()[0] -= bOut->data()[j];
wIn->data()[0] -= learningRate * wOutI->data()[j];
wIn->data()[1] -= learningRate * wOutJ->data()[j];
bIn->data()[0] -= learningRate * bOut->data()[j];
}
}
}
@ -77,14 +75,16 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegression) {
EXPECT_LT(wIn->data()[0], 0.01);
EXPECT_GT(wIn->data()[1], 1.0);
EXPECT_LT(bIn->data()[0], 0.0);
EXPECT_LT(bIn->data()[0], 0.0);
SPDLOG_ERROR("Result wIn: {}, bIn: {}",
wIn->data(), bIn->data());
SPDLOG_WARN("Result wIn: {}, bIn: {}, loss: {}",
wIn->data(), bIn->data(), lOut->data());
}
TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy) {
uint32_t ITERATIONS = 100;
float learningRate = 0.1;
std::vector<float> wInVec = { 0.001, 0.001 };
std::vector<float> bInVec = { 0 };
@ -103,8 +103,10 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy) {
new kp::Tensor(bInVec, kp::Tensor::TensorTypes::eStaging)};
std::shared_ptr<kp::Tensor> bOut{ new kp::Tensor({ 0, 0, 0, 0, 0 })};
std::shared_ptr<kp::Tensor> lOut{ new kp::Tensor({ 0, 0, 0, 0, 0 })};
std::vector<std::shared_ptr<kp::Tensor>> params =
{xI, xJ, y, wIn, wOutI, wOutJ, bIn, bOut};
{xI, xJ, y, wIn, wOutI, wOutJ, bIn, bOut, lOut};
{
kp::Manager mgr;
@ -126,7 +128,7 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy) {
params,
"test/shaders/glsl/test_logistic_regression.comp");
sq->record<kp::OpTensorSyncLocal>({wOutI, wOutJ, bOut});
sq->record<kp::OpTensorSyncLocal>({wOutI, wOutJ, bOut, lOut});
sq->end();
@ -136,9 +138,9 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy) {
sq->eval();
for(size_t j = 0; j < bOut->size(); j++) {
wIn->data()[0] -= wOutI->data()[j];
wIn->data()[1] -= wOutJ->data()[j];
bIn->data()[0] -= bOut->data()[j];
wIn->data()[0] -= learningRate * wOutI->data()[j];
wIn->data()[1] -= learningRate * wOutJ->data()[j];
bIn->data()[0] -= learningRate * bOut->data()[j];
}
wIn->mapDataIntoHostMemory();
bIn->mapDataIntoHostMemory();
@ -156,6 +158,6 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy) {
EXPECT_GT(wIn->data()[1], 1.0);
EXPECT_LT(bIn->data()[0], 0.0);
SPDLOG_ERROR("Result wIn: {}, bIn: {}",
wIn->data(), bIn->data());
SPDLOG_WARN("Result wIn: {}, bIn: {}, loss: {}",
wIn->data(), bIn->data(), lOut->data());
}