def testTensorArrayReadWrongIndexOrDataTypeFails(self):
    with self.test_session(use_gpu=self._use_gpu):
      ta = tensor_array_ops.TensorArray(
          dtype=tf.float32, tensor_array_name="foo", size=3)

      w0 = ta.write(0, [[4.0, 5.0]])

      # Test reading wrong datatype
      r0_bad = gen_data_flow_ops._tensor_array_read(
          handle=w0.handle, index=0, dtype=tf.int64, flow_in=w0.flow)
      with self.assertRaisesOpError(
          "TensorArray dtype is float but Op requested dtype int64."):
        r0_bad.eval()

      # Test reading from a different index than the one we wrote to
      r1 = w0.read(1)
      with self.assertRaisesOpError(
          "Could not read from TensorArray index 1 because "
          "it has not yet been written to."):
        r1.eval()

      # Test reading from a negative index
      with self.assertRaisesOpError(
          r"Tried to read from index -1 but array size is: 3"):
        ta.read(-1).eval()

      # Test reading from too large an index
      with self.assertRaisesOpError(
          "Tried to read from index 3 but array size is: 3"):
        ta.read(3).eval()
 def read(self, index):
     """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)
     return value
Exemple #3
0
  def testTensorArrayReadWrongIndexOrDataTypeFails(self):
    with self.test_session(use_gpu=self._use_gpu):
      ta = tensor_array_ops.TensorArray(
          dtype=tf.float32, tensor_array_name="foo", size=3)

      w0 = ta.write(0, [[4.0, 5.0]])

      # Test reading wrong datatype
      r0_bad = gen_data_flow_ops._tensor_array_read(
          handle=w0.handle, index=0, dtype=tf.int64, flow_in=w0.flow)
      with self.assertRaisesOpError(
          "TensorArray dtype is float but Op requested dtype int64."):
        r0_bad.eval()

      # Test reading from a different index than the one we wrote to
      r1 = w0.read(1)
      with self.assertRaisesOpError(
          "Could not read from TensorArray index 1 because "
          "it has not yet been written to."):
        r1.eval()

      # Test reading from a negative index
      with self.assertRaisesOpError(
          r"Tried to read from index -1 but array size is: 3"):
        ta.read(-1).eval()

      # Test reading from too large an index
      with self.assertRaisesOpError(
          "Tried to read from index 3 but array size is: 3"):
        ta.read(3).eval()
  def _testMultiTensorArray(self, use_gpu):
    with self.test_session(use_gpu=use_gpu):
      h1 = gen_data_flow_ops._tensor_array(
          size=1, dtype=tf.float32, tensor_array_name="foo")
      with tf.control_dependencies([
          gen_data_flow_ops._tensor_array_write(h1, 0, 4.0)]):
        r1 = gen_data_flow_ops._tensor_array_read(h1, 0, tf.float32)

      h2 = gen_data_flow_ops._tensor_array(
          size=1, dtype=tf.float32, tensor_array_name="bar")

      with tf.control_dependencies([
          gen_data_flow_ops._tensor_array_write(h2, 0, 5.0)]):
        r2 = gen_data_flow_ops._tensor_array_read(h2, 0, tf.float32)
      r = r1 + r2
      self.assertAllClose(9.0, r.eval())
  def _testTensorArrayWriteRead(self, use_gpu):
    with self.test_session(use_gpu=use_gpu) as sess:
      h = gen_data_flow_ops._tensor_array(
          dtype=tf.float32, tensor_array_name="foo", size=3)

      writes = [
          gen_data_flow_ops._tensor_array_write(h, 0, [[4.0, 5.0]]),
          gen_data_flow_ops._tensor_array_write(h, 1, [[1.0]]),
          gen_data_flow_ops._tensor_array_write(h, 2, -3.0)]

      with tf.control_dependencies(writes):
        r0 = gen_data_flow_ops._tensor_array_read(h, 0, tf.float32)
        r1 = gen_data_flow_ops._tensor_array_read(h, 1, tf.float32)
        r2 = gen_data_flow_ops._tensor_array_read(h, 2, tf.float32)

      d0, d1, d2 = sess.run([r0, r1, r2])
      self.assertAllEqual([[4.0, 5.0]], d0)
      self.assertAllEqual([[1.0]], d1)
      self.assertAllEqual(-3.0, d2)
  def _testTensorArrayReadWrongIndexOrDataTypeFails(self, use_gpu):
    with self.test_session(use_gpu=use_gpu):
      h = gen_data_flow_ops._tensor_array(
          dtype=tf.float32, tensor_array_name="foo", size=3)

      with tf.control_dependencies([
          gen_data_flow_ops._tensor_array_write(h, 0, [[4.0, 5.0]])]):

        # Test reading wrong datatype
        r0_bad = gen_data_flow_ops._tensor_array_read(h, 0, tf.int64)
        with self.assertRaisesOpError(
            "TensorArray dtype is float but Op requested dtype int64."):
          r0_bad.eval()

        # Test reading from a different index than the one we wrote to
        r1 = gen_data_flow_ops._tensor_array_read(h, 1, tf.float32)
        with self.assertRaisesOpError(
            "Could not read from TensorArray index 1 because "
            "it has not yet been written to."):
          r1.eval()

      # Test reading from a negative index
      with self.assertRaisesOpError(
          "Tried to read from index -1 but array size is: 3"):
        gen_data_flow_ops._tensor_array_read(h, -1, tf.float32).eval()

      # Test reading from too large an index
      with self.assertRaisesOpError(
          "Tried to read from index 3 but array size is: 3"):
        gen_data_flow_ops._tensor_array_read(h, 3, tf.float32).eval()
    def read(self, index, name=None):
        """Read the value at location `index` in the TensorArray.

    Args:
      index: 0-D.  int32 tensor with the index to read from.
      name: A name for the operation (optional).

    Returns:
      The tensor at index `index`.
    """
        with ops.colocate_with(self._handle):
            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 _testTensorGradAccessTwiceReceiveSameObject(self, use_gpu):
    with self.test_session(use_gpu=use_gpu) as sess:
      h = gen_data_flow_ops._tensor_array(
          dtype=tf.float32, tensor_array_name="foo", size=3)
      g_h_0 = gen_data_flow_ops._tensor_array_grad(h)
      g_h_1 = gen_data_flow_ops._tensor_array_grad(h)

      with tf.control_dependencies([
          gen_data_flow_ops._tensor_array_write(g_h_0, 0, [[4.0, 5.0]])]):
        # Write with one gradient handle, read with another copy of it
        r1_0 = gen_data_flow_ops._tensor_array_read(g_h_1, 0, tf.float32)

      t_g_h_0, t_g_h_1, d_r1_0 = sess.run([g_h_0, g_h_1, r1_0])
      self.assertAllEqual(t_g_h_0, t_g_h_1)
      self.assertAllEqual([[4.0, 5.0]], d_r1_0)
  def _testTensorArrayWriteGradientAddMultipleAddsType(self, use_gpu, dtype):
    with self.test_session(use_gpu=use_gpu):
      h = gen_data_flow_ops._tensor_array(
          dtype=dtype, tensor_array_name="foo", size=3)

      c = lambda x: np.asarray(x, dtype=dtype.as_numpy_dtype)

      writes = [
          gen_data_flow_ops._tensor_array_write(
              h, 2, c(3.0), gradient_add=True),
          gen_data_flow_ops._tensor_array_write(
              h, 2, c(4.0), gradient_add=True)]

      with tf.control_dependencies(writes):
        self.assertAllEqual(
            c(7.00), gen_data_flow_ops._tensor_array_read(h, 2, dtype).eval())
Exemple #10
0
    def read(self, index, name=None):
        """Read the value at location `index` in the TensorArray.

    Args:
      index: 0-D.  int32 tensor with the index to read from.
      name: A name for the operation (optional).

    Returns:
      The tensor at index `index`.
    """
        with ops.colocate_with(self._handle):
            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 _testTensorArrayUnpackRead(self, tf_dtype, use_gpu):
    dtype = tf_dtype.as_numpy_dtype()
    with self.test_session(use_gpu=use_gpu) as sess:
      h = gen_data_flow_ops._tensor_array(
          dtype=tf_dtype, tensor_array_name="foo", size=3)

      if tf_dtype == tf.string:
        convert = lambda x: np.asarray(x).astype(np.str)
      else:
        convert = lambda x: np.asarray(x).astype(dtype)

      # Unpack a vector into scalars
      with tf.control_dependencies([
          gen_data_flow_ops._tensor_array_unpack(
              h, convert([1.0, 2.0, 3.0]))]):
        r0 = gen_data_flow_ops._tensor_array_read(h, 0, tf_dtype)
        r1 = gen_data_flow_ops._tensor_array_read(h, 1, tf_dtype)
        r2 = gen_data_flow_ops._tensor_array_read(h, 2, tf_dtype)

      d0, d1, d2 = sess.run([r0, r1, r2])
      self.assertAllEqual(convert(1.0), d0)
      self.assertAllEqual(convert(2.0), d1)
      self.assertAllEqual(convert(3.0), d2)

      # Unpack a matrix into vectors
      with tf.control_dependencies([
          gen_data_flow_ops._tensor_array_unpack(
              h, convert([[1.0, 1.1], [2.0, 2.1], [3.0, 3.1]]))]):
        r0 = gen_data_flow_ops._tensor_array_read(h, 0, tf_dtype)
        r1 = gen_data_flow_ops._tensor_array_read(h, 1, tf_dtype)
        r2 = gen_data_flow_ops._tensor_array_read(h, 2, tf_dtype)

      d0, d1, d2 = sess.run([r0, r1, r2])
      self.assertAllEqual(convert([1.0, 1.1]), d0)
      self.assertAllEqual(convert([2.0, 2.1]), d1)
      self.assertAllEqual(convert([3.0, 3.1]), d2)
Exemple #12
0
 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