# ====================================================================== def ffnet(indim, outdim): outdim = int(outdim) l_in = lasagne.layers.InputLayer(shape=(None,) + indim) l_in = lasagne.layers.FlattenLayer(l_in) l_in = lasagne.layers.DropoutLayer(l_in, p=0.3) l_hid = lasagne.layers.DenseLayer(l_in, num_units=256, nonlinearity=lasagne.nonlinearities.rectify) l_hid = lasagne.layers.DropoutLayer(l_hid, p=0.3) return lasagne.layers.DenseLayer(l_hid, num_units=outdim, nonlinearity=lasagne.nonlinearities.softmax) # ====================================================================== # Load data # ====================================================================== m = dnntoolkit.model('tmp/tmp.ai') m.set_model(ffnet, api='lasagne', indim=ds['X_train'].shape[1:], outdim=ds['y_train'].shape[1]) net = m.create_model() y = tensor.matrix(name='y', dtype=theano.config.floatX) input_var = [l.input_var for l in lasagne.layers.find_layers(net, types=lasagne.layers.InputLayer)] dropout = lasagne.layers.find_layers(net, types=lasagne.layers.DropoutLayer) # ====== Create prediction ====== # y_pred_deter = lasagne.layers.get_output(net, deterministic=True) f_pred = theano.function( inputs=input_var, outputs=y_pred_deter, allow_input_downcast=True
l_in = lasagne.layers.DropoutLayer(l_in, p=0.3) l_hid = lasagne.layers.DenseLayer(l_in, num_units=128) l_hid = lasagne.layers.DropoutLayer(l_hid, p=0.3) l_out = lasagne.layers.DenseLayer(l_hid, num_units=10, nonlinearity=lasagne.nonlinearities.softmax) return l_out # =========================================================================== # Test # =========================================================================== ds = dnntoolkit.dataset.load_mnist() print(ds) m = dnntoolkit.model('/volumes/backup/model/mnist.ai') m.set_model(test, 'lasagne') ai = m.create_model() input_var = [i.input_var for i in lasagne.layers.find_layers(ai, types=lasagne.layers.InputLayer)] y = tensor.matrix('y') y_pred_stoch = lasagne.layers.get_output(ai, deterministic=False) y_pred_deter = lasagne.layers.get_output(ai, deterministic=True) cost_monitor = lasagne.objectives.categorical_accuracy(y_pred_deter, y).mean() cost_train = lasagne.objectives.categorical_crossentropy(y_pred_stoch, y).mean() regu = lasagne.regularization.L1L2(l2=dnntoolkit.dnn.calc_weights_decay(m.get_nweights())) cost_regu = regu.apply_network(ai) cost_train += cost_regu