神经网络学习之----Hopfield神经网络(代码实现)
生活随笔
收集整理的這篇文章主要介紹了
神经网络学习之----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")
總結
以上是生活随笔為你收集整理的神经网络学习之----Hopfield神经网络(代码实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AMD OpenCL 大学课程
- 下一篇: IPMI如何使用IPMI进行远程运维,安