簡體   English   中英

如何對張量執行閾值處理

[英]How to perform thresholding on a tensor

以下是我的代碼:

...
result=tf.div(product_norm,denom)
    if(result>0.5):
        result=1
    else:
        result=0
    return result

如果張量中的值小於 0.5,則應將其替換為 0,否則為 1。但它會不斷返回錯誤。

TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.

在這種簡單的情況下,您可以使用

result = tf.cast(result + 0.5, tf.int32)

當 if 語句變得更復雜時,考慮使用tf.cond

最簡單的方法是

result = tf.where(result>0.5, 1,0)

tf.where(condition, x, y)的文檔解釋了會發生什么:

如果還提供了 x 和 y(都具有非 None 值),則條件張量充當掩碼,用於選擇輸出中的相應元素/行是否應取自 x(如果條件中的元素為 True)或 y(如果它是假的)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM