-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Neural Network widget #2553
Merged
Merged
Neural Network widget #2553
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import re | ||
import sys | ||
|
||
from AnyQt.QtWidgets import QApplication | ||
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"] | ||
act_lbl = ["Identity", "Logistic", "tanh", "ReLu"] | ||
solver = ["lbfgs", "sgd", "adam"] | ||
solv_lbl = ["L-BFGS-B", "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. Length of list " | ||
"defines the number of layers. E.g. 4, 2, 2, 3.", | ||
placeholderText="e.g. 100,") | ||
self.activation_combo = gui.comboBox( | ||
box, self, "activation_index", orientation=Qt.Horizontal, | ||
label="Activation:", items=[i for i in self.act_lbl], | ||
callback=self.settings_changed) | ||
self.solver_combo = gui.comboBox( | ||
box, self, "solver_index", orientation=Qt.Horizontal, | ||
label="Solver:", items=[i for i in self.solv_lbl], | ||
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, | ||
alignment=Qt.AlignRight, 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", ', '.join(map(str, self.get_hidden_layers()))), | ||
("Activation", self.act_lbl[self.activation_index]), | ||
("Solver", self.solv_lbl[self.solver_index]), | ||
("Alpha", self.alpha), | ||
("Max iterations", self.max_iterations)) | ||
|
||
def get_hidden_layers(self): | ||
layers = tuple(map(int, re.findall(r'\d+', self.hidden_layers_input))) | ||
if not layers: | ||
layers = (100,) | ||
self.hidden_layers_edit.setText("100,") | ||
return layers | ||
|
||
|
||
if __name__ == "__main__": | ||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+178 KB
doc/visual-programming/source/widgets/model/images/NN-Example-Predict.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+88.9 KB
doc/visual-programming/source/widgets/model/images/NN-Example-Test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.4 KB
doc/visual-programming/source/widgets/model/images/NeuralNetwork-stamped.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions
69
doc/visual-programming/source/widgets/model/neuralnetwork.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
Neural Network | ||
============== | ||
|
||
.. figure:: icons/nn.png | ||
|
||
A multi-layer perceptron (MLP) algorithm with backpropagation. | ||
|
||
Signals | ||
------- | ||
|
||
**Inputs**: | ||
|
||
- **Data** | ||
|
||
A data set | ||
|
||
- **Preprocessor** | ||
|
||
Preprocessing method(s) | ||
|
||
**Outputs**: | ||
|
||
- **Learner** | ||
|
||
A MLP learning algorithm with settings as specified in the dialog. | ||
|
||
- **Model** | ||
|
||
A trained model. Output signal sent only if input *Data* is present. | ||
|
||
Description | ||
----------- | ||
|
||
The **Neural Network** widget uses sklearn's `Multi\-layer Perceptron algorithm <http://scikit-learn.org/stable/modules/neural_networks_supervised.html>`_ that can learn non-linear models as well as linear. | ||
|
||
.. figure:: images/NeuralNetwork-stamped.png | ||
|
||
1. A name under which it will appear in other widgets. The default name is | ||
"Neural Network". | ||
2. Set model parameters: | ||
- Neurons per hidden layer: defined as the ith element represents the number of neurons in the ith hidden layer. E.g. a neural network with 3 layers can be defined as 2, 3, 2. | ||
- Activation function for the hidden layer: | ||
- Identity: no-op activation, useful to implement linear bottleneck | ||
- Logistic: the logistic sigmoid function | ||
- tanh: the hyperbolic tan function | ||
- ReLu: the rectified linear unit function | ||
- Solver for weight optimization: | ||
- L-BFGS-B: an optimizer in the family of quasi-Newton methods | ||
- SGD: stochastic gradient descent | ||
- Adam: stochastic gradient-based optimizer | ||
- Alpha: L2 penalty (regularization term) parameter | ||
- Max iterations: maximum number of iterations | ||
|
||
Other parameters are set to `sklearn's defaults <http://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html>`_. | ||
|
||
3. Produce a report. | ||
4. When the box is ticked (*Apply Automatically*), the widget will | ||
communicate changes automatically. Alternatively, click *Apply*. | ||
|
||
Examples | ||
-------- | ||
|
||
The first example is a classification task on *iris* data set. We compare the results of **Neural Network** with the :doc:`Logistic Regression <../model/logisticregression>`. | ||
|
||
.. figure:: images/NN-Example-Test.png | ||
|
||
The second example is a prediction task, still using the *iris* data. This workflow shows how to use the *Learner* output. We input the **Neural Network** prediction model into :doc:`Predictions <../evaluation/predictions>` and observe the predicted values. | ||
|
||
.. figure:: images/NN-Example-Predict.png |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right-align.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean Qt.AlignRight as parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Umm, yes add,
alignment=Qt.AlignRight
to the last spinbox. Numbers usually look better if aligned to the right. (Neurons per hidden layer is different.)