假设我有以下两个向量,一个用于开始索引,另一个用于结束索引
start_index = [1, 1, 2]
end_index = [3, 4, 3]
在下面我有最终布尔矩阵的形状
shape = [3, 6]
我想生成以下布尔矩阵
bool_mat = [[False, True, True, True, False, False]
[False, True, True, True, True, False]
[False, False, True, True, False, False]]
对于每一行,True 从 start_index 中的索引开始,以 end_index 中的索引结束,其他地方为 False,
bool_mat[i, start_index[i]:end_index[i]+1] = True
如何在 TensorFlow 中做到这一点?谢谢!
你可以这样做:
import tensorflow as tf
start_index = tf.constant([1, 1, 2])
end_index = tf.constant([3, 4, 3])
shape = tf.constant([3, 6])
col = tf.range(shape[1])
result = (col >= start_index[:, tf.newaxis]) & (col <= end_index[:, tf.newaxis])
with tf.Session() as sess:
print(sess.run(result))
输出:
[[False True True True False False]
[False True True True True False]
[False False True True False False]]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句