forked from taku910/crfpp
-
Notifications
You must be signed in to change notification settings - Fork 11
/
node.cpp
44 lines (40 loc) · 1.09 KB
/
node.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
//
// CRF++ -- Yet Another CRF toolkit
//
// $Id: node.cpp 1595 2007-02-24 10:18:32Z taku $;
//
// Copyright(C) 2005-2007 Taku Kudo <[email protected]>
//
#include <stdlib.h>
#include <cmath>
#include "node.h"
#include "common.h"
namespace CRFPP {
void Node::calcAlpha() {
alpha = 0.0;
for (const_Path_iterator it = lpath.begin(); it != lpath.end(); ++it) {
alpha = logsumexp(alpha,
(*it)->cost +(*it)->lnode->alpha,
(it == lpath.begin()));
}
alpha += cost;
}
void Node::calcBeta() {
beta = 0.0;
for (const_Path_iterator it = rpath.begin(); it != rpath.end(); ++it) {
beta = logsumexp(beta,
(*it)->cost +(*it)->rnode->beta,
(it == rpath.begin()));
}
beta += cost;
}
void Node::calcExpectation(double *expected, double Z, size_t size) const {
const double c = std::exp(alpha + beta - cost - Z);
for (const int *f = fvector; *f != -1; ++f) {
expected[*f + y] += c;
}
for (const_Path_iterator it = lpath.begin(); it != lpath.end(); ++it) {
(*it)->calcExpectation(expected, Z, size);
}
}
}