Skip to content

Commit

Permalink
Remove arbitrary float restrictions for constant_ and random_uniform_…
Browse files Browse the repository at this point in the history
…initializer

constant_initializer clearly makes sense for any type.
random_uniform_initializer also should work for integers now that
random_uniform works for integers.
Change: 129489215
  • Loading branch information
girving authored and tensorflower-gardener committed Aug 5, 2016
1 parent 408354c commit a8f6a4f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
27 changes: 16 additions & 11 deletions tensorflow/python/kernel_tests/init_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ def testConstantOneInitializer(self):
x.initializer.run()
self.assertAllEqual(x.eval(), np.ones(shape))

def testConstantIntInitializer(self):
with self.test_session():
shape = [2, 3]
x = tf.get_variable(
"x", shape=shape, dtype=tf.int32,
initializer=tf.constant_initializer(7))
x.initializer.run()
self.assertEqual(x.dtype.base_dtype, tf.int32)
self.assertAllEqual(x.eval(), 7 * np.ones(shape, dtype=np.int32))


class RandomNormalInitializationTest(tf.test.TestCase):

Expand Down Expand Up @@ -178,28 +188,23 @@ class RandomUniformInitializationTest(tf.test.TestCase):

def testInitializerIdentical(self):
for use_gpu in [False, True]:
for dtype in [tf.float32, tf.float64]:
init1 = tf.random_uniform_initializer(0.0, 1.0, seed=1, dtype=dtype)
init2 = tf.random_uniform_initializer(0.0, 1.0, seed=1, dtype=dtype)
for dtype in [tf.float32, tf.float64, tf.int64]:
init1 = tf.random_uniform_initializer(0, 7, seed=1, dtype=dtype)
init2 = tf.random_uniform_initializer(0, 7, seed=1, dtype=dtype)
self.assertTrue(identicaltest(self, init1, init2, use_gpu))

def testInitializerDifferent(self):
for use_gpu in [False, True]:
for dtype in [tf.float32, tf.float64]:
init1 = tf.random_uniform_initializer(0.0, 1.0, seed=1, dtype=dtype)
init2 = tf.random_uniform_initializer(0.0, 1.0, seed=2, dtype=dtype)
for dtype in [tf.float32, tf.float64, tf.int32, tf.int64]:
init1 = tf.random_uniform_initializer(0, 7, seed=1, dtype=dtype)
init2 = tf.random_uniform_initializer(0, 7, seed=2, dtype=dtype)
self.assertFalse(identicaltest(self, init1, init2, use_gpu))

def testDuplicatedInitializer(self):
for use_gpu in [False, True]:
init = tf.random_uniform_initializer(0.0, 1.0)
self.assertFalse(duplicated_initializer(self, init, use_gpu, 1))

def testInvalidDataType(self):
self.assertRaises(
ValueError,
tf.random_uniform_initializer, 0.0, 1.0, dtype=tf.string)


class UniformUnitScalingInitializationTest(tf.test.TestCase):

Expand Down
24 changes: 9 additions & 15 deletions tensorflow/python/ops/init_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,46 +55,40 @@ def ones_initializer(shape, dtype=dtypes.float32):
return array_ops.ones(shape, dtype)


def constant_initializer(value=0.0, dtype=dtypes.float32):
def constant_initializer(value=0, dtype=dtypes.float32):
"""Returns an initializer that generates tensors with a single value.
Args:
value: A Python scalar. All elements of the initialized variable
will be set to this value.
dtype: The data type. Only floating point types are supported.
dtype: The data type.
Returns:
An initializer that generates tensors with a single value.
Raises:
ValueError: if `dtype` is not a floating point type.
"""
def _initializer(shape, dtype=_assert_float_dtype(dtype)):
def _initializer(shape, dtype=dtype):
return constant_op.constant(value, dtype=dtype, shape=shape)
return _initializer


def random_uniform_initializer(minval=0.0, maxval=1.0, seed=None,
def random_uniform_initializer(minval=0, maxval=None, seed=None,
dtype=dtypes.float32):
"""Returns an initializer that generates tensors with a uniform distribution.
Args:
minval: a python scalar or a scalar tensor. lower bound of the range
of random values to generate.
maxval: a python scalar or a scalar tensor. upper bound of the range
minval: A python scalar or a scalar tensor. Lower bound of the range
of random values to generate.
maxval: A python scalar or a scalar tensor. Upper bound of the range
of random values to generate. Defaults to 1 for float types.
seed: A Python integer. Used to create random seeds. See
[`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)
for behavior.
dtype: The data type. Only floating point types are supported.
dtype: The data type.
Returns:
An initializer that generates tensors with a uniform distribution.
Raises:
ValueError: if `dtype` is not a floating point type.
"""
def _initializer(shape, dtype=_assert_float_dtype(dtype)):
def _initializer(shape, dtype=dtype):
return random_ops.random_uniform(shape, minval, maxval, dtype, seed=seed)
return _initializer

Expand Down

0 comments on commit a8f6a4f

Please sign in to comment.