Created
September 14, 2015 08:17
-
-
Save dnouri/fe855653e9757e1ce8c4 to your computer and use it in GitHub Desktop.
nolearn.lasagne.NeuralNet toy examples for classification and regression
This file contains hidden or 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
# Toy examples that demonstrate how to configure | |
# nolearn.lasagne.NeuralNet and what data to send in for | |
# classification problems with single and multiple classes, and | |
# regression problems with and without multiple targets. | |
from lasagne.layers import DenseLayer | |
from lasagne.layers import InputLayer | |
from lasagne.nonlinearities import softmax | |
from nolearn.lasagne import NeuralNet | |
import numpy as np | |
from sklearn.datasets import make_classification | |
from sklearn.datasets import make_regression | |
def classif(X, y): | |
l = InputLayer(shape=(None, X.shape[1])) | |
l = DenseLayer(l, num_units=len(np.unique(y)), nonlinearity=softmax) | |
net = NeuralNet(l, update_learning_rate=0.01) | |
net.fit(X, y) | |
print(net.score(X, y)) | |
def regr(X, y): | |
l = InputLayer(shape=(None, X.shape[1])) | |
l = DenseLayer(l, num_units=y.shape[1], nonlinearity=None) | |
net = NeuralNet(l, regression=True, update_learning_rate=0.01) | |
net.fit(X, y) | |
print(net.score(X, y)) | |
def main(): | |
# Classification with two classes: | |
X, y = make_classification() | |
y = y.astype(np.int32) | |
classif(X, y) | |
# Classification with ten classes: | |
X, y = make_classification(n_classes=10, n_informative=10) | |
y = y.astype(np.int32) | |
classif(X, y) | |
# Regression with one target: | |
X, y = make_regression() | |
y = y.reshape(-1, 1).astype(np.float32) | |
regr(X, y) | |
# Regression with ten targets: | |
X, y = make_regression(n_targets=10) | |
y = y.astype(np.float32) | |
regr(X, y) | |
if __name__ == '__main__': | |
main() |
Should the two class classification example use sigmoid nonlinearity and binary_crossentropy loss? I tried to modify the example to do so, but receive dimensionality errors.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got
'DenseLayer' object is not iterable