-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_txt_file.cpp
86 lines (72 loc) · 2.35 KB
/
generate_txt_file.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
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
84
85
86
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2015 Kostiantyn Antoniuk
* Copyright (C) 2015 Kostiantyn Antoniuk
*/
#include "sparse_matrix.h"
#include "dense_vector.h"
#include "data.h"
#include "loss.h"
#include "model_evaluator.h"
#include <string>
#include <fstream>
#include <iostream>
#include <chrono>
#include <vector>
using namespace std;
template <class T>
using DenseVec = Vilma::DenseVector<T>;
// create txt file each row of wich is
// y_l y_r idx_1:val_1 idx_2:val_2 ... idx_n:val_n
//
void GenerateTxt(const string &input_dir, const string &output_filename) {
Data data;
std::cout << "Loading train data from file: " << (input_dir + "-trn.bin")
<< endl;
if (LoadData(input_dir + "-trn.bin", &data, 33000, 0)) {
std::cout << "Data loaded.\n";
} else {
std::cout << "Failed to load data!\n";
return;
}
const int dim = data.x->kRows;
const int ny = data.ny;
const int n_examples = data.x->kRows;
std::ofstream feat_file(output_filename + "_features.txt", std::ios::out);
std::ofstream suplab_file(output_filename + "_supervised_labeling.txt",
std::ios::out);
std::ofstream parlab_file(output_filename + "_partial_labeling.txt",
std::ios::out);
for (int i = 0; i < n_examples; ++i) {
const int yl = data.yl->data_[i];
const int yr = data.yr->data_[i];
const int y = data.y->data_[i];
auto x = data.x->GetRow(i);
for (int k = 0; k < x->non_zero_; ++k) {
feat_file << x->index_[k] << ":" << x->vals_[k];
if (k + 1 < x->non_zero_) {
feat_file << " ";
} else {
feat_file << endl;
}
}
suplab_file << y << " " << y << endl;
parlab_file << yl << " " << yr << endl;
}
feat_file.close();
suplab_file.close();
parlab_file.close();
cout << "dim: " << dim << " ny: " << ny << endl;
}
int main(int argc, const char *argv[]) {
const string input_dir =
"/Users/Shared/research/code/vilma/vilma/data/morph"; // argv[1];
const string output_dir =
"/Users/Shared/research/code/vilma/vilma/data/morph"; // argv[2];
GenerateTxt(input_dir, output_dir);
return 0;
}