[英]Filtering tensors element in Tensorflow
Tensorflow 中的等效操作是什么? 例如,我有一個x = np.array([-12,4,6,8,100])
。 我想做這樣簡單的事情: x = x[x>5]
,但我找不到任何 TF 操作。 謝謝!
在TF
中,您可以執行類似的操作來獲得類似的結果。
import numpy as np
import tensorflow as tf
x = np.array([-12,4,6,8,100])
y = tf.gather(x, tf.where(x > 5))
y.numpy().reshape(-1)
array([ 6, 8, 100])
細節
tf.where
將返回condition
索引為True
。 如
x = np.array([-12,4,6,8,100])
tf.where(x > 5)
<tf.Tensor: shape=(3, 1), dtype=int64, numpy=
array([[2],
[3],
[4]])>
接下來,使用tf.gather
,它根據索引(來自tf.where
)從參數( x
)軸切片。 如
tf.gather(x, tf.where(x > 5))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.