1# Copyright 2017 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"""Datasets for random number generators.""" 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20import functools 21 22from tensorflow.python import tf2 23from tensorflow.python.data.ops import dataset_ops 24from tensorflow.python.data.util import random_seed 25from tensorflow.python.framework import dtypes 26from tensorflow.python.framework import tensor_spec 27from tensorflow.python.ops import gen_experimental_dataset_ops 28from tensorflow.python.util.tf_export import tf_export 29 30 31@tf_export("data.experimental.RandomDataset", v1=[]) 32class RandomDatasetV2(dataset_ops.DatasetSource): 33 """A `Dataset` of pseudorandom values.""" 34 35 def __init__(self, seed=None): 36 """A `Dataset` of pseudorandom values.""" 37 self._seed, self._seed2 = random_seed.get_seed(seed) 38 variant_tensor = gen_experimental_dataset_ops.random_dataset( 39 seed=self._seed, seed2=self._seed2, **self._flat_structure) 40 super(RandomDatasetV2, self).__init__(variant_tensor) 41 42 @property 43 def element_spec(self): 44 return tensor_spec.TensorSpec([], dtypes.int64) 45 46 47@tf_export(v1=["data.experimental.RandomDataset"]) 48class RandomDatasetV1(dataset_ops.DatasetV1Adapter): 49 """A `Dataset` of pseudorandom values.""" 50 51 @functools.wraps(RandomDatasetV2.__init__) 52 def __init__(self, seed=None): 53 wrapped = RandomDatasetV2(seed) 54 super(RandomDatasetV1, self).__init__(wrapped) 55 56 57if tf2.enabled(): 58 RandomDataset = RandomDatasetV2 59else: 60 RandomDataset = RandomDatasetV1 61