hopfield可以分为离散型神经网络和连续型神经网络(DHNN\ CHNN)
在之前的文章中讲过的单层感知器和BP都是离散的,目前连续的神经网络可能还没有碰见。
本文章是通过《人工神经网络理论、设计及应用——第2版》的例子讲解离散型Hopfield,离散型反馈网络的拓扑结果如下图所示:
所有神经元排列成一层,感觉像单层的,没有分层的概率。每一层都有输入,x1、x2 .。 图中没有标记偏置值,但我们清楚知道每个神经元都有一个权值。T值代表控制输入的信息,需要达到某种强度才会对神经元做出反应。跟其他神经网络一样,都有输入和输出,不一样的地方就是输出的x1有一个反馈的回路送到x2、x3,没有送给x1,可以把他反馈给自己的权值当作0。也可以变形画成如下环的形式,就是一种全连接。
环状全连接:
具体的推导过程你们可以看书中的第120页。本文章主要讲如何利用python对hopfield进行分类。
比如我们有两张图片,分别是0和1,如下图:
下面我们用添加了噪点的1,如下图,并通过hopfield进行迭代运算,找到1的吸引子,进行分类。
代码如下:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import neurolab as nl
img0 = np.array(Image.open('D:\\pythoncode\\learn1\\lesson1\\zero.jpg').convert('L'))
img1 = np.array(Image.open('D:\\pythoncode\\learn1\\lesson1\\one.jpg').convert('L'))
def normalize_data(data): # 0.1<=data[i][j]<=0.9
data = (data - np.mean(data)) / np.std(data)
rows,cols = data.shape
for i in range(rows):
for j in range(cols):
if (data[i,j] >= 0):
data[i,j] = 1
else:
data[i,j] = -1
return data
img0 = normalize_data(img0)
img1 = normalize_data(img1)
ax = plt.subplot(2, 2, 1)
ax.set_title("0")
plt.imshow(img0, cmap='gray')
ax = plt.subplot(2, 2, 2)
ax.set_title("1")
plt.imshow(img1, cmap='gray')
chars = ['zero', 'one']
nrows,ncols = img0.shape
img0 = img0.reshape(1,nrows*ncols)
nrows,ncols = img1.shape
img1 = img1.reshape(1,nrows*ncols)
target = np.concatenate((img0,img1))
target = np.asfarray(target)
# Create and train network
net = nl.net.newhop(target)
output = net.sim(target)
print("Test on train samples:")
for i in range(len(target)):
print(chars[i], (output[i] == target[i]).all())
print("\nTest on defaced one:")
data = np.ones((1,nrows*ncols))
#给数字1添加噪点
for col in range(len(img1[0])):
if col%10 == 0:
data[0][col] = 1
else:
data[0][col] = img1[0][col]
data1 = data.reshape(nrows,ncols)
ax = plt.subplot(2, 2, 4)
ax.set_title("norise points")
plt.imshow(data1, cmap='gray')
plt.gray()
plt.show()
#测试代码
out = net.sim([data[0]])
for i in range(len(target)):
if (out[0] == target[i]).all():
print("char is ", chars[i])
该部分是通过训练的审计网络,进行分类:最后知道该图片是1.
但是hopfield在进行迭代过程中会有一些问题。例子如下:
比如现在我们有两个吸引子分别是101和000,通过《人工神经网络理论、设计及应用——第2版》的第124页的例子6.1, 我们也画出网络演变过程图,如下图:
比如说在箭头指向的100这个点, 他有1/3会演变为101,也有1/3概率演变为000。所以在用hopfield进行分类或者预测过程中,也容易产生误判。