2016-12-20 8 views
0

theano.tensor.nnet.softmax를 수행하기 전에 마스크를 적용 할 수 있는지 궁금합니다. theano의 masked softmax

내가 찾고 있어요 동작입니다 : am이 행렬 것을

>>>a = np.array([[1,2,3,4]]) 
>>>m = np.array([[1,0,1,0]]) # ignore index 1 and 3 
>>>theano.tensor.nnet.softmax(a,m) 
array([[ 0.11920292, 0. , 0.88079708, 0. ]]) 

주, 그래서 전체 매트릭스에 대한 작업과 함께이 softmax를 좋아하고 행 방향 마스크 softmax를 수행합니다.

또한 출력은 a과 동일한 모양이어야하므로 솔루션에서 고급 색인 생성을 수행 할 수 없습니다. theano.tensor.softmax(a[0,[0,2]])

답변

0
def masked_softmax(a, m, axis): 
    e_a = T.exp(a) 
    masked_e = e_a * m 
    sum_masked_e = T.sum(masked_e, axis, keepdims=True) 
    return masked_e/sum_masked_e 
0

theano.tensor.switch이 방법 중 하나입니다. 당신은 다음과 같은 작업을 수행 할 수 계산 그래프에서

:

a_mask = theano.tensor.switch(m, a, np.NINF) 
sm = theano.tensor.softmax(a_mask) 

은 다른 사람을 도움이되기를 바랍니다.