神经网络学习之----Hopfield神经网络(代码实现)
生活随笔
收集整理的這篇文章主要介紹了
神经网络学习之----Hopfield神经网络(代码实现)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
思路:
定義三個(gè)訓(xùn)練測(cè)試圖片0 1 2(16*8),即三個(gè)吸引子。然后創(chuàng)建一個(gè)Hopfield神經(jīng)網(wǎng)絡(luò),把訓(xùn)練數(shù)據(jù)輸入。然后在用測(cè)試數(shù)據(jù)輸入測(cè)試結(jié)果。
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]])
#畫圖函數(shù)
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網(wǎng)絡(luò)的值是1和-1
target[target == 0] = -1
#創(chuàng)建一個(gè)hopfield神經(jīng)網(wǎng)絡(luò),吸引子為target(012)
net = nl.net.newhop(target)
#定義3個(gè)測(cè)試數(shù)據(jù)
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])
#顯示測(cè)試數(shù)據(jù)
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
#把測(cè)試數(shù)據(jù)輸入hopfield網(wǎng)絡(luò),得到輸出
out1 = net.sim([test_data1])
#判斷測(cè)試數(shù)據(jù)的數(shù)字是多少
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
#把測(cè)試數(shù)據(jù)輸入hopfield網(wǎng)絡(luò),得到輸出
out2 = net.sim([test_data2])
#判斷測(cè)試數(shù)據(jù)的數(shù)字是多少
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
#把測(cè)試數(shù)據(jù)輸入hopfield網(wǎng)絡(luò),得到輸出
out3 = net.sim([test_data3])
#判斷測(cè)試數(shù)據(jù)的數(shù)字是多少
for i in range(len(target)):
if((out3 == target[i]).all()):
print("test_data is :",i)
#顯示輸出
visualized(np.reshape(out3, (16,8)), "output3")
總結(jié)
以上是生活随笔為你收集整理的神经网络学习之----Hopfield神经网络(代码实现)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AMD OpenCL 大学课程
- 下一篇: 苹果前CEO斯卡利变身创业导师:欲寻下个