英文:
Select index of a 2D Tensor with exact values
问题
抱歉,我看不到代码部分,所以我无法提供完整的翻译。以下是文本的翻译部分:
抱歉问一个这么琐碎的问题,但我是Tensorflow的新手。
我有两个张量。
y_true = [[1,0], [0,1], [1,0], [1,0], [0,1], [0,1], [1,0], [0,1], [1,0], [0,1]]
y_pred = [[0.6,0.4], [0.3,0.7], [0.8,0.2], [0.8,0.2], [0.3,0.7],[0.1,0.9],[0.9, 0.1],[0.4,0.6],[0.6,0.4],[0.2,0.8]]
此外,我想根据[1,0]或[0,1]的每个值来过滤y_true。
我有以下概念,我认为不太有效。例如,当在[0,1]上过滤y_true时:
ind_zero = tf.math.equal(y_true,[1,0])
index_zero = tf.math.logical_and(ind_zero[:,0],ind_zero[:,1])
zeros = tf.gather_nd(y_pred,tf.where(index_zero))
是否有其他更有效的方法?
提前感谢。
英文:
I'm sorry for asking such a trivial question, but I'm new to Tensorflow.
I've got two tensors.
y_true = [[1,0], [0,1], [1,0], [1,0], [0,1], [0,1], [1,0], [0,1], [1,0], [0,1]]
y_pred = [[0.6,0.4], [0.3,0.7], [0.8,0.2], [0.8,0.2], [0.3,0.7],[0.1,0.9],[0.9, 0.1],[0.4,0.6],[0.6,0.4],[0.2,0.8]]
Additionally, I want to filter y_true according to each of the [1,0] or [0,1] values.
I had the following concept, which I don't think is very effective. For instance, when filtering y_true on [0,1]:
ind_zero = tf.math.equal(y_true,[1,0])
index_zero = tf.math.logical_and(ind_zero[:,0],ind_zero[:,1])
zeros = tf.gather_nd(y_pred,tf.where(index_zero))
Exists another idea that functions more effectively?
Thanks in advance.
答案1
得分: 1
你可以在[1,0]上筛选y_true:
zeros = tf.gather_nd(y_pred,tf.where(tf.argmin(y_true, axis = 1)))
对于[0,1],使用argmax而不是argmin:
zeros = tf.gather_nd(y_pred,tf.where(tf.argmax(y_true, axis = 1)))
英文:
You could filter y_true on [1,0]:
zeros = tf.gather_nd(y_pred,tf.where(tf.argmin(y_true, axis = 1)))
The same for [0,1] use argmax instead of argmin:
zeros = tf.gather_nd(y_pred,tf.where(tf.argmax(y_true, axis = 1)))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论