阅读(11.1k) 书签 (0)

如何插入TensorFlow数据张量的值

2018-10-11 16:43 更新

tf.dynamic_stitch

dynamic_stitch(
    indices,
    data,
    name=None
)

参见指南:张量变换>分割和连接

将数据张量的值插入一个张量.

建立一个合并的张量

merged[indices[m][i, ..., j], ...] = data[m][i, ..., j, ...]

例如,如果每个 indices[m] 都是标量或向量,则我们有:

# Scalar indices:
merged[indices[m], ...] = data[m][...]

# Vector indices:
merged[indices[m][i], ...] = data[m][i, ...]

每个 data[i].shape 必须以相应的 indices[i].shape 开始,并且其余的 data[i].shape 必须是恒定的 w.r.t.i.也就是说我们必须有 data[i].shape = indices[i].shape + constant.就这个常量而言,输出形状为:

merged.shape = [max(indices)] + constant

将按顺序合并值,因此,如果一个索引同时出现在 indices[m][i] 和 indices[n][j],并且 (m,i) < (n,j),那么切片 data[n][j] 会出现在合并后的结果中.

例如:

indices[0] = 6
indices[1] = [4, 1]
indices[2] = [[5, 2], [0, 3]]
data[0] = [61, 62]
data[1] = [[41, 42], [11, 12]]
data[2] = [[[51, 52], [21, 22]], [[1, 2], [31, 32]]]
merged = [[1, 2], [11, 12], [21, 22], [31, 32], [41, 42],
          [51, 52], [61, 62]]

此方法可用于合并由 dynamic_partition 创建的分区,如以下示例所示:

# Apply function (increments x_i) on elements for which a certain condition
# apply (x_i != -1 in this example).
x=tf.constant([0.1, -1., 5.2, 4.3, -1., 7.4])
condition_mask=tf.not_equal(x,tf.constant(-1.))
partitioned_data = tf.dynamic_partition(
    x, tf.cast(condition_mask, tf.int32) , 2)
partitioned_data[1] = partitioned_data[1] + 1.0
condition_indices = tf.dynamic_partition(
    tf.range(tf.shape(x)[0]), tf.cast(condition_mask, tf.int32) , 2)
x = tf.dynamic_stitch(condition_indices, partitioned_data)
# Here x=[1.1, -1., 6.2, 5.3, -1, 8.4], the -1. values remain
# unchanged.

插入TensorFlow数据张量的值

ARGS:

  • indices:至少有一个 int32 类型的张量对象的列表.
  • data:与相同类型的张量对象的索引长度相同的列表.
  • name:操作的名称(可选).

返回:

返回张量.与数据具有相同的类型.