丶霁灵 发表于 2020-6-8 21:39:03

hopfield神经网络不能达到预期

#!/usr/bin/env python
# coding: utf-8

# In:


import numpy as np
import neurolab as nl
import matplotlib.pyplot as plt


# In:


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,1,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,1,1,1,1,1,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,0,0,0,0,1,0,
                   0,1,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,0,0,
                   0,0,0,0,1,0,0,0,
                   0,0,0,1,0,0,0,0,
                   0,0,1,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,1,1,1,1,1,0,
                   0,0,0,0,0,0,0,0]
                  ])


# In:


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

for i in range(len(target)):
    visualized(np.reshape(target,(16,8)),i)


# In:


#HOPfield是1和-1
target = -1

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


# In:


#测试数据
test_data1 = 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,0,0,
                     0,1,0,1,0,0,1,0,
                     0,1,0,0,0,0,1,1,
                     0,1,0,0,1,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,0,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,1,0,0,0,
                     0,0,0,0,1,0,1,0,
                     0,1,0,1,0,0,0,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])
test_data2 = np.array([0,1,0,0,0,1,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,1,1,0,0,0,
                     0,0,1,0,1,0,0,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,1,0,1,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,1,0,0,1,0,
                     0,1,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,0,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     1,1,1,1,0,1,1,1,
                     0,0,1,0,0,0,1,0])
test_data3 = np.array([0,0,1,0,0,0,0,0,
                     0,0,1,0,1,1,0,0,
                     0,0,0,0,0,0,0,0,
                     0,0,0,0,0,0,1,0,
                     1,1,0,0,1,0,0,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,1,0,0,0,
                     0,1,0,0,0,1,0,0,
                     0,0,0,0,0,0,0,0,
                     0,0,1,1,0,0,1,1,
                     0,0,1,0,0,0,0,0,
                     0,1,0,0,0,0,0,0,
                     0,1,0,1,0,0,1,0,
                     0,1,0,1,0,0,0,0,
                     0,1,1,0,0,1,1,0,
                     0,0,0,0,0,0,0,0])
test_data4 = np.array([0,0,0,0,0,0,0,0,
                     0,0,1,1,1,1,0,0,
                     0,1,0,0,0,0,1,0,
                     0,1,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,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,1,0,0,0,0,
                     0,0,1,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,1,1,1,1,1,0,
                     0,0,0,0,0,0,0,0])


# In:


visualized(np.reshape(test_data1,(16,8)),'test1')
visualized(np.reshape(test_data2,(16,8)),'test2')
visualized(np.reshape(test_data3,(16,8)),'test3')
visualized(np.reshape(test_data4,(16,8)),'test4')


# In:


def test(test_data):
    global target
    test_data = -1
    out = net.sim()
    for i in range(len(target)):
      if ((out == target).all()):
            print('test_data is ',i)
    visualized(np.reshape(out,(16,8)),'a')
   
test(test_data4)


# In:


test_data4 = -1
out1 = net.sim()
for i in range(len(target)):
    if ((out1 == target).all()):
      print('test_Data is :',i)
visualized(np.reshape(out1,(16,8)),'a')


# In[ ]:







这是三个输出图,问题就是,不管换哪一个测试数据,都会得到同一个输出,实在是找不到问题所在。
哪里都找不到答案,只能回到鱼C老家提问了呜呜呜

老八秘制 发表于 2020-6-8 21:41:03

艹,介么高级,不会

BIO-张磊 发表于 2020-6-11 10:07:20

很高级,我有同学也做这个,他有次跟我说,python做深度学习,确实方便,但是精度不够,不知道为什么,结果不能达到预期,换了matlib同样的代码思路,结果就跑出来。
我只是听他说过,具体什么原因,我不知道。如果有错误,请指出。

_2_ 发表于 2020-6-11 12:17:48

……
据我所知,这个论坛好像没多少人干这个吧
页: [1]
查看完整版本: hopfield神经网络不能达到预期