2015-01-07 78 views
3

Theano中的NumPy的a[a < 0] = 0的等效物(張量變量)是什麼? 我希望我的所有矩陣元素都小於等於零的數字。什麼等於Theano中的[a <0] = 0?

+0

文檔中的相關位置似乎http://deeplearning.net/software/theano/library/tensor/basic.html#indexing –

回答

5

這項工作:

import theano 
a=theano.tensor.matrix() 
idxs=(a<0).nonzero() 
new_a=theano.tensor.set_subtensor(a[idxs], 0) 

不要忘記,Theano是一個象徵性的語言。所以變量a在用戶圖中沒有改變。這是新變量new_a包含新值並且仍舊具有舊值。

Theano將優化這個如果可能的話就地工作。

+1

感謝。並且還與「a = T.where(a <0,0,a)」一起工作 –

1

這也適用,並且還可以添加上限限制

import theano 
import theano.tensor as T 
a = T.matrix() 
b = a.clip(0.0) 

或者如果你想上限,以及,你可能想嘗試:

b = T.clip(a, 0.0, 1.0) 

其中1.0的地方一個人想設置上限。

檢查文件here

相關問題