Skip to main content
 首页 » 编程设计

python之theano 中的负对数似然(cox 回归)

2025年05月04日17jyk

我正在尝试在 theano 中实现 cox 回归。

我使用逻辑回归教程 (http://deeplearning.net/tutorial/logreg.html) 作为框架,并将逻辑对数似然 (LL) 函数替换为 cox 回归 LL 函数 (https://en.wikipedia.org/wiki/Proportional_hazards_model#The_partial_likelihood)。

这是我目前所拥有的:

class CoxRegression(object): 
def __init__(self, x, n_in): 
    self.W = theano.shared(value=numpy.zeros(n_in,dtype=theano.config.floatX), name='W',borrow=True) 
    self.b = theano.shared(numpy.cast['float64'](0), borrow=True) 
    self.theta = T.dot(x, self.W) + self.b 
    self.exp_theta = T.exp(self.theta) 
    self.params = [self.W, self.b] 
    self.x = x 
 
def negative_log_likelihood(self, ytime, ystatus): 
    LL_i = T.switch(T.eq(ystatus[i],1), self.theta - T.log(T.sum(self.exp_theta * T.gt(ytime, ytime[i]))),0) 

基本上,我需要对 LL_i(其中 i 是 0 到 ytime.shape - 1)求和。但我不确定该怎么做。我应该使用扫描功能吗?

请您参考如下方法:

想通了。诀窍不是使用扫描函数,而是将双重求和转换为纯矩阵运算。