W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
dynamic_stitch(
indices,
data,
name=None
)
參見指南:張量變換>分割和連接
將數(shù)據(jù)張量的值插入一個張量.
建立一個合并的張量
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
將按順序合并值,因此,如果一個索引同時出現(xiàn)在 indices[m][i] 和 indices[n][j],并且 (m,i) < (n,j),那么切片 data[n][j] 會出現(xiàn)在合并后的結果中.
例如:
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 創(chuàng)建的分區(qū),如以下示例所示:
# 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.
返回張量.與數(shù)據(jù)具有相同的類型.
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: