如何修复 python 中的 hopfield 库错误

How to fix hopfield library errors in python

我有一些关于 hopfield 网络的代码。我正在尝试使用 hebbian 训练来训练一些字母,然后添加噪音并对其进行降噪。但是,我对图书馆有疑问。我进行了搜索,但找不到问题的正确答案。 Hopfieldnet 库不工作,如果我尝试替换它,我会出现其他错误。你能帮帮我吗?

 from random import randint
import numpy as np
import HopfieldNetwork
from hopfieldnet.trainers import hebbian_training
from matplotlib import pyplot as plt


# Create the training patterns
j_pattern = np.array([[1, 1, 1, 1, 1],
                      [0, 0, 1, 0, 0],
                      [0, 0, 1, 0, 0],
                      [0, 0, 1, 0, 0],
                      [0, 0, 1, 0, 0],
                      [1, 0, 1, 0, 0],
                      [1, 1, 1, 0, 0]])

a_pattern = np.array([[0, 0, 1, 0, 0],
                      [0, 1, 0, 1, 0],
                      [1, 0, 0, 0, 1],
                      [1, 1, 1, 1, 1],
                      [1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1]])

m_pattern = np.array([[1, 0, 0, 0, 1],
                      [1, 1, 0, 1, 1],
                      [1, 0, 1, 0, 1],
                      [1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1]])

e_pattern = np.array([[1, 1, 1, 1, 1],
                      [1, 0, 0, 0, 0],
                      [1, 0, 0, 0, 0],
                      [1, 1, 1, 1, 1],
                      [1, 0, 0, 0, 0],
                      [1, 0, 0, 0, 0],
                      [1, 1, 1, 1, 1]])

s_pattern = np.array([[1, 1, 1, 1, 1],
                      [1, 0, 0, 0, 0],
                      [0, 1, 0, 0, 0],
                      [0, 0, 1, 0, 0],
                      [0, 0, 0, 1, 0],
                      [0, 0, 0, 0, 1],
                      [1, 1, 1, 1, 1]])

j_pattern *= 2
j_pattern -= 1

a_pattern *= 2
a_pattern -= 1

m_pattern *= 2
m_pattern -= 1

e_pattern *= 2
e_pattern -= 1

s_pattern *= 2
s_pattern -= 1

input_patterns = np.array([j_pattern.flatten(), a_pattern.flatten(), m_pattern.flatten(), e_pattern.flatten(), s_pattern.flatten()])

# Create the neural network and train it using the training patterns
network = HopfieldNetwork(35)

hebbian_training(network, input_patterns)

# Create the test patterns by using the training patterns and adding some noise to them
# and use the neural network to denoise them
j_test = j_pattern.flatten()

for i in range(4):
    p = randint(0, 34)
    j_test[p] *= -1

j_result = network.run(j_test)

j_result.shape = (7, 5)
j_test.shape = (7, 5)

a_test = a_pattern.flatten()

for i in range(4):
    p = randint(0, 34)
    a_test[p] *= -1

a_result = network.run(a_test)

a_result.shape = (7, 5)
a_test.shape = (7, 5)

m_test = m_pattern.flatten()

for i in range(4):
    p = randint(0, 34)
    m_test[p] *= -1

m_result = network.run(m_test)

m_result.shape = (7, 5)
m_test.shape = (7, 5)

e_test = e_pattern.flatten()

for i in range(4):
    p = randint(0, 34)
    e_test[p] *= -1

e_result = network.run(e_test)

e_result.shape = (7, 5)
e_test.shape = (7, 5)

s_test = s_pattern.flatten()

for i in range(4):
    p = randint(0, 34)
    s_test[p] *= -1

s_result = network.run(s_test)

s_result.shape = (7, 5)
s_test.shape = (7, 5)

# Show the results
plt.subplot(3, 2, 1)
plt.imshow(j_test, interpolation="nearest")
plt.subplot(3, 2, 2)
plt.imshow(j_result, interpolation="nearest")

plt.subplot(3, 2, 3)
plt.imshow(a_test, interpolation="nearest")
plt.subplot(3, 2, 4)
plt.imshow(a_result, interpolation="nearest")

plt.subplot(3, 2, 5)
plt.imshow(m_test, interpolation="nearest")
plt.subplot(3, 2, 6)
plt.imshow(m_result, interpolation="nearest")

plt.subplot(3, 2, 7)
plt.imshow(e_test, interpolation="nearest")
plt.subplot(3, 2, 8)
plt.imshow(e_result, interpolation="nearest")

plt.subplot(3, 2, 9)
plt.imshow(s_test, interpolation="nearest")
plt.subplot(3, 2, 10)
plt.imshow(s_result, interpolation="nearest")

plt.show()

错误:

"C:/Users/chriss/PycharmProjects/untitled3/hopfield.py", line 3, in <module> import HopfieldNetwork ModuleNotFoundError: No module named 'HopfieldNetwork'

不确定这是否是问题的实际解决方案。我无法重现错误。代码经过 3 次轻微修改后适用于我。

(1) 进口

from random import randint
import numpy as np
from hopfieldnet.net import HopfieldNetwork  # modified import
from hopfieldnet.trainers import hebbian_training
from matplotlib import pyplot as plt

(2) 图

plt.subplot(3, 4, ...)  # 3 * 4 = 12 places for 10 plots

(3) hopfieldnet 模块 -> net.py -> class HopfieldNetwork -> 方法 run

update_list = list(range(self._num_inputs))  # list(range(...)) instead of just range(...)