Skip to content

Commit

Permalink
Neural Network widget
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdapretnar committed Aug 30, 2017
1 parent 5e65852 commit 8998523
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Orange/tests/test_neuralnetwork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring

import unittest

from Orange.data import Table
from Orange.modelling import NNLearner, ConstantLearner
from Orange.evaluation import CA, CrossValidation, MSE


class TestKNNLearner(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.iris = Table('iris')
cls.housing = Table('housing')
cls.learner = NNLearner()

def test_NeuralNetwork(self):
results = CrossValidation(self.iris, [self.learner], k=3)
ca = CA(results)
self.assertGreater(ca, 0.8)
self.assertLess(ca, 0.99)

def test_NeuralNetwork_regression(self):
const = ConstantLearner()
results = CrossValidation(self.housing, [self.learner, const], k=3)
mse = MSE(results)
self.assertLess(mse[0], mse[1])
48 changes: 48 additions & 0 deletions Orange/widgets/model/icons/NN.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 94 additions & 0 deletions Orange/widgets/model/owneuralnetwork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import re
from AnyQt.QtCore import Qt

from Orange.data import Table
from Orange.modelling import NNLearner
from Orange.widgets import gui
from Orange.widgets.settings import Setting
from Orange.widgets.utils.owlearnerwidget import OWBaseLearner


class OWNNLearner(OWBaseLearner):
name = "Neural Network"
description = "A multi-layer perceptron (MLP) algorithm with " \
"backpropagation."
icon = "icons/NN.svg"
priority = 90

LEARNER = NNLearner

activation = ["identity", "logistic", "tanh", "relu"]
solver = ["lbfgs", "sgd", "adam"]

learner_name = Setting("Neural Network")
hidden_layers_input = Setting("100")
activation_index = Setting(3)
solver_index = Setting(2)
alpha = Setting(0.0001)
max_iterations = Setting(200)

def add_main_layout(self):
box = gui.vBox(self.controlArea, "Network")
self.hidden_layers_edit = gui.lineEdit(box, self,
"hidden_layers_input",
label="Neurons per hidden "
"layer:",
orientation=Qt.Horizontal,
callback=self.settings_changed,
tooltip="A list of integers "
"defining neurons. "
"Lenght of list "
"defines number of "
"layers. E.g. 4, 2, 2, 3.")
self.activation_combo = gui.comboBox(
box, self, "activation_index", orientation=Qt.Horizontal,
label="Activation:", items=[i.upper() for i in self.activation],
callback=self.settings_changed)
self.solver_combo = gui.comboBox(
box, self, "solver_index", orientation=Qt.Horizontal,
label="Solver:", items=[i.upper() for i in self.solver],
callback=self.settings_changed)
self.alpha_spin = gui.doubleSpin(
box, self, "alpha", 1e-5, 1.0, 1e-2,
label="Alpha:", decimals=5, alignment=Qt.AlignRight,
callback=self.settings_changed, controlWidth=80)
self.max_iter_spin = gui.spin(
box, self, "max_iterations", 10, 300, step=10,
label="Max iterations:", orientation=Qt.Horizontal,
callback=self.settings_changed, controlWidth=80)

def create_learner(self):
return self.LEARNER(
hidden_layer_sizes=self.get_hidden_layers(),
activation=self.activation[self.activation_index],
solver=self.solver[self.solver_index],
alpha=self.alpha,
max_iter=self.max_iterations,
preprocessors=self.preprocessors)

def get_learner_parameters(self):
return (("Hidden layers", self.get_hidden_layers()),
("Activation", self.activation[self.activation_index].capitalize()),
("Solver", self.solver[self.solver_index].capitalize()),
("Alpha", self.alpha),
("Max iterations", self.max_iterations))

def get_hidden_layers(self):
layers = tuple(map(int, re.findall('\d+', self.hidden_layers_input)))
if not layers:
layers = (100,)
self.hidden_layers_edit.setText("100")
return layers


if __name__ == "__main__":
import sys
from AnyQt.QtWidgets import QApplication

a = QApplication(sys.argv)
ow = OWNNLearner()
d = Table(sys.argv[1] if len(sys.argv) > 1 else 'iris')
ow.set_data(d)
ow.show()
a.exec_()
ow.saveSettings()
9 changes: 9 additions & 0 deletions Orange/widgets/model/tests/test_owneuralnetwork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from Orange.widgets.model.owneuralnetwork import OWNNLearner
from Orange.widgets.tests.base import WidgetTest, WidgetLearnerTestMixin


class TestOWNeuralNetwork(WidgetTest, WidgetLearnerTestMixin):
def setUp(self):
self.widget = self.create_widget(OWNNLearner,
stored_settings={"auto_apply": False})
self.init()

0 comments on commit 8998523

Please sign in to comment.