scikit 神经网络_使用scikit学习的深层神经多层感知器mlp

scikit 神经网络

In the world of deep learning, TensorFlow, Keras, Microsoft Cognitive Toolkit (CNTK), and PyTorch are very popular. Most of us may not realise that the very popular machine learning library Scikit-learn is also capable of a basic deep learning modelling. In this article, I will discuss the realms of deep learning modelling feasibility in Scikit-learn and limitations. Further, I will discuss hands-on implementation with two examples.

在深度学习领域,TensorFlow,Keras,Microsoft Cognitive Toolkit(CNTK)和PyTorch非常受欢迎。 我们大多数人可能没有意识到,非常受欢迎的机器学习库Scikit-learn也能够进行基本的深度学习建模。 在本文中,我将讨论Scikit学习中的深度学习建模可行性和局限性领域。 此外,我将通过两个示例讨论动手实施。

Salient points of Multilayer Perceptron (MLP) in Scikit-learn

Scikit学习中多层感知器(MLP)的显着点

  • There is no activation function in the output layer.

    输出层中没有激活功能。
  • For regression scenarios, the square error is the loss function, and cross-entropy is the loss function for the classification

    对于回归方案,平方误差是损失函数,交叉熵是分类的损失函数
  • It can work with single as well as multiple target values regression.

    它可以与单个以及多个目标值回归一起使用。
  • Unlike other popular packages, likes Keras the implementation of MLP in Scikit doesn’t support GPU.

    与其他流行的软件包不同,像Keras一样,Scikit中的MLP实现不支持GPU。
  • We cannot fine-tune the parameters like different activation functions, weight initializers etc. for each layer.

    我们无法为每层微调参数,例如不同的激活函数,权重初始化器等。

Regression Example

回归示例

Step 1: In the Scikit-Learn package, MLPRegressor is implemented in neural_network module. We will import the other modules like “train_test_split” to split the dataset into training and train set to test the model, “fetch_california_housing” to get the data, and “StandardScaler” to scale the data as different features( independent variables) have wide value range scale. It is very important to scale the data used for training the model.

步骤1:Scikit-Learn软件包中,MLPRegressor在Neuro_network模块中实现。 我们将导入其他模块,例如“ train_test_split”以将数据集分为训练和训练集以测试模型,“ fetch_california_housing”以获取数据,以及“ StandardScaler”以缩放数据,因为不同的特征(自变量)具有广泛的价值范围刻度。 缩放用于训练模型的数据非常重要。

You can learn more about different scalers in the article Feature Scaling — Effect Of Different Scikit-Learn Scalers: Deep Dive

您可以在文章功能缩放-不同的Scikit-Learn缩放器的影响:深入学习中了解有关不同缩放器的更多信息。

"""Import the required modules"""from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import r2_score
import pandas as pd

Step 2: We will split the dataset into train and test dataset. We have reserved 20% of the dataset for checking the accuracy of the trained model. Independent train and test dataset are further scaled to make sure that the input data is standard normally distributed are centred around zero and have variance in the same order.

步骤2:我们将数据集分为训练和测试数据集。 我们已保留20%的数据集以检查训练模型的准确性。 独立的训练和测试数据集将进一步缩放,以确保输入数据是标准正态分布的,并且以零为中心并且方差相同。

cal_housing = fetch_california_housing()
X = pd.DataFrame(cal_housing.data,columns=cal_housing.feature_names)
y = cal_housing.target
X_train, X_test, y_train, y_test = train_test_split(X, y,random_state=1, test_size=0.2)

Step 3: We scale the data just like the above regression example and for the same reason.

步骤3:由于相同的原因,我们像上面的回归示例一样对数据进行缩放。

sc_X = StandardScaler()
X_trainscaled=sc_X.fit_transform(X_train)
X_testscaled=sc_X.transform(X_test)

Step 4: In the below code, three hidden layers are modelled, with 64 neurons in each layer. Considering the input and output layer, we have a total of 5 layers in the model. In case any optimiser is not mentioned then “Adam” is the default optimiser and it can manage pretty large dataset.

步骤4:在下面的代码中,对三个隐藏层进行了建模,每层中有64个神经元。 考虑到输入和输出层,我们在模型中总共有5个层。 如果未提及任何优化程序,则“ Adam”是默认优化程序,它可以管理相当大的数据集。

reg = MLPRegressor(hidden_layer_sizes=(64,64,64),activation="relu" ,random_state=1, max_iter=2000).fit(X_trainscaled, y_train)

In addition to “RELU” activation, MLPRegressor supports the “sigmoid” and “hyperbolic tan” function.

除了激活“ RELU”外,MLPRegressor还支持“ Sigmoid”和“双曲线棕褐色”功能。

Step 5: In the below code, the trained model is used to predict the target values of the reserved test dataset, which model has not seen before.

步骤5:在下面的代码中,训练后的模型用于预测保留的测试数据集的目标值,而该模型以前从未见过。

y_pred=reg.predict(X_testscaled)
print("The Score with ", (r2_score(y_pred, y_test))

Classification Example

分类例

We have seen a regression example. Next, we will go through a classification example. In Scikit-learn “ MLPClassifier” is available for Multilayer Perceptron (MLP) classification scenarios.

我们已经看到了一个回归示例。 接下来,我们将介绍一个分类示例。 在Scikit学习中,“ MLPClassifier”可用于多层感知器(MLP)分类方案。

Step1: Like always first we will import the modules which we will use in the example. We will use the Iris database and MLPClassifierfrom for the classification example.

步骤1:像往常一样,首先将导入示例中要使用的模块。 对于分类示例,我们将使用Iris数据库和MLPClassifierfrom。

from sklearn.datasets import load_iris
from sklearn.neural_network import MLPClassifierfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import StandardScaler
import pandas as pd
from sklearn.metrics import plot_confusion_matrix
import matplotlib.pyplot as plt

Step 2: In separate data frames “X” and “y”, the values of the independent and dependent features are stored.

步骤2:在单独的数据帧“ X”和“ y”中,存储独立要素和从属要素的值。

iris_data = load_iris()
X = pd.DataFrame(iris_data.data, columns=iris_data.feature_names)
y = iris_data.target

Step 3: Similar to the regression example above we will split the dataset into train and test dataset. We have reserved 20% of the dataset for checking the accuracy of the trained model. Independent train and test dataset are further scaled to make sure that the input data is standard normally distributed are centred around zero and have variance in the same order.

步骤3:类似于上面的回归示例,我们将数据集分为训练数据集和测试数据集。 我们已保留20%的数据集以检查训练模型的准确性。 独立的训练和测试数据集将进一步缩放,以确保输入数据是标准正态分布的,并且以零为中心并且方差相同。

X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=1, test_size=0.2)
sc_X = StandardScaler()
X_trainscaled=sc_X.fit_transform(X_train)
X_testscaled=sc_X.transform(X_test)

Step 4: In the below code, we have modelled four hidden layers with different neurons in each layer. Considering the input and output layer, we have a total of 6 layers in the model. In case any optimiser is not mentioned then “Adam” is the default optimiser.

步骤4:在下面的代码中,我们对四个隐藏层进行了建模,每一层中都有不同的神经元。 考虑到输入和输出层,我们在模型中总共有6个层。 如果未提及任何优化程序,则“ Adam”为默认优化程序。

clf = MLPClassifier(hidden_layer_sizes=(256,128,64,32),activation="relu",random_state=1).fit(X_trainscaled, y_train)
y_pred=clf.predict(X_testscaled)
print(clf.score(X_testscaled, y_test))

The classifier shows quite a high score for the test data. It is important to understand the areas in which the classification model is making an error to make a full sense of model accuracy.

分类器显示测试数据得分很高。 重要的是要了解分类模型出错的区域,以充分了解模型的准确性。

You can read more on the reason we should use the confusion matrix to judge the classification model accuracy in “Accuracy Visualisation: Supervised Machine Learning Classification Algorithms”.

您可以在“ 准确度可视化:有监督的机器学习分类算法 ”中阅读有关使用混淆矩阵来判断分类模型准确性的原因的更多信息。

Step 5: We will draw a confusion matrix to understand the classifications which are made incorrect by the model.

步骤5:我们将绘制一个混淆矩阵,以了解模型造成的错误分类。

fig=plot_confusion_matrix(clf, X_testscaled, y_test,display_labels=["Setosa","Versicolor","Virginica"])
fig.figure_.suptitle("Confusion Matrix for Iris Dataset")
plt.show()

It seems only one “Versicolor” species is incorrectly identified as “Virginica” and rest by the model.

似乎只有一种“ Versicolor”物种被错误地识别为“ Virginica”,其余被模型识别。

Conclusion: We can do simple deep learning regression and classification model in Scikit-learn. In my view, it is not up for any real-life large-scale modelling as there is no GPU support and very limited options to tweak the parameters.

结论 :我们可以在Scikit-learn中建立简单的深度学习回归和分类模型。 在我看来,它不适合任何实际的大规模建模,因为它不支持GPU,而且调整参数的选项非常有限。

You can learn more about the deep learning visualisation technique in the article Accuracy Visualisation In Deep Learning

您可以在文章深度学习中的精度可视化中了解有关深度学习可视化技术的更多信息

翻译自: https://towardsdatascience.com/deep-neural-multilayer-perceptron-mlp-with-scikit-learn-2698e77155e

scikit 神经网络

你可能感兴趣的:(神经网络,深度学习,人工智能)