Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
minor changes in comments, added TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
arogozhnikov committed Apr 30, 2015
1 parent 3b0de88 commit fa8ee05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
17 changes: 10 additions & 7 deletions cmsuml/nnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -214,17 +217,17 @@ 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.
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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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_
Expand All @@ -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')
Expand Down
11 changes: 6 additions & 5 deletions tests/test_nnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


0 comments on commit fa8ee05

Please sign in to comment.