Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tensorflow-如何选择数组中除索引序列之外的所有元素?_Python_Tensorflow - Fatal编程技术网

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)
所以,你可以定义一个遮罩来删除第一维度中张量的特定向量