Note that the np.array dataset used in Scikit-learn could not be used immediately in Chainer (1.11).
To prepare
--Training dataset: train_X, train_y --Test dataset: test_X, test_y
here
--train_X and test_X are np.array of (number of samples, dimensions). Usually float32. --train_y and test_y are np.array of (number of samples,). Probably int64 by default.
SVM example with sklearn
from sklearn import svm
clf = svm.SVC()
clf.fit(train_X, train_y) #Learning
clf.score(test_X, test_y) #Forecast / score calculation
prob_X = clf.predict_proba(test_X) #Probability prediction
Use chainer trainer
#First, create a class.
class sk_dataset(chainer.dataset.DatasetMixin):
def __init__(self, X, y):
self.X = X.astype('float32') #Float32 is required, so convert here
self.y = y.astype('int32') #Since int32 is required, convert here
def __len__(self):
return self.X.shape[0]
def get_example(self, i):
return self.X[i], self.y[i]
# np.sk array_Convert to dataset type
train = sk_dataset(train_X, train_y)
test = sk_dataset(test_X, test_y)
#The rest is tutorial street
model = L.Classifier(MLP())
optimizer = chainer.optimizers.SGD()
optimizer.setup(model)
train_iter = iterators.SerialIterator(train, batch_size=100)
test_iter = iterators.SerialIterator(test, batch_size=100, repeat=False, shuffle=False)
updater = training.StandardUpdater(train_iter, optimizer)
trainer = training.Trainer(updater, (20, 'epoch'), out='result')
trainer.run() #Learning
###Forecast
i = 10 #i-th data
dim = 4096 #Data dimension
print("predict:", model.predictor(Variable(test[i][0].reshape((1,dim)))).data) #Raw output
print("probability:", F.softmax(model.predictor(Variable(test[i][0].reshape((1,dim)))).data).data) #Probability prediction
print("label:", F.softmax(model.predictor(Variable(test[i][0].reshape((1,dim)))).data).data.argmax()) #Label prediction
Recommended Posts