aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/ops/tensor_array_ops.py
blob: c12506694f6143014a1ee5b67e07fecc9bfda650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Data Flow Operations."""
# pylint: disable=g-bad-name
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.framework import dtypes as _dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape
from tensorflow.python.ops import constant_op
from tensorflow.python.ops import gen_data_flow_ops


# pylint: disable=protected-access
class TensorArray(object):
  """Class wrapping dynamic-sized, per-time-step, write-once Tensor arrays.

  This class is meant to be used with dynamic iteration primitives such as
  `While` loops, and supports gradient back-propagation via special "flow"
  control flow dependencies.

  @@handle
  @@flow

  @@read
  @@unpack

  @@write
  @@pack

  @@grad
  """

  def __init__(
      self, dtype, size=None, tensor_array_name=None, handle=None, name=None):
    """Construct a new TensorArray or wrap an existing TensorArray handle.

    Args:
      dtype: (required) data type of the TensorArray.
      size: (optional) int32 scalar `Tensor`: the size of the TensorArray.
        Required if handle is not provided.
      tensor_array_name: (optional) Python string: the name of the TensorArray.
        This is used when creating the TensorArray handle.  If this value is
        set, handle should be None.
      handle: (optional) A `Tensor` handle to an existing TensorArray.  If this
        is set, tensor_array_name should be None.
      name: A name for the operation (optional).

    Raises:
      ValueError: if both handle and tensor_array_name are provided.
      TypeError: if handle is provided but is not a Tensor.
    """
    if handle and tensor_array_name:
      raise ValueError(
          "Cannot construct with both handle and tensor_array_name")
    if handle and not isinstance(handle, ops.Tensor):
      raise TypeError("Handle must be a Tensor")
    if handle is None and size is None:
      raise ValueError("Size must be provided if handle is not provided")

    with ops.op_scope([handle, size], name, "TensorArray") as scope:
      if handle:
        self._handle = handle
      else:
        self._handle = gen_data_flow_ops._tensor_array(
            dtype=dtype, size=size, tensor_array_name=tensor_array_name,
            name=scope)

      self._flow = constant_op.constant(0, dtype=_dtypes.float32)
      self._dtype = dtype

  @property
  def flow(self):
    """The flow `Tensor` forcing ops leading to this TensorArray state."""
    return self._flow

  @property
  def handle(self):
    """The reference to the TensorArray."""
    return self._handle

  def grad(self, source):
    g_handle = gen_data_flow_ops._tensor_array_grad(
        handle=self._handle, source=source)
    g = TensorArray(dtype=self._dtype, size=None, handle=g_handle)
    return g

  def read(self, index, name=None):
    """Read the value at location `index` in the TensorArray."""
    value = gen_data_flow_ops._tensor_array_read(
        handle=self._handle, index=index, flow_in=self._flow, dtype=self._dtype,
        name=name)
    return value

  def write(self, index, value, name=None):
    """Write `value` into index `index` of the TensorArray."""
    flow_out = gen_data_flow_ops._tensor_array_write(
        handle=self._handle, index=index, value=value, flow_in=self._flow,
        name=name)
    # Size below is ignored
    ta = TensorArray(dtype=self._dtype, size=-1, handle=self._handle)
    ta._flow = flow_out
    return ta

  def pack(self, name=None):
    """Return the values in the TensorArray as a packed `Tensor`."""
    value = gen_data_flow_ops._tensor_array_pack(
        handle=self._handle, flow_in=self._flow, dtype=self._dtype,
        name=name)

    return value

  def unpack(self, value, name=None):
    """Packs the values of a `Tensor` in the TensorArray."""
    flow_out = gen_data_flow_ops._tensor_array_unpack(
        handle=self._handle, value=value, flow_in=self._flow,
        name=name)
    ta = TensorArray(dtype=self._dtype, size=-1, handle=self._handle)
    ta._flow = flow_out
    return ta

  def close(self, name=None):
    """Close the current TensorArray."""
    return gen_data_flow_ops._tensor_array_close(
        handle=self._handle, name=name)


@ops.RegisterShape("TensorArray")
def _TensorArrayShape(op):
  # size is a scalar
  op.inputs[0].get_shape().merge_with(tensor_shape.scalar())
  return [tensor_shape.vector(2)]


@ops.RegisterShape("TensorArrayRead")
def _TensorArrayReadShape(op):
  # handle, index, flow_in
  op.inputs[0].get_shape().merge_with(tensor_shape.vector(2))
  op.inputs[1].get_shape().merge_with(tensor_shape.scalar())
  op.inputs[2].get_shape().merge_with(tensor_shape.scalar())
  # value
  return [tensor_shape.unknown_shape()]


@ops.RegisterShape("TensorArrayWrite")
def _TensorArrayWriteShape(op):
  # handle, index, value, flow_in
  op.inputs[0].get_shape().merge_with(tensor_shape.vector(2))
  op.inputs[1].get_shape().merge_with(tensor_shape.scalar())
  op.inputs[3].get_shape().merge_with(tensor_shape.scalar())
  # flow_out
  return [tensor_shape.scalar()]


@ops.RegisterShape("TensorArrayClose")
def _TensorArrayCloseShape(op):
  """Shape function for ops that take a scalar and produce no outputs."""
  op.inputs[0].get_shape().merge_with(tensor_shape.vector(2))
  return []


@ops.RegisterShape("TensorArrayGrad")
def _TensorArrayGradShape(op):
  """Shape function for ops that take a scalar and produce no outputs."""
  op.inputs[0].get_shape().merge_with(tensor_shape.vector(2))
  return [tensor_shape.vector(2)]


@ops.RegisterShape("TensorArrayPack")
def _TensorArrayPackShape(op):
  # handle, flow_in
  op.inputs[0].get_shape().merge_with(tensor_shape.vector(2))
  op.inputs[1].get_shape().merge_with(tensor_shape.scalar())
  # value
  return [tensor_shape.unknown_shape()]


@ops.RegisterShape("TensorArrayUnpack")
def _TensorArrayUnpackShape(op):
  # handle, value, flow_in
  op.inputs[0].get_shape().merge_with(tensor_shape.vector(2))
  op.inputs[2].get_shape().merge_with(tensor_shape.scalar())
  # flow_out
  return [tensor_shape.scalar()]
# pylint: enable=protected-access