aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/kernel_tests/learn_test.py
blob: 4ae014934846e5bc87e51f0f328a29cb648bfb2a (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# Copyright 2015 Google Inc. All Rights Reserved.
#
# 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 tf.learn."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import re

import tensorflow.python.platform  # pylint: disable=unused-import,g-bad-import-order

import numpy as np
import six
import tensorflow as tf


def assert_summary_scope(regexp):
  """Assert that all generated summaries match regexp."""
  for summary in tf.get_collection(tf.GraphKeys.SUMMARIES):
    tag = tf.contrib.util.constant_value(summary.op.inputs[0])
    assert tag is not None, 'All summaries must have constant tags'

    tag = str(tag)
    # Tags in Python 3 erroneously acquire the 'b\'' prefix and '\'' suffix
    # This is a temporary measure meant to expediently overcome test failure.
    # TODO(danmane): Get rid of the extraneous prefix and suffix.
    if tag.startswith('b\'') and tag.endswith('\''):
      tag = tag[2:-1]

    assert isinstance(tag[0], six.string_types), tag[0]
    assert re.match(regexp, tag), "tag doesn't match %s: %s" % (regexp, tag)


class FullyConnectedTest(tf.test.TestCase):

  def setUp(self):
    tf.test.TestCase.setUp(self)
    tf.set_random_seed(1234)
    self.input = tf.constant([[1., 2., 3.], [-4., 15., -6.]])
    assert not tf.get_collection(tf.GraphKeys.SUMMARIES)

  def test_fully_connected_basic_use(self):
    output = tf.learn.fully_connected(self.input, 8, activation_fn=tf.nn.relu)

    with tf.Session() as sess:
      with self.assertRaises(tf.errors.FailedPreconditionError):
        sess.run(output)

      tf.initialize_all_variables().run()
      out_value = sess.run(output)

    self.assertEqual(output.get_shape().as_list(), [2, 8])
    self.assertTrue(np.all(out_value >= 0),
                    'Relu should have all values >= 0.')

    self.assertGreater(
        len(tf.get_collection(tf.GraphKeys.SUMMARIES)), 0,
        'Some summaries should have been added.')
    self.assertEqual(2,
                     len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)))
    self.assertEqual(0,
                     len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)))
    assert_summary_scope('fully_connected')

  def test_relu_layer_basic_use(self):
    output = tf.learn.relu_layer(self.input, 8)

    with tf.Session() as sess:
      with self.assertRaises(tf.errors.FailedPreconditionError):
        sess.run(output)

      tf.initialize_all_variables().run()
      out_value = sess.run(output)

    self.assertEqual(output.get_shape().as_list(), [2, 8])
    self.assertTrue(np.all(out_value >= 0),
                    'Relu should have all values >= 0.')

    self.assertGreater(
        len(tf.get_collection(tf.GraphKeys.SUMMARIES)), 0,
        'Some summaries should have been added.')
    self.assertEqual(2,
                     len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)))
    self.assertEqual(0,
                     len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)))
    assert_summary_scope('fully_connected')

  def test_relu6_layer_basic_use(self):
    output = tf.learn.relu6_layer(self.input, 8)

    with tf.Session() as sess:
      with self.assertRaises(tf.errors.FailedPreconditionError):
        sess.run(output)

      tf.initialize_all_variables().run()
      out_value = sess.run(output)

    self.assertEqual(output.get_shape().as_list(), [2, 8])
    self.assertTrue(np.all(out_value >= 0),
                    'Relu6 should have all values >= 0.')
    self.assertTrue(np.all(out_value <= 6),
                    'Relu6 should have all values <= 6.')

    self.assertGreater(
        len(tf.get_collection(tf.GraphKeys.SUMMARIES)), 0,
        'Some summaries should have been added.')
    self.assertEqual(2,
                     len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)))
    self.assertEqual(0,
                     len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)))
    assert_summary_scope('fully_connected')

  def test_variable_reuse_with_scope(self):
    with tf.variable_scope('test') as vs:
      output1 = tf.learn.fully_connected(self.input,
                                         8,
                                         activation_fn=tf.nn.relu)
      output2 = tf.learn.fully_connected(self.input,
                                         8,
                                         activation_fn=tf.nn.relu)

    with tf.variable_scope(vs, reuse=True):
      output3 = tf.learn.fully_connected(self.input,
                                         8,
                                         activation_fn=tf.nn.relu)

    with tf.Session() as sess:
      tf.initialize_all_variables().run()
      out_value1, out_value2, out_value3 = sess.run([output1, output2, output3])

    self.assertFalse(np.allclose(out_value1, out_value2))
    self.assertAllClose(out_value1, out_value3)

  def test_variable_reuse_with_template(self):
    tmpl1 = tf.make_template('test',
                             tf.learn.fully_connected,
                             num_output_nodes=8)
    output1 = tmpl1(self.input)
    output2 = tmpl1(self.input)

    with tf.Session() as sess:
      tf.initialize_all_variables().run()
      out_value1, out_value2 = sess.run([output1, output2])
    self.assertAllClose(out_value1, out_value2)
    assert_summary_scope(r'test(_\d)?/fully_connected')

  def test_custom_initializers(self):
    output = tf.learn.fully_connected(self.input,
                                      2,
                                      activation_fn=tf.nn.relu,
                                      weight_init=tf.constant_initializer(2.0),
                                      bias_init=tf.constant_initializer(1.0))

    with tf.Session() as sess:
      tf.initialize_all_variables().run()
      out_value = sess.run(output)

    self.assertAllClose(np.array([[13.0, 13.0], [11.0, 11.0]]), out_value)

  def test_custom_collections(self):
    tf.learn.fully_connected(self.input,
                             2,
                             activation_fn=tf.nn.relu,
                             weight_collections=['unbiased'],
                             bias_collections=['biased'])

    self.assertEqual(1, len(tf.get_collection('unbiased')))
    self.assertEqual(1, len(tf.get_collection('biased')))

  def test_all_custom_collections(self):
    tf.learn.fully_connected(self.input,
                             2,
                             activation_fn=tf.nn.relu,
                             weight_collections=['unbiased', 'all'],
                             bias_collections=['biased', 'all'])

    self.assertEqual(1, len(tf.get_collection('unbiased')))
    self.assertEqual(1, len(tf.get_collection('biased')))
    self.assertEqual(2, len(tf.get_collection('all')))
    self.assertEqual(
        tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES),
        tf.get_collection('all'))

  def test_no_summaries(self):
    tf.learn.fully_connected(self.input,
                             2,
                             activation_fn=tf.nn.relu,
                             create_summaries=False)
    self.assertEqual([], tf.get_collection(tf.GraphKeys.SUMMARIES))

  # Verify fix of a bug where no_summaries + activation_fn=None led to a
  # NoneType exception.
  def test_no_summaries_no_activation(self):
    tf.learn.fully_connected(self.input,
                             2,
                             activation_fn=None,
                             create_summaries=False)
    self.assertEqual([], tf.get_collection(tf.GraphKeys.SUMMARIES))

  def test_regularizer(self):
    cnt = [0]
    tensor = tf.constant(5.0)
    def test_fn(_):
      cnt[0] += 1
      return tensor

    tf.learn.fully_connected(self.input, 2, weight_regularizer=test_fn)

    self.assertEqual([tensor],
                     tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
    self.assertEqual(1, cnt[0])

  def test_shape_enforcement(self):
    place = tf.placeholder(tf.float32)
    with self.assertRaises(ValueError):
      tf.learn.fully_connected(place, 8)
    tf.learn.fully_connected(place, 8, num_input_nodes=5)  # No error

    place.set_shape([None, None])
    with self.assertRaises(ValueError):
      tf.learn.fully_connected(place, 8)
    tf.learn.fully_connected(place, 8, num_input_nodes=5)  # No error

    place.set_shape([None, 6])
    tf.learn.fully_connected(place, 8)  # No error
    with self.assertRaises(ValueError):
      tf.learn.fully_connected(place, 8, num_input_nodes=5)

    place = tf.placeholder(tf.float32)
    place.set_shape([2, 6, 5])
    with self.assertRaises(ValueError):
      tf.learn.fully_connected(place, 8)

  def test_no_bias(self):
    tf.learn.fully_connected(self.input, 2, bias_init=None)

    self.assertEqual(1,
                     len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)))


class Convolution2dTest(tf.test.TestCase):

  def setUp(self):
    tf.test.TestCase.setUp(self)
    tf.set_random_seed(1234)
    self.input = tf.constant(np.arange(2 * 3 * 3 * 4).reshape(
        [2, 3, 3, 4]).astype(np.float32))
    assert not tf.get_collection(tf.GraphKeys.SUMMARIES)

  def test_basic_use(self):
    output = tf.learn.convolution2d(self.input, 8, (3, 3),
                                    activation_fn=tf.nn.relu)

    with tf.Session() as sess:
      with self.assertRaises(tf.errors.FailedPreconditionError):
        sess.run(output)

      tf.initialize_all_variables().run()
      out_value = sess.run(output)

    self.assertEqual(output.get_shape().as_list(), [2, 3, 3, 8])
    self.assertTrue(np.all(out_value >= 0),
                    'Relu should have capped all values.')

    self.assertGreater(
        len(tf.get_collection(tf.GraphKeys.SUMMARIES)), 0,
        'Some summaries should have been added.')
    self.assertEqual(2,
                     len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)))
    self.assertEqual(0,
                     len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)))
    assert_summary_scope('convolution2d')

  def test_variable_reuse_with_scope(self):
    with tf.variable_scope('test') as vs:
      output1 = tf.learn.convolution2d(self.input,
                                       8, (3, 3),
                                       activation_fn=tf.nn.relu)
      output2 = tf.learn.convolution2d(self.input,
                                       8, (3, 3),
                                       activation_fn=tf.nn.relu)

    with tf.variable_scope(vs, reuse=True):
      output3 = tf.learn.convolution2d(self.input,
                                       8, (3, 3),
                                       activation_fn=tf.nn.relu)

    with tf.Session() as sess:
      tf.initialize_all_variables().run()
      out_value1, out_value2, out_value3 = sess.run([output1, output2, output3])

    self.assertFalse(np.allclose(out_value1, out_value2))
    self.assertAllClose(out_value1, out_value3)

  def test_variable_reuse_with_template(self):
    tmpl1 = tf.make_template('test',
                             tf.learn.convolution2d,
                             kernel_size=(3, 3),
                             num_output_channels=8)
    output1 = tmpl1(self.input)
    output2 = tmpl1(self.input)

    with tf.Session() as sess:
      tf.initialize_all_variables().run()
      out_value1, out_value2 = sess.run([output1, output2])
    self.assertAllClose(out_value1, out_value2)
    assert_summary_scope(r'test(_\d)?/convolution2d')

  def test_custom_initializers(self):
    output = tf.learn.convolution2d(self.input,
                                    2,
                                    (3, 3),
                                    activation_fn=tf.nn.relu,
                                    weight_init=tf.constant_initializer(2.0),
                                    bias_init=tf.constant_initializer(1.0),
                                    padding='VALID')

    with tf.Session() as sess:
      tf.initialize_all_variables().run()
      out_value = sess.run(output)

    self.assertAllClose(
        np.array([[[[1261., 1261.]]], [[[3853., 3853.]]]]), out_value)

  def test_custom_collections(self):
    tf.learn.convolution2d(self.input,
                           2, (3, 3),
                           activation_fn=tf.nn.relu,
                           weight_collections=['unbiased'],
                           bias_collections=['biased'])

    self.assertEqual(1, len(tf.get_collection('unbiased')))
    self.assertEqual(1, len(tf.get_collection('biased')))

  def test_all_custom_collections(self):
    tf.learn.convolution2d(self.input,
                           2, (3, 3),
                           activation_fn=tf.nn.relu,
                           weight_collections=['unbiased', 'all'],
                           bias_collections=['biased', 'all'])

    self.assertEqual(1, len(tf.get_collection('unbiased')))
    self.assertEqual(1, len(tf.get_collection('biased')))
    self.assertEqual(2, len(tf.get_collection('all')))
    self.assertEqual(
        tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES),
        tf.get_collection('all'))

  def test_no_summaries(self):
    tf.learn.convolution2d(self.input,
                           2, (3, 3),
                           activation_fn=tf.nn.relu,
                           create_summaries=False)
    self.assertEqual([], tf.get_collection(tf.GraphKeys.SUMMARIES))

  def test_regularizer(self):
    cnt = [0]
    tensor = tf.constant(5.0)
    def test_fn(_):
      cnt[0] += 1
      return tensor

    tf.learn.convolution2d(self.input, 2, (3, 3), weight_regularizer=test_fn)

    self.assertEqual([tensor],
                     tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
    self.assertEqual(1, cnt[0])

  def test_shape_enforcement(self):
    place = tf.placeholder(tf.float32)
    with self.assertRaises(ValueError):
      tf.learn.convolution2d(place, 8, (3, 3))
    tf.learn.convolution2d(place, 8, (3, 3), num_input_channels=5)  # No error

    place.set_shape([None, None, None, None])
    with self.assertRaises(ValueError):
      tf.learn.convolution2d(place, 8, (3, 3))
    tf.learn.convolution2d(place, 8, (3, 3), num_input_channels=5)  # No error

    place.set_shape([None, None, None, 6])
    tf.learn.convolution2d(place, 8, (3, 3))  # No error
    with self.assertRaises(ValueError):
      tf.learn.convolution2d(place, 8, (3, 3), num_input_channels=5)

    place = tf.placeholder(tf.float32)
    place.set_shape([2, 6, 5])
    with self.assertRaises(ValueError):
      tf.learn.convolution2d(place, 8, (3, 3))

  def test_no_bias(self):
    tf.learn.convolution2d(self.input, 2, (3, 3), bias_init=None)

    self.assertEqual(1,
                     len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)))


class RegularizerTest(tf.test.TestCase):

  def test_l1(self):
    with self.assertRaises(ValueError):
      tf.learn.l1_regularizer(2.)
    with self.assertRaises(ValueError):
      tf.learn.l1_regularizer(-1.)
    with self.assertRaises(ValueError):
      tf.learn.l1_regularizer(0)

    self.assertIsNone(tf.learn.l1_regularizer(0.)(None))

    values = np.array([1., -1., 4., 2.])
    weights = tf.constant(values)
    with tf.Session() as sess:
      result = sess.run(tf.learn.l1_regularizer(.5)(weights))

    self.assertAllClose(np.abs(values).sum() * .5, result)

  def test_l2(self):
    with self.assertRaises(ValueError):
      tf.learn.l2_regularizer(2.)
    with self.assertRaises(ValueError):
      tf.learn.l2_regularizer(-1.)
    with self.assertRaises(ValueError):
      tf.learn.l2_regularizer(0)

    self.assertIsNone(tf.learn.l2_regularizer(0.)(None))

    values = np.array([1., -1., 4., 2.])
    weights = tf.constant(values)
    with tf.Session() as sess:
      result = sess.run(tf.learn.l2_regularizer(.42)(weights))

    self.assertAllClose(np.power(values, 2).sum() / 2.0 * .42, result)

if __name__ == '__main__':
  tf.test.main()