[英]Pad a tensor by a weight with tensorflow in Python
我有一個3d張量A
的形狀[n, ?, m]
沿第三軸有一個非零元素。 例如
A[0,0,:] = [0,0,0,1,0,0]
A[1,0,:] = [0,0,1,0,0,0]
A[0,1,:] = [0,1,0,0,0,0]
A[1,1,:] = [1,0,0,0,0,0]
我的重張量w
形狀的(1,)
我想通過權重w
擴張張量A
,以便我可以如下變換張量A
A[0,0,:] = [0,0,w,1,w,0]
A[1,0,:] = [0,w,1,w,0,0]
A[0,1,:] = [w,1,w,0,0,0]
A[1,1,:] = [1,w,0,0,0,w]
請注意,權重w
是在非零元素1
附近添加的,如果它在邊界處,則我們將索引包裹起來。
我怎么能在python中使用tensorflow
來做到這tensorflow
。
編輯:
這是一個更通用的版本,適用於具有多個元素的填充向量:
import tensorflow as tf
def surround_nonzero(a, w):
# Find non-zero positions
idx = tf.where(tf.not_equal(a, 0))
# A vector to shift the last value in the indices
w_len = tf.shape(w, out_type=tf.int64)[0]
shift1 = tf.concat([tf.zeros(tf.shape(idx)[-1] - 1, dtype=tf.int64), [1]], axis=0)
shift_len = shift1 * tf.expand_dims(tf.range(1, w_len + 1), 1)
# Shift last value of indices using module to wrap around
a_shape = tf.shape(a, out_type=tf.int64)
d = a_shape[-1]
idx_exp = tf.expand_dims(idx, 1)
idx_prev_exp = (idx_exp - shift_len) % d
idx_next_exp = (idx_exp + shift_len) % d
# Reshape shifted indices
a_rank = tf.rank(a)
idx_prev = tf.reshape(idx_prev_exp, [-1, a_rank])
idx_next = tf.reshape(idx_next_exp, [-1, a_rank])
# Take non-zero values
nonzero = tf.gather_nd(a, idx)
# Tile wrapping value twice the number of non-zero values
n = tf.shape(nonzero)[0]
w2n = tf.tile(w, [2 * n])
# Make full index and values for scattering with non-zero values and wrapping value
idx_full = tf.concat([idx, idx_prev, idx_next], axis=0)
values_full = tf.concat([nonzero, w2n], axis=0)
# Make output tensor with scattering
return tf.scatter_nd(idx_full, values_full, a_shape)
# Test
with tf.Graph().as_default():
A = tf.constant([[[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 1, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0]]],
dtype=tf.int32)
w = tf.constant([2, 3, 4], dtype=tf.int32)
out = surround_nonzero(A, w)
with tf.Session() as sess:
print(sess.run(out))
輸出:
[[[4 0 4 3 2 1 2 3]
[3 2 1 2 3 4 0 4]]
[[0 4 3 2 1 2 3 4]
[1 2 3 4 0 4 3 2]]]
和以前一樣,這假設填充總是“適合”,並且不保證填充值重疊的情況下的行為。
這是使用tf.scatter_nd
執行此操作的tf.scatter_nd
:
import tensorflow as tf
def surround_nonzero(a, w):
# Find non-zero positions
idx = tf.where(tf.not_equal(a, 0))
# A vector to shift the last value in the indices by one
shift1 = tf.concat([tf.zeros(tf.shape(idx)[-1] - 1, dtype=tf.int64), [1]], axis=0)
# Shift last value of indices using module to wrap around
a_shape = tf.shape(a, out_type=tf.int64)
d = a_shape[-1]
idx_prev = (idx - shift1) % d
idx_next = (idx + shift1) % d
# Take non-zero values
nonzero = tf.gather_nd(a, idx)
# Tile wrapping value twice the number of non-zero values
n = tf.shape(nonzero)[0]
w2n = tf.tile(w, [2 * n])
# Make full index and values for scattering with non-zero values and wrapping value
idx_full = tf.concat([idx, idx_prev, idx_next], axis=0)
values_full = tf.concat([nonzero, w2n], axis=0)
# Make output tensor with scattering
return tf.scatter_nd(idx_full, values_full, a_shape)
# Test
with tf.Graph().as_default():
A = tf.constant([[[0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 0]],
[[0, 0, 1, 0, 0, 0],
[1, 0, 0, 0, 0, 0]]],
dtype=tf.int32)
w = tf.constant([2], dtype=tf.int32)
out = surround_nonzero(A, w)
with tf.Session() as sess:
print(sess.run(out))
輸出:
[[[0 0 2 1 2 0]
[2 1 2 0 0 0]]
[[0 2 1 2 0 0]
[1 2 0 0 0 2]]]
請注意,這假設每個非零值都被零包圍(就像您的情況一樣)。 否則,分散操作將找到重復的索引,並且輸出將不是確定性的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.