2016-07-06 18 views
0

Der folgende Befehl ist die Kreuz Entropie-Funktion in 5_convolutional_net.py. Ich möchte dieser Kostenfunktion eine L1- oder L2-Regularisierung hinzufügen. Ich weiß nicht, warum die TypeError: bad operand type for abs(): 'list' auftaucht?Hinzufügen l1 oder l2 Regularisierung zu crossentropy() - Funktion

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)

Einmal verwendet cost+=T.sum(abs(params)) es gibt mir TypeError: bad operand type for abs(): 'list'

Antwort

0

Sie können keine abs() auf eine Liste anwenden, sondern auf jedes Element einer Liste:

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

'*** AsTensorError: ('Elemwise {abs_, no_inplace} .0, Elemwise {abs_, no_inplace} .0, Elemwise {abs_, no_inplace} .0, Ele mwise {abs_, no_inplace} .0, Elemwise {abs_, no_inplace} .0] zu TensorType ', ) ' –

Verwandte Themen