2
我写了一个简单的Python代码来计算一个集合的熵,我试图在Theano中写同样的东西。Theano中的熵和概率
import math
# this computes the probabilities of each element in the set
def prob(values):
return [float(values.count(v))/len(values) for v in values]
# this computes the entropy
def entropy(values):
p = prob(values)
return -sum([v*math.log(v) for v in p])
我试图写在Theno等效代码,但我不知道如何做到这一点:
import theano
import theano.tensor as T
v = T.vector('v') # I create a symbolic vector to represent my initial values
p = T.vector('p') # The same for the probabilities
# this is my attempt to compute the probabilities which would feed vector p
theano.scan(fn=prob,outputs_info=p,non_sequences=v,n_steps=len(values))
# considering the previous step would work, the entropy is just
e = -T.sum(p*T.log(p))
entropy = theano.function([values],e)
然而,扫描线是不正确的,我得到吨的错误。我不确定是否有简单的方法来实现它(计算矢量的熵),还是必须在扫描功能上付出更多的努力。有任何想法吗?
Theano无法在列表上进行计算。您必须更新您的代码才能使用ndarray。首先只用numpy来做到这一点。这应该已经加快你的代码。 – nouiz