tensorflow dataset_ops map()方法 (返回数据集通过函数“ map_func”的元素映射)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                tensorflow dataset_ops map()方法 (返回数据集通过函数“ map_func”的元素映射)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                
                            
                            
                            def map(self, map_func, num_parallel_calls=None):"""Maps `map_func` across the elements of this dataset.跨此數(shù)據(jù)集的元素映射“ 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.此轉(zhuǎn)換將`map_func`應(yīng)用于此數(shù)據(jù)集的每個(gè)元素,并返回一個(gè)包含已轉(zhuǎn)換元素的新數(shù)據(jù)集,其順序與輸入中出現(xiàn)的順序相同。For example:```python# NOTE: The following examples use `{ ... }` to represent the# contents of a dataset.# 以下示例使用“ {...}”表示數(shù)據(jù)集的內(nèi)容。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”的輸入簽名由該數(shù)據(jù)集中每個(gè)元素的結(jié)構(gòu)決定。 例如:```python# Each element is a `tf.Tensor` object. 每個(gè)元素都是一個(gè)“ tf.Tensor”對(duì)象。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”類(lèi)型的單個(gè)參數(shù)。result = a.map(lambda x: ...)# Each element is a tuple containing two `tf.Tensor` objects.# 每個(gè)元素都是一個(gè)包含兩個(gè)tf.Tensor對(duì)象的元組。b = { (1, "foo"), (2, "bar"), (3, "baz") }# `map_func` takes two arguments of type `tf.Tensor`.# map_func具有兩個(gè)類(lèi)型為tf.Tensor的參數(shù)。result = b.map(lambda x_int, y_str: ...)# Each element is a dictionary mapping strings to `tf.Tensor` objects.# 每個(gè)元素都是一個(gè)字典,將字符串映射到`tf.Tensor`對(duì)象。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”類(lèi)型的單個(gè)參數(shù),并具有與元素相同的鍵。result = c.map(lambda d: ...)```The value or values returned by `map_func` determine the structure of eachelement in the returned dataset.由map_func返回的一個(gè)或多個(gè)值確定返回的數(shù)據(jù)集中每個(gè)元素的結(jié)構(gòu)。```python# `map_func` returns a scalar `tf.Tensor` of type `tf.float32`.# map_func返回類(lèi)型為tf.float32的標(biāo)量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返回兩個(gè)tf.Tensor對(duì)象。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數(shù)組被隱式轉(zhuǎn)換為`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可以返回嵌套結(jié)構(gòu)。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`對(duì)象之外,`map_func`可以接受作為參數(shù)并返回`tf.SparseTensor`對(duì)象。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.一個(gè)將張量的嵌套結(jié)構(gòu)(具有由self.output_shapes和self.output_types定義的形狀和類(lèi)型)映射到另一個(gè)張量的嵌套結(jié)構(gòu)的函數(shù)。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標(biāo)量tf.Tensor,表示要并行處理的數(shù)字元素。 如果未指定,則將按順序處理元素。Returns:Dataset: A `Dataset`. 數(shù)據(jù)集"""if num_parallel_calls is None:return MapDataset(self, map_func)else:return ParallelMapDataset(self, map_func, num_parallel_calls)
 
                        
                        
                        參考文章:TensorFlow2.0(6):數(shù)據(jù)預(yù)處理中的Dataset
總結(jié)
以上是生活随笔為你收集整理的tensorflow dataset_ops map()方法 (返回数据集通过函数“ map_func”的元素映射)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: tensorflow tf.data.T
- 下一篇: tensorflow dataset_o
