神经网络是最著名且使用最广泛的算法之一。在监督学习中,我们标记了输出数据,我们将预测输出与实际标签进行比较,然后计算误差。在神经网络中,我们以同样的方式定义神经网络体系结构,通过比较实际标签和预测标签来计算误差,然后使用某种优化算法来优化该误差。训练神经网络最广泛使用的算法是反向传播和梯度下降。但是,我们可以使用任何优化算法来训练我们的神经网络模型。今天我们将看看如何在Python中使用Numpy用粒子群优化来训练神经网络模型。
神经网络
导入Python库
导入sklearn加载Iris flower机器学习数据集,pso_numpy使用PSO算法,numpy执行神经网络的forward pass。
加载机器学习数据集
从sklearn加载Iris数据集,并将输入数据分配给X,将目标标签分配给Y。
定义架构
在神经网络模型中定义输入,隐藏和输出节点数。
One-hot编码
如果我们要计算分类交叉熵损失,则使用One-hot编码。将唯一的向量分配给每个目标标签(类)。该函数将Y作为输入,并为每个类返回One-hot编码向量。
Softmax激活