-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added different approximation to probabilistic PhocNet
- Loading branch information
1 parent
4e89d7f
commit ddb401a
Showing
5 changed files
with
208 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from __future__ import absolute_import | ||
|
||
import math | ||
from scipy.misc import logsumexp | ||
import numpy as np | ||
import torch | ||
|
||
|
||
def _logmexpm1(x): | ||
try: | ||
return math.log(-math.expm1(x)) | ||
except ValueError: | ||
return -np.inf | ||
|
||
|
||
def compute_independent(a, b): | ||
assert torch.is_tensor(a) | ||
assert torch.is_tensor(b) | ||
assert len(a) == len(b) | ||
a, b = a.view(-1), b.view(-1) | ||
|
||
result = 0.0 | ||
for a1, b1 in zip(a, b): | ||
h1 = a1 + b1 | ||
try: | ||
h0 = math.log(-math.expm1(a1)) + math.log(-math.expm1(b1)) | ||
except ValueError: | ||
h0 = -np.inf | ||
result += logsumexp([h0, h1]) | ||
|
||
return result | ||
|
||
|
||
def compute_upper_bound(a, b): | ||
assert torch.is_tensor(a) | ||
assert torch.is_tensor(b) | ||
assert len(a) == len(b) | ||
a, b = a.view(-1), b.view(-1) | ||
if len(a) == 0: | ||
return 0.0 | ||
|
||
ma = max(a[0], -math.expm1(a[0])) | ||
mb = max(b[0], -math.expm1(b[0])) | ||
result = max(a[0] + b[0], -(math.expm1(a[0]) + math.expm1(b[0]))) | ||
for a1, b1 in zip(a[1:], b[1:]): | ||
a0, b0 = _logmexpm1(a1), _logmexpm1(b1) | ||
aux0 = min(a0 + b0, a0 + mb, b0 + ma, result) | ||
aux1 = min(a1 + b1, a1 + mb, b1 + ma, result) | ||
result = max(aux0, aux1) | ||
ma = min(ma, max(a0, a1)) | ||
mb = min(mb, max(b0, b1)) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.