2015-09-26 26 views
1

我有一個theano張量,我想剪輯它的值,但是每個索引的範圍不同。例如,如果我有一個向量[a,b,c],我想剪輯a到[0,1],剪輯b到[2,3]和c到[3,5]。張量的剪輯部分

我該如何有效地做到這一點? 謝謝!

回答

1

theano.tensor.clip操作支持符號的最小值和最大值,因此您可以傳遞三個張量,所有相同的形狀,並且它將執行第一個元素相對於第二個(最小)和第三個(最大值)。

此代碼顯示此主題的兩個變體。 v1要求將最小值和最大值作爲單獨的向量傳遞,而v2允許最小值和最大值更像傳遞對列表那樣傳遞,表示爲兩列矩陣。

import theano 
import theano.tensor as tt 


def v1(): 
    x = tt.vector() 
    min_x = tt.vector() 
    max_x = tt.vector() 
    y = tt.clip(x, min_x, max_x) 
    f = theano.function([x, min_x, max_x], outputs=y) 
    print f([2, 1, 4], [0, 2, 3], [1, 3, 5]) 


def v2(): 
    x = tt.vector() 
    min_max = tt.matrix() 
    y = tt.clip(x, min_max[:, 0], min_max[:, 1]) 
    f = theano.function([x, min_max], outputs=y) 
    print f([2, 1, 4], [[0, 1], [2, 3], [3, 5]]) 


def main(): 
    v1() 
    v2() 


main()