选择具有精确数值的2D张量的索引。

huangapple go评论50阅读模式
英文:

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)))

huangapple
  • 本文由 发表于 2023年2月6日 19:32:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定