From fa8ee05e088d8131261d7eca72fd3c0e59c080f6 Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 30 Apr 2015 15:45:32 +0300 Subject: [PATCH] minor changes in comments, added TODOs --- cmsuml/nnet.py | 17 ++++++++++------- tests/test_nnet.py | 11 ++++++----- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cmsuml/nnet.py b/cmsuml/nnet.py index de03013..c8885a0 100755 --- a/cmsuml/nnet.py +++ b/cmsuml/nnet.py @@ -204,6 +204,9 @@ def adadelta_trainer(x, y, w, parameters, derivatives, loss, stages=1000, decay_ # TODO think of dropper and noises +# TODO add scaler +# TODO yield system + def normalize_weight(y, sample_weight): """ guarantees that sample weight is numpy.array of shape [n_samples], @@ -214,7 +217,7 @@ def normalize_weight(y, sample_weight): class AbstractNeuralNetworkClassifier(BaseEstimator, ClassifierMixin): - def __init__(self, layers=None, loss=log_loss, trainer='irprop-', trainer_parameters=None, random_state=None): + def __init__(self, layers=None, loss='log_loss', trainer='irprop-', trainer_parameters=None, random_state=None): """ Constructs the neural network based on Theano (for classification purposes). Supports only binary classification, supports weights, which makes it usable in boosting. @@ -222,9 +225,9 @@ def __init__(self, layers=None, loss=log_loss, trainer='irprop-', trainer_parame Works in sklearn fit-predict way: X is [n_samples, n_features], y is [n_samples], sample_weight is [n_samples]. Works as usual sklearn classifier, can be used in boosting, for instance, pickled, etc. :param layers: list of int, e.g [9, 7] - the number of units in each *hidden* layer - :param loss: loss function used (log_loss by default) - :param trainer: string, describes the method - :param trainer_parameters: parameters passed to trainer function (learning_rate, etc., trainer-specific). + :param loss: loss function used (log_loss by default), str ot function(y, pred, w) -> float + :param trainer: string, name of optimization method used + :param dict trainer_parameters: parameters passed to trainer function (learning_rate, etc., trainer-specific). """ self.layers = layers self.loss = loss @@ -362,7 +365,7 @@ def prepare(self): n1, n2, n3 = self.layers_ W1 = self._create_shared_matrix('W1', n1, n2) W2 = self._create_shared_matrix('W2', n2, n3) - # this parameter is responsible for scaling, it is computed as well + # this parameter is responsible for scaling, it is optimised too G = theano.shared(value=0.1, name='G') self.parameters['G'] = G @@ -406,7 +409,7 @@ def activation(input): class PairwiseSoftplusNeuralNetwork(AbstractNeuralNetworkClassifier): - """The result is computed as h = sigmoid(Ax), output = sum_{ij} B_ij h_i (1 - h_j) """ + """The result is computed as h = softplus(Ax), output = sum_{ij} B_ij h_i (1 - h_j) """ def prepare(self): n1, n2, n3 = self.layers_ @@ -426,7 +429,7 @@ class ObliviousNeuralNetwork(AbstractNeuralNetworkClassifier): """ Uses idea of oblivious trees, but not strict cuts on features and not rectangular cuts, but linear conditions """ - # TODO: needs pretraining, like oblivious tree first + # TODO: needs pretraining (i.e. by oblivious tree) first def prepare(self): n1, n2, n3 = self.layers_ W1 = theano.shared(value=self.random_state.normal(size=[n1, n2]).astype(floatX), name='W1') diff --git a/tests/test_nnet.py b/tests/test_nnet.py index e6a3c45..afc40d3 100644 --- a/tests/test_nnet.py +++ b/tests/test_nnet.py @@ -38,16 +38,17 @@ def test_nnet(n_samples=200, n_features=5, distance=0.5): assert 0 == 1 -test_nnet() - def test_oblivious(n_samples=200, n_features=5, distance=0.5): # Since oblivious NN is very slow, it is tested separately - nn = nnet.ObliviousNeuralNetwork(layers=[n_features, 3, 1], trainer='irprop-') - X, y = make_blobs(n_samples=n_samples, n_features=5, centers=[numpy.ones(n_features) * distance, - - numpy.ones(n_features) * distance]) + nn = nnet.ObliviousNeuralNetwork(layers=[3], trainer='irprop-') + X, y = make_blobs(n_samples=n_samples, n_features=5, + centers=[numpy.ones(n_features) * distance, - numpy.ones(n_features) * distance]) nn.fit(X, y, batch=100, verbose=10) print(roc_auc_score(y, nn.predict_proba(X)[:, 1]), nn) +test_oblivious() +test_nnet() +