In [45]:
from sknn.mlp import Classifier, Convolution, FastVectorSpace, Layer, MultiLayerPerceptron
from sknn.mlp import NeuralNetwork, Regressor, SparseDesignMatrix
from time import time
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.grid_search import RandomizedSearchCV
from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.externals import joblib
from sklearn import datasets
from random import randint
In [2]:
np.set_printoptions(precision=4)
np.set_printoptions(suppress=True)
In [3]:
def XOR(n):
# 1000개의 데이터를 생성
X = [[randint(0, 1), randint(0, 1)] for _ in range(n)]
y = []
for a, b in X:
if a == b:
y.append([0])
else:
y.append([1])
X = np.array(X)
Y = np.array(y)
return X, Y
In [45]:
features, labels = XOR(n=1000)
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.3)
In [46]:
print "the shape of training set %s rows, %s columns" %(trainX.shape[0], trainX.shape[1])
print "the shape of test set %s rows, %s columns" %(testX.shape[0], testX.shape[1])
print "the range of training set : %s ~ %s" %(trainX.min(),trainX.max())
print "the range of test set : %s ~ %s" %(testX.min(),testX.max())
In [92]:
# Regression Example
nn = Regressor(
layers=[
Layer("Rectifier", units=100),
Layer("Linear")],
learning_rate=0.2,
n_iter=100)
nn.fit(trainX, trainY)
# compute the predictions for the test data and show a classification report
preds = nn.predict(testX)
preds = preds.astype('int64')
print "accuracy score : %s" %(accuracy_score(testY, preds))
print "classification report : "
print classification_report(testY, preds)
In [93]:
# Classification
nn = Classifier(
layers=[
Layer("Maxout", units=100, pieces=2),
Layer("Softmax")],
learning_rate=0.001,
n_iter=25)
nn.fit(trainX, trainY)
# compute the predictions for the test data and show a classification report
preds = nn.predict(testX)
preds = preds.astype('int64')
print "accuracy score : %s" %(accuracy_score(testY, preds))
print "classification report : "
print classification_report(testY, preds)
In [139]:
dataset = datasets.fetch_mldata("MNIST Original", data_home=".")
features = np.array(dataset.data, 'float32') / 255
labels = np.array(dataset.target, 'int')
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.3)
# reshape for convolutions
trainX = trainX.reshape((trainX.shape[0], 1, 28, 28))
testX = testX.reshape((testX.shape[0], 1, 28, 28))
In [142]:
print "the shape of training set %s rows, %s columns" %(trainX.shape[0], trainX.shape[1])
print "the shape of test set %s rows, %s columns" %(testX.shape[0], testX.shape[1])
print "the range of training set : %s ~ %s" %(trainX.min(),trainX.max())
print "the range of test set : %s ~ %s" %(testX.min(),testX.max())
In [149]:
# Convolution
nn = Classifier(
layers=[
Convolution("Rectifier", channels=9, kernel_shape=(3,3), border_mode='full'),
Layer("Softmax")],
learning_rate=0.02,
n_iter=10)
nn.fit(trainX, trainY)
# compute the predictions for the test data and show a classification report
preds = nn.predict(testX)
print "accuracy score : %s" %(accuracy_score(testY, preds))
print "classification report : "
print classification_report(testY, preds)
In [24]:
# Grid Search
# "Rectifier", "Sigmoid", "Tanh", "Maxout", "Linear", "Softmax", "Gaussian"
features, labels = XOR(n=100)
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.3)
nn = Regressor(
layers=[
Layer("Sigmoid", units=10),# 첫번째 히든레이어
Layer("Sigmoid", units=10),# 두번째 히든레이어
Layer("Linear")], # 아웃풋 레이어
verbose=1)
gs = GridSearchCV(nn, param_grid={
'learning_rate': [0.05, 0.01],
'n_iter' : [5],
'hidden0__units': [20, 30],
'hidden0__type': ["Sigmoid", "Tanh"], # 첫번째 히든레이어
'hidden1__units': [20, 30],
'hidden1__type': ["Sigmoid", "Tanh"]}) # 두번째 히든레이어
gs.fit(trainX, trainY)
Out[24]:
In [25]:
print gs.best_params_
print
print gs.best_estimator_
In [26]:
# compute the predictions for the test data and show a classification report
preds = gs.predict(testX)
preds = preds.astype('int64')
print "accuracy score : %s" %(accuracy_score(testY, preds))
print "classification report : "
print classification_report(testY, preds)
In [42]:
dataset = datasets.fetch_mldata("MNIST Original", data_home=".")
features = np.array(dataset.data, 'float32') / 255
labels = np.array(dataset.target, 'int')
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.3)
# reshape for convolutions
trainX = trainX.reshape((trainX.shape[0], 1, 28, 28))
testX = testX.reshape((testX.shape[0], 1, 28, 28))
# Convolution
nn = Classifier(
layers=[
Convolution("Rectifier", channels=9, kernel_shape=(3,3), border_mode='full'),
Convolution("Rectifier", channels=9, kernel_shape=(2,2), border_mode='full'),
Layer("Softmax")],
learning_rate=0.02,
n_iter=10)
nn.fit(trainX, trainY)
# compute the predictions for the test data and show a classification report
preds = nn.predict(testX)
print "accuracy score : %s" %(accuracy_score(testY, preds))
print "classification report : "
print classification_report(testY, preds)
In [43]:
nn
Out[43]:
In [44]:
testY
Out[44]:
In [54]:
from sknn.ae import AutoEncoder, Layer
# 위에서와 Layer가 이름이 겹치므로 주의
dataset = datasets.fetch_mldata("MNIST Original", data_home=".")
features = np.array(dataset.data, 'float32') / 255
labels = np.array(dataset.target, 'int')
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.3)
# Regression Example
nn = AutoEncoder(
layers=[
Layer("Sigmoid", units=100),
Layer("Tanh", units=10)],
learning_rate=0.2,
n_iter=10)
nn.fit(trainX)
Out[54]:
In [65]:
print nn.layers
print nn.f_stable
In [82]:
print testX.shape
print nn.transform(testX).shape # "Tanh", units=10 이라서 아웃풋이 10으로 나온거임
In [84]:
print np.unique(nn.transform(testX))
print len(np.unique(nn.transform(testX)))
In [81]:
print nn.is_convolution
print nn.learning_momentum
print nn.learning_rate
print nn.learning_rule
print nn.loss_type
print nn.n_iter
print nn.n_stable
print nn.random_state
print nn.regularize
print nn.valid_set
print nn.weight_decay
In [37]:
# 왜 안될까?
# Grid Search
# "Rectifier", "Sigmoid", "Tanh", "Maxout", "Linear", "Softmax", "Gaussian"
features, labels = XOR(n=100)
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.3)
nn = Classifier(
layers=[
Layer("Maxout", units=100, pieces=2),
Layer("Softmax")],
learning_rate=0.001,
n_iter=5)
# nn.fit(trainX, trainY)
gs = GridSearchCV(nn, param_grid={'learning_rate': [0.05, 0.01]})
gs.fit(trainX, trainY)
# gs = GridSearchCV(nn, param_grid={
# 'learning_rate': [0.05, 0.01],
# 'n_iter' : [5],
# 'pieces' : [2]}) # 두번째 히든레이어
# gs.fit(trainX, trainY)
# # compute the predictions for the test data and show a classification report
# preds = gs.predict(testX)
# preds = preds.astype('int64')
# print "accuracy score : %s" %(accuracy_score(testY, preds))
# print "classification report : "
# print classification_report(testY, preds)
In [39]:
print trainY.dtype
print testY.dtype