神经网络可视化_可视化神经网络从头开始的工作方式

神经网络可视化

Neural Network is often considered a black-box algorithm. Data visualization can help us better understand the principles of this algorithm. Since standard packages don’t give all details of how the parameters are found, we will code a neural network from scratch. And in order to visualize simply the results, we have chosen a simple dataset.

神经网络通常被认为是黑盒算法。 数据可视化可以帮助我们更好地理解该算法的原理。 由于标准软件包并未提供有关如何找到参数的所有详细信息,因此我们将从头开始编写神经网络代码。 为了简单地显示结果,我们选择了一个简单的数据集。

简单的数据集和神经网络结构 (Simple dataset and neural network structure)

Let’s use this simple dataset with only one feature X.

让我们使用仅具有一个特征X的简单数据集。

import numpy as npX=np.array([[-1.51], [-1.29], [-1.18], [-0.64],
[-0.53], [-0.09], [0.13], [0.35],
[0.89], [1.11], [1.33], [1.44]])y=np.array([[0], [0], [0], [0],
[1], [1], [1], [1],[0], [0], [0], [0]])

X is a single column vector with 12 observations, and y is also a column vector with 12 values that represent the target. We can visualize this dataset.

X是具有12个观测值的单列向量,y也是具有12个代表目标值的列向量。 我们可以可视化此数据集。

import matplotlib.pyplot as plt
plt.scatter(X,y)

For those of you who already know how a neural network works, you should be able to find a simple structure by seeing this graph. In the next part, the activation function will be the sigmoid function.

对于已经知道神经网络如何工作的人,您应该可以通过查看此图来找到简单的结构。 在下一部分中,激活函数将为S型函数。

So the question is: how many layers and neurons do we need in order to build a neural network that would fit the dataset above?

所以问题是: 为了建立适合上面数据集的神经网络,我们需要多少层和神经元?

If we use only one neuron it will be the same thing as doing a Logistic Regression because the activation function is the sigmoid function. And we know that it won’t work because the dataset is not linearly separable, and Simple Logistic Regression doesn’t work with not linearly separable data. So we have to add a hidden layer. Each neuron in the hidden layer will result in a linear decision boundary.

如果我们只使用一个神经元,那将与进行Logistic回归相同,因为激活函数是S形函数。 而且我们知道它不起作用,因为数据集不是线性可分离的,并且简单Logistic回归不适用于线性不可分离的数据。 因此,我们必须添加一个隐藏层。 隐藏层中的每个神经元将导致线性决策边界。

In general, a Logistic Regression creates a hyperplane as a decision boundary. Since here we only have one feature, then this hyperplane is only a dot. Visually, we can see that we would need two dots to separate one class from the other. And their values would be around for one -0.5 and the other 0.5.

通常,逻辑回归会创建一个超平面作为决策边界。 因为这里我们只有一个特征,所以这个超平面只是一个点。 从视觉上看,我们需要两个点来将一个类与另一个类分开。 它们的值大约为-0.5,另一个为0.5。

Image for post

So a neural network with the following structure would be a good classifier for our dataset.

因此,具有以下结构的神经网络对于我们的数据集将是一个很好的分类器。

Image for post

If it is not clear for you, you can read this article.

如果您不清楚,则可以阅读本文。

使用scikit学习MLPClassifier (Using scikit learn MLPClassifier)

Before building the neural network from scratch, let’s first use algorithms already built to confirm that such a neural network is suitable, and visualize the results.

在从头开始构建神经网络之前,让我们首先使用已经构建的算法来确认这种神经网络是否合适,并可视化结果。

We can use the MLPClassifier in scikit learn. In the following code, we specify the number of hidden layers and the number of neurons with the argument hidden_layer_sizes.

我们可以在scikit中使用MLPClassifier学习。 在下面的代码中,我们使用参数hidden_​​layer_sizes指定隐藏层的数量和神经元的数量。

from sklearn.neural_network import MLPClassifierclf = MLPClassifier(solver=’lbfgs’,hidden_layer_sizes=(2,), activation=”logistic”,max_iter=1000)clf.fit(X, y)

Then we can calculate the score. (You should get 1.0, otherwise, you may have to run the code again because of local minimums).

然后我们可以计算分数。 (您应该获得1.0,否则,由于局部最小值,您可能不得不再次运行代码)。

clf.score(X,y)

So great, how can we visualize the results of the algorithm? Since we know that this neural network is made by 2+1 Logistic Regression, we can get the parameters with the following code.

太好了,我们如何可视化算法的结果? 由于我们知道该神经网络是由2 + 1 Logistic回归构成的,因此可以使用以下代码获取参数。

clf.coefs_
clf.intercepts_

How do we interpret the results?

我们如何解释结果?

For clf.coefs_, you will get (for example):

对于clf.coefs_ ,您将获得(例如):

[array([[-20.89123833, -8.09121263]]), array([[-20.19430919], [ 17.74430684]])]

And for clf.intercepts_

对于clf.intercepts_

[array([-12.35004862, 4.62846821]), array([-8.19425129])]

The first items of the lists contain the parameters for the hidden layer, and the second items contain the pa

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值