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.data.ops import dataset_ops 23from tensorflow.python.data.util import random_seed 24from tensorflow.python.data.util import structure 25from tensorflow.python.framework import dtypes 26from tensorflow.python.ops import gen_experimental_dataset_ops 27from tensorflow.python.util.tf_export import tf_export 28 29 30@tf_export("data.experimental.RandomDataset", v1=[]) 31class RandomDatasetV2(dataset_ops.DatasetSource): 32 """A `Dataset` of pseudorandom values.""" 33 34 def __init__(self, seed=None): 35 """A `Dataset` of pseudorandom values.""" 36 self._seed, self._seed2 = random_seed.get_seed(seed) 37 variant_tensor = gen_experimental_dataset_ops.experimental_random_dataset( 38 seed=self._seed, seed2=self._seed2, **dataset_ops.flat_structure(self)) 39 super(RandomDatasetV2, self).__init__(variant_tensor) 40 41 @property 42 def _element_structure(self): 43 return structure.TensorStructure(dtypes.int64, []) 44 45 46@tf_export(v1=["data.experimental.RandomDataset"]) 47class RandomDatasetV1(dataset_ops.DatasetV1Adapter): 48 """A `Dataset` of pseudorandom values.""" 49 50 @functools.wraps(RandomDatasetV2.__init__) 51 def __init__(self, seed=None): 52 wrapped = RandomDatasetV2(seed) 53 super(RandomDatasetV1, self).__init__(wrapped) 54 55 56# TODO(b/119044825): Until all `tf.data` unit tests are converted to V2, keep 57# this alias in place. 58RandomDataset = RandomDatasetV1 59