Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/data/util/random_seed.py: 44%
18 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
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."""
17from tensorflow.python.framework import constant_op
18from tensorflow.python.framework import dtypes
19from tensorflow.python.framework import ops
20from tensorflow.python.framework import random_seed
21from tensorflow.python.ops import array_ops
22from tensorflow.python.ops import math_ops
25def get_seed(seed):
26 """Returns the local seeds an operation should use given an op-specific seed.
28 See `random_seed.get_seed` for more details. This wrapper adds support for
29 the case where `seed` may be a tensor.
31 Args:
32 seed: An integer or a `tf.int64` scalar tensor.
34 Returns:
35 A tuple of two `tf.int64` scalar tensors that should be used for the local
36 seed of the calling dataset.
37 """
38 seed, seed2 = random_seed.get_seed(seed)
39 if seed is None:
40 seed = constant_op.constant(0, dtype=dtypes.int64, name="seed")
41 else:
42 seed = ops.convert_to_tensor(seed, dtype=dtypes.int64, name="seed")
43 if seed2 is None:
44 seed2 = constant_op.constant(0, dtype=dtypes.int64, name="seed2")
45 else:
46 with ops.name_scope("seed2") as scope:
47 seed2 = ops.convert_to_tensor(seed2, dtype=dtypes.int64)
48 seed2 = array_ops.where_v2(
49 math_ops.logical_and(
50 math_ops.equal(seed, 0), math_ops.equal(seed2, 0)),
51 constant_op.constant(2**31 - 1, dtype=dtypes.int64),
52 seed2,
53 name=scope)
54 return seed, seed2