zoukankan      html  css  js  c++  java
  • 神经网络学习之----Hopfield神经网络(代码实现)

    思路:

      定义三个训练测试图片0 1 2(16*8),即三个吸引子。然后创建一个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)
    
    
    # In[2]:
    
    #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")
    
    
    # In[3]:
    
    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") 
  • 相关阅读:
    PC端列表数据无限下拉加载
    Oracle中的LOB数据类型分类
    jmeter系列(4)-JSON提取器
    jmeter系列(9)-常见函数__V拼接函数
    jmeter系列(3)-属性
    jmeter系列(8)-常见函数__counter
    jmeter系列(6)-查看函数用法
    jmeter系列(2)-用户参数
    jmeter系列(1)-用户自定义的变量
    b_lc_完成任务的最少工作时间段(贪心(×) / 枚举)
  • 原文地址:https://www.cnblogs.com/mengqimoli/p/11103733.html
Copyright © 2011-2022 走看看