我编写了一个简单的 python 代码来计算集合的熵,并且我正在尝试在 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)
但是,扫描线不正确,我收到大量错误。我不确定是否有一种简单的方法可以做到这一点(计算向量的熵),或者我是否必须在扫描功能上投入更多精力。有任何想法吗?