check_ops_test.py revision 5a2ec29b037a9ad6ef5b3a4fc1d98af08473f240
1# Copyright 2016 Google Inc. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Tests for tensorflow.ops.check_ops."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import tensorflow as tf
21
22
23class AssertEqualTest(tf.test.TestCase):
24
25  def test_doesnt_raise_when_equal(self):
26    with self.test_session():
27      small = tf.constant([1, 2], name="small")
28      with tf.control_dependencies([tf.assert_equal(small, small)]):
29        out = tf.identity(small)
30      out.eval()
31
32  def test_raises_when_greater(self):
33    with self.test_session():
34      small = tf.constant([1, 2], name="small")
35      big = tf.constant([3, 4], name="big")
36      with tf.control_dependencies([tf.assert_equal(big, small)]):
37        out = tf.identity(small)
38      with self.assertRaisesOpError("big.*small"):
39        out.eval()
40
41  def test_raises_when_less(self):
42    with self.test_session():
43      small = tf.constant([3, 1], name="small")
44      big = tf.constant([4, 2], name="big")
45      with tf.control_dependencies([tf.assert_equal(small, big)]):
46        out = tf.identity(small)
47      with self.assertRaisesOpError("small.*big"):
48        out.eval()
49
50  def test_doesnt_raise_when_equal_and_broadcastable_shapes(self):
51    with self.test_session():
52      small = tf.constant([1, 2], name="small")
53      small_2 = tf.constant([1, 2], name="small_2")
54      with tf.control_dependencies([tf.assert_equal(small, small_2)]):
55        out = tf.identity(small)
56      out.eval()
57
58  def test_raises_when_equal_but_non_broadcastable_shapes(self):
59    with self.test_session():
60      small = tf.constant([1, 1, 1], name="small")
61      small_2 = tf.constant([1, 1], name="small_2")
62      with self.assertRaisesRegexp(ValueError, "broadcast"):
63        with tf.control_dependencies([tf.assert_equal(small, small_2)]):
64          out = tf.identity(small)
65        out.eval()
66
67  def test_doesnt_raise_when_both_empty(self):
68    with self.test_session():
69      larry = tf.constant([])
70      curly = tf.constant([])
71      with tf.control_dependencies([tf.assert_equal(larry, curly)]):
72        out = tf.identity(larry)
73      out.eval()
74
75
76class AssertLessTest(tf.test.TestCase):
77
78  def test_raises_when_equal(self):
79    with self.test_session():
80      small = tf.constant([1, 2], name="small")
81      with tf.control_dependencies([tf.assert_less(small, small)]):
82        out = tf.identity(small)
83      with self.assertRaisesOpError("small.*small"):
84        out.eval()
85
86  def test_raises_when_greater(self):
87    with self.test_session():
88      small = tf.constant([1, 2], name="small")
89      big = tf.constant([3, 4], name="big")
90      with tf.control_dependencies([tf.assert_less(big, small)]):
91        out = tf.identity(small)
92      with self.assertRaisesOpError("big.*small"):
93        out.eval()
94
95  def test_doesnt_raise_when_less(self):
96    with self.test_session():
97      small = tf.constant([3, 1], name="small")
98      big = tf.constant([4, 2], name="big")
99      with tf.control_dependencies([tf.assert_less(small, big)]):
100        out = tf.identity(small)
101      out.eval()
102
103  def test_doesnt_raise_when_less_and_broadcastable_shapes(self):
104    with self.test_session():
105      small = tf.constant([1], name="small")
106      big = tf.constant([3, 2], name="big")
107      with tf.control_dependencies([tf.assert_less(small, big)]):
108        out = tf.identity(small)
109      out.eval()
110
111  def test_raises_when_less_but_non_broadcastable_shapes(self):
112    with self.test_session():
113      small = tf.constant([1, 1, 1], name="small")
114      big = tf.constant([3, 2], name="big")
115      with self.assertRaisesRegexp(ValueError, "broadcast"):
116        with tf.control_dependencies([tf.assert_less(small, big)]):
117          out = tf.identity(small)
118        out.eval()
119
120  def test_doesnt_raise_when_both_empty(self):
121    with self.test_session():
122      larry = tf.constant([])
123      curly = tf.constant([])
124      with tf.control_dependencies([tf.assert_less(larry, curly)]):
125        out = tf.identity(larry)
126      out.eval()
127
128
129class AssertLessEqualTest(tf.test.TestCase):
130
131  def test_doesnt_raise_when_equal(self):
132    with self.test_session():
133      small = tf.constant([1, 2], name="small")
134      with tf.control_dependencies([tf.assert_less_equal(small, small)]):
135        out = tf.identity(small)
136      out.eval()
137
138  def test_raises_when_greater(self):
139    with self.test_session():
140      small = tf.constant([1, 2], name="small")
141      big = tf.constant([3, 4], name="big")
142      with tf.control_dependencies([tf.assert_less_equal(big, small)]):
143        out = tf.identity(small)
144      with self.assertRaisesOpError("big.*small"):
145        out.eval()
146
147  def test_doesnt_raise_when_less_equal(self):
148    with self.test_session():
149      small = tf.constant([1, 2], name="small")
150      big = tf.constant([3, 2], name="big")
151      with tf.control_dependencies([tf.assert_less_equal(small, big)]):
152        out = tf.identity(small)
153      out.eval()
154
155  def test_doesnt_raise_when_less_equal_and_broadcastable_shapes(self):
156    with self.test_session():
157      small = tf.constant([1], name="small")
158      big = tf.constant([3, 1], name="big")
159      with tf.control_dependencies([tf.assert_less_equal(small, big)]):
160        out = tf.identity(small)
161      out.eval()
162
163  def test_raises_when_less_equal_but_non_broadcastable_shapes(self):
164    with self.test_session():
165      small = tf.constant([1, 1, 1], name="small")
166      big = tf.constant([3, 1], name="big")
167      with self.assertRaisesRegexp(ValueError, "broadcast"):
168        with tf.control_dependencies([tf.assert_less_equal(small, big)]):
169          out = tf.identity(small)
170        out.eval()
171
172  def test_doesnt_raise_when_both_empty(self):
173    with self.test_session():
174      larry = tf.constant([])
175      curly = tf.constant([])
176      with tf.control_dependencies([tf.assert_less_equal(larry, curly)]):
177        out = tf.identity(larry)
178      out.eval()
179
180
181class AssertNegativeTest(tf.test.TestCase):
182
183  def test_doesnt_raise_when_negative(self):
184    with self.test_session():
185      frank = tf.constant([-1, -2], name="frank")
186      with tf.control_dependencies([tf.assert_negative(frank)]):
187        out = tf.identity(frank)
188      out.eval()
189
190  def test_raises_when_positive(self):
191    with self.test_session():
192      doug = tf.constant([1, 2], name="doug")
193      with tf.control_dependencies([tf.assert_negative(doug)]):
194        out = tf.identity(doug)
195      with self.assertRaisesOpError("doug"):
196        out.eval()
197
198  def test_raises_when_zero(self):
199    with self.test_session():
200      claire = tf.constant([0], name="claire")
201      with tf.control_dependencies([tf.assert_negative(claire)]):
202        out = tf.identity(claire)
203      with self.assertRaisesOpError("claire"):
204        out.eval()
205
206  def test_empty_tensor_doesnt_raise(self):
207    # A tensor is negative when it satisfies:
208    #   For every element x_i in x, x_i < 0
209    # and an empty tensor has no elements, so this is trivially satisfied.
210    # This is standard set theory.
211    with self.test_session():
212      empty = tf.constant([], name="empty")
213      with tf.control_dependencies([tf.assert_negative(empty)]):
214        out = tf.identity(empty)
215      out.eval()
216
217
218class AssertPositiveTest(tf.test.TestCase):
219
220  def test_raises_when_negative(self):
221    with self.test_session():
222      freddie = tf.constant([-1, -2], name="freddie")
223      with tf.control_dependencies([tf.assert_positive(freddie)]):
224        out = tf.identity(freddie)
225      with self.assertRaisesOpError("freddie"):
226        out.eval()
227
228  def test_doesnt_raise_when_positive(self):
229    with self.test_session():
230      remmy = tf.constant([1, 2], name="remmy")
231      with tf.control_dependencies([tf.assert_positive(remmy)]):
232        out = tf.identity(remmy)
233      out.eval()
234
235  def test_raises_when_zero(self):
236    with self.test_session():
237      meechum = tf.constant([0], name="meechum")
238      with tf.control_dependencies([tf.assert_positive(meechum)]):
239        out = tf.identity(meechum)
240      with self.assertRaisesOpError("meechum"):
241        out.eval()
242
243  def test_empty_tensor_doesnt_raise(self):
244    # A tensor is positive when it satisfies:
245    #   For every element x_i in x, x_i > 0
246    # and an empty tensor has no elements, so this is trivially satisfied.
247    # This is standard set theory.
248    with self.test_session():
249      empty = tf.constant([], name="empty")
250      with tf.control_dependencies([tf.assert_positive(empty)]):
251        out = tf.identity(empty)
252      out.eval()
253
254
255class AssertRankTest(tf.test.TestCase):
256
257  def test_rank_zero_tensor_raises_if_rank_too_small_static_rank(self):
258    with self.test_session():
259      tensor = tf.constant(1, name="my_tensor")
260      desired_rank = 1
261      with self.assertRaisesRegexp(ValueError, "my_tensor.*rank"):
262        with tf.control_dependencies([tf.assert_rank(tensor, desired_rank)]):
263          tf.identity(tensor).eval()
264
265  def test_rank_zero_tensor_raises_if_rank_too_small_dynamic_rank(self):
266    with self.test_session():
267      tensor = tf.placeholder(tf.float32, name="my_tensor")
268      desired_rank = 1
269      with tf.control_dependencies([tf.assert_rank(tensor, desired_rank)]):
270        with self.assertRaisesOpError("my_tensor.*rank"):
271          tf.identity(tensor).eval(feed_dict={tensor: 0})
272
273  def test_rank_zero_tensor_doesnt_raise_if_rank_just_right_static_rank(self):
274    with self.test_session():
275      tensor = tf.constant(1, name="my_tensor")
276      desired_rank = 0
277      with tf.control_dependencies([tf.assert_rank(tensor, desired_rank)]):
278        tf.identity(tensor).eval()
279
280  def test_rank_zero_tensor_doesnt_raise_if_rank_just_right_dynamic_rank(self):
281    with self.test_session():
282      tensor = tf.placeholder(tf.float32, name="my_tensor")
283      desired_rank = 0
284      with tf.control_dependencies([tf.assert_rank(tensor, desired_rank)]):
285        tf.identity(tensor).eval(feed_dict={tensor: 0})
286
287  def test_rank_one_tensor_raises_if_rank_too_large_static_rank(self):
288    with self.test_session():
289      tensor = tf.constant([1, 2], name="my_tensor")
290      desired_rank = 0
291      with self.assertRaisesRegexp(ValueError, "my_tensor.*rank"):
292        with tf.control_dependencies([tf.assert_rank(tensor, desired_rank)]):
293          tf.identity(tensor).eval()
294
295  def test_rank_one_tensor_raises_if_rank_too_large_dynamic_rank(self):
296    with self.test_session():
297      tensor = tf.placeholder(tf.float32, name="my_tensor")
298      desired_rank = 0
299      with tf.control_dependencies([tf.assert_rank(tensor, desired_rank)]):
300        with self.assertRaisesOpError("my_tensor.*rank"):
301          tf.identity(tensor).eval(feed_dict={tensor: [1, 2]})
302
303  def test_rank_one_tensor_doesnt_raise_if_rank_just_right_static_rank(self):
304    with self.test_session():
305      tensor = tf.constant([1, 2], name="my_tensor")
306      desired_rank = 1
307      with tf.control_dependencies([tf.assert_rank(tensor, desired_rank)]):
308        tf.identity(tensor).eval()
309
310  def test_rank_one_tensor_doesnt_raise_if_rank_just_right_dynamic_rank(self):
311    with self.test_session():
312      tensor = tf.placeholder(tf.float32, name="my_tensor")
313      desired_rank = 1
314      with tf.control_dependencies([tf.assert_rank(tensor, desired_rank)]):
315        tf.identity(tensor).eval(feed_dict={tensor: [1, 2]})
316
317  def test_rank_one_tensor_raises_if_rank_too_small_static_rank(self):
318    with self.test_session():
319      tensor = tf.constant([1, 2], name="my_tensor")
320      desired_rank = 2
321      with self.assertRaisesRegexp(ValueError, "my_tensor.*rank"):
322        with tf.control_dependencies([tf.assert_rank(tensor, desired_rank)]):
323          tf.identity(tensor).eval()
324
325  def test_rank_one_tensor_raises_if_rank_too_small_dynamic_rank(self):
326    with self.test_session():
327      tensor = tf.placeholder(tf.float32, name="my_tensor")
328      desired_rank = 2
329      with tf.control_dependencies([tf.assert_rank(tensor, desired_rank)]):
330        with self.assertRaisesOpError("my_tensor.*rank"):
331          tf.identity(tensor).eval(feed_dict={tensor: [1, 2]})
332
333
334class AssertRankAtLeastTest(tf.test.TestCase):
335
336  def test_rank_zero_tensor_raises_if_rank_too_small_static_rank(self):
337    with self.test_session():
338      tensor = tf.constant(1, name="my_tensor")
339      desired_rank = 1
340      with self.assertRaisesRegexp(ValueError, "my_tensor.*rank"):
341        with tf.control_dependencies([tf.assert_rank_at_least(tensor,
342                                                              desired_rank)]):
343          tf.identity(tensor).eval()
344
345  def test_rank_zero_tensor_raises_if_rank_too_small_dynamic_rank(self):
346    with self.test_session():
347      tensor = tf.placeholder(tf.float32, name="my_tensor")
348      desired_rank = 1
349      with tf.control_dependencies([tf.assert_rank_at_least(tensor,
350                                                            desired_rank)]):
351        with self.assertRaisesOpError("my_tensor.*rank"):
352          tf.identity(tensor).eval(feed_dict={tensor: 0})
353
354  def test_rank_zero_tensor_doesnt_raise_if_rank_just_right_static_rank(self):
355    with self.test_session():
356      tensor = tf.constant(1, name="my_tensor")
357      desired_rank = 0
358      with tf.control_dependencies([tf.assert_rank_at_least(tensor,
359                                                            desired_rank)]):
360        tf.identity(tensor).eval()
361
362  def test_rank_zero_tensor_doesnt_raise_if_rank_just_right_dynamic_rank(self):
363    with self.test_session():
364      tensor = tf.placeholder(tf.float32, name="my_tensor")
365      desired_rank = 0
366      with tf.control_dependencies([tf.assert_rank_at_least(tensor,
367                                                            desired_rank)]):
368        tf.identity(tensor).eval(feed_dict={tensor: 0})
369
370  def test_rank_one_ten_doesnt_raise_raise_if_rank_too_large_static_rank(self):
371    with self.test_session():
372      tensor = tf.constant([1, 2], name="my_tensor")
373      desired_rank = 0
374      with tf.control_dependencies([tf.assert_rank_at_least(tensor,
375                                                            desired_rank)]):
376        tf.identity(tensor).eval()
377
378  def test_rank_one_ten_doesnt_raise_if_rank_too_large_dynamic_rank(self):
379    with self.test_session():
380      tensor = tf.placeholder(tf.float32, name="my_tensor")
381      desired_rank = 0
382      with tf.control_dependencies([tf.assert_rank_at_least(tensor,
383                                                            desired_rank)]):
384        tf.identity(tensor).eval(feed_dict={tensor: [1, 2]})
385
386  def test_rank_one_tensor_doesnt_raise_if_rank_just_right_static_rank(self):
387    with self.test_session():
388      tensor = tf.constant([1, 2], name="my_tensor")
389      desired_rank = 1
390      with tf.control_dependencies([tf.assert_rank_at_least(tensor,
391                                                            desired_rank)]):
392        tf.identity(tensor).eval()
393
394  def test_rank_one_tensor_doesnt_raise_if_rank_just_right_dynamic_rank(self):
395    with self.test_session():
396      tensor = tf.placeholder(tf.float32, name="my_tensor")
397      desired_rank = 1
398      with tf.control_dependencies([tf.assert_rank_at_least(tensor,
399                                                            desired_rank)]):
400        tf.identity(tensor).eval(feed_dict={tensor: [1, 2]})
401
402  def test_rank_one_tensor_raises_if_rank_too_small_static_rank(self):
403    with self.test_session():
404      tensor = tf.constant([1, 2], name="my_tensor")
405      desired_rank = 2
406      with self.assertRaisesRegexp(ValueError, "my_tensor.*rank"):
407        with tf.control_dependencies([tf.assert_rank_at_least(tensor,
408                                                              desired_rank)]):
409          tf.identity(tensor).eval()
410
411  def test_rank_one_tensor_raises_if_rank_too_small_dynamic_rank(self):
412    with self.test_session():
413      tensor = tf.placeholder(tf.float32, name="my_tensor")
414      desired_rank = 2
415      with tf.control_dependencies([tf.assert_rank_at_least(tensor,
416                                                            desired_rank)]):
417        with self.assertRaisesOpError("my_tensor.*rank"):
418          tf.identity(tensor).eval(feed_dict={tensor: [1, 2]})
419
420
421class AssertNonNegativeTest(tf.test.TestCase):
422
423  def test_raises_when_negative(self):
424    with self.test_session():
425      zoe = tf.constant([-1, -2], name="zoe")
426      with tf.control_dependencies([tf.assert_non_negative(zoe)]):
427        out = tf.identity(zoe)
428      with self.assertRaisesOpError("zoe"):
429        out.eval()
430
431  def test_doesnt_raise_when_zero_and_positive(self):
432    with self.test_session():
433      lucas = tf.constant([0, 2], name="lucas")
434      with tf.control_dependencies([tf.assert_non_negative(lucas)]):
435        out = tf.identity(lucas)
436      out.eval()
437
438  def test_empty_tensor_doesnt_raise(self):
439    # A tensor is non-negative when it satisfies:
440    #   For every element x_i in x, x_i >= 0
441    # and an empty tensor has no elements, so this is trivially satisfied.
442    # This is standard set theory.
443    with self.test_session():
444      empty = tf.constant([], name="empty")
445      with tf.control_dependencies([tf.assert_non_negative(empty)]):
446        out = tf.identity(empty)
447      out.eval()
448
449
450class AssertNonPositiveTest(tf.test.TestCase):
451
452  def test_doesnt_raise_when_zero_and_negative(self):
453    with self.test_session():
454      tom = tf.constant([0, -2], name="tom")
455      with tf.control_dependencies([tf.assert_non_positive(tom)]):
456        out = tf.identity(tom)
457      out.eval()
458
459  def test_raises_when_positive(self):
460    with self.test_session():
461      rachel = tf.constant([0, 2], name="rachel")
462      with tf.control_dependencies([tf.assert_non_positive(rachel)]):
463        out = tf.identity(rachel)
464      with self.assertRaisesOpError("rachel"):
465        out.eval()
466
467  def test_empty_tensor_doesnt_raise(self):
468    # A tensor is non-positive when it satisfies:
469    #   For every element x_i in x, x_i <= 0
470    # and an empty tensor has no elements, so this is trivially satisfied.
471    # This is standard set theory.
472    with self.test_session():
473      empty = tf.constant([], name="empty")
474      with tf.control_dependencies([tf.assert_non_positive(empty)]):
475        out = tf.identity(empty)
476      out.eval()
477
478
479class AssertIntegerTest(tf.test.TestCase):
480
481  def test_doesnt_raise_when_integer(self):
482    with self.test_session():
483      integers = tf.constant([1, 2], name="integers")
484      with tf.control_dependencies([tf.assert_integer(integers)]):
485        out = tf.identity(integers)
486      out.eval()
487
488  def test_raises_when_float(self):
489    with self.test_session():
490      floats = tf.constant([1.0, 2.0], name="floats")
491      with tf.control_dependencies([tf.assert_integer(floats)]):
492        out = tf.identity(floats)
493      with self.assertRaisesOpError("x is not of integer dtype.*"):
494        out.eval()
495
496
497class IsStrictlyIncreasingTest(tf.test.TestCase):
498
499  def test_constant_tensor_is_not_strictly_increasing(self):
500    with self.test_session():
501      self.assertFalse(tf.is_strictly_increasing([1, 1, 1]).eval())
502
503  def test_decreasing_tensor_is_not_strictly_increasing(self):
504    with self.test_session():
505      self.assertFalse(tf.is_strictly_increasing([1, 0, -1]).eval())
506
507  def test_2d_decreasing_tensor_is_not_strictly_increasing(self):
508    with self.test_session():
509      self.assertFalse(tf.is_strictly_increasing([[1, 3], [2, 4]]).eval())
510
511  def test_increasing_tensor_is_increasing(self):
512    with self.test_session():
513      self.assertTrue(tf.is_strictly_increasing([1, 2, 3]).eval())
514
515  def test_increasing_rank_two_tensor(self):
516    with self.test_session():
517      self.assertTrue(tf.is_strictly_increasing([[-1, 2], [3, 4]]).eval())
518
519  def test_tensor_with_one_element_is_strictly_increasing(self):
520    with self.test_session():
521      self.assertTrue(tf.is_strictly_increasing([1]).eval())
522
523  def test_empty_tensor_is_strictly_increasing(self):
524    with self.test_session():
525      self.assertTrue(tf.is_strictly_increasing([]).eval())
526
527
528class IsNonDecreasingTest(tf.test.TestCase):
529
530  def test_constant_tensor_is_non_decreasing(self):
531    with self.test_session():
532      self.assertTrue(tf.is_non_decreasing([1, 1, 1]).eval())
533
534  def test_decreasing_tensor_is_not_non_decreasing(self):
535    with self.test_session():
536      self.assertFalse(tf.is_non_decreasing([3, 2, 1]).eval())
537
538  def test_2d_decreasing_tensor_is_not_non_decreasing(self):
539    with self.test_session():
540      self.assertFalse(tf.is_non_decreasing([[1, 3], [2, 4]]).eval())
541
542  def test_increasing_rank_one_tensor_is_non_decreasing(self):
543    with self.test_session():
544      self.assertTrue(tf.is_non_decreasing([1, 2, 3]).eval())
545
546  def test_increasing_rank_two_tensor(self):
547    with self.test_session():
548      self.assertTrue(tf.is_non_decreasing([[-1, 2], [3, 3]]).eval())
549
550  def test_tensor_with_one_element_is_non_decreasing(self):
551    with self.test_session():
552      self.assertTrue(tf.is_non_decreasing([1]).eval())
553
554  def test_empty_tensor_is_non_decreasing(self):
555    with self.test_session():
556      self.assertTrue(tf.is_non_decreasing([]).eval())
557
558
559if __name__ == "__main__":
560  tf.test.main()
561