improve a calculation of feature probability

This commit is contained in:
falsycat 2022-10-12 20:51:07 +09:00
parent df6302c699
commit b61b6dd21b
4 changed files with 29 additions and 14 deletions

View File

@ -17,9 +17,6 @@ HelpFlag help {
parser, "help", "display this menu", {'h', "help"}, parser, "help", "display this menu", {'h', "help"},
}; };
ValueFlag<std::string> smap {
parser, "path", "step map file path", {"smap"},
};
ValueFlag<std::string> fmap { ValueFlag<std::string> fmap {
parser, "path", "feature map file path", {"fmap"}, parser, "path", "feature map file path", {"fmap"},
}; };
@ -41,13 +38,30 @@ static void Exec() {
for (size_t t = 0; t < aprobs.size(); ++t) { for (size_t t = 0; t < aprobs.size(); ++t) {
const auto tidx = t % fmap.size(); const auto tidx = t % fmap.size();
const auto bnum = aprobs[t].size();
for (size_t c = 0; c < fmap[tidx].size(); ++c) { for (size_t c = 0; c < fmap[tidx].size(); ++c) {
double sum = 0; const auto& blocks = fmap[tidx][c];
for (auto i : fmap[tidx][c]) {
Enforce(i < aprobs[t].size(), "aprob has no enough columns"); double positive = 0, negative = 0;
sum += aprobs[t][i]; for (uint32_t b = 0; b < aprobs[t].size(); ++b) {
if (blocks.end() != std::find(blocks.begin(), blocks.end(), b)) {
positive += aprobs[t][b];
} else {
negative += aprobs[t][b];
}
} }
std::cout << sum / fmap[tidx][c].size() << ' '; if (blocks.size() > 0) {
positive /= blocks.size();
} else {
positive = 1;
}
if (bnum > blocks.size()) {
negative /= bnum - blocks.size();
} else {
negative = 0;
}
const auto prob = positive * (1-negative);
std::cout << prob << ' ';
} }
std::cout << '\n'; std::cout << '\n';
} }

View File

@ -42,6 +42,7 @@ auto ReadTensor3(std::istream& st) noexcept {
std::istream_iterator<T> {}); std::istream_iterator<T> {});
} }
} }
if (ret.back().empty()) ret.pop_back();
return ret; return ret;
} }

View File

@ -30,7 +30,7 @@ ValueFlag<uint32_t> fnum {
static void Exec() { static void Exec() {
const auto fnum = args::get(param::fnum); const auto fnum = args::get(param::fnum);
Enforce(fnum > 0, "fc-num must be greater than 0"); Enforce(fnum > 0, "fnum must be greater than 0");
std::ifstream smap_st {args::get(param::smap)}; std::ifstream smap_st {args::get(param::smap)};
Enforce(!!smap_st, "smap path is invalid"); Enforce(!!smap_st, "smap path is invalid");

View File

@ -20,8 +20,8 @@ HelpFlag help {
ValueFlag<size_t> dur { ValueFlag<size_t> dur {
parser, "100", "duration", {"dur"}, 100, parser, "100", "duration", {"dur"}, 100,
}; };
ValueFlag<size_t> fcnum { ValueFlag<size_t> fnum {
parser, "50", "number of feature code alphabet", {"fc-num"}, 50, parser, "50", "number of feature code alphabet", {"fnum"}, 50,
}; };
ValueFlag<size_t> branch { ValueFlag<size_t> branch {
parser, "2", "number of branch", {"branch"}, 2, parser, "2", "number of branch", {"branch"}, 2,
@ -51,7 +51,7 @@ Flag inc_time {
size_t Step(size_t t, size_t c, size_t b) { size_t Step(size_t t, size_t c, size_t b) {
const auto fcnum = args::get(param::fcnum); const auto fnum = args::get(param::fnum);
size_t ret; size_t ret;
switch (args::get(param::algo)) { switch (args::get(param::algo)) {
@ -60,7 +60,7 @@ size_t Step(size_t t, size_t c, size_t b) {
if (param::inc_time) { if (param::inc_time) {
ret += t; ret += t;
} }
return ret%fcnum; return ret%fnum;
default: default:
assert(false); assert(false);
@ -70,7 +70,7 @@ size_t Step(size_t t, size_t c, size_t b) {
void Exec() { void Exec() {
for (size_t t = 0; t < args::get(param::dur); ++t) { for (size_t t = 0; t < args::get(param::dur); ++t) {
for (size_t c = 0; c < args::get(param::fcnum); ++c) { for (size_t c = 0; c < args::get(param::fnum); ++c) {
for (size_t b = 0; b < args::get(param::branch); ++b) { for (size_t b = 0; b < args::get(param::branch); ++b) {
std::cout << Step(t, c, b) << ' '; std::cout << Step(t, c, b) << ' ';
} }