简体   繁体   中英

Changing values in a theano tensor vector

I have a theano tensor vector next_probs something like [0.222, 0.34342, 0.41324, 0.1231, ...] , which is the output of the following function:

next_probs = tensor.nnet.softmax(logit)

logit is a vector which has the same dimension as next_probs .

How can I change one specific value to 1 , and the others to 0 in next_probs vector?

What value specifically you want to change? if you just want have a vector which have same dimension as next_probs you can use zeros and set_subtensor like below

ret = T.zeros(next_probs.shape)
ret = T.set_subtensor(ret[index],1)

If you want to use this in classification model, and you need the class with highest probability in next_probs become 1 and the others become 0 this is my other answer:

ret = T.zeros(next_probs.shape)
ret = T.set_subtensor(ret[T.argmax(next_probs)],1)

ret is a vector with 1 in the class with highest probability, and 0 for the others

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM