上一次修改时间:2018-08-11 03:04:42

Hopfield神经网络代码示例

import numpy as np
import neurolab as nl#神经网络工具包
import matplotlib.pyplot as plt
# 0 1 2-----------16*8   
target =  np.array([[0,0,0,0,0,0,0,0,
                     0,0,0,1,1,0,0,0,
                     0,0,1,0,0,1,0,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,0,1,0,0,1,0,0,
                     0,0,0,1,1,0,0,0,
                     0,0,0,0,0,0,0,0],
           
                    [0,0,0,0,0,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,1,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,1,1,1,0,0,
                     0,0,0,0,0,0,0,0],   
           
                    [0,0,0,0,0,0,0,0,
                     0,0,1,1,1,1,0,0,
                     0,1,1,0,0,1,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,0,0,0,0,1,1,0,
                     0,0,0,0,1,1,0,0,
                     0,0,0,1,1,0,0,0,
                     0,0,1,1,0,0,0,0,
                     0,1,1,0,0,0,0,0,
                     0,1,0,0,0,0,0,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,1,1,1,1,1,0,
                     0,0,0,0,0,0,0,0]])

#画图函数
def visualized (data, title): 
    fig, ax = plt.subplots()
    ax.imshow(data, cmap=plt.cm.gray, interpolation='nearest')
    ax.set_title(title)
    plt.show()

#显示012
for i in range(len(target)):
    visualized(np.reshape(target[i], (16,8)), i)

image.png

image.png

#hopfield网络的值是1和-1
target[target == 0] = -1

#创建一个hopfield神经网络,吸引子为target(012)
net = nl.net.newhop(target)


#定义3个测试数据
test_data1 =np.asfarray([0,0,0,0,0,0,0,0,
                         0,0,0,1,1,0,1,0,
                         0,0,1,0,0,1,0,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,0,1,0,1,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,1,0,0,1,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,0,1,0,1,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,1,0,0,1,0,
                         0,0,1,0,0,1,0,0,
                         0,0,1,1,1,0,0,0,
                         0,0,0,0,0,0,0,0])

test_data2 =np.asfarray([0,0,0,1,0,0,0,0,
                         0,0,0,0,1,0,0,0,
                         0,0,0,1,1,0,0,0,
                         0,0,0,0,0,0,1,0,
                         0,1,0,0,1,0,0,0,
                         0,0,0,0,1,0,0,1,
                         0,0,0,1,1,0,1,0,
                         0,1,0,0,1,0,1,0,
                         0,0,0,0,1,0,0,0,
                         0,0,1,0,1,0,1,0,
                         0,0,0,1,1,0,0,0,
                         0,0,0,0,1,0,0,0,
                         0,0,0,0,1,0,0,1,
                         0,0,1,0,1,0,0,0,
                         0,0,0,1,1,1,0,0,
                         0,1,0,0,0,0,0,0])

test_data3 =np.asfarray([0,0,0,1,0,0,0,0,
                         0,0,0,0,1,0,0,0,
                         0,0,0,1,1,0,0,0,
                         0,0,0,1,0,0,1,0,
                         0,1,0,0,0,0,0,0,
                         0,0,0,0,1,0,0,1,
                         0,0,0,1,0,0,1,0,
                         0,1,0,0,1,0,1,0,
                         0,0,0,0,1,0,0,0,
                         0,0,1,0,0,0,1,0,
                         0,0,0,1,1,0,0,0,
                         0,0,0,0,1,0,0,0,
                         0,0,0,0,0,0,0,1,
                         0,0,1,0,0,0,0,0,
                         0,0,0,0,1,1,0,0,
                         0,1,0,0,0,0,0,0])

#显示测试数据
visualized(np.reshape(test_data1, (16,8)), "test_data1")
visualized(np.reshape(test_data2, (16,8)), "test_data2")
visualized(np.reshape(test_data3, (16,8)), "test_data3")

image.png

image.png

'''
判断数字的依据是输入的图片经过训练好的Hopfield网络后,
得到的输出结果和作为吸引子的图片一模型一样的话,就输出吸引子的标签
'''
test_data1[test_data1==0] = -1
#把测试数据输入hopfield网络,得到输出
out1 = net.sim([test_data1])
#判断测试数据的数字是多少
for i in range(len(target)):
    if((out1 == target[i]).all()):
        print("test_data is :",i)
#显示输出
visualized(np.reshape(out1, (16,8)), "output1")        


test_data2[test_data2==0] = -1
#把测试数据输入hopfield网络,得到输出
out2 = net.sim([test_data2])
#判断测试数据的数字是多少
for i in range(len(target)):
    if((out2 == target[i]).all()):
        print("test_data is :",i)
#显示输出
visualized(np.reshape(out2, (16,8)), "output2")        


test_data3[test_data3==0] = -1
#把测试数据输入hopfield网络,得到输出
out3 = net.sim([test_data3])
#判断测试数据的数字是多少
for i in range(len(target)):
    if((out3 == target[i]).all()):
        print("test_data is :",i)
#显示输出
visualized(np.reshape(out3, (16,8)), "output3")

image.png

image.png

注:output3可能是朝着数字2的图片进行迭代,然后掉入了一个伪吸引子里面;