Theano 损失函数中的对数行列式

Log Determinant in Theano Loss Function

我正在使用 Theano(python 深度学习包),但我对它很陌生,我 运行 遇到了损失函数中的一个术语的问题。该术语涉及对矩阵的行列式取对数;矩阵是我网络中隐藏单元层的函数。 我导入 Tensor,然后 Tensor.nlinalg:

import theano
import theano.tensor as T
import theano.tensor.nlinalg as Tnlinalg

然后在我的损失函数中加入这个术语:

my_mat_det = Tnlinalg.Det(computed_matrix)
log_det_term = -T.log(my_mat_det)

但是当我尝试训练它时,我得到以下异常和回溯:

File "/SdaModule.py", line 88, in __init__
  log_det_term = -T.log(my_mat_det)
File "/home/username/anaconda/lib/python2.7/site-packages/theano/gof/op.py", line 481, in __call__
  node = self.make_node(*inputs, **kwargs)
File "/home/username/anaconda/lib/python2.7/site-packages/theano/tensor/elemwise.py", line 527, in make_node
  inputs = map(as_tensor_variable, inputs)
File "/home/username/anaconda/lib/python2.7/site-packages/theano/tensor/basic.py", line 202, in as_tensor_variable
  raise AsTensorError("Cannot convert %s to TensorType" % str_x, type(x))
theano.tensor.var.AsTensorError: ('Cannot convert Det to TensorType', <class 'theano.tensor.nlinalg.Det'>)

谁能给点建议? 干杯, 麦克

linear algebra package中的

theano.tensor.nlinalg.Det是运算class,不是运算函数。您需要首先初始化 class 的实例,然后将其应用于代表矩阵的节点。例如,

import numpy

import theano
import theano.tensor.nlinalg

x = theano.tensor.matrix('x', dtype=theano.config.floatX)
p = theano.shared(numpy.array([[2, 0], [0, 3]], dtype=theano.config.floatX))
y = theano.dot(x, p)
c = theano.tensor.log(theano.tensor.nlinalg.Det()(y))
g = theano.grad(c, x)

print theano.printing.pp(g)

注意 theano.tensor.nlinalg.Det()(y)theano.tensor.nlinalg.Det(y) 的区别。