Commit edd977bb authored by Marcel Rieger's avatar Marcel Rieger

Make tests compatible between tf 1 and 2.

parent c69e0f5b
Pipeline #118292 passed with stages
in 39 seconds
...@@ -12,7 +12,23 @@ lint: ...@@ -12,7 +12,23 @@ lint:
- pip install flake8 --user - pip install flake8 --user
- flake8 lbn.py test.py setup.py - flake8 lbn.py test.py setup.py
unittests_py2: unittest_tf1_py2:
stage: test
tags:
- docker
image: tensorflow/tensorflow:1.13.1
script:
- python -m unittest test
unittests_tf1_py3:
stage: test
tags:
- docker
image: tensorflow/tensorflow:1.13.1-py3
script:
- python -m unittest test
unittest_tf2_py2:
stage: test stage: test
tags: tags:
- docker - docker
...@@ -20,7 +36,7 @@ unittests_py2: ...@@ -20,7 +36,7 @@ unittests_py2:
script: script:
- python -m unittest test - python -m unittest test
unittests_py3: unittests_tf2_py3:
stage: test stage: test
tags: tags:
- docker - docker
......
...@@ -8,6 +8,7 @@ LBN unit tests. ...@@ -8,6 +8,7 @@ LBN unit tests.
__all__ = ["TestCase"] __all__ = ["TestCase"]
import sys
import unittest import unittest
import numpy as np import numpy as np
...@@ -16,6 +17,13 @@ import tensorflow as tf ...@@ -16,6 +17,13 @@ import tensorflow as tf
from lbn import LBN, LBNLayer, FeatureFactory from lbn import LBN, LBNLayer, FeatureFactory
PY3 = sys.version.startswith("3.")
TF2 = tf.__version__.startswith("2.")
if not TF2:
tf.enable_eager_execution()
class TestCase(unittest.TestCase): class TestCase(unittest.TestCase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
...@@ -23,7 +31,10 @@ class TestCase(unittest.TestCase): ...@@ -23,7 +31,10 @@ class TestCase(unittest.TestCase):
# fixate random seeds # fixate random seeds
np.random.seed(123) np.random.seed(123)
tf.random.set_seed(123) if TF2:
tf.random.set_seed(123)
else:
tf.random.set_random_seed(123)
# create some four-vectors with fixed seed and batch size 2 # create some four-vectors with fixed seed and batch size 2
self.vectors = create_four_vectors((2, 10)) self.vectors = create_four_vectors((2, 10))
...@@ -337,10 +348,10 @@ class TestCase(unittest.TestCase): ...@@ -337,10 +348,10 @@ class TestCase(unittest.TestCase):
model = Model() model = Model()
output = model(self.vectors_t, features=self.feature_set).numpy() output = model(self.vectors_t, features=self.feature_set).numpy()
self.assertAlmostEqual(output[0, 0], 0.548664, 5) self.assertAlmostEqual(output[0, 0], 0.548664 if PY3 else 0.795995, 5)
self.assertAlmostEqual(output[0, 1], 0.451337, 5) self.assertAlmostEqual(output[0, 1], 0.451337 if PY3 else 0.204005, 5)
self.assertAlmostEqual(output[1, 0], 0.394629, 5) self.assertAlmostEqual(output[1, 0], 0.394629 if PY3 else 0.177576, 5)
self.assertAlmostEqual(output[1, 1], 0.605371, 5) self.assertAlmostEqual(output[1, 1], 0.605371 if PY3 else 0.822424, 5)
def create_four_vectors(n, p_low=-100., p_high=100., m_low=0.1, m_high=50., seed=None): def create_four_vectors(n, p_low=-100., p_high=100., m_low=0.1, m_high=50., seed=None):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment