簡體   English   中英

向crossentropy()函數添加l1或l2正則化

[英]Add l1 or l2 regularization to crossentropy() function

以下命令是5_convolutional_net.py中的交叉熵函數。 我想向此成本函數添加L1或L2正則化。 我不知道為什么TypeError: bad operand type for abs(): 'list'

def RMSprop(cost, params, lr=0.001, rho=0.9, epsilon=1e-6):
grads = T.grad(cost=cost, wrt=params)
updates = []
for p, g in zip(params, grads):
    acc = theano.shared(p.get_value() * 0.)
    acc_new = rho * acc + (1 - rho) * g ** 2
    gradient_scaling = T.sqrt(acc_new + epsilon)
    g = g / gradient_scaling
    updates.append((acc, acc_new))
    updates.append((p, p - lr * g))
return updates

cost = T.mean(T.nnet.categorical_crossentropy(noise_py_x, Y)) params = [w, w2, w3, w4, w_o] updates = RMSprop(cost, params, lr=0.001)

一旦使用cost+=T.sum(abs(params))它就會給我TypeError: bad operand type for abs(): 'list'

您不能將abs()應用於列表,而是應用於列表的每個元素:

cost+=T.sum([abs(p) for p in params])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM