This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsinkhorn_ops_test.py
160 lines (133 loc) · 5.96 KB
/
sinkhorn_ops_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for sinkhorn_ops library."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
#import google3
import numpy as np
import tensorflow as tf
from tensorflow.python.platform import test
import sinkhorn_ops
class SinkhornTest(test.TestCase):
def setUp(self):
self.rng = np.random.RandomState(0)
tf.set_random_seed(1)
def test_approximately_stochastic(self):
with self.test_session(use_gpu=True):
for dims in [2, 5, 10]:
for batch_size in [1, 2, 10]:
log_alpha = self.rng.randn(batch_size, dims, dims)
result = sinkhorn_ops.sinkhorn(log_alpha)
self.assertAllClose(np.sum(result.eval(), 1),
np.tile([1.0], (batch_size, dims)),
atol=1e-3)
self.assertAllClose(np.sum(result.eval(), 2),
np.tile([1.0], (batch_size, dims)),
atol=1e-3)
def test_equivalence_gumbel_sinkhorn_and_sinkhorn(self):
"""Tests the equivalence between sinkhorn and gumbel_sinhorn in a case.
When noise_factor = 0.0 the output of gumbel_sinkhorn should be the same
as the output of sinkhorn, 'modulo' possible many repetitions of the same
matrix given by gumbel_sinkhorn.
"""
with self.test_session(use_gpu=True):
batch_size = 10
dims = 5
n_samples = 20
temp = 1.0
noise_factor = 0.0
log_alpha = self.rng.randn(batch_size, dims, dims)
result_sinkhorn = sinkhorn_ops.sinkhorn(log_alpha)
result_sinkhorn_reshaped = tf.reshape(
result_sinkhorn, [batch_size, 1, dims, dims])
result_sinkhorn_tiled = tf.tile(
result_sinkhorn_reshaped, [1, n_samples, 1, 1])
result_gumbel_sinkhorn, _ = sinkhorn_ops.gumbel_sinkhorn(
log_alpha, temp, n_samples, noise_factor)
self.assertAllEqual(result_gumbel_sinkhorn.eval(),
result_sinkhorn_tiled.eval())
def test_gumbel_sinkhorn_high_temperature(self):
"""At very high temperatures, the resulting matrix approaches the uniform.
"""
n_samples = 1
temp = 100000.0
with self.test_session(use_gpu=True):
for dims in [2, 5, 10]:
for batch_size in [1, 2, 10]:
for noise_factor in [1.0, 5.0]:
log_alpha = tf.cast(self.rng.randn(batch_size, dims, dims),
dtype=tf.float32)
result_gumbel_sinkhorn, _ = sinkhorn_ops.gumbel_sinkhorn(
log_alpha, temp, n_samples, noise_factor, squeeze=True)
uniform = np.ones((batch_size, dims, dims)) / dims
self.assertAllClose(uniform, result_gumbel_sinkhorn.eval(),
atol=1e-3)
def test_matching(self):
"""The solution of the matching for the identity matrix is range(N).
"""
with self.test_session(use_gpu=True):
dims = 10
identity = np.eye(dims)
result_matching = sinkhorn_ops.matching(identity)
self.assertAllEqual(result_matching.eval(),
np.reshape(range(dims), [1, dims]))
def test_perm_inverse(self):
"""The product of a permutation and its inverse is the identity."""
with self.test_session(use_gpu=True):
dims = 10
permutation = np.reshape(self.rng.permutation(dims), [1, -1])
permutation_matrix = sinkhorn_ops.listperm2matperm(permutation)
inverse = sinkhorn_ops.invert_listperm(permutation)
inverse_matrix = sinkhorn_ops.listperm2matperm(inverse)
prod = tf.matmul(permutation_matrix, inverse_matrix)
self.assertAllEqual(prod.eval(),
np.reshape(np.eye(dims), [1, dims, dims]))
def test_listperm2matperm(self):
"""The matrix form of the permutation range(N) is the identity."""
with self.test_session(use_gpu=True):
dims = 10
permutation_list = np.reshape(np.arange(dims), [1, -1])
permutation_matrix = sinkhorn_ops.listperm2matperm(permutation_list)
self.assertAllEqual(permutation_matrix.eval(),
np.reshape(np.eye(dims), [1, dims, dims]))
def test_matperm2listperm(self):
"""The list form of the matrix permutation identity is range(N)."""
with self.test_session(use_gpu=True):
dims = 10
permutation_matrix = np.eye(dims)
permutation_list = sinkhorn_ops.matperm2listperm(permutation_matrix)
self.assertAllEqual(permutation_list.eval(),
np.reshape(np.arange(dims), [1, dims]))
def test_sample_uniform_and_order(self):
"""Ordered numbers form indeed an increasing sequence."""
n_lists = 1
n_numbers = 10
prob_inc = 1.0
with self.test_session(use_gpu=True):
ordered, _, _ = sinkhorn_ops.sample_uniform_and_order(n_lists,
n_numbers,
prob_inc)
self.assertTrue(np.min(np.diff(ordered.eval())) > 0)
def test_sample_permutations(self):
"""What is being sampled are indeed permutations of range(N)."""
n_permutations = 10
n_objects = 5
with self.test_session(use_gpu=True):
permutations = sinkhorn_ops.sample_permutations(n_permutations, n_objects)
tiled_range = np.tile(np.reshape(
np.arange(n_objects), [1, n_objects]), [n_permutations, 1])
self.assertAllEqual(np.sort(permutations.eval()), tiled_range)
if __name__ == '__main__':
tf.test.main()