• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Scan dataset transformation."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20from tensorflow.python.util import deprecation
21from tensorflow.python.util.tf_export import tf_export
22
23
24@deprecation.deprecated(None, "Use `tf.data.Dataset.scan(...) instead")
25@tf_export("data.experimental.scan")
26def scan(initial_state, scan_func):
27  """A transformation that scans a function across an input dataset.
28
29  This transformation is a stateful relative of `tf.data.Dataset.map`.
30  In addition to mapping `scan_func` across the elements of the input dataset,
31  `scan()` accumulates one or more state tensors, whose initial values are
32  `initial_state`.
33
34  Args:
35    initial_state: A nested structure of tensors, representing the initial state
36      of the accumulator.
37    scan_func: A function that maps `(old_state, input_element)` to
38      `(new_state, output_element)`. It must take two arguments and return a
39      pair of nested structures of tensors. The `new_state` must match the
40      structure of `initial_state`.
41
42  Returns:
43    A `Dataset` transformation function, which can be passed to
44    `tf.data.Dataset.apply`.
45  """
46  def _apply_fn(dataset):
47    return dataset.scan(initial_state=initial_state, scan_func=scan_func)
48
49  return _apply_fn
50