Train a neural network classifier.

    Parameters
    ----------
    X : numpy array
        A list of feature vectors
    y : numpy array
        A list of labels

    Returns
    -------
    Theano expression :
        The trained neural network

def train_nn_segmentation_classifier(X, y):
    """
    Train a neural network classifier.

    Parameters
    ----------
    X : numpy array
        A list of feature vectors
    y : numpy array
        A list of labels

    Returns
    -------
    Theano expression :
        The trained neural network
    """

    def build_mlp(input_var=None):
        n_classes = 2
        # First, construct an input layer. The shape parameter defines the
        # expected input shape, which is just the shape of our data matrix X.
        l_in = lasagne.layers.InputLayer(shape=X.shape,
                                         input_var=input_var)
        # A dense layer implements a linear mix (xW + b) followed by a
        # nonlinear function.
        hiddens = [64, 64, 64]  # sollte besser als 0.12 sein (mit [32])
        layers = [l_in]

        for n_units in hiddens:
            l_hidden_1 = lasagne.layers.DenseLayer(
                layers[-1],  # The first argument is the input to this layer
                num_units=n_units,  # the layer's output dimensionality
                nonlinearity=lasagne.nonlinearities.tanh)
            layers.append(l_hidden_1)
        # For our output layer, we'll use a dense layer with a softmax
        # nonlinearity.
        l_output = lasagne.layers.DenseLayer(layers[-1], num_units=n_classes,
                                             nonlinearity=softmax)
        return l_output

    # Batch iterator, Copied directly from the Lasagne example.
    def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
        """
        This is just a simple helper function iterating over training data in
        mini-batches of a particular size, optionally in random order. It
        assumes data is available as numpy arrays. For big datasets, you could
        load numpy arrays as memory-mapped files (np.load(..., mmap_mode='r')),
        or write your own custom data iteration function. For small datasets,
        you can also copy them to GPU at once for slightly improved
        performance. This would involve several changes in the main program,
        though, and is not demonstrated here.
        """
        assert len(inputs) == len(targets)
        if shuffle:
            indices = np.arange(len(inputs))
            np.random.shuffle(indices)
        for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
            if shuffle:
                excerpt = indices[start_idx:start_idx + batchsize]
            else:
                excerpt = slice(start_idx, start_idx + batchsize)
            yield inputs[excerpt], targets[excerpt]

    input_var = T.matrix('inputs')
    target_var = T.ivector('targets')
    network = build_mlp(input_var)
    num_epochs = 7

    # We reserve the last 100 training examples for validation.
    X_train, X_val = X[:-100], X[-100:]
    y_train, y_val = y[:-100], y[-100:]

    # Create a loss expression for training, i.e., a scalar objective we want
    # to minimize (for our multi-class problem, it is the cross-entropy loss):
    prediction = lasagne.layers.get_output(network)
    loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
    loss = loss.mean()
    # We could add some weight decay as well here, see lasagne.regularization.

    # Create update expressions for training, i.e., how to modify the
    # parameters at each training step. Here, we'll use Stochastic Gradient
    # Descent (SGD) with Nesterov momentum, but Lasagne offers plenty more.
    params = lasagne.layers.get_all_params(network, trainable=True)
    updates = lasagne.updates.nesterov_momentum(loss,
                                                params,
                                                learning_rate=0.01,
                                                momentum=0.9)

    # Create a loss expression for validation/testing. The crucial difference
    # here is that we do a deterministic forward pass through the network,
    # disabling dropout layers.
    test_prediction = lasagne.layers.get_output(network, deterministic=True)

    # Compile a function performing a training step on a mini-batch (by giving
    # the updates dictionary) and returning the corresponding training loss:
    train_fn = theano.function([input_var, target_var], loss, updates=updates)

    # Finally, launch the training loop.
    print("Starting training...")
    # We iterate over epochs:
    for epoch in range(num_epochs):
        # In each epoch, we do a full pass over the training data:
        train_err = 0
        train_batches = 0
        start_time = time.time()
        for batch in iterate_minibatches(X_train, y_train, 20, shuffle=True):
            inputs, targets = batch
            train_err += train_fn(inputs, targets)
            train_batches += 1

        # Then we print the results for this epoch:
        print("Epoch {0} of {1} took {2:.3f}s".format(
              epoch + 1, num_epochs, time.time() - start_time))
        print("  training loss:\t\t{0:.6f}".format(train_err / train_batches))

    predict_fn = theano.function([input_var],
                                 test_prediction)
    return predict_fn