Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Lucas Miranda
deepOF
Commits
343cf82d
Commit
343cf82d
authored
Sep 17, 2020
by
lucas_miranda
Browse files
Added tests for model_utils.py
parent
9a9b2aeb
Changes
2
Hide whitespace changes
Inline
Side-by-side
deepof/model_utils.py
View file @
343cf82d
...
...
@@ -12,13 +12,13 @@ from itertools import combinations
from
tensorflow.keras
import
backend
as
K
from
tensorflow.keras.constraints
import
Constraint
from
tensorflow.keras.layers
import
Layer
import
networkx
as
nx
import
tensorflow
as
tf
import
tensorflow_probability
as
tfp
tfd
=
tfp
.
distributions
tfpl
=
tfp
.
layers
# Helper functions
@
tf
.
function
def
far_away_uniform_initialiser
(
...
...
@@ -58,7 +58,21 @@ def far_away_uniform_initialiser(
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
]
y_size
=
tf
.
shape
(
y
)[
0
]
dim
=
tf
.
shape
(
x
)[
1
]
...
...
@@ -68,13 +82,26 @@ def compute_kernel(x, y):
tiled_y
=
tf
.
tile
(
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
)
)
return
kernel
@
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
]
y
=
tensors
[
1
]
...
...
@@ -82,11 +109,12 @@ def compute_mmd(tensors):
x_kernel
=
compute_kernel
(
x
,
x
)
y_kernel
=
compute_kernel
(
y
,
y
)
xy_kernel
=
compute_kernel
(
x
,
y
)
return
(
mmd
=
(
tf
.
reduce_mean
(
x_kernel
)
+
tf
.
reduce_mean
(
y_kernel
)
-
2
*
tf
.
reduce_mean
(
xy_kernel
)
)
return
mmd
# Custom auxiliary classes
...
...
tests/test_model_utils.py
View file @
343cf82d
...
...
@@ -11,7 +11,10 @@ Testing module for deepof.model_utils
from
hypothesis
import
given
from
hypothesis
import
settings
from
hypothesis
import
strategies
as
st
from
hypothesis.extra.numpy
import
arrays
import
deepof.model_utils
import
tensorflow
as
tf
from
tensorflow.python.framework.ops
import
EagerTensor
@
settings
(
deadline
=
None
)
...
...
@@ -22,19 +25,34 @@ import deepof.model_utils
)
def
test_far_away_uniform_initialiser
(
shape
):
far
=
deepof
.
model_utils
.
far_away_uniform_initialiser
(
shape
,
0
,
15
,
100
)
random
=
tf
.
random
.
uniform
(
shape
,
0
,
15
)
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)
# @given()
# def test_compute_kernel():
# pass
#
@
settings
(
deadline
=
None
)
@
given
(
tensor
=
arrays
(
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)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment