-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.h
More file actions
83 lines (76 loc) · 3.24 KB
/
inference.h
File metadata and controls
83 lines (76 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "fastknn.h"
#include "embedding_parser.h"
#include "logger.h"
#include "csv_parser.h"
#include <iostream>
#include "queue_recommendations.h"
#include <thread>
#include <mutex>
#include <atomic>
namespace inference {
void InferenceKNN(const std::string &path_embeddings, size_t num_projections = 10) {
auto embedding_paser = EmbeddingParser(path_embeddings, "");
auto test_loader = CSVParser("../results/test_mod_v3.csv");
std::cout << "knn start...\n";
auto embeddings = embedding_paser.GetVector();
auto knn = FastKNN(embeddings, num_projections);
std::cout << "knn builded\n";
auto graph = test_loader.GetGraph();
auto bar = ProgressBar(graph.size(), graph.size() / 100);
std::vector<size_t> k_metrics;
for (size_t i = 1; i <= 20; ++i) {
k_metrics.push_back(i);
}
auto logger = Logger(k_metrics);
for (size_t i = 0; i < embeddings.size(); ++i) {
if (graph[i].empty()) {
continue;
}
auto predicted = knn.FindNeighbours(i, 10, {i});
logger.UpdateMetrics(predicted, graph[i]);
bar.UpgradeProgress(i);
}
logger.PrintResult();
}
void InferenceBFS(const std::string &path_embeddings) {
auto embedding_paser = EmbeddingParser(path_embeddings, "");
auto test_loader = CSVParser("../results/test_mod_v3.csv");
auto train_loader = CSVParser("../results/train_mod_v3.csv");
auto train_graph = train_loader.GetGraph();
auto graph = test_loader.GetGraph();
while (train_graph.size() < graph.size()) {
train_graph.push_back({});
}
auto embeddings = embedding_paser.GetVector();
auto recommender = BFSRecommendations(train_graph, embeddings);
auto bar = ProgressBar(graph.size(), graph.size() / 100);
std::vector<size_t> k_metrics;
for (size_t i = 1; i <= 20; ++i) {
k_metrics.push_back(i);
}
auto logger = Logger(k_metrics);
size_t n_threads = std::thread::hardware_concurrency() - 2;
std::mutex mtx;
std::vector<std::thread> threads_processor(n_threads);
int atom_len = (embeddings.size() + n_threads - 1) / n_threads;
for (int k = 0; k < embeddings.size(); k += atom_len) {
threads_processor[k / atom_len] = std::thread(
[atom_len, &mtx, &recommender, &logger, &embeddings, &graph](size_t k) {
for (size_t i = k; i < std::min(k + atom_len, embeddings.size()); ++i) {
if (graph[i].empty()) {
continue;
}
auto predicted = recommender.GetNeighbours(i, 3, 100);
{
std::lock_guard<std::mutex> lock(mtx);
logger.UpdateMetrics(predicted, graph[i]);
}
}
}, k);
}
for (size_t i = 0; i < n_threads; ++i) {
threads_processor[i].join();
}
logger.PrintResult();
}
}