2

Ich möchte logistische Regression mit Minibatches auf MNIST mit Theano implementieren. Ich möchte verschiedene Optimierer ausprobieren, also entschied ich mich für eine Bibliothek namens climin. Climin bietet mehrere Funktionen, die die Parameter des Modells, Verlust und/oder die Steigung des Verlustes, und einen Strom der Daten als Parameter erhalten. Da dieano-Funktionen kompiliert werden müssen, habe ich das folgende Modell entworfen.Logistische Regression in Theano mit Climin implementieren

# loads the MNIST dataset 
train_set_x, train_set_y = load_data(dataset)[0] 

print('... building the model') 

W = theano.shared(
     value = np.zeros((28 * 28, 10), dtype = theano.config.floatX), 
     name = 'W', 
     borrow = True 
     ) # weights dimension 28 * 28, 10 

b = theano.shared(
     value = np.zeros((10,), dtype = theano.config.floatX), 
     name = 'b', 
     borrow = True 
     ) # biases 

x = T.matrix('x') # data, represented as rasterized images dimension 28 * 28 
y = T.ivector('y') # labes, represented as 1D vector of [int] labels dimension 10 

p_y_given_x    = T.nnet.softmax(T.dot(x, W) + b) 
y_pred     = T.argmax(p_y_given_x, axis=1) 

NLL = -T.sum(T.log(p_y_given_x)[T.arange(y.shape[0]), y]) 

# negative_log_likelihood = -T.mean(T.log(y_pred)[T.arange(y.shape[0]), y]) 

loss = theano.function(inputs = [ x, y ], outputs = NLL) 

g_W = theano.function(inputs = [ x, y ], outputs = T.grad(cost=loss, wrt=W)) 
g_b = theano.function(inputs = [ x, y ], outputs = T.grad(cost=loss, wrt=b)) 

def d_loss_wrt_pars(parameters, inputs, targets): 
    # climin passes the parametes 
    # as 1-D concatinated array to 
    # this function so I need to 
    # unpack the parameter array 
    W = parameters[:28 * 28 * 10].reshape((28 * 28, 10)) 
    b = parameters[28 * 28 * 10:].reshape((1, 10)) 

    return np.concatinate([g_W(x, y).flatten(), g_b(x, y)]) 

wrt = np.empty(7850) # allocate space for the parameters (MNIST dimensionality 28 * 28 * 10) 
cli.initialize.randomize_normal(wrt, 0, 1) # initialize the parameters with random numbers 

if batch_size is None: 
    args = itertools.repeat(([train_set_x, train_set_y], {})) 
    batches_per_pass = 1 
else: 
    args = cli.util.iter_minibatches([train_set_x, train_set_y], batch_size, [0, 0]) 
    args = ((i, {}) for i in args) 
    batches_per_pass = train_set_x.shape[0]/batch_size 

if optimizer == 'gd': 
    opt = cli.GradientDescent(wrt, d_loss_wrt_pars, step_rate=0.1, momentum=.95, args=args) 
else: 
    print('unknown optimizer') 
    return 1 

print('... training the model') 

for info in opt: 
    if info['n_iter'] >= n_epochs and (not done_looping): 
     break 

Leider erzeugt dies nichts anderes als:

Traceback (most recent call last): 
File "logreg/logistic_sgd.py", line 160, in <module> sys.exit(sgd_optimization_mnist()) 
File "logreg/logistic_sgd.py", line 69, in sgd_optimization_mnist 
g_W = theano.function(inputs = [ x, y ], outputs = T.grad(cost=loss, wrt=W)) 
File "/Users/romancpodolski/anaconda/lib/python2.7/site-packages/theano/gradient.py", line 430, in grad 
if cost is not None and isinstance(cost.type, NullType): 

Attribute: ‚Funktion‘ Objekt kein Attribut ‚type‘

jemand eine Idee, wie diese Arbeit zu machen, und die beiden Bibliotheken zu kombinieren?

Antwort

2

Ihr Code übergibt eine kompilierte Funktion (anstelle eines Theano-Ausdrucks) an T.grad. Ersetzen loss mit NLL und Sie sollten

g_W = theano.function(inputs = [ x, y ], outputs = T.grad(cost=NLL, wrt=W)) 
g_b = theano.function(inputs = [ x, y ], outputs = T.grad(cost=NLL, wrt=b)) 

auch in Ordnung sein, möchten Sie vielleicht eine Theano tragende Bibliothek, um zu versuchen (z Lasagne) für die Optimierung.

Verwandte Themen