Commit 343cf82d authored by lucas_miranda's avatar lucas_miranda
Browse files

Added tests for model_utils.py

parent 9a9b2aeb
...@@ -12,13 +12,13 @@ from itertools import combinations ...@@ -12,13 +12,13 @@ from itertools import combinations
from tensorflow.keras import backend as K from tensorflow.keras import backend as K
from tensorflow.keras.constraints import Constraint from tensorflow.keras.constraints import Constraint
from tensorflow.keras.layers import Layer from tensorflow.keras.layers import Layer
import networkx as nx
import tensorflow as tf import tensorflow as tf
import tensorflow_probability as tfp import tensorflow_probability as tfp
tfd = tfp.distributions tfd = tfp.distributions
tfpl = tfp.layers tfpl = tfp.layers
# Helper functions # Helper functions
@tf.function @tf.function
def far_away_uniform_initialiser( def far_away_uniform_initialiser(
...@@ -58,7 +58,21 @@ def far_away_uniform_initialiser( ...@@ -58,7 +58,21 @@ def far_away_uniform_initialiser(
return init return init
def compute_kernel(x, y): def compute_kernel(x: tf.Tensor, y: tf.Tensor) -> tf.Tensor:
"""
Computes the MMD between the two specified vectors using a gaussian kernel.
Parameters:
- x (tf.Tensor): left tensor
- y (tf.Tensor): right tensor
Returns
- kernel (tf.Tensor): returns the result of applying the kernel, for
each training instance
"""
x_size = tf.shape(x)[0] x_size = tf.shape(x)[0]
y_size = tf.shape(y)[0] y_size = tf.shape(y)[0]
dim = tf.shape(x)[1] dim = tf.shape(x)[1]
...@@ -68,13 +82,26 @@ def compute_kernel(x, y): ...@@ -68,13 +82,26 @@ def compute_kernel(x, y):
tiled_y = tf.tile( tiled_y = tf.tile(
tf.reshape(y, tf.stack([1, y_size, dim])), tf.stack([x_size, 1, 1]) tf.reshape(y, tf.stack([1, y_size, dim])), tf.stack([x_size, 1, 1])
) )
return tf.exp( kernel = tf.exp(
-tf.reduce_mean(tf.square(tiled_x - tiled_y), axis=2) / tf.cast(dim, tf.float32) -tf.reduce_mean(tf.square(tiled_x - tiled_y), axis=2) / tf.cast(dim, tf.float32)
) )
return kernel
@tf.function @tf.function
def compute_mmd(tensors): def compute_mmd(tensors: tuple) -> tf.Tensor:
"""
Computes the MMD between the two specified vectors using a gaussian kernel.
Parameters:
- tensors (tuple): tuple containing two tf.Tensor objects
Returns
- mmd (tf.Tensor): returns the maximum mean discrepancy for each
training instance
"""
x = tensors[0] x = tensors[0]
y = tensors[1] y = tensors[1]
...@@ -82,11 +109,12 @@ def compute_mmd(tensors): ...@@ -82,11 +109,12 @@ def compute_mmd(tensors):
x_kernel = compute_kernel(x, x) x_kernel = compute_kernel(x, x)
y_kernel = compute_kernel(y, y) y_kernel = compute_kernel(y, y)
xy_kernel = compute_kernel(x, y) xy_kernel = compute_kernel(x, y)
return ( mmd = (
tf.reduce_mean(x_kernel) tf.reduce_mean(x_kernel)
+ tf.reduce_mean(y_kernel) + tf.reduce_mean(y_kernel)
- 2 * tf.reduce_mean(xy_kernel) - 2 * tf.reduce_mean(xy_kernel)
) )
return mmd
# Custom auxiliary classes # Custom auxiliary classes
......
...@@ -11,7 +11,10 @@ Testing module for deepof.model_utils ...@@ -11,7 +11,10 @@ Testing module for deepof.model_utils
from hypothesis import given from hypothesis import given
from hypothesis import settings from hypothesis import settings
from hypothesis import strategies as st from hypothesis import strategies as st
from hypothesis.extra.numpy import arrays
import deepof.model_utils import deepof.model_utils
import tensorflow as tf
from tensorflow.python.framework.ops import EagerTensor
@settings(deadline=None) @settings(deadline=None)
...@@ -22,19 +25,34 @@ import deepof.model_utils ...@@ -22,19 +25,34 @@ import deepof.model_utils
) )
def test_far_away_uniform_initialiser(shape): def test_far_away_uniform_initialiser(shape):
far = deepof.model_utils.far_away_uniform_initialiser(shape, 0, 15, 100) far = deepof.model_utils.far_away_uniform_initialiser(shape, 0, 15, 100)
random = tf.random.uniform(shape, 0, 15)
assert far.shape == shape assert far.shape == shape
assert tf.abs(tf.norm(tf.math.subtract(far[1:], far[:1]))) > tf.abs(
tf.norm(tf.math.subtract(random[1:], random[:1]))
)
# @settings(deadline=None) @settings(deadline=None)
# @given() @given(
# def test_compute_kernel(): tensor=arrays(
# pass shape=(10, 10),
# dtype=float,
unique=True,
elements=st.floats(min_value=-300, max_value=300),
),
)
def test_compute_mmd(tensor):
tensor1 = tf.cast(tf.convert_to_tensor(tensor), dtype=tf.float32)
tensor2 = tf.random.uniform(tensor1.shape, -300, 300, dtype=tf.float32)
mmd_kernel = deepof.model_utils.compute_mmd(tuple([tensor1, tensor2]))
null_kernel = deepof.model_utils.compute_mmd(tuple([tensor1, tensor1]))
assert type(mmd_kernel) == EagerTensor
assert null_kernel == 0
# @settings(deadline=None)
# @given()
# def test_compute_mmd():
# pass
# #
# #
# @settings(deadline=None) # @settings(deadline=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