diff --git a/.vscode/launch.json b/.vscode/launch.json index 407feb6..0ff1c47 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -25,12 +25,13 @@ "program": "${workspaceFolder}/build/src/Platform/main", "args": [ "-m", - "AODELd", + "AODE", "-p", "/Users/rmontanana/Code/discretizbench/datasets", "--stratified", "-d", - "wine" + "letter", + "--discretize" // "--hyperparameters", // "{\"repeatSparent\": true, \"maxModels\": 12}" ], diff --git a/src/BayesNet/Ensemble.cc b/src/BayesNet/Ensemble.cc index d7ee5b9..eed6a3c 100644 --- a/src/BayesNet/Ensemble.cc +++ b/src/BayesNet/Ensemble.cc @@ -34,18 +34,22 @@ namespace bayesnet { throw logic_error("Ensemble has not been fitted"); } Tensor y_pred = torch::zeros({ X.size(1), n_models }, kInt32); - //Create a threadpool - auto threads{ vector() }; - mutex mtx; + // //Create a threadpool + // auto threads{ vector() }; + // mutex mtx; + // for (auto i = 0; i < n_models; ++i) { + // threads.push_back(thread([&, i]() { + // auto ypredict = models[i]->predict(X); + // lock_guard lock(mtx); + // y_pred.index_put_({ "...", i }, ypredict); + // })); + // Hacer voting aquĆ­ ? ? ? + // } + // for (auto& thread : threads) { + // thread.join(); + // } for (auto i = 0; i < n_models; ++i) { - threads.push_back(thread([&, i]() { - auto ypredict = models[i]->predict(X); - lock_guard lock(mtx); - y_pred.index_put_({ "...", i }, ypredict); - })); - } - for (auto& thread : threads) { - thread.join(); + y_pred.index_put_({ "...", i }, models[i]->predict(X)); } return torch::tensor(voting(y_pred)); } diff --git a/src/BayesNet/Network.cc b/src/BayesNet/Network.cc index 15fcacc..fb8bac8 100644 --- a/src/BayesNet/Network.cc +++ b/src/BayesNet/Network.cc @@ -174,42 +174,10 @@ namespace bayesnet { { setStates(states); laplaceSmoothing = 1.0 / samples.size(1); // To use in CPT computation - int maxThreadsRunning = static_cast(std::thread::hardware_concurrency() * maxThreads); - if (maxThreadsRunning < 1) { - maxThreadsRunning = 1; + for (auto& node : nodes) { + node.second->computeCPT(samples, features, laplaceSmoothing, weights); + fitted = true; } - vector threads; - mutex mtx; - condition_variable cv; - int activeThreads = 0; - int nextNodeIndex = 0; - while (nextNodeIndex < nodes.size()) { - unique_lock lock(mtx); - cv.wait(lock, [&activeThreads, &maxThreadsRunning]() { return activeThreads < maxThreadsRunning; }); - threads.emplace_back([this, &nextNodeIndex, &mtx, &cv, &activeThreads, &weights]() { - while (true) { - unique_lock lock(mtx); - if (nextNodeIndex >= nodes.size()) { - break; // No more work remaining - } - auto& pair = *std::next(nodes.begin(), nextNodeIndex); - ++nextNodeIndex; - lock.unlock(); - pair.second->computeCPT(samples, features, laplaceSmoothing, weights); - lock.lock(); - nodes[pair.first] = std::move(pair.second); - lock.unlock(); - } - lock_guard lock(mtx); - --activeThreads; - cv.notify_one(); - }); - ++activeThreads; - } - for (auto& thread : threads) { - thread.join(); - } - fitted = true; } torch::Tensor Network::predict_tensor(const torch::Tensor& samples, const bool proba) { @@ -331,19 +299,25 @@ namespace bayesnet { vector Network::exactInference(map& evidence) { vector result(classNumStates, 0.0); - vector threads; - mutex mtx; + // vector threads; + // mutex mtx; + // for (int i = 0; i < classNumStates; ++i) { + // threads.emplace_back([this, &result, &evidence, i, &mtx]() { + // auto completeEvidence = map(evidence); + // completeEvidence[getClassName()] = i; + // double factor = computeFactor(completeEvidence); + // lock_guard lock(mtx); + // result[i] = factor; + // }); + // } + // for (auto& thread : threads) { + // thread.join(); + // } for (int i = 0; i < classNumStates; ++i) { - threads.emplace_back([this, &result, &evidence, i, &mtx]() { - auto completeEvidence = map(evidence); - completeEvidence[getClassName()] = i; - double factor = computeFactor(completeEvidence); - lock_guard lock(mtx); - result[i] = factor; - }); - } - for (auto& thread : threads) { - thread.join(); + auto completeEvidence = map(evidence); + completeEvidence[getClassName()] = i; + double factor = computeFactor(completeEvidence); + result[i] = factor; } // Normalize result double sum = accumulate(result.begin(), result.end(), 0.0); diff --git a/src/BayesNet/Network.h b/src/BayesNet/Network.h index a26e790..7e2e397 100644 --- a/src/BayesNet/Network.h +++ b/src/BayesNet/Network.h @@ -27,6 +27,7 @@ namespace bayesnet { Network(); explicit Network(float); explicit Network(Network&); + ~Network() = default; torch::Tensor& getSamples(); float getmaxThreads(); void addNode(const string&); diff --git a/src/Platform/Experiment.cc b/src/Platform/Experiment.cc index ac26972..83cb840 100644 --- a/src/Platform/Experiment.cc +++ b/src/Platform/Experiment.cc @@ -179,6 +179,7 @@ namespace platform { result.addTimeTrain(train_time[item].item()); result.addTimeTest(test_time[item].item()); item++; + clf.reset(); } cout << "end. " << flush; } diff --git a/src/Platform/Models.cc b/src/Platform/Models.cc index 1a66156..08b4a45 100644 --- a/src/Platform/Models.cc +++ b/src/Platform/Models.cc @@ -26,7 +26,7 @@ namespace platform { instance = it->second(); // wrap instance in a shared ptr and return if (instance != nullptr) - return shared_ptr(instance); + return unique_ptr(instance); else return nullptr; }