This repository has been archived by the owner on Feb 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helper): add as_numpy_array decorator
- Loading branch information
hanhxiao
committed
Aug 26, 2019
1 parent
543d561
commit a584c7e
Showing
4 changed files
with
81 additions
and
29 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
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,35 @@ | ||
import unittest | ||
|
||
import numpy as np | ||
import torch | ||
from numpy.testing import assert_allclose | ||
|
||
from gnes.encoder.numeric.pooling import PoolingEncoder | ||
|
||
|
||
class TestEncoder(unittest.TestCase): | ||
def setUp(self): | ||
self.seq_data = np.random.random([5, 10]) | ||
self.seq_embed_data = np.random.random([5, 10, 32]) | ||
self.mask_data = np.array(self.seq_data > 0.5, np.float32) | ||
self.data = [ | ||
(torch.tensor(self.seq_embed_data, dtype=torch.float32), torch.tensor(self.mask_data, dtype=torch.float32)), | ||
(self.seq_embed_data, self.mask_data), | ||
(self.seq_embed_data, self.mask_data)] | ||
|
||
def _test_strategy(self, strategy): | ||
pe_to = PoolingEncoder(strategy, 'torch') | ||
pe_tf = PoolingEncoder(strategy, 'tensorflow') | ||
pe_np = PoolingEncoder(strategy, 'numpy') | ||
return [pe.encode(self.data[idx]) for idx, pe in enumerate([pe_to, pe_tf, pe_np])] | ||
|
||
def test_all(self): | ||
for s in {'REDUCE_MEAN', 'REDUCE_MAX', 'REDUCE_MEAN_MAX'}: | ||
with self.subTest(strategy=s): | ||
r = self._test_strategy(s) | ||
for rr in r: | ||
print(type(rr)) | ||
print(rr) | ||
print('---') | ||
assert_allclose(r[0], r[1], rtol=1e-5) | ||
assert_allclose(r[1], r[2], rtol=1e-5) |