-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtg_node2vec_sub.cpp
34 lines (29 loc) · 985 Bytes
/
tg_node2vec_sub.cpp
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
// node2vec function: given random walk sequence, this function trains vector using skip-gram model
inline void tg_node2vec_sub(int dimension, string input_file, string output_file){
Model model(dimension);
model.sample_ = 0;
// model.window = 10;
int n_workers = 4;
std::vector<SentenceP> sentences;
size_t count =0;
const size_t max_sentence_len = 200;
SentenceP sentence(new Sentence);
std::ifstream in(input_file);
while (true) {
std::string s;
in >> s;
if (s.empty()) break;
++count;
sentence->tokens_.push_back(std::move(s));
if (count == max_sentence_len) {
count = 0;
sentences.push_back(std::move(sentence));
sentence.reset(new Sentence);
}
}
if (!sentence->tokens_.empty())
sentences.push_back(std::move(sentence));
model.build_vocab(sentences);
model.train(sentences, n_workers);
model.save(output_file);
}