Maps `map_func` across the elements of this dataset. This transformation applies `map_func` to each element of this dataset, and returns a new dataset containing the transformed elements, in the same order as they appeared in the input. For example: ```python a = Dataset.r
(self, map_func, num_parallel_calls=None)
| 1150 | drop_remainder) |
| 1151 | |
| 1152 | def map(self, map_func, num_parallel_calls=None): |
| 1153 | """Maps `map_func` across the elements of this dataset. |
| 1154 | |
| 1155 | This transformation applies `map_func` to each element of this dataset, and |
| 1156 | returns a new dataset containing the transformed elements, in the same |
| 1157 | order as they appeared in the input. |
| 1158 | |
| 1159 | For example: |
| 1160 | |
| 1161 | ```python |
| 1162 | a = Dataset.range(1, 6) # ==> [ 1, 2, 3, 4, 5 ] |
| 1163 | |
| 1164 | a.map(lambda x: x + 1) # ==> [ 2, 3, 4, 5, 6 ] |
| 1165 | ``` |
| 1166 | |
| 1167 | The input signature of `map_func` is determined by the structure of each |
| 1168 | element in this dataset. For example: |
| 1169 | |
| 1170 | ```python |
| 1171 | # NOTE: The following examples use `{ ... }` to represent the |
| 1172 | # contents of a dataset. |
| 1173 | # Each element is a `tf.Tensor` object. |
| 1174 | a = { 1, 2, 3, 4, 5 } |
| 1175 | # `map_func` takes a single argument of type `tf.Tensor` with the same |
| 1176 | # shape and dtype. |
| 1177 | result = a.map(lambda x: ...) |
| 1178 | |
| 1179 | # Each element is a tuple containing two `tf.Tensor` objects. |
| 1180 | b = { (1, "foo"), (2, "bar"), (3, "baz") } |
| 1181 | # `map_func` takes two arguments of type `tf.Tensor`. |
| 1182 | result = b.map(lambda x_int, y_str: ...) |
| 1183 | |
| 1184 | # Each element is a dictionary mapping strings to `tf.Tensor` objects. |
| 1185 | c = { {"a": 1, "b": "foo"}, {"a": 2, "b": "bar"}, {"a": 3, "b": "baz"} } |
| 1186 | # `map_func` takes a single argument of type `dict` with the same keys as |
| 1187 | # the elements. |
| 1188 | result = c.map(lambda d: ...) |
| 1189 | ``` |
| 1190 | |
| 1191 | The value or values returned by `map_func` determine the structure of each |
| 1192 | element in the returned dataset. |
| 1193 | |
| 1194 | ```python |
| 1195 | # `map_func` returns a scalar `tf.Tensor` of type `tf.float32`. |
| 1196 | def f(...): |
| 1197 | return tf.constant(37.0) |
| 1198 | result = dataset.map(f) |
| 1199 | result.output_classes == tf.Tensor |
| 1200 | result.output_types == tf.float32 |
| 1201 | result.output_shapes == [] # scalar |
| 1202 | |
| 1203 | # `map_func` returns two `tf.Tensor` objects. |
| 1204 | def g(...): |
| 1205 | return tf.constant(37.0), tf.constant(["Foo", "Bar", "Baz"]) |
| 1206 | result = dataset.map(g) |
| 1207 | result.output_classes == (tf.Tensor, tf.Tensor) |
| 1208 | result.output_types == (tf.float32, tf.string) |
| 1209 | result.output_shapes == ([], [3]) |