本文整理匯總了Python中tensorflow.compat.v1.TensorArray方法的典型用法代碼示例。如果您正苦於以下問題:Python v1.TensorArray方法的具體用法?Python v1.TensorArray怎麽用?Python v1.TensorArray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.TensorArray方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_tensor_array_write_read
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def test_tensor_array_write_read():
def run(dtype_str, infer_shape, element_shape):
with tf.Graph().as_default():
dtype = tf_dtypes[dtype_str]
np_data = np.array([[1.0, 2.0], [3.0, 4.0]]).astype(dtype_str)
in_data = [np_data, np_data]
t1 = tf.constant(np_data, dtype=dtype)
t2 = tf.constant(np_data, dtype=dtype)
ta1 = tf.TensorArray(dtype=dtype, size=2, infer_shape=infer_shape,
element_shape=element_shape)
ta2 = ta1.write(0, t1)
ta3 = ta2.write(1, t2)
out = ta3.read(0)
g = tf.get_default_graph()
compare_tf_with_tvm([], [], 'TensorArrayReadV3:0', mode='vm')
for dtype in ["float32", "int8"]:
run(dtype, False, None)
run(dtype, False, tf.TensorShape([None, 2]))
run(dtype, True, None)
示例2: test_tensor_array_scatter
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def test_tensor_array_scatter():
def run(dtype_str, infer_shape):
with tf.Graph().as_default():
dtype = tf_dtypes[dtype_str]
if infer_shape:
element_shape = tf.TensorShape([tf.Dimension(None)])
else:
element_shape = None
t = tf.constant(np.array([[1.0], [2.0], [3.0]]).astype(dtype_str), dtype=dtype)
indices = tf.constant([2, 1, 0])
ta1 = tf.TensorArray(dtype=dtype, size=3,
infer_shape=infer_shape,
element_shape=element_shape)
ta2 = ta1.scatter(indices, t)
out0 = ta2.read(0)
out1 = ta2.read(1)
out2 = ta2.read(2)
g = tf.get_default_graph()
compare_tf_with_tvm([], [], ['TensorArrayReadV3:0'], mode='vm')
compare_tf_with_tvm([], [], ['TensorArrayReadV3_1:0'], mode='vm')
compare_tf_with_tvm([], [], ['TensorArrayReadV3_2:0'], mode='vm')
for dtype in ["float32", "int8"]:
run(dtype, False)
run(dtype, True)
示例3: test_tensor_array_split
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def test_tensor_array_split():
def run(dtype_str, infer_shape):
with tf.Graph().as_default():
dtype = tf_dtypes[dtype_str]
t = tf.constant(np.array([[1.0], [2.0], [3.0], [4.0], [5.0], [6.0], [7.0], [8.0]]).astype(dtype_str), dtype=dtype)
split_length = tf.constant([2, 2, 2, 2], dtype=tf.int32)
ta1 = tf.TensorArray(dtype=dtype, size=4, infer_shape=infer_shape)
ta2 = ta1.split(t, split_length)
out0 = ta2.read(0)
out1 = ta2.read(1)
out2 = ta2.read(2)
out3 = ta2.read(3)
g = tf.get_default_graph()
compare_tf_with_tvm([], [], ['TensorArrayReadV3:0'], mode='debug')
compare_tf_with_tvm([], [], ['TensorArrayReadV3_1:0'], mode='debug')
compare_tf_with_tvm([], [], ['TensorArrayReadV3_2:0'], mode='debug')
compare_tf_with_tvm([], [], ['TensorArrayReadV3_3:0'], mode='debug')
for dtype in ["float32", "int8"]:
run(dtype, False)
run(dtype, True)
示例4: test_tensor_array_size
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def test_tensor_array_size():
if package_version.parse(tf.VERSION) >= package_version.parse('1.15.0'):
pytest.skip("Needs fixing for tflite >= 1.15.0")
def run(dtype_str, infer_shape):
with tf.Graph().as_default():
dtype = tf_dtypes[dtype_str]
np_data = np.array([[1.0, 2.0], [3.0, 4.0]]).astype(dtype_str)
in_data = [np_data, np_data]
t1 = tf.constant(np_data, dtype=dtype)
t2 = tf.constant(np_data, dtype=dtype)
ta1 = tf.TensorArray(dtype=dtype, size=2, infer_shape=infer_shape)
ta2 = ta1.write(0, t1)
ta3 = ta2.write(1, t2)
out = ta3.size()
g = tf.get_default_graph()
compare_tf_with_tvm([], [], 'TensorArraySizeV3:0', mode='debug')
for dtype in ["float32", "int8"]:
run(dtype, False)
run(dtype, True)
示例5: test_tensor_array_stack
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def test_tensor_array_stack():
def run(dtype_str, infer_shape):
if package_version.parse(tf.VERSION) >= package_version.parse('1.15.0'):
pytest.skip("Needs fixing for tflite >= 1.15.0")
with tf.Graph().as_default():
dtype = tf_dtypes[dtype_str]
t = tf.constant(np.array([[1.0], [2.0], [3.0]]).astype(dtype_str))
scatter_indices = tf.constant([2, 1, 0])
ta1 = tf.TensorArray(dtype=dtype, size=3, infer_shape=infer_shape)
ta2 = ta1.scatter(scatter_indices, t)
t1 = ta2.stack()
print(t1)
g = tf.get_default_graph()
compare_tf_with_tvm([], [], ['TensorArrayStack/TensorArrayGatherV3:0'], mode='vm')
for dtype in ["float32", "int8"]:
run(dtype, True)
示例6: test_tensor_array_unstack
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def test_tensor_array_unstack():
def run(dtype_str, input_shape, infer_shape):
if package_version.parse(tf.VERSION) >= package_version.parse('1.15.0'):
pytest.skip("Needs fixing for tflite >= 1.15.0")
with tf.Graph().as_default():
dtype = tf_dtypes[dtype_str]
t = tf.constant(np.random.choice([0, 1, 2, 3],
size=input_shape).astype(dtype.name))
ta1 = tf.TensorArray(dtype=dtype, infer_shape=infer_shape, size=input_shape[0])
ta2 = ta1.unstack(t)
out0 = ta2.size()
out1 = ta2.read(0)
compare_tf_with_tvm([], [], 'TensorArraySizeV3:0', mode='debug')
compare_tf_with_tvm([], [], 'TensorArrayReadV3:0', mode='debug')
for dtype in ["float32", "int8"]:
run(dtype, (5,), False)
run(dtype, (5, 5), True)
run(dtype, (5, 5, 5), False)
run(dtype, (5, 5, 5, 5), True)
#######################################################################
# ConcatV2
# --------
示例7: _unstack_ta
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def _unstack_ta(inp):
return tf.TensorArray(
dtype=inp.dtype, size=tf.shape(inp)[0],
element_shape=inp.get_shape()[1:]).unstack(inp)
示例8: nms_tf
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def nms_tf(dets, thresh):
"""Non-maximum suppression with tf graph mode."""
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
order = tf.argsort(scores, direction='DESCENDING')
keep = tf.TensorArray(tf.int32, size=0, dynamic_size=True)
index = 0
while tf.size(order) > 0:
i = order[0]
keep = keep.write(index, i)
xx1 = tf.maximum(x1[i], tf.gather(x1, order[1:]))
yy1 = tf.maximum(y1[i], tf.gather(y1, order[1:]))
xx2 = tf.minimum(x2[i], tf.gather(x2, order[1:]))
yy2 = tf.minimum(y2[i], tf.gather(y2, order[1:]))
w = tf.maximum(0.0, xx2 - xx1 + 1)
h = tf.maximum(0.0, yy2 - yy1 + 1)
intersection = w * h
overlap = intersection / (
areas[i] + tf.gather(areas, order[1:]) - intersection)
inds = tf.where_v2(overlap <= thresh)
order = tf.concat(tf.gather(order, inds + 1), axis=1)
order = tf.squeeze(order, axis=-1)
index += 1
return keep.stack()
示例9: batch_image_files_decode
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def batch_image_files_decode(image_files):
#raw_images = tf.TensorArray(tf.uint8, size=0, dynamic_size=True)
raw_images = tf.TensorArray(tf.float32, size=0, dynamic_size=True)
for i in tf.range(tf.shape(image_files)[0]):
#image = tf.io.decode_image(image_files[i])
image = tf.io.decode_image(image_files[i], dtype=tf.float32)
image.set_shape([None, None, None])
raw_images = raw_images.write(i, image)
return raw_images.stack()
###############################################################################
示例10: test_forward_squeeze
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def test_forward_squeeze():
""" Squeeze """
# Nothing to squeeze.
_test_squeeze(np.arange(2).reshape((2)))
_test_squeeze(np.arange(6).reshape((2, 3)))
# Squeeze the middle element away.
_test_squeeze(np.arange(4).reshape((2, 1, 2)))
# Squeeze on both ends.
_test_squeeze(np.arange(6).reshape((1, 2, 1, 3, 1)))
# Positive squeeze dim index.
_test_squeeze(np.arange(6).reshape((1, 2, 1, 3, 1)), [0])
_test_squeeze(np.arange(6).reshape((1, 2, 1, 3, 1)), [2, 4])
_test_squeeze(np.arange(6).reshape((1, 2, 1, 3, 1)), [0, 4, 2])
# Negative squeeze dim index.
_test_squeeze(np.arange(6).reshape((1, 2, 1, 3, 1)), [-1])
_test_squeeze(np.arange(6).reshape((1, 2, 1, 3, 1)), [-3, -5])
_test_squeeze(np.arange(6).reshape((1, 2, 1, 3, 1)), [-3, -5, -1])
#######################################################################
# TensorArray
# -----------
示例11: test_tensor_array_gather
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def test_tensor_array_gather():
def run(dtype_str, infer_shape):
with tf.Graph().as_default():
dtype = tf_dtypes[dtype_str]
t = tf.constant(np.array([[1.0], [2.0], [3.0]]).astype(dtype_str))
scatter_indices = tf.constant([2, 1, 0])
gather_indices = tf.constant([1, 2])
ta1 = tf.TensorArray(dtype=dtype, size=3, infer_shape=infer_shape)
ta2 = ta1.scatter(scatter_indices, t)
t1 = ta2.gather(gather_indices)
g = tf.get_default_graph()
compare_tf_with_tvm([], [], ['TensorArrayGatherV3:0'], mode='vm')
for dtype in ["float32", "int8"]:
run(dtype, True)
示例12: _scan_initial_state
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def _scan_initial_state(self):
"""Create TensorArrays and indices to track bin assignment.
availability: TensorArray[queue_size, num_sequences]
This represents the number of tokens available in the ith bin.
See implementation note below.
contents: TensorArray[queue_size, num_sequences * 2]
This holds the actual contents of the packed strings as well as a bit
mask indicating where sequences begin. It is stored in a flat vector and
is accessed in offsets of packed_length.
top_index: scalar [0, queue_size)
Integer tensor indicating which index is the "top" bin. See implementation
note below.
IMPLEMENTATION_NOTE:
The FFD algorithm periodically pops the topmost queue and pushes a new
one to replace it. In order to replicate those semantics with a fixed size
TensorArray, indexing operations are shifted by top_index. For example,
instead of:
`queue_available.read(i)`
a read is instead performed as:
`queue_available.read((i - top_index) % queue_size)`
to account for the fact that the "ith" logical FFD queue is stored at
position j. This means that the pop / push update can be performed by
simply incrementing top_index. (And zeroing the old top_index position.)
Returns:
The state for the binning scan.
"""
all_available = tf.ones((self._queue_size, self._num_sequences),
dtype=INDEX_DTYPE) * self._packed_length
total_size = self._packed_length * self._queue_size
total_size_range = tf.range(total_size, dtype=INDEX_DTYPE)
empty = tf.zeros((total_size, self._num_sequences * 2),
dtype=self._token_dtype)
availability = tf.TensorArray(
dtype=INDEX_DTYPE, size=self._queue_size, dynamic_size=False,
clear_after_read=False, element_shape=(self._num_sequences,)
).scatter(tf.range(self._queue_size, dtype=INDEX_DTYPE), all_available)
contents = tf.TensorArray(
dtype=self._token_dtype, size=total_size, dynamic_size=False,
clear_after_read=False, element_shape=(self._num_sequences * 2,)
).scatter(total_size_range, empty)
# Which index should be considered the "top" bucket for the purpose of
# the first-fit descending algorithm.
top_index = tf.zeros((), dtype=INDEX_DTYPE)
return availability, contents, top_index
示例13: _benchmark_handwritten_dynamic_rnn
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def _benchmark_handwritten_dynamic_rnn(self, batch_size, max_seq_len):
def my_dynamic_rnn(rnn_cell,
input_data,
initial_state,
sequence_length=None):
"""A handwritten reimplementation of dynamic_rnn."""
input_data = tf.transpose(input_data, [1, 0, 2])
outputs = tf.TensorArray(tf.float32, input_data.shape[0])
if sequence_length is None:
max_seq_len = input_data.shape[0]
else:
max_seq_len = tf.reduce_max(sequence_length)
def while_body(i, state, outputs):
new_output, new_state = rnn_cell(input_data[i], state)
output = tf.where(i < sequence_length, new_output,
tf.zeros(new_output.shape))
state = tf.where(i < sequence_length, new_state, state)
outputs = outputs.write(i, output)
return i + 1, state, outputs
def while_cond(i, unused_state, unused_outputs):
return i < max_seq_len
_, state, outputs = tf.while_loop(
while_cond,
while_body,
loop_vars=(tf.constant(0), initial_state, outputs))
return tf.transpose(outputs.stack(), [1, 0, 2]), state
with tf.Graph().as_default():
input_data, sequence_lengths = self._generate_fake_rnn_inputs(
batch_size=batch_size, max_seq_len=max_seq_len)
rnn_cell, initial_state = self._create_rnn_cell(batch_size=batch_size)
graph_output_t = my_dynamic_rnn(rnn_cell, input_data, initial_state,
sequence_lengths)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
def target():
sess.run(graph_output_t)
self.time_execution(
('Handwritten', batch_size, max_seq_len),
target,
iter_volume=batch_size,
iter_unit='examples',
extras={
'max_seq_len': max_seq_len,
'batch_size': batch_size,
})
示例14: _benchmark_ag_dynamic_rnn
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import TensorArray [as 別名]
def _benchmark_ag_dynamic_rnn(self, batch_size, max_seq_len):
def ag_dynamic_rnn(rnn_cell,
input_data,
initial_state,
sequence_length=None):
"""An autograph-able reimplementation of subset of dynamic_rnn."""
# [batch, time, features] -> [time, batch, features]
input_data = tf.transpose(input_data, [1, 0, 2])
if sequence_length is None:
max_seq_len = input_data.shape[0]
else:
max_seq_len = tf.reduce_max(sequence_length)
outputs = tf.TensorArray(tf.float32, size=max_seq_len)
state = initial_state
for i in tf.range(max_seq_len):
new_output, new_state = rnn_cell(input_data[i], state)
output = tf.where(i < sequence_length, new_output,
tf.zeros(new_output.shape))
state = tf.where(i < sequence_length, new_state, state)
outputs = outputs.write(i, output)
return tf.transpose(outputs.stack(), [1, 0, 2]), state
ag_dynamic_rnn = tf.autograph.to_graph(ag_dynamic_rnn)
with tf.Graph().as_default():
input_data, sequence_lengths = self._generate_fake_rnn_inputs(
batch_size=batch_size, max_seq_len=max_seq_len)
rnn_cell, initial_state = self._create_rnn_cell(batch_size=batch_size)
rnn_output = ag_dynamic_rnn(rnn_cell, input_data, initial_state,
sequence_lengths)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
def target():
sess.run(rnn_output)
self.time_execution(
('AutoGraph', batch_size, max_seq_len),
target,
iter_volume=batch_size,
iter_unit='examples',
extras={
'max_seq_len': max_seq_len,
'batch_size': batch_size,
})