本文整理汇总了Python中tensorflow.python.ops.tensor_array_ops.TensorArray方法的典型用法代码示例。如果您正苦于以下问题:Python tensor_array_ops.TensorArray方法的具体用法?Python tensor_array_ops.TensorArray怎么用?Python tensor_array_ops.TensorArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.ops.tensor_array_ops
的用法示例。
在下文中一共展示了tensor_array_ops.TensorArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert_network_state_tensorarray
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def convert_network_state_tensorarray(tensorarray):
"""Converts a source TensorArray to a source Tensor.
Performs a permutation between the steps * [stride, D] shape of a
source TensorArray and the (flattened) [stride * steps, D] shape of
a source Tensor.
The TensorArrays used during recurrence have an additional zeroth step that
needs to be removed.
Args:
tensorarray: TensorArray object to be converted.
Returns:
Tensor object after conversion.
"""
tensor = tensorarray.stack() # Results in a [steps, stride, D] tensor.
tensor = tf.slice(tensor, [1, 0, 0], [-1, -1, -1]) # Lop off the 0th step.
tensor = tf.transpose(tensor, [1, 0, 2]) # Switch steps and stride.
return tf.reshape(tensor, [-1, tf.shape(tensor)[2]])
示例2: create_array
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def create_array(self, stride):
"""Creates a new tensor array to store this layer's activations.
Arguments:
stride: Possibly dynamic batch * beam size with which to initialize the
tensor array
Returns:
TensorArray object
"""
check.Gt(self.dim, 0, 'Cannot create array when dimension is dynamic')
tensor_array = ta.TensorArray(dtype=tf.float32,
size=0,
dynamic_size=True,
clear_after_read=False,
infer_shape=False,
name='%s_array' % self.name)
# Start each array with all zeros. Special values will still be learned via
# the extra embedding dimension stored for each linked feature channel.
initial_value = tf.zeros([stride, self.dim])
return tensor_array.write(0, initial_value)
示例3: create
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def create(self,
fixed_embeddings,
linked_embeddings,
context_tensor_arrays,
attention_tensor,
during_training,
stride=None):
"""Constructs a feed-forward unit based on the features and context tensors.
Args:
fixed_embeddings: list of NamedTensor objects
linked_embeddings: list of NamedTensor objects
context_tensor_arrays: optional list of TensorArray objects used for
implicit recurrence.
attention_tensor: optional Tensor used for attention.
during_training: whether to create a network for training (vs inference).
stride: int scalar tensor containing the stride required for
bulk computation.
Returns:
A list of tensors corresponding to the list of layers.
"""
pass
示例4: _TensorArrayWriteGrad
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def _TensorArrayWriteGrad(op, flow):
"""Gradient for TensorArrayWrite.
Args:
op: Forward TensorArrayWrite op.
flow: Gradient `Tensor` flow to TensorArrayWrite.
Returns:
A grad `Tensor`, the gradient created in an upstream ReadGrad or PackGrad.
"""
# handle is the output store_handle of TensorArrayReadGrad or
# the handle output of TensorArrayWriteGrad. we must use this one.
handle = op.inputs[0]
index = op.inputs[1]
dtype = op.get_attr("T")
grad_source = _GetGradSource(flow)
g = (tensor_array_ops.TensorArray(dtype=dtype, handle=handle, flow=flow,
colocate_with_first_write_call=False)
.grad(source=grad_source, flow=flow))
grad = g.read(index)
return [None, None, grad, flow]
示例5: _TensorArrayScatterGrad
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def _TensorArrayScatterGrad(op, flow):
"""Gradient for TensorArrayScatter.
Args:
op: Forward TensorArrayScatter op.
flow: Gradient `Tensor` flow to TensorArrayScatter.
Returns:
A grad `Tensor`, the gradient created in upstream ReadGrads or PackGrad.
"""
handle = op.inputs[0]
indices = op.inputs[1]
dtype = op.get_attr("T")
grad_source = _GetGradSource(flow)
g = (tensor_array_ops.TensorArray(dtype=dtype, handle=handle, flow=flow,
colocate_with_first_write_call=False)
.grad(source=grad_source, flow=flow))
grad = g.gather(indices)
return [None, None, grad, flow]
示例6: _TensorArraySplitGrad
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def _TensorArraySplitGrad(op, flow):
"""Gradient for TensorArraySplit.
Args:
op: Forward TensorArraySplit op.
flow: Gradient `Tensor` flow to TensorArraySplit.
Returns:
A grad `Tensor`, the gradient created in upstream ReadGrads or PackGrad.
"""
handle = op.inputs[0]
dtype = op.get_attr("T")
grad_source = _GetGradSource(flow)
g = (tensor_array_ops.TensorArray(dtype=dtype, handle=handle, flow=flow,
colocate_with_first_write_call=False)
.grad(source=grad_source, flow=flow))
grad = g.concat()
# handle, value, lengths, flow_in
return [None, grad, None, flow]
示例7: _maybe_split_batch_beams
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def _maybe_split_batch_beams(self, t, s):
"""Maybe splits the tensor from a batch by beams into a batch of beams.
We do this so that we can use nest and not run into problems with shapes.
Args:
t: Tensor of dimension [batch_size*beam_width, s]
s: Tensor, Python int, or TensorShape.
Returns:
Either a reshaped version of t with dimension
[batch_size, beam_width, s] if t's first dimension is of size
batch_size*beam_width or t if not.
Raises:
TypeError: If t is an instance of TensorArray.
ValueError: If the rank of t is not statically known.
"""
_check_maybe(t)
if t.shape.ndims >= 1:
return self._split_batch_beams(t, s)
else:
return t
示例8: _maybe_merge_batch_beams
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def _maybe_merge_batch_beams(self, t, s):
"""Splits the tensor from a batch by beams into a batch of beams.
More exactly, t is a tensor of dimension [batch_size*beam_width, s]. We
reshape this into [batch_size, beam_width, s]
Args:
t: Tensor of dimension [batch_size*beam_width, s]
s: Tensor, Python int, or TensorShape.
Returns:
A reshaped version of t with dimension [batch_size, beam_width, s].
Raises:
TypeError: If t is an instance of TensorArray.
ValueError: If the rank of t is not statically known.
"""
_check_maybe(t)
if t.shape.ndims >= 2:
return self._merge_batch_beams(t, s)
else:
return t
示例9: _TensorArrayWriteGrad
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def _TensorArrayWriteGrad(op, flow):
"""Gradient for TensorArrayWrite.
Args:
op: Forward TensorArrayWrite op.
flow: Gradient `Tensor` flow to TensorArrayWrite.
Returns:
A grad `Tensor`, the gradient created in an upstream ReadGrad or PackGrad.
"""
# handle is the output store_handle of TensorArrayReadGrad or
# the handle output of TensorArrayWriteGrad. we must use this one.
handle = op.inputs[0]
index = op.inputs[1]
dtype = op.get_attr("T")
grad_source = _GetGradSource(flow)
g = tensor_array_ops.TensorArray(
dtype=dtype, handle=handle, flow=flow).grad(
source=grad_source, flow=flow)
grad = g.read(index)
return [None, None, grad, flow]
示例10: _TensorArrayScatterGrad
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def _TensorArrayScatterGrad(op, flow):
"""Gradient for TensorArrayScatter.
Args:
op: Forward TensorArrayScatter op.
flow: Gradient `Tensor` flow to TensorArrayScatter.
Returns:
A grad `Tensor`, the gradient created in upstream ReadGrads or PackGrad.
"""
handle = op.inputs[0]
indices = op.inputs[1]
dtype = op.get_attr("T")
grad_source = _GetGradSource(flow)
g = tensor_array_ops.TensorArray(
dtype=dtype, handle=handle, flow=flow).grad(
source=grad_source, flow=flow)
grad = g.gather(indices)
return [None, None, grad, flow]
示例11: _TensorArraySplitGrad
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def _TensorArraySplitGrad(op, flow):
"""Gradient for TensorArraySplit.
Args:
op: Forward TensorArraySplit op.
flow: Gradient `Tensor` flow to TensorArraySplit.
Returns:
A grad `Tensor`, the gradient created in upstream ReadGrads or PackGrad.
"""
handle = op.inputs[0]
dtype = op.get_attr("T")
grad_source = _GetGradSource(flow)
g = tensor_array_ops.TensorArray(
dtype=dtype, handle=handle, flow=flow).grad(
source=grad_source, flow=flow)
grad = g.concat()
# handle, value, lengths, flow_in
return [None, grad, None, flow]
示例12: normalize_tensors
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def normalize_tensors(tensors):
"""Converts a nested structure of tensor-like objects to tensors.
* `SparseTensor`-like inputs are converted to `SparseTensor`.
* `TensorArray` inputs are passed through.
* Everything else is converted to a dense `Tensor`.
Args:
tensors: A nested structure of tensor-like, list,
`SparseTensor`, `SparseTensorValue`, or `TensorArray` objects.
Returns:
A nested structure of tensor, `SparseTensor`, or `TensorArray` objects.
"""
flat_tensors = nest.flatten(tensors)
prepared = []
with ops.name_scope("normalize_tensors"):
for i, t in enumerate(flat_tensors):
if sparse_tensor_lib.is_sparse(t):
prepared.append(sparse_tensor_lib.SparseTensor.from_value(t))
elif isinstance(t, tensor_array_ops.TensorArray):
prepared.append(t)
else:
prepared.append(ops.convert_to_tensor(t, name="component_%d" % i))
return nest.pack_sequence_as(tensors, prepared)
示例13: create_array
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def create_array(self, stride):
"""Creates a new tensor array to store this layer's activations.
Arguments:
stride: Possibly dynamic batch * beam size with which to initialize the
tensor array
Returns:
TensorArray object
"""
check.Gt(self.dim, 0, 'Cannot create array when dimension is dynamic')
tensor_array = ta.TensorArray(
dtype=tf.float32,
size=0,
dynamic_size=True,
clear_after_read=False,
infer_shape=False,
name='%s_array' % self.name)
# Start each array with all zeros. Special values will still be learned via
# the extra embedding dimension stored for each linked feature channel.
initial_value = tf.zeros([stride, self.dim])
return tensor_array.write(0, initial_value)
示例14: testTensorArrayWriteRead
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def testTensorArrayWriteRead(self):
with self.test_session(use_gpu=self._use_gpu) as session:
ta = tensor_array_ops.TensorArray(
dtype=tf.float32, tensor_array_name="foo", size=3, infer_shape=False)
w0 = ta.write(0, [[4.0, 5.0]])
w1 = w0.write(1, [[1.0]])
w2 = w1.write(2, -3.0)
r0 = w2.read(0)
r1 = w2.read(1)
r2 = w2.read(2)
d0, d1, d2 = session.run([r0, r1, r2])
self.assertAllEqual([[4.0, 5.0]], d0)
self.assertAllEqual([[1.0]], d1)
self.assertAllEqual(-3.0, d2)
示例15: _testTensorArrayWritePack
# 需要导入模块: from tensorflow.python.ops import tensor_array_ops [as 别名]
# 或者: from tensorflow.python.ops.tensor_array_ops import TensorArray [as 别名]
def _testTensorArrayWritePack(self, tf_dtype, legacy):
dtype = tf_dtype.as_numpy_dtype()
with self.test_session(use_gpu=self._use_gpu):
ta = tensor_array_ops.TensorArray(
dtype=tf_dtype, tensor_array_name="foo", size=3)
if tf_dtype == tf.string:
# In Python3, np.str is unicode, while we always want bytes
convert = lambda x: np.asarray(x).astype("|S")
else:
convert = lambda x: np.asarray(x).astype(dtype)
w0 = ta.write(0, convert([[4.0, 5.0]]))
w1 = w0.write(1, convert([[6.0, 7.0]]))
w2 = w1.write(2, convert([[8.0, 9.0]]))
if legacy:
c0 = w2._legacy_pack()
else:
c0 = w2.pack()
self.assertAllEqual(
convert([[[4.0, 5.0]], [[6.0, 7.0]], [[8.0, 9.0]]]), c0.eval())