Created
July 13, 2015 09:32
-
-
Save lucidfrontier45/9e33d626e9e094bc5208 to your computer and use it in GitHub Desktop.
Multi-Layer Perceptron using scikit-chainer
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
| __author__ = 'du' | |
| from chainer import FunctionSet, functions as F, optimizers | |
| from skchainer import ChainerClassifier | |
| class MultiLayerPerceptron(ChainerClassifier): | |
| def _setup_network(self, **params): | |
| print(params) | |
| network = FunctionSet( | |
| l1=F.Linear(params["input_dim"], params["hidden_dim"]), | |
| l2=F.Linear(params["hidden_dim"], params["n_classes"]) | |
| ) | |
| return network | |
| def forward(self, x): | |
| h = F.relu(self.network.l1(x)) | |
| y = self.network.l2(h) | |
| return y | |
| def loss_func(self, y, t): | |
| return F.softmax_cross_entropy(y, t) | |
| def output_func(self, h): | |
| return F.softmax(h) | |
| if __name__ == "__main__": | |
| import numpy as np | |
| from sklearn import datasets | |
| iris = datasets.load_iris() | |
| x = iris.data.astype(np.float32) | |
| y = iris.target.astype(np.int32) | |
| input_dim = x.shape[1] | |
| n_classes = len(set(y)) | |
| model = MultiLayerPerceptron(optimizer=optimizers.AdaDelta(rho=0.5), | |
| input_dim=input_dim, hidden_dim=10, n_classes=n_classes, report=100).fit(x, y) | |
| print(model.score(x, y)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment