Python tensorflow-如何选择数组中除索引序列之外的所有元素?
可以使用Python tensorflow-如何选择数组中除索引序列之外的所有元素?,python,tensorflow,Python,Tensorflow,可以使用np执行等效的numpy操作。按规定删除。由于没有tf.delete,我不知道如何在tensorflow中执行此操作: import tensorflow as tf def delete_tf(a, idx, axis=0): n = tf.shape(a)[axis] t = tf.ones_like(idx, dtype=tf.bool) m = ~tf.scatter_nd(tf.expand_dims(idx, 1), t, [n]) retu
np执行等效的numpy操作。按规定删除。由于没有tf.delete
,我不知道如何在tensorflow
中执行此操作:
import tensorflow as tf
def delete_tf(a, idx, axis=0):
n = tf.shape(a)[axis]
t = tf.ones_like(idx, dtype=tf.bool)
m = ~tf.scatter_nd(tf.expand_dims(idx, 1), t, [n])
return tf.boolean_mask(a, m, axis=axis)
with tf.Graph().as_default(), tf.Session() as sess:
data = tf.reshape(tf.range(12), [3, 4])
print(sess.run(delete_tf(data, [1], 0)))
# [[ 0 1 2 3]
# [ 8 9 10 11]]
print(sess.run(delete_tf(data, [0, 2], 1)))
# [[ 1 3]
# [ 5 7]
# [ 9 11]]
我想你可能想用tf.boolean_面具。比如说,
labels = tf.Variable([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
a = tf.Variable([1, 0, 0])
a1 = tf.cast(a, dtype=tf.bool)
print(a1)
mask = tf.math.logical_not(a1)
print(mask)
print(tf.boolean_mask(labels, mask))
输出是,
tf.Tensor([ True False False], shape=(3,), dtype=bool)
tf.Tensor([False True True], shape=(3,), dtype=bool)
tf.Tensor(
[[0 1 0]
[0 0 1]], shape=(2, 3), dtype=int32)
所以,你可以定义一个遮罩来删除第一维度中张量的特定向量