-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoftmax.py
More file actions
35 lines (31 loc) · 704 Bytes
/
softmax.py
File metadata and controls
35 lines (31 loc) · 704 Bytes
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
'''
Softmax with temperature and probability weighting.
'''
from math import log, exp
COLDNESS = 1
def softmax(X, coldness = COLDNESS):
try:
X[0][0]
except TypeError:
X = [(x, 1) for x in X]
return log(
sum([
exp(x * coldness) * prob
for (x, prob) in X
]) / sum(
[prob for (x, prob) in X]
)
) / coldness
def softmin(X, coldness = COLDNESS):
try:
X[0][0]
except TypeError:
X = [(x, 1) for x in X]
return - log(
sum([
exp(-x * coldness) * prob
for (x, prob) in X
]) / sum(
[prob for (x, prob) in X]
)
) / coldness