Python TensorFlow ``dynamic_stitch()`` 方法详解与代码示例


发布日期 : 2021-09-03 14:57:17 UTC

访问量: 9 次浏览

Python – tensorflow.dynamic\_stitch()

TensorFlow是谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

dynamic\_stitch()用于将多个张量合并为一个张量。

语法:
tensorflow.dynamic_stitch( indices, data, name)

参数:

  • indices:它是一个张量列表,至少有1个张量,每个张量的 dtype 为int32。
  • data:它是具有相同长度索引的张量列表。
  • name(可选):它定义了该操作的名称。

结果:它返回一个与数据的 dtype 相同的张量。

示例 1:

# Importing the library
import tensorflow as tf

# Initializing the input
indices = [[0, 1, 5], [2, 4, 3, 6]]
data = [[1, 2, 3], [4, 5, 6, 7]]

# Printing the input
print('indices:', indices)
print('data: ', data)

# Calculating result
x = tf.dynamic_stitch(indices, data)

# Printing the result
print('x: ', x)

输出:

indices: [[0, 1, 5], [2, 4, 3, 6]]
data: [[1, 2, 3], [4, 5, 6, 7]]
x: tf.Tensor([1 2 4 6 5 3 7], shape=(7, ), dtype=int32)

示例 2:

# Importing the library
import tensorflow as tf

# Initializing the input
indices = [[0, 1, 6], [5, 4, 3]]
data = [[1, 2, 3], [4, 5, 6]]

# Printing the input
print('indices:', indices)
print('data: ', data)

# Calculating result
x = tf.dynamic_stitch(indices, data)

# Printing the result
print('x: ', x)

输出:

indices: [[0, 1, 2], [5, 4, 3]]
data: [[1, 2, 3], [4, 5, 6]]
x: tf.Tensor([1 2 3 6 5 4], shape=(6, ), dtype=int32)