Artificial Intelligence With Python 简明教程
AI with Python – Neural Networks
神经网络是使计算机模型成为大脑的一种尝试的并行计算设备。其背后的主要目标是开发一种比传统系统更快地执行各种计算任务的系统。这些任务包括模式识别和分类、近似、优化和数据聚类。
Neural networks are parallel computing devices that are an attempt to make a computer model of brain. The main objective behind is to develop a system to perform various computational task faster than the traditional systems. These tasks include Pattern Recognition and Classification, Approximation, Optimization and Data Clustering.
What is Artificial Neural Networks (ANN)
人工神经网络 (ANN) 是一种高效的计算系统,其中心主题借鉴了生物神经网络的类比。ANN 也被称为人造神经系统、并行分布式处理系统和连接主义系统。ANN 获取大量以某种模式互连的单元,以允许它们之间进行通信。这些单元,也称为 nodes 或 neurons ,是并行运行的简单处理器。
Artificial Neural network (ANN) is an efficient computing system whose central theme is borrowed from the analogy of biological neural networks. ANNs are also named as Artificial Neural Systems, Parallel Distributed Processing Systems, and Connectionist Systems. ANN acquires large collection of units that are interconnected in some pattern to allow communications between them. These units, also referred to as nodes or neurons, are simple processors which operate in parallel.
每个神经元通过 connection link 与其他神经元连接。每个连接链路都与具有输入信号信息的权重相关联。这是神经元解决特定问题最有用的信息,因为 weight 通常会激发或抑制正在通信的信号。每个神经元都有其内部状态,称为 activation signal 。在组合输入信号和激活规则后产生的输出信号可能会被发送到其他单元。
Every neuron is connected with other neuron through a connection link. Each connection link is associated with a weight having the information about the input signal. This is the most useful information for neurons to solve a particular problem because the weight usually excites or inhibits the signal that is being communicated. Each neuron is having its internal state which is called activation signal. Output signals, which are produced after combining input signals and activation rule, may be sent to other units.
如果您想详细研究神经网络,则可以按照链接 − Artificial Neural Network 。
If you want to study neural networks in detail then you can follow the link − Artificial Neural Network.
Installing Useful Packages
为了在 Python 中创建神经网络,我们可以使用一个名为 NeuroLab 的强大神经网络软件包。它是一个 Python 的基本神经网络算法库,具有灵活的网络配置和学习算法。您可以通过在命令提示符下使用以下命令来安装此软件包 −
For creating neural networks in Python, we can use a powerful package for neural networks called NeuroLab. It is a library of basic neural networks algorithms with flexible network configurations and learning algorithms for Python. You can install this package with the help of the following command on command prompt −
pip install NeuroLab
如果您使用的是 Anaconda 环境,请使用以下命令安装 NeuroLab:
If you are using the Anaconda environment, then use the following command to install NeuroLab −
conda install -c labfabulous neurolab
Building Neural Networks
在本部分中,让我们使用 NeuroLab 包在 Python 中构建一些神经网络。
In this section, let us build some neural networks in Python by using the NeuroLab package.
Perceptron based Classifier
感知器是 ANN 的构建模块。如果您想了解更多关于感知器的信息,您可以点击 artificial_neural_network 。
Perceptrons are the building blocks of ANN. If you want to know more about Perceptron, you can follow the link − artificial_neural_network
以下是基于分类器构建简单神经网络感知器的 Python 代码逐步执行过程:
Following is a stepwise execution of the Python code for building a simple neural network perceptron based classifier −
导入必要的包,如下所示
Import the necessary packages as shown −
import matplotlib.pyplot as plt
import neurolab as nl
输入值。请注意,这是一个监督学习的例子,因此您也必须提供目标值。
Enter the input values. Note that it is an example of supervised learning, hence you will have to provide target values too.
input = [[0, 0], [0, 1], [1, 0], [1, 1]]
target = [[0], [0], [0], [1]]
使用两个输入创建一个带有 1 个神经元的网络:
Create the network with 2 inputs and 1 neuron −
net = nl.net.newp([[0, 1],[0, 1]], 1)
现在,训练网络。这儿,我们使用 Delta 法则进行训练。
Now, train the network. Here, we are using Delta rule for training.
error_progress = net.train(input, target, epochs=100, show=10, lr=0.1)
现在,可视化输出并绘制图像:
Now, visualize the output and plot the graph −
plt.figure()
plt.plot(error_progress)
plt.xlabel('Number of epochs')
plt.ylabel('Training error')
plt.grid()
plt.show()
您可以看到以下图像,该图像显示了使用误差度量训练的进度:
You can see the following graph showing the training progress using the error metric −
Single - Layer Neural Networks
在这个例子中,我们创建一个单层神经网络,它由对输入数据进行操作以产生输出的独立神经元组成。请注意,我们使用名为 neural_simple.txt 的文本文件作为我们的输入。
In this example, we are creating a single layer neural network that consists of independent neurons acting on input data to produce the output. Note that we are using the text file named neural_simple.txt as our input.
导入有用的包:
Import the useful packages as shown −
import numpy as np
import matplotlib.pyplot as plt
import neurolab as nl
加载数据集如下:
Load the dataset as follows −
input_data = np.loadtxt(“/Users/admin/neural_simple.txt')
以下是我们将要使用的数据。请注意,在这些数据中,前两列是特征,后两列是标签。
The following is the data we are going to use. Note that in this data, first two columns are the features and last two columns are the labels.
array([[2. , 4. , 0. , 0. ],
[1.5, 3.9, 0. , 0. ],
[2.2, 4.1, 0. , 0. ],
[1.9, 4.7, 0. , 0. ],
[5.4, 2.2, 0. , 1. ],
[4.3, 7.1, 0. , 1. ],
[5.8, 4.9, 0. , 1. ],
[6.5, 3.2, 0. , 1. ],
[3. , 2. , 1. , 0. ],
[2.5, 0.5, 1. , 0. ],
[3.5, 2.1, 1. , 0. ],
[2.9, 0.3, 1. , 0. ],
[6.5, 8.3, 1. , 1. ],
[3.2, 6.2, 1. , 1. ],
[4.9, 7.8, 1. , 1. ],
[2.1, 4.8, 1. , 1. ]])
现在,将这四列分割为 2 个数据列和 2 个标签:
Now, separate these four columns into 2 data columns and 2 labels −
data = input_data[:, 0:2]
labels = input_data[:, 2:]
使用以下命令绘制输入数据:
Plot the input data using the following commands −
plt.figure()
plt.scatter(data[:,0], data[:,1])
plt.xlabel('Dimension 1')
plt.ylabel('Dimension 2')
plt.title('Input data')
现在,如此处所示,定义每个维度的最小值和最大值:
Now, define the minimum and maximum values for each dimension as shown here −
dim1_min, dim1_max = data[:,0].min(), data[:,0].max()
dim2_min, dim2_max = data[:,1].min(), data[:,1].max()
接下来,如下所示,定义输出层中的神经元数:
Next, define the number of neurons in the output layer as follows −
nn_output_layer = labels.shape[1]
现在,定义一个单层神经网络:
Now, define a single-layer neural network −
dim1 = [dim1_min, dim1_max]
dim2 = [dim2_min, dim2_max]
neural_net = nl.net.newp([dim1, dim2], nn_output_layer)
使用显示的 epoch 数和学习速率训练神经网络:
Train the neural network with number of epochs and learning rate as shown −
error = neural_net.train(data, labels, epochs = 200, show = 20, lr = 0.01)
现在,使用以下命令可视化并绘制训练进度 −
Now, visualize and plot the training progress using the following commands −
plt.figure()
plt.plot(error)
plt.xlabel('Number of epochs')
plt.ylabel('Training error')
plt.title('Training error progress')
plt.grid()
plt.show()
现在,在上个月分类器中使用测试数据点 −
Now, use the test data-points in above classifier −
print('\nTest Results:')
data_test = [[1.5, 3.2], [3.6, 1.7], [3.6, 5.7],[1.6, 3.9]] for item in data_test:
print(item, '-->', neural_net.sim([item])[0])
您可以找到此处所示的测试结果 −
You can find the test results as shown here −
[1.5, 3.2] --> [1. 0.]
[3.6, 1.7] --> [1. 0.]
[3.6, 5.7] --> [1. 1.]
[1.6, 3.9] --> [1. 0.]
您可以看到以下图表作为迄今为止讨论的代码的输出 −
You can see the following graphs as the output of the code discussed till now −
Multi-Layer Neural Networks
在此示例中,我们正在创建一个多层神经网络,该网络由多层组成,以提取训练数据中的潜在模式。这个多层神经网络将像回归器一样工作。我们用以下公式生成一些数据点:y = 2x2+8。
In this example, we are creating a multi-layer neural network that consists of more than one layer to extract the underlying patterns in the training data. This multilayer neural network will work like a regressor. We are going to generate some data points based on the equation: y = 2x2+8.
导入必要的包,如下所示
Import the necessary packages as shown −
import numpy as np
import matplotlib.pyplot as plt
import neurolab as nl
基于上述方程式生成一些数据点 −
Generate some data point based on the above mentioned equation −
min_val = -30
max_val = 30
num_points = 160
x = np.linspace(min_val, max_val, num_points)
y = 2 * np.square(x) + 8
y /= np.linalg.norm(y)
现在,重新塑造此数据集如下 −
Now, reshape this data set as follows −
data = x.reshape(num_points, 1)
labels = y.reshape(num_points, 1)
使用以下命令可视化并绘制输入数据集 −
Visualize and plot the input data set using the following commands −
plt.figure()
plt.scatter(data, labels)
plt.xlabel('Dimension 1')
plt.ylabel('Dimension 2')
plt.title('Data-points')
现在,构建具有两个隐藏层的神经网络,其中第一个隐藏层有 neurolab 神经元,第二个隐藏层有 six 神经元,输出层有 one 神经元。
Now, build the neural network having two hidden layers with neurolab with ten neurons in the first hidden layer, six in the second hidden layer and one in the output layer.
neural_net = nl.net.newff([[min_val, max_val]], [10, 6, 1])
现在使用梯度训练算法 −
Now use the gradient training algorithm −
neural_net.trainf = nl.train.train_gd
现在,使用在上述数据上进行学习的目标训练神经网络 −
Now train the network with goal of learning on the data generated above −
error = neural_net.train(data, labels, epochs = 1000, show = 100, goal = 0.01)
现在,在训练数据点上运行神经网络 −
Now, run the neural networks on the training data-points −
output = neural_net.sim(data)
y_pred = output.reshape(num_points)
现在进行绘制和可视化任务 −
Now plot and visualization task −
plt.figure()
plt.plot(error)
plt.xlabel('Number of epochs')
plt.ylabel('Error')
plt.title('Training error progress')
现在我们将绘制实际输出和预测输出 −
Now we will be plotting the actual versus predicted output −
x_dense = np.linspace(min_val, max_val, num_points * 2)
y_dense_pred = neural_net.sim(x_dense.reshape(x_dense.size,1)).reshape(x_dense.size)
plt.figure()
plt.plot(x_dense, y_dense_pred, '-', x, y, '.', x, y_pred, 'p')
plt.title('Actual vs predicted')
plt.show()
通过上述命令,您可以观察如下所示的图表 −
As a result of the above commands, you can observe the graphs as shown below −