{"id":1320,"date":"2023-03-25T10:12:45","date_gmt":"2023-03-25T02:12:45","guid":{"rendered":""},"modified":"2023-03-25T10:12:45","modified_gmt":"2023-03-25T02:12:45","slug":"\u795e\u7ecf\u7f51\u7edc","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1320.html","title":{"rendered":"\u795e\u7ecf\u7f51\u7edc"},"content":{"rendered":"
\n
\u795e\u7ecf\u7f51\u7edc\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n\u4ec0\u4e48\u662f\u4eba\u5de5\u795e\u7ecf\u7f51\u7edc(ANN)<\/h2>\n
\u5b89\u88c5\u6709\u7528\u7684\u5305<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
pip install NeuroLab
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
conda install -c labfabulous neurolab
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u6784\u5efa\u795e\u7ecf\u7f51\u7edc<\/h2>\n
\u57fa\u4e8e\u611f\u77e5\u5668\u7684\u5206\u7c7b\u5668<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
import <\/span>matplotlib.pyplot as <\/span>plt
import <\/span>neurolab as <\/span>nl
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
input = [[0, 0], [0, 1], [1, 0], [1, 1]]
target = [[0], [0], [0], [1]]
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
net = nl.net.newp<\/span>([[0, 1],[0, 1]], 1)
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
error_progress = net.train<\/span>(input, target, epochs=100, show=10, lr=0.1)
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
plt.figure<\/span>()
plt.plot<\/span>(error_progress)
plt.xlabel<\/span>('Number of epochs'<\/span>)
plt.ylabel<\/span>('Training error'<\/span>)
plt.grid<\/span>()
plt.show<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n <\/div>\n
\u5355\u5c42\u795e\u7ecf\u7f51\u7edc<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
import <\/span>numpy as <\/span>np
import <\/span>matplotlib.pyplot as <\/span>plt
import <\/span>neurolab as <\/span>nl
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
input_data = np.loadtxt<\/span>(\u201c\/Users\/admin\/neural_simple.txt')
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
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. ]])
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
data = input_data[:, 0:2]
labels = input_data[:, 2:]
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
plt.figure<\/span>()
plt.scatter<\/span>(data[:,0], data[:,1])
plt.xlabel<\/span>('Dimension 1'<\/span>)
plt.ylabel<\/span>('Dimension 2'<\/span>)
plt.title<\/span>('Input data'<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
dim1_min, dim1_max = data[:,0].min<\/span>(), data[:,0].max<\/span>()
dim2_min, dim2_max = data[:,1].min<\/span>(), data[:,1].max<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
nn_output_layer = labels.shape[1]
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
dim1 = [dim1_min, dim1_max]
dim2 = [dim2_min, dim2_max]
neural_net = nl.net.newp<\/span>([dim1, dim2], nn_output_layer)
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
error <\/span>= neural_net.train<\/span>(data, labels, epochs = 200, show = 20, lr = 0.01)
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
plt.figure<\/span>()
plt.plot<\/span>(error)
plt.xlabel<\/span>('Number of epochs'<\/span>)
plt.ylabel<\/span>('Training error'<\/span>)
plt.title<\/span>('Training error <\/span>progress'<\/span>)
plt.grid<\/span>()
plt.show<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
print('\\nTest Results:'<\/span>)
data_test = [[1.5, 3.2], [3.6, 1.7], [3.6, 5.7],[1.6, 3.9]] for <\/span><\/span>item in <\/span>data_test:
print(item, '-->'<\/span>, neural_net.sim<\/span>([item])[0])
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
[1.5, 3.2] --> [1. 0.]
[3.6, 1.7] --> [1. 0.]
[3.6, 5.7] --> [1. 1.]
[1.6, 3.9] --> [1. 0.]
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n <\/div>\n
\n <\/div>\n
\u591a\u5c42\u795e\u7ecf\u7f51\u7edc<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
import <\/span>numpy