tensorflow dataset_ops map()方法 (返回数据集通过函数“ map_func”的元素映射)
生活随笔
收集整理的這篇文章主要介紹了
tensorflow dataset_ops map()方法 (返回数据集通过函数“ map_func”的元素映射)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
def map(self, map_func, num_parallel_calls=None):"""Maps `map_func` across the elements of this dataset.跨此數據集的元素映射“ map_func”。This transformation applies `map_func` to each element of this dataset, andreturns a new dataset containing the transformed elements, in the sameorder as they appeared in the input.此轉換將`map_func`應用于此數據集的每個元素,并返回一個包含已轉換元素的新數據集,其順序與輸入中出現的順序相同。For example:```python# NOTE: The following examples use `{ ... }` to represent the# contents of a dataset.# 以下示例使用“ {...}”表示數據集的內容。a = { 1, 2, 3, 4, 5 }a.map(lambda x: x + 1) = { 2, 3, 4, 5, 6 }```The input signature of `map_func` is determined by the structure of eachelement in this dataset. For example:“ map_func”的輸入簽名由該數據集中每個元素的結構決定。 例如:```python# Each element is a `tf.Tensor` object. 每個元素都是一個“ tf.Tensor”對象。a = { 1, 2, 3, 4, 5 }# `map_func` takes a single argument of type `tf.Tensor` with the same# shape and dtype.# “ map_func”采用形狀和dtype相同的“ tf.Tensor”類型的單個參數。result = a.map(lambda x: ...)# Each element is a tuple containing two `tf.Tensor` objects.# 每個元素都是一個包含兩個tf.Tensor對象的元組。b = { (1, "foo"), (2, "bar"), (3, "baz") }# `map_func` takes two arguments of type `tf.Tensor`.# map_func具有兩個類型為tf.Tensor的參數。result = b.map(lambda x_int, y_str: ...)# Each element is a dictionary mapping strings to `tf.Tensor` objects.# 每個元素都是一個字典,將字符串映射到`tf.Tensor`對象。c = { {"a": 1, "b": "foo"}, {"a": 2, "b": "bar"}, {"a": 3, "b": "baz"} }# `map_func` takes a single argument of type `dict` with the same keys as# the elements.# “ map_func”采用“ dict”類型的單個參數,并具有與元素相同的鍵。result = c.map(lambda d: ...)```The value or values returned by `map_func` determine the structure of eachelement in the returned dataset.由map_func返回的一個或多個值確定返回的數據集中每個元素的結構。```python# `map_func` returns a scalar `tf.Tensor` of type `tf.float32`.# map_func返回類型為tf.float32的標量tf.Tensor。def f(...):return tf.constant(37.0)result = dataset.map(f)result.output_classes == tf.Tensorresult.output_types == tf.float32result.output_shapes == [] # scalar# `map_func` returns two `tf.Tensor` objects. map_func返回兩個tf.Tensor對象。def g(...):return tf.constant(37.0), tf.constant(["Foo", "Bar", "Baz"])result = dataset.map(g)result.output_classes == (tf.Tensor, tf.Tensor)result.output_types == (tf.float32, tf.string)result.output_shapes == ([], [3])# Python primitives, lists, and NumPy arrays are implicitly converted to# `tf.Tensor`.# Python基元,列表和NumPy數組被隱式轉換為`tf.Tensor`。def h(...):return 37.0, ["Foo", "Bar", "Baz"], np.array([1.0, 2.0] dtype=np.float64)result = dataset.map(h)result.output_classes == (tf.Tensor, tf.Tensor, tf.Tensor)result.output_types == (tf.float32, tf.string, tf.float64)result.output_shapes == ([], [3], [2])# `map_func` can return nested structures. map_func可以返回嵌套結構。def i(...):return {"a": 37.0, "b": [42, 16]}, "foo"result.output_classes == ({"a": tf.Tensor, "b": tf.Tensor}, tf.Tensor)result.output_types == ({"a": tf.float32, "b": tf.int32}, tf.string)result.output_shapes == ({"a": [], "b": [2]}, [])```In addition to `tf.Tensor` objects, `map_func` can accept as arguments andreturn `tf.SparseTensor` objects.除了`tf.Tensor`對象之外,`map_func`可以接受作為參數并返回`tf.SparseTensor`對象。Args:map_func: A function mapping a nested structure of tensors (havingshapes and types defined by `self.output_shapes` and`self.output_types`) to another nested structure of tensors.一個將張量的嵌套結構(具有由self.output_shapes和self.output_types定義的形狀和類型)映射到另一個張量的嵌套結構的函數。num_parallel_calls: (Optional.) A `tf.int32` scalar `tf.Tensor`,representing the number elements to process in parallel. If notspecified, elements will be processed sequentially.tf.int32標量tf.Tensor,表示要并行處理的數字元素。 如果未指定,則將按順序處理元素。Returns:Dataset: A `Dataset`. 數據集"""if num_parallel_calls is None:return MapDataset(self, map_func)else:return ParallelMapDataset(self, map_func, num_parallel_calls)
參考文章:TensorFlow2.0(6):數據預處理中的Dataset
總結
以上是生活随笔為你收集整理的tensorflow dataset_ops map()方法 (返回数据集通过函数“ map_func”的元素映射)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tensorflow tf.data.T
- 下一篇: tensorflow dataset_o