• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 The TensorFlow Authors. 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"""Utilities for generating Tensor-valued random seeds."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21from tensorflow.python.framework import constant_op
22from tensorflow.python.framework import dtypes
23from tensorflow.python.framework import ops
24from tensorflow.python.framework import random_seed
25from tensorflow.python.ops import array_ops
26from tensorflow.python.ops import math_ops
27
28
29def get_seed(seed):
30  """Returns the local seeds an operation should use given an op-specific seed.
31
32  See `tf.get_seed` for more details. This wrapper adds support for the case
33  where `seed` may be a tensor.
34
35  Args:
36    seed: An integer or a `tf.int64` scalar tensor.
37
38  Returns:
39    A tuple of two `tf.int64` scalar tensors that should be used for the local
40    seed of the calling dataset.
41  """
42  seed, seed2 = random_seed.get_seed(seed)
43  if seed is None:
44    seed = constant_op.constant(0, dtype=dtypes.int64, name="seed")
45  else:
46    seed = ops.convert_to_tensor(seed, dtype=dtypes.int64, name="seed")
47  if seed2 is None:
48    seed2 = constant_op.constant(0, dtype=dtypes.int64, name="seed2")
49  else:
50    with ops.name_scope("seed2") as scope:
51      seed2 = ops.convert_to_tensor(seed2, dtype=dtypes.int64)
52      seed2 = array_ops.where(
53          math_ops.logical_and(
54              math_ops.equal(seed, 0), math_ops.equal(seed2, 0)),
55          constant_op.constant(2**31 - 1, dtype=dtypes.int64),
56          seed2,
57          name=scope)
58  return seed, seed2
59