如何修复python中的Hopfield库错误

时间:2019-05-15 13:12:09

标签: python neural-network training-data

我有一些有关Hopfield网络的代码。我正在尝试用希伯来语训练训练一些字母,然后添加噪音并对其进行降噪。但是,我对库有问题。我进行了搜索,但找不到正确的答案。 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'

1 个答案:

答案 0 :(得分:0)

不确定是不是实际的解决方案。我无法重现该错误。经过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->类HopfieldNetwork->方法run

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