partial_fit Sklearn 的 MLPClassifier [英] partial_fit Sklearn's MLPClassifier

查看:82
本文介绍了partial_fit Sklearn 的 MLPClassifier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 Sklearn 的神经网络 MLPClassifier.我有一个大小为 1000 个实例(带有二进制输出)的数据集,我想应用一个带有 1 个隐藏层的基本神经网络.

I've been trying to use Sklearn's neural network MLPClassifier. I have a dataset that is of size 1000 instances (with binary outputs) and I want to apply a basic Neural Net with 1 hidden layer to it.

问题是我的数据实例并非同时可用.在任何时候,我只能访问 1 个数据实例.我认为 MLPClassifier 的 partial_fit 方法可以用于此,所以我用 1000 个输入的假想数据集模拟了这个问题,并一次一个地循环输入一个,partial_fit 到每个实例,但是当我运行代码时,神经网络什么也没学到并且预测输出全为零.

The issue is that my data instances are not available all at the same time. At any point in time, I only have access to 1 data instance. I thought that partial_fit method of MLPClassifier can be used for this so I simulated the problem with an imaginary dataset of 1000 inputs and looped over the inputs one at a time and partial_fit to each instance but when I run the code, the neural net learns nothing and the predicted output is all zeros.

我对可能导致问题的原因一无所知.任何想法都非常感谢.

I am clueless as to what might be causing the problem. Any thought is hugely appreciated.

from __future__ import division 
import numpy as np
from sklearn.datasets import make_classification
from sklearn.neural_network import MLPClassifier

#Creating an imaginary dataset
input, output = make_classification(1000, 30, n_informative=10, n_classes=2)
input= input / input.max(axis=0)
N = input.shape[0]
train_input = input[0:N/2,:]
train_target = output[0:N/2]

test_input= input[N/2:N,:]
test_target = output[N/2:N]

#Creating and training the Neural Net
clf = MLPClassifier(activation='tanh', algorithm='sgd', learning_rate='constant',
 alpha=1e-4, hidden_layer_sizes=(15,), random_state=1, batch_size=1,verbose= True,
 max_iter=1, warm_start=True)
classes=[0,1]
for j in xrange(0,100):
for i in xrange(0,train_input.shape[0]):
    input_inst = [train_input[i,:]]
    input_inst = np.asarray(input_inst)
    target_inst= [train_target[i]]
    target_inst = np.asarray(target_inst)
    clf=clf.partial_fit(input_inst,target_inst,classes)

#Testing the Neural Net
y_pred = clf.predict(test_input)
print y_pred

推荐答案

问题说明

问题出在 multilayer_perceptron.py 的第 895 行中的 self.label_binarizer_.fit(y).

Explanation of the problem

The problem is with self.label_binarizer_.fit(y) in line 895 in multilayer_perceptron.py.

每当您调用 clf.partial_fit(input_inst,target_inst,classes) 时,您都会调用 self.label_binarizer_.fit(y) 其中 y在这种情况下,只有一个样本对应一个类.因此,如果最后一个样本属于 0 类,那么您的 clf 会将所有内容归类为 0 类.

Whenever you call clf.partial_fit(input_inst,target_inst,classes), you call self.label_binarizer_.fit(y) where y has only one sample corresponding to one class, in this case. Therefore, if the last sample is of class 0, then your clf will classify everything as class 0.

作为临时修复,您可以在第 895 行编辑 multilayer_perceptron.py.它位于类似于此 python2.7/site-packages/sklearn/neural_network/

As a temporary fix, you can edit multilayer_perceptron.py at line 895. It is found in a directory similar to this python2.7/site-packages/sklearn/neural_network/

在第 895 行,更改,

At line 895, change,

self.label_binarizer_.fit(y)

if not incremental:
    self.label_binarizer_.fit(y)

else:
    self.label_binarizer_.fit(self.classes_)

这样,如果您使用 partial_fit,则 self.label_binarizer_ 适合类而不是单个样本.

That way, if you are using partial_fit, then self.label_binarizer_ fits on the classes rather than on the individual sample.

此外,您可以将您发布的代码更改为以下内容以使其工作,

Further, the code you posted can be changed to the following to make it work,

from __future__ import division 
import numpy as np
from sklearn.datasets import make_classification
from sklearn.neural_network import MLPClassifier

#Creating an imaginary dataset
input, output = make_classification(1000, 30, n_informative=10, n_classes=2)
input= input / input.max(axis=0)
N = input.shape[0]
train_input = input[0:N/2,:]
train_target = output[0:N/2]

test_input= input[N/2:N,:]
test_target = output[N/2:N]

#Creating and training the Neural Net 
# 1. Disable verbose (verbose is annoying with partial_fit)

clf = MLPClassifier(activation='tanh', algorithm='sgd', learning_rate='constant',
 alpha=1e-4, hidden_layer_sizes=(15,), random_state=1, batch_size=1,verbose= False,
 max_iter=1, warm_start=True)

# 2. Set what the classes are
clf.classes_ = [0,1]

for j in xrange(0,100):
    for i in xrange(0,train_input.shape[0]):
       input_inst = train_input[[i]]
       target_inst= train_target[[i]]

       clf=clf.partial_fit(input_inst,target_inst)

    # 3. Monitor progress
    print "Score on training set: %0.8f" % clf.score(train_input, train_target)
#Testing the Neural Net
y_pred = clf.predict(test_input)
print y_pred

# 4. Compute score on testing set
print clf.score(test_input, test_target)

代码中有 4 个主要更改.这应该可以为您提供对训练集和测试集的良好预测!

There are 4 main changes in the code. This should give you a good prediction on both the training and the testing set!

干杯.

这篇关于partial_fit Sklearn 的 MLPClassifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆