Hopfield神經網路

2021-08-13 20:20:45 字數 4134 閱讀 4921

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)

#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")

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神經網路

初次接觸hopfield神經網路,有一些約束沒有注意,導致生出很多愚蠢的想法。一 hopfield神經網路其實沒有層的概念,所有的神經元全連線就形成了乙個hopdield神經網路的基本結構。既然是全連線,那麼誰先誰後 誰左誰右就沒有意義了,反正都是互聯的。二 hopfield神經網路既然沒有層的概念...

Hopfield神經網路

多層神經網路 模式識別 相互連線型網路 通過聯想記憶去除資料中的雜訊 1982年提出的hopfield神經網路是最典型的相互鏈結型網路。當輸入模式為某種狀態時,輸出端要給出與之相應的輸出模式。如果輸入模式與輸出模式一致,稱為自聯想記憶,否則,稱為異聯想記憶。網路結構上,hopfield神經網路是一種...

Hopfield神經網路

目錄hopfield網路計算圖 hopfield網路的能量函式 hnn型別 chnn 定義 forall x in r n,forall a in a,lim d f n x f n a 0 a為吸引子 半 正 負定函式 自治系統 衡點 dot f x 李雅普諾夫候選函式v dot x nabla ...