Ignore:
Timestamp:
05/29/13 16:19:55 (11 months ago)
Author:
Jure Zbontar <jure.zbontar@…>
Branch:
default
Message:

Add normalize flag for NNs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/classification/neural.py

    r10984 r11559  
    127127class NeuralNetworkLearner(Orange.classification.Learner): 
    128128    """ 
    129     NeuralNetworkLearner uses jzbontar's implementation of neural networks and wraps it in 
    130     an Orange compatible learner.  
    131  
    132     NeuralNetworkLearner supports all types of data and returns a classifier, regression is currently not supported. 
    133  
    134     More information about neural networks can be found at http://en.wikipedia.org/wiki/Artificial_neural_network. 
     129    NeuralNetworkLearner implements a multilayer perceptron. Learning is performed by minimizing an L2-regularized 
     130    cost function with scipy's implementation of L-BFGS. The current implementations is limited to a single 
     131    hidden layer.  
     132 
     133    Regression is currently not supported. 
    135134 
    136135    :param name: learner name. 
     
    138137 
    139138    :param n_mid: Number of nodes in the hidden layer 
    140     :type n_mid: integer 
     139    :type n_mid: int 
    141140 
    142141    :param reg_fact: Regularization factor. 
     
    144143 
    145144    :param max_iter: Maximum number of iterations. 
    146     :type max_iter: integer 
     145    :type max_iter: int 
     146 
     147    :param normalize: Normalize the data prior to learning (subtract each column by the mean and divide by the standard deviation) 
     148    :type normalize: bool 
    147149 
    148150    :rtype: :class:`Orange.multitarget.neural.neuralNetworkLearner` or  
     
    159161            return self(data,weight) 
    160162 
    161     def __init__(self, name="NeuralNetwork", n_mid=10, reg_fact=1, max_iter=1000, rand=None): 
     163    def __init__(self, name="NeuralNetwork", n_mid=10, reg_fact=1, max_iter=300, normalize=True, rand=None): 
    162164        """ 
    163165        Current default values are the same as in the original implementation (neural_networks.py) 
    164166        """ 
    165  
    166167        self.name = name 
    167168        self.n_mid = n_mid 
     
    169170        self.max_iter = max_iter 
    170171        self.rand = rand 
     172        self.normalize = normalize 
    171173 
    172174        if not self.rand: 
     
    193195        X = data.to_numpy()[0]  
    194196 
     197        mean = X.mean(axis=0) 
     198        std = X.std(axis=0) 
     199        if self.normalize: 
     200            X = (X - mean) / std 
     201 
    195202        #converts multi-target or single-target classes to numpy 
    196  
    197  
    198203        if data.domain.class_vars: 
    199204            for cv in data.domain.class_vars: 
     
    228233        self.nn.fit(X,Y) 
    229234                
    230         return NeuralNetworkClassifier(classifier=self.nn.predict, domain = data.domain) 
     235        return NeuralNetworkClassifier(classifier=self.nn.predict, 
     236            domain=data.domain, normalize=self.normalize, mean=mean, std=std) 
    231237 
    232238class NeuralNetworkClassifier(): 
     
    258264        input = np.array([[float(e) for e in example]]) 
    259265 
     266        if self.normalize: 
     267            input = (input - self.mean) / self.std 
     268 
    260269        # transform results from numpy 
    261270        results = self.classifier(input).tolist()[0] 
     271        if len(results) == 1: 
     272            prob_positive = results[0] 
     273            results = [1 - prob_positive, prob_positive] 
    262274        mt_prob = [] 
    263275        mt_value = [] 
     
    295307    global_timer = time.time() 
    296308 
    297     data = Orange.data.Table('iris') 
    298     l1 = NeuralNetworkLearner(n_mid=10, reg_fact=1, max_iter=1000) 
    299     res = Orange.evaluation.testing.cross_validation([l1],data, 3) 
     309    data = Orange.data.Table('wine') 
     310    l1 = NeuralNetworkLearner(n_mid=40, reg_fact=1, max_iter=200) 
     311 
     312#    c1 = l1(data) 
     313#    print c1(data[0], 3), data[0] 
     314 
     315    l2 = Orange.classification.bayes.NaiveLearner() 
     316    res = Orange.evaluation.testing.cross_validation([l1, l2],data, 5) 
    300317    scores = Orange.evaluation.scoring.CA(res) 
    301  
    302318    for i in range(len(scores)): 
    303319        print res.classifierNames[i], scores[i] 
Note: See TracChangeset for help on using the changeset viewer.