Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add identity_initializer #589

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions tensorflow/python/ops/init_ops.py
Expand Up @@ -22,6 +22,7 @@
from tensorflow.python.framework import dtypes
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import constant_op
from tensorflow.python.ops import gen_array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import nn_ops
from tensorflow.python.ops import random_ops
Expand All @@ -42,6 +43,22 @@ def _initializer(shape, dtype=dtypes.float32):
return constant_op.constant(value, dtype=dtype, shape=shape)
return _initializer

def identity_initializer(x):
"""Returns an initializer that generates identical tensors.

Args:
x: A tensor. All elements of the initialized variable
will be set to the elements of this tensor.

Returns:
An initializer that generates tensors with same shape and contents.
"""
def _initializer(shape, dtype):
x_ = gen_array_ops.identity(x)
x_.get_shape().assert_is_compatible_with(shape)
return x_
return _initializer

def random_uniform_initializer(minval=0.0, maxval=1.0, seed=None):
"""Returns an initializer that generates tensors with a uniform distribution.

Expand Down
39 changes: 39 additions & 0 deletions tensorflow/python/ops/init_ops_test.py
@@ -0,0 +1,39 @@
# 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 tensorflow.ops.init_ops."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
from tensorflow.python.ops import init_ops


class IdentityInitializerTest(tf.test.TestCase):

def testIdentityInitialize(self):
with self.test_session():
v = tf.constant([1.0, 2.5], name="var")
x=tf.get_variable("x", [2], initializer=init_ops.identity_initializer(v))

tf.initialize_all_variables().run()

self.assertEqual(x.get_shape().as_list(), [2])
self.assertAllEqual(x.eval(), [1.0, 2.5])


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