def testTensorArrayUnpackWrongMajorSizeFails(self): with self.test_session(): h = gen_data_flow_ops._tensor_array( dtype=tf.float32, tensor_array_name="foo", size=3) with self.assertRaisesOpError( r"Input value must have first dimension " r"equal to the array size \(2 vs. 3\)"): gen_data_flow_ops._tensor_array_unpack(h, [1.0, 2.0]).run()
def unpack(self, value, name=None): """Pack the values of a `Tensor` in the TensorArray. Args: value: (N+1)-D. Tensor of type `dtype`. The Tensor to unpack. name: A name for the operation (optional). Returns: A new TensorArray object with flow that ensures the unpack occurs. Use this object all for subsequent operations. Raises: ValueError: if the shape inference fails. """ with ops.colocate_with(self._handle): 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, handle=self._handle) ta._flow = flow_out ta._infer_shape = self._infer_shape ta._elem_shape = self._elem_shape if ta._infer_shape: val_shape = flow_out.op.inputs[1].get_shape() elem_shape = tensor_shape.unknown_shape() if val_shape.dims is not None: elem_shape = tensor_shape.TensorShape(val_shape.dims[1:]) if ta._elem_shape: if not elem_shape == ta._elem_shape[0]: raise ValueError( "Inconsistent shapes: saw %s but expected %s " "(and infer_shape=True)" % (elem_shape, ta._elem_shape[0])) else: ta._elem_shape.append(elem_shape) return ta
def unpack(self, value, name=None): """Pack the values of a `Tensor` in the TensorArray. Args: value: (N+1)-D. Tensor of type `dtype`. The Tensor to unpack. name: A name for the operation (optional). Returns: A new TensorArray object with flow that ensures the unpack occurs. Use this object all for subsequent operations. Raises: ValueError: if the shape inference fails. """ with ops.colocate_with(self._handle): 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, handle=self._handle) ta._flow = flow_out ta._infer_shape = self._infer_shape ta._elem_shape = self._elem_shape if ta._infer_shape: val_shape = flow_out.op.inputs[1].get_shape() elem_shape = tensor_shape.unknown_shape() if val_shape.dims: elem_shape = tensor_shape.TensorShape(val_shape.dims[1:]) if ta._elem_shape: if not elem_shape == ta._elem_shape[0]: raise ValueError("Shape inference failed.") else: ta._elem_shape.append(elem_shape) return ta
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 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, handle=self._handle) ta._flow = flow_out return ta
def unpack(self, value): """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, gradient_add=self._gradient_add) ta = TensorArray(dtype=self._dtype, size=-1, handle=self._handle) ta._gradient_add = self._gradient_add ta._flow = flow_out return ta
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)
def unpack(self, value, name=None): """Pack the values of a `Tensor` in the TensorArray. Args: value: (N+1)-D. Tensor of type `dtype`. The Tensor to unpack. name: A name for the operation (optional). Returns: A new TensorArray object with flow that ensures the unpack occurs. Use this object all for subsequent operations. """ with ops.colocate_with(self._handle): 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, handle=self._handle) ta._flow = flow_out return ta
def unpack(self, value, name=None): """Pack the values of a `Tensor` in the TensorArray. Args: value: (N+1)-D. Tensor of type `dtype`. The Tensor to unpack. name: A name for the operation (optional). Returns: A new TensorArray object with flow that ensures the unpack occurs. Use this object all for subsequent operations. """ with ops.colocate_with(self._handle): 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, handle=self._handle) ta._flow = flow_out return ta
def _legacy_unpack(self, value, name=None): """Pack the values of a `Tensor` in the TensorArray. This is the legacy version of pack, kept for testing backwards compatibility. Args: value: (N+1)-D. Tensor of type `dtype`. The Tensor to unpack. name: A name for the operation (optional). Returns: A new TensorArray object with flow that ensures the unpack occurs. Use this object all for subsequent operations. Raises: ValueError: if the shape inference fails. """ with ops.colocate_with(self._handle): 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, handle=self._handle) ta._flow = flow_out ta._infer_shape = self._infer_shape ta._elem_shape = self._elem_shape if ta._infer_shape: val_shape = flow_out.op.inputs[1].get_shape() elem_shape = tensor_shape.unknown_shape() if val_shape.dims is not None: elem_shape = tensor_shape.TensorShape(val_shape.dims[1:]) if ta._elem_shape: if not elem_shape == ta._elem_shape[0]: raise ValueError( "Inconsistent shapes: saw %s but expected %s " "(and infer_shape=True)" % (elem_shape, ta._elem_shape[0])) else: ta._elem_shape.append(elem_shape) return ta