forked from blei-lab/deep-exponential-families
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink_function.hpp
69 lines (61 loc) · 1.27 KB
/
link_function.hpp
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
#pragma once
#include "utils.hpp"
#include <cassert>
#include <cmath>
#include <unordered_map>
using namespace std;
struct LinkFunction {
virtual double f(double v) = 0;
virtual double g(double v) = 0;
virtual double f_inv(double v) = 0;
};
// log(1+exp(x)) (really softplus)
struct SoftMax : public LinkFunction {
virtual double f(double x) {
if (x < -5)
return exp(x);
else if (x > 10)
return x;
else
return log(1+exp(x));
}
virtual double g(double x) {
if (x > 10)
return 1;
else
return exp(x) / (1+exp(x));
}
virtual double f_inv(double y) {
assert(y > 0);
if (y > 10)
return y;
else
return log(exp(y) - 1);
}
};
struct ShiftedSoftMax : public LinkFunction {
SoftMax h;
double shift = 10;
virtual double f(double x) {
return h.f(x - shift);
}
virtual double g(double x) {
return h.g(x - shift);
}
virtual double f_inv(double y) {
return h.f_inv(y) + shift;
}
};
struct IdentityLink : public LinkFunction {
virtual double f(double x) {
return x;
}
virtual double g(double x) {
return 1;
}
virtual double f_inv(double y) {
return y;
}
};
LinkFunction* get_link_function(const string& lf_name);
void init_shifted_softmax(const pt::ptree& ptree);