First step of migrating layers to new API.

Change: 121435753
This commit is contained in:
A. Unique TensorFlower 2016-05-03 16:38:28 -08:00 committed by TensorFlower Gardener
parent ec0b1393c7
commit 0393436023

View File

@ -22,6 +22,7 @@ import numpy as np
import tensorflow as tf import tensorflow as tf
# TODO(b/28426988): Add separate tests for non-legacy versions.
class FullyConnectedTest(tf.test.TestCase): class FullyConnectedTest(tf.test.TestCase):
def setUp(self): def setUp(self):
@ -41,7 +42,8 @@ class FullyConnectedTest(tf.test.TestCase):
assert not tf.get_collection(tf.GraphKeys.SUMMARIES) assert not tf.get_collection(tf.GraphKeys.SUMMARIES)
def _fully_connected_basic_use(self, x, num_output_units, expected_shape): def _fully_connected_basic_use(self, x, num_output_units, expected_shape):
output = tf.contrib.layers.fully_connected(x, num_output_units, output = tf.contrib.layers.legacy_fully_connected(x,
num_output_units,
activation_fn=tf.nn.relu) activation_fn=tf.nn.relu)
with tf.Session() as sess: with tf.Session() as sess:
@ -71,7 +73,7 @@ class FullyConnectedTest(tf.test.TestCase):
self.input_3_dim, last_dim, [2, 4, last_dim]) self.input_3_dim, last_dim, [2, 4, last_dim])
def test_relu_layer_basic_use(self): def test_relu_layer_basic_use(self):
output = tf.contrib.layers.relu(self.input, 8) output = tf.contrib.layers.legacy_relu(self.input, 8)
with tf.Session() as sess: with tf.Session() as sess:
with self.assertRaises(tf.errors.FailedPreconditionError): with self.assertRaises(tf.errors.FailedPreconditionError):
@ -90,7 +92,7 @@ class FullyConnectedTest(tf.test.TestCase):
len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))) len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)))
def test_relu6_layer_basic_use(self): def test_relu6_layer_basic_use(self):
output = tf.contrib.layers.relu6(self.input, 8) output = tf.contrib.layers.legacy_relu6(self.input, 8)
with tf.Session() as sess: with tf.Session() as sess:
with self.assertRaises(tf.errors.FailedPreconditionError): with self.assertRaises(tf.errors.FailedPreconditionError):
@ -112,11 +114,11 @@ class FullyConnectedTest(tf.test.TestCase):
def test_variable_reuse_with_scope(self): def test_variable_reuse_with_scope(self):
with tf.variable_scope('test') as vs: with tf.variable_scope('test') as vs:
output1 = tf.contrib.layers.relu(self.input, 8) output1 = tf.contrib.layers.legacy_relu(self.input, 8)
output2 = tf.contrib.layers.relu(self.input, 8) output2 = tf.contrib.layers.legacy_relu(self.input, 8)
with tf.variable_scope(vs, reuse=True): with tf.variable_scope(vs, reuse=True):
output3 = tf.contrib.layers.relu(self.input, 8) output3 = tf.contrib.layers.legacy_relu(self.input, 8)
with tf.Session() as sess: with tf.Session() as sess:
tf.initialize_all_variables().run() tf.initialize_all_variables().run()
@ -127,7 +129,7 @@ class FullyConnectedTest(tf.test.TestCase):
def test_variable_reuse_with_template(self): def test_variable_reuse_with_template(self):
tmpl1 = tf.make_template('test', tmpl1 = tf.make_template('test',
tf.contrib.layers.fully_connected, tf.contrib.layers.legacy_fully_connected,
num_output_units=8) num_output_units=8)
output1 = tmpl1(self.input) output1 = tmpl1(self.input)
output2 = tmpl1(self.input) output2 = tmpl1(self.input)
@ -138,7 +140,9 @@ class FullyConnectedTest(tf.test.TestCase):
self.assertAllClose(out_value1, out_value2) self.assertAllClose(out_value1, out_value2)
def _custom_initializers(self, x, num_output_units, expected_outputs): def _custom_initializers(self, x, num_output_units, expected_outputs):
output = tf.contrib.layers.relu(x, num_output_units, output = tf.contrib.layers.legacy_relu(
x,
num_output_units,
weight_init=tf.constant_initializer(2.0), weight_init=tf.constant_initializer(2.0),
bias_init=tf.constant_initializer(1.0)) bias_init=tf.constant_initializer(1.0))
@ -165,7 +169,8 @@ class FullyConnectedTest(tf.test.TestCase):
[49.6, 49.6]]]) [49.6, 49.6]]])
def test_custom_collections(self): def test_custom_collections(self):
tf.contrib.layers.relu(self.input, 2, tf.contrib.layers.legacy_relu(self.input,
2,
weight_collections=['unbiased'], weight_collections=['unbiased'],
bias_collections=['biased'], bias_collections=['biased'],
output_collections=['output']) output_collections=['output'])
@ -176,7 +181,8 @@ class FullyConnectedTest(tf.test.TestCase):
self.assertEquals(2, len(tf.get_collection(tf.GraphKeys.VARIABLES))) self.assertEquals(2, len(tf.get_collection(tf.GraphKeys.VARIABLES)))
def test_all_custom_collections(self): def test_all_custom_collections(self):
tf.contrib.layers.relu(self.input, 2, tf.contrib.layers.legacy_relu(self.input,
2,
weight_collections=['unbiased', 'all'], weight_collections=['unbiased', 'all'],
bias_collections=['biased', 'all']) bias_collections=['biased', 'all'])
@ -186,16 +192,16 @@ class FullyConnectedTest(tf.test.TestCase):
tf.get_collection('all')) tf.get_collection('all'))
def test_no_bias(self): def test_no_bias(self):
tf.contrib.layers.relu(self.input, 2, bias_init=None) tf.contrib.layers.legacy_relu(self.input, 2, bias_init=None)
self.assertEqual(1, len(tf.get_collection(tf.GraphKeys.VARIABLES))) self.assertEqual(1, len(tf.get_collection(tf.GraphKeys.VARIABLES)))
def test_no_activation(self): def test_no_activation(self):
y = tf.contrib.layers.fully_connected(self.input, 2) y = tf.contrib.layers.legacy_fully_connected(self.input, 2)
self.assertEquals(2, len(tf.get_collection(tf.GraphKeys.VARIABLES))) self.assertEquals(2, len(tf.get_collection(tf.GraphKeys.VARIABLES)))
self.assertEquals('BiasAdd', y.op.type) self.assertEquals('BiasAdd', y.op.type)
def test_no_activation_no_bias(self): def test_no_activation_no_bias(self):
y = tf.contrib.layers.fully_connected(self.input, 2, bias_init=None) y = tf.contrib.layers.legacy_fully_connected(self.input, 2, bias_init=None)
self.assertEquals(1, len(tf.get_collection(tf.GraphKeys.VARIABLES))) self.assertEquals(1, len(tf.get_collection(tf.GraphKeys.VARIABLES)))
self.assertEquals('MatMul', y.op.type) self.assertEquals('MatMul', y.op.type)
@ -206,7 +212,9 @@ class FullyConnectedTest(tf.test.TestCase):
cnt[0] += 1 cnt[0] += 1
return tensor return tensor
tf.contrib.layers.fully_connected(self.input, 2, weight_regularizer=test_fn) tf.contrib.layers.legacy_fully_connected(self.input,
2,
weight_regularizer=test_fn)
self.assertEqual([tensor], self.assertEqual([tensor],
tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
@ -219,9 +227,11 @@ class FullyConnectedTest(tf.test.TestCase):
cnt[0] += 1 cnt[0] += 1
return tensor return tensor
tf.contrib.layers.fully_connected(self.input, 2, tf.contrib.layers.legacy_fully_connected(self.input,
2,
weight_regularizer=test_fn) weight_regularizer=test_fn)
tf.contrib.layers.fully_connected(self.input, 2, tf.contrib.layers.legacy_fully_connected(self.input,
2,
weight_regularizer=test_fn) weight_regularizer=test_fn)
self.assertEqual([tensor, tensor], self.assertEqual([tensor, tensor],
@ -236,11 +246,13 @@ class FullyConnectedTest(tf.test.TestCase):
return tensor return tensor
with tf.variable_scope('test') as vs: with tf.variable_scope('test') as vs:
tf.contrib.layers.fully_connected(self.input, 2, tf.contrib.layers.legacy_fully_connected(self.input,
2,
weight_regularizer=test_fn) weight_regularizer=test_fn)
with tf.variable_scope(vs, reuse=True): with tf.variable_scope(vs, reuse=True):
tf.contrib.layers.fully_connected(self.input, 2, tf.contrib.layers.legacy_fully_connected(self.input,
2,
weight_regularizer=test_fn) weight_regularizer=test_fn)
self.assertEqual([tensor], self.assertEqual([tensor],
@ -254,7 +266,9 @@ class FullyConnectedTest(tf.test.TestCase):
with self.test_session(): with self.test_session():
x = tf.constant([[]], shape=[0, 3]) x = tf.constant([[]], shape=[0, 3])
self.assertEqual(0, tf.size(x).eval()) self.assertEqual(0, tf.size(x).eval())
y = tf.contrib.layers.fully_connected(x, 2, activation_fn=tf.nn.softmax) y = tf.contrib.layers.legacy_fully_connected(x,
2,
activation_fn=tf.nn.softmax)
tf.initialize_all_variables().run() tf.initialize_all_variables().run()
expected_y = np.array([]).reshape(0, 2) expected_y = np.array([]).reshape(0, 2)
np.testing.assert_array_equal(expected_y, y.eval()) np.testing.assert_array_equal(expected_y, y.eval())
@ -262,7 +276,7 @@ class FullyConnectedTest(tf.test.TestCase):
def test_shapes_variable_first_dim(self): def test_shapes_variable_first_dim(self):
# first dimension is not known statically. # first dimension is not known statically.
x = tf.placeholder(tf.float32, shape=[None, 4, 3]) x = tf.placeholder(tf.float32, shape=[None, 4, 3])
y = tf.contrib.layers.fully_connected(x, 1) y = tf.contrib.layers.legacy_fully_connected(x, 1)
# in the output we still only know the 2nd and 3rd dimensions statically. # in the output we still only know the 2nd and 3rd dimensions statically.
self.assertEquals(y.get_shape().as_list(), [None, 4, 1]) self.assertEquals(y.get_shape().as_list(), [None, 4, 1])
with self.test_session() as sess: with self.test_session() as sess:
@ -280,7 +294,7 @@ class FullyConnectedTest(tf.test.TestCase):
def _unknown_dim_invalid_input(self, last_dim): def _unknown_dim_invalid_input(self, last_dim):
x = tf.placeholder(tf.float32, shape=[3, last_dim]) x = tf.placeholder(tf.float32, shape=[3, last_dim])
tf.contrib.layers.fully_connected(x, 2, activation_fn=None) tf.contrib.layers.legacy_fully_connected(x, 2, activation_fn=None)
def test_known_dim_valid_input(self): def test_known_dim_valid_input(self):
self._unknown_dim_invalid_input(last_dim=3) self._unknown_dim_invalid_input(last_dim=3)
@ -295,7 +309,9 @@ class FullyConnectedTest(tf.test.TestCase):
with self.assertRaisesRegexp(ValueError, with self.assertRaisesRegexp(ValueError,
'rank of x must be at least 2 not: 1'): 'rank of x must be at least 2 not: 1'):
x = tf.constant([[]], shape=[0]) x = tf.constant([[]], shape=[0])
tf.contrib.layers.fully_connected(x, 2, activation_fn=tf.nn.softmax) tf.contrib.layers.legacy_fully_connected(x,
2,
activation_fn=tf.nn.softmax)
class Convolution2dTest(tf.test.TestCase): class Convolution2dTest(tf.test.TestCase):
@ -308,7 +324,8 @@ class Convolution2dTest(tf.test.TestCase):
assert not tf.get_collection(tf.GraphKeys.SUMMARIES) assert not tf.get_collection(tf.GraphKeys.SUMMARIES)
def test_basic_use(self): def test_basic_use(self):
output = tf.contrib.layers.convolution2d(self.input, 8, (3, 3), output = tf.contrib.layers.legacy_convolution2d(self.input,
8, (3, 3),
activation_fn=tf.nn.relu) activation_fn=tf.nn.relu)
with tf.Session() as sess: with tf.Session() as sess:
@ -328,15 +345,15 @@ class Convolution2dTest(tf.test.TestCase):
def test_variable_reuse_with_scope(self): def test_variable_reuse_with_scope(self):
with tf.variable_scope('test') as vs: with tf.variable_scope('test') as vs:
output1 = tf.contrib.layers.convolution2d(self.input, output1 = tf.contrib.layers.legacy_convolution2d(self.input,
8, (3, 3), 8, (3, 3),
activation_fn=tf.nn.relu) activation_fn=tf.nn.relu)
output2 = tf.contrib.layers.convolution2d(self.input, output2 = tf.contrib.layers.legacy_convolution2d(self.input,
8, (3, 3), 8, (3, 3),
activation_fn=tf.nn.relu) activation_fn=tf.nn.relu)
with tf.variable_scope(vs, reuse=True): with tf.variable_scope(vs, reuse=True):
output3 = tf.contrib.layers.convolution2d(self.input, output3 = tf.contrib.layers.legacy_convolution2d(self.input,
8, (3, 3), 8, (3, 3),
activation_fn=tf.nn.relu) activation_fn=tf.nn.relu)
@ -349,7 +366,7 @@ class Convolution2dTest(tf.test.TestCase):
def test_variable_reuse_with_template(self): def test_variable_reuse_with_template(self):
tmpl1 = tf.make_template('test', tmpl1 = tf.make_template('test',
tf.contrib.layers.convolution2d, tf.contrib.layers.legacy_convolution2d,
kernel_size=(3, 3), kernel_size=(3, 3),
num_output_channels=8) num_output_channels=8)
output1 = tmpl1(self.input) output1 = tmpl1(self.input)
@ -361,10 +378,9 @@ class Convolution2dTest(tf.test.TestCase):
self.assertAllClose(out_value1, out_value2) self.assertAllClose(out_value1, out_value2)
def test_custom_initializers(self): def test_custom_initializers(self):
output = tf.contrib.layers.convolution2d( output = tf.contrib.layers.legacy_convolution2d(
self.input, self.input,
2, 2, (3, 3),
(3, 3),
activation_fn=tf.nn.relu, activation_fn=tf.nn.relu,
weight_init=tf.constant_initializer(2.0), weight_init=tf.constant_initializer(2.0),
bias_init=tf.constant_initializer(1.0), bias_init=tf.constant_initializer(1.0),
@ -378,7 +394,7 @@ class Convolution2dTest(tf.test.TestCase):
np.array([[[[1261., 1261.]]], [[[3853., 3853.]]]]), out_value) np.array([[[[1261., 1261.]]], [[[3853., 3853.]]]]), out_value)
def test_custom_collections(self): def test_custom_collections(self):
tf.contrib.layers.convolution2d(self.input, tf.contrib.layers.legacy_convolution2d(self.input,
2, (3, 3), 2, (3, 3),
activation_fn=tf.nn.relu, activation_fn=tf.nn.relu,
weight_collections=['unbiased'], weight_collections=['unbiased'],
@ -388,7 +404,8 @@ class Convolution2dTest(tf.test.TestCase):
self.assertEquals(1, len(tf.get_collection('biased'))) self.assertEquals(1, len(tf.get_collection('biased')))
def test_all_custom_collections(self): def test_all_custom_collections(self):
tf.contrib.layers.convolution2d(self.input, tf.contrib.layers.legacy_convolution2d(
self.input,
2, (3, 3), 2, (3, 3),
activation_fn=tf.nn.relu, activation_fn=tf.nn.relu,
weight_collections=['unbiased', 'all'], weight_collections=['unbiased', 'all'],
@ -407,7 +424,8 @@ class Convolution2dTest(tf.test.TestCase):
cnt[0] += 1 cnt[0] += 1
return tensor return tensor
tf.contrib.layers.convolution2d(self.input, 2, (3, 3), tf.contrib.layers.legacy_convolution2d(self.input,
2, (3, 3),
weight_regularizer=test_fn) weight_regularizer=test_fn)
self.assertEqual([tensor], self.assertEqual([tensor],
@ -415,7 +433,9 @@ class Convolution2dTest(tf.test.TestCase):
self.assertEqual(1, cnt[0]) self.assertEqual(1, cnt[0])
def test_no_bias(self): def test_no_bias(self):
tf.contrib.layers.convolution2d(self.input, 2, (3, 3), bias_init=None) tf.contrib.layers.legacy_convolution2d(self.input,
2, (3, 3),
bias_init=None)
self.assertEqual(1, self.assertEqual(1,
len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES))) len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)))