以下命令是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'
答案 0 :(得分:0)
您不能将abs()应用于列表,但应用于列表的每个元素:
cost+=T.sum([abs(p) for p in params])