Hopfield神经网络

来源:互联网 发布:人工智能对人类的影响 编辑:程序博客网 时间:2024/05/18 13:27
import numpy as npimport neurolab as nlimport 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()#显示012for i in range(len(target)):    visualized(np.reshape(target[i], (16,8)), i)#hopfield网络的值是1和-1target[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")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")

原创粉丝点击