Reformatted
This commit is contained in:
parent
c5df89c17b
commit
fdc1d3b91a
23 changed files with 455 additions and 418 deletions
|
|
@ -4,33 +4,35 @@
|
|||
#include "fmt/ranges.h"
|
||||
#include "kompute/Kompute.hpp"
|
||||
|
||||
TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegression) {
|
||||
TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegression)
|
||||
{
|
||||
|
||||
uint32_t ITERATIONS = 100;
|
||||
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> 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> y{ new kp::Tensor({ 0, 0, 0, 1, 1 }) };
|
||||
|
||||
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> 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({ 0 })};
|
||||
std::shared_ptr<kp::Tensor> bOut{ new kp::Tensor({ 0, 0, 0, 0, 0 })};
|
||||
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::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, lOut};
|
||||
std::vector<std::shared_ptr<kp::Tensor>> params = { xI, xJ, y,
|
||||
wIn, wOutI, wOutJ,
|
||||
bIn, bOut, lOut };
|
||||
|
||||
{
|
||||
kp::Manager mgr;
|
||||
|
||||
if (std::shared_ptr<kp::Sequence> sq =
|
||||
mgr.getOrCreateManagedSequence("createTensors").lock()) {
|
||||
if (std::shared_ptr<kp::Sequence> sq =
|
||||
mgr.getOrCreateManagedSequence("createTensors").lock()) {
|
||||
|
||||
sq->begin();
|
||||
|
||||
|
|
@ -42,13 +44,12 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegression) {
|
|||
// Record op algo base
|
||||
sq->begin();
|
||||
|
||||
sq->record<kp::OpTensorSyncDevice>({wIn, bIn});
|
||||
sq->record<kp::OpTensorSyncDevice>({ wIn, bIn });
|
||||
|
||||
sq->record<kp::OpAlgoBase<>>(
|
||||
params,
|
||||
"test/shaders/glsl/test_logistic_regression.comp");
|
||||
params, "test/shaders/glsl/test_logistic_regression.comp");
|
||||
|
||||
sq->record<kp::OpTensorSyncLocal>({wOutI, wOutJ, bOut, lOut});
|
||||
sq->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
||||
sq->end();
|
||||
|
||||
|
|
@ -57,7 +58,7 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegression) {
|
|||
|
||||
sq->eval();
|
||||
|
||||
for(size_t j = 0; j < bOut->size(); j++) {
|
||||
for (size_t j = 0; j < bOut->size(); j++) {
|
||||
wIn->data()[0] -= learningRate * wOutI->data()[j];
|
||||
wIn->data()[1] -= learningRate * wOutJ->data()[j];
|
||||
bIn->data()[0] -= learningRate * bOut->data()[j];
|
||||
|
|
@ -66,7 +67,6 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegression) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// Based on the inputs the outputs should be at least:
|
||||
// * wi < 0.01
|
||||
// * wj > 1.0
|
||||
|
|
@ -77,11 +77,14 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegression) {
|
|||
EXPECT_LT(bIn->data()[0], 0.0);
|
||||
EXPECT_LT(bIn->data()[0], 0.0);
|
||||
|
||||
SPDLOG_WARN("Result wIn: {}, bIn: {}, loss: {}",
|
||||
wIn->data(), bIn->data(), lOut->data());
|
||||
SPDLOG_WARN("Result wIn: {}, bIn: {}, loss: {}",
|
||||
wIn->data(),
|
||||
bIn->data(),
|
||||
lOut->data());
|
||||
}
|
||||
|
||||
TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy) {
|
||||
TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy)
|
||||
{
|
||||
|
||||
uint32_t ITERATIONS = 100;
|
||||
float learningRate = 0.1;
|
||||
|
|
@ -89,30 +92,31 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy) {
|
|||
std::vector<float> wInVec = { 0.001, 0.001 };
|
||||
std::vector<float> bInVec = { 0 };
|
||||
|
||||
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> 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> y{ new kp::Tensor({ 0, 0, 0, 1, 1 }) };
|
||||
|
||||
std::shared_ptr<kp::Tensor> wIn{
|
||||
new kp::Tensor(wInVec, kp::Tensor::TensorTypes::eStaging)};
|
||||
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> wIn{ new kp::Tensor(
|
||||
wInVec, kp::Tensor::TensorTypes::eStaging) };
|
||||
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, kp::Tensor::TensorTypes::eStaging)};
|
||||
std::shared_ptr<kp::Tensor> bOut{ new kp::Tensor({ 0, 0, 0, 0, 0 })};
|
||||
std::shared_ptr<kp::Tensor> bIn{ 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::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, lOut};
|
||||
std::vector<std::shared_ptr<kp::Tensor>> params = { xI, xJ, y,
|
||||
wIn, wOutI, wOutJ,
|
||||
bIn, bOut, lOut };
|
||||
|
||||
{
|
||||
kp::Manager mgr;
|
||||
|
||||
if (std::shared_ptr<kp::Sequence> sq =
|
||||
mgr.getOrCreateManagedSequence("createTensors").lock()) {
|
||||
if (std::shared_ptr<kp::Sequence> sq =
|
||||
mgr.getOrCreateManagedSequence("createTensors").lock()) {
|
||||
|
||||
sq->begin();
|
||||
|
||||
|
|
@ -125,10 +129,9 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy) {
|
|||
sq->begin();
|
||||
|
||||
sq->record<kp::OpAlgoBase<>>(
|
||||
params,
|
||||
"test/shaders/glsl/test_logistic_regression.comp");
|
||||
params, "test/shaders/glsl/test_logistic_regression.comp");
|
||||
|
||||
sq->record<kp::OpTensorSyncLocal>({wOutI, wOutJ, bOut, lOut});
|
||||
sq->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
||||
sq->end();
|
||||
|
||||
|
|
@ -137,7 +140,7 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy) {
|
|||
|
||||
sq->eval();
|
||||
|
||||
for(size_t j = 0; j < bOut->size(); j++) {
|
||||
for (size_t j = 0; j < bOut->size(); j++) {
|
||||
wIn->data()[0] -= learningRate * wOutI->data()[j];
|
||||
wIn->data()[1] -= learningRate * wOutJ->data()[j];
|
||||
bIn->data()[0] -= learningRate * bOut->data()[j];
|
||||
|
|
@ -148,7 +151,6 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// Based on the inputs the outputs should be at least:
|
||||
// * wi < 0.01
|
||||
// * wj > 1.0
|
||||
|
|
@ -158,6 +160,8 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy) {
|
|||
EXPECT_GT(wIn->data()[1], 1.0);
|
||||
EXPECT_LT(bIn->data()[0], 0.0);
|
||||
|
||||
SPDLOG_WARN("Result wIn: {}, bIn: {}, loss: {}",
|
||||
wIn->data(), bIn->data(), lOut->data());
|
||||
SPDLOG_WARN("Result wIn: {}, bIn: {}, loss: {}",
|
||||
wIn->data(),
|
||||
bIn->data(),
|
||||
lOut->data());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue