Ejemplo n.º 1
0
    def test_iterative_updates(self):
        data = np.concatenate([zero, one, two], axis=0)
        dhnet_full = algorithms.DiscreteHopfieldNetwork(mode='sync')
        dhnet_full.train(data)

        dhnet_iterative = algorithms.DiscreteHopfieldNetwork(mode='sync')
        for digit in [zero, one, two]:
            dhnet_iterative.train(digit)

        np.testing.assert_array_almost_equal(dhnet_iterative.weight,
                                             dhnet_full.weight)
Ejemplo n.º 2
0
    def test_check_limit_option(self):
        data = np.matrix([
            [1, 1, 0, 1],
            [1, 0, 1, 1],
            [0, 0, 1, 1],
        ])

        with self.assertRaises(ValueError):
            # To many data samples comparison to number of feature
            dhnet = algorithms.DiscreteHopfieldNetwork(check_limit=True)
            dhnet.train(data)

        # The same must be OK without validation
        dhnet = algorithms.DiscreteHopfieldNetwork(check_limit=False)
        dhnet.train(data)
Ejemplo n.º 3
0
    def test_argument_in_predict_method(self):
        data = np.concatenate([zero, one, two], axis=0)
        dhnet = algorithms.DiscreteHopfieldNetwork(mode='async', n_times=1)
        dhnet.train(data)

        self.assertTrue(np.any(zero != dhnet.predict(half_zero)))
        np.testing.assert_array_almost_equal(
            zero, dhnet.predict(half_zero, n_times=100))
Ejemplo n.º 4
0
 def test_predict_different_inputs(self):
     dhnet = algorithms.DiscreteHopfieldNetwork()
     data = np.array([[1, 0, 0, 1]])
     dhnet.train(data)
     self.assertInvalidVectorPred(dhnet,
                                  np.array([1, 0, 0, 1]),
                                  data,
                                  row1d=True)
Ejemplo n.º 5
0
    def test_discrete_hn_warning(self):
        with catch_stdout() as out:
            algorithms.DiscreteHopfieldNetwork(verbose=True,
                                               n_times=100,
                                               mode='sync')
            terminal_output = out.getvalue()

        self.assertIn('only in `async` mode', terminal_output)
Ejemplo n.º 6
0
    def test_check_limit_option_for_iterative_updates(self):
        data = np.matrix([
            [1, 1, 0, 1],
            [1, 0, 1, 1],
            [0, 0, 1, 1],
        ])

        dhnet = algorithms.DiscreteHopfieldNetwork(check_limit=True)
        dhnet.train(data[0])
        dhnet.train(data[1])

        with self.assertRaises(ValueError):
            dhnet.train(data[2])

        # The same must be OK without validation
        dhnet = algorithms.DiscreteHopfieldNetwork(check_limit=False)
        dhnet.train(data[0])
        dhnet.train(data[1])
        dhnet.train(data[2])
Ejemplo n.º 7
0
    def test_input_data_validation(self):
        dhnet = algorithms.DiscreteHopfieldNetwork()
        dhnet.weight = np.array([[0, 1], [1, 0]])

        # Invalid discrete input values
        with self.assertRaises(ValueError):
            dhnet.train(np.array([-1, 1]))

        with self.assertRaises(ValueError):
            dhnet.energy(np.array([-1, 1]))

        with self.assertRaises(ValueError):
            dhnet.predict(np.array([-1, 1]))
def train(patterns):

    r, c = patterns.shape
    #print('Training...' , r , c)

    #patterns = patterns.reshape((1 , c * c))

    dhnet = algorithms.DiscreteHopfieldNetwork(mode='async',
                                               n_times=2500,
                                               check_limit=False,
                                               rule='oja')
    dhnet.train(patterns)

    return dhnet
Ejemplo n.º 9
0
    def test_energy_function(self):
        input_vector = np.array([[1, 0, 0, 1, 1, 0, 0]])
        dhnet = algorithms.DiscreteHopfieldNetwork()
        dhnet.train(input_vector)

        self.assertEqual(-21, dhnet.energy(input_vector))
        self.assertEqual(3, dhnet.energy(np.array([[0, 0, 0, 0, 0, 0, 0]])))
        self.assertEqual(-21, dhnet.energy(np.array([0, 1, 1, 0, 0, 1, 1])))

        # Test energy calculatin for the 1d array
        self.assertEqual(3, dhnet.energy(np.array([0, 0, 0, 0, 0, 0, 0])))

        np.testing.assert_array_almost_equal(
            np.array([-21, 3]),
            dhnet.energy(
                np.array([
                    [0, 1, 1, 0, 0, 1, 1],
                    [0, 0, 0, 0, 0, 0, 0],
                ])))
Ejemplo n.º 10
0
def save_password(real_password, noise_level=5):
    if noise_level < 1:
        raise ValueError("`noise_level` must be equal or greater than 1.")

    binary_password = str2bin(real_password)
    bin_password_len = len(binary_password)

    data = [binary_password]

    for _ in range(noise_level):
        # The farther from the 0.5 value the less likely
        # password recovery
        noise = np.random.binomial(1, 0.55, bin_password_len)
        data.append(noise)

    dhnet = algorithms.DiscreteHopfieldNetwork(mode='sync')
    dhnet.train(np.array(data))

    return dhnet
Ejemplo n.º 11
0
    def test_discrete_hopfield_async(self):
        data = np.concatenate([zero, one, two], axis=0)
        data_before = data.copy()
        dhnet = algorithms.DiscreteHopfieldNetwork(mode='async', n_times=1000)
        dhnet.train(data)

        half_zero_before = half_zero.copy()
        np.testing.assert_array_almost_equal(zero, dhnet.predict(half_zero))
        np.testing.assert_array_almost_equal(one, dhnet.predict(half_one))
        np.testing.assert_array_almost_equal(two, dhnet.predict(half_two))

        multiple_outputs = np.vstack([zero, one, two])
        multiple_inputs = np.vstack([half_zero, half_one, half_two])
        np.testing.assert_array_almost_equal(
            multiple_outputs,
            dhnet.predict(multiple_inputs),
        )

        np.testing.assert_array_equal(data_before, data)
        np.testing.assert_array_equal(half_zero, half_zero_before)
Ejemplo n.º 12
0
    def test_discrete_hopfield_sync(self):
        data = np.concatenate([zero, one, two], axis=0)
        data_before = data.copy()
        dhnet = algorithms.DiscreteHopfieldNetwork(mode='sync')
        dhnet.train(data)

        half_zero_before = half_zero.copy()
        np.testing.assert_array_almost_equal(zero, dhnet.predict(half_zero))
        np.testing.assert_array_almost_equal(two, dhnet.predict(half_two))

        # Test predicition for the 1d array
        np.testing.assert_array_almost_equal(one,
                                             dhnet.predict(half_one.ravel()))

        multiple_inputs = np.vstack([zero, one, two])
        np.testing.assert_array_almost_equal(multiple_inputs,
                                             dhnet.predict(multiple_inputs))

        np.testing.assert_array_equal(data_before, data)
        np.testing.assert_array_equal(half_zero, half_zero_before)
Ejemplo n.º 13
0
    def test_discrete_hopfield_network_exceptions(self):
        dhnet = algorithms.DiscreteHopfieldNetwork()

        dhnet.train(np.ones((1, 5)))
        with self.assertRaises(ValueError):
            dhnet.train(np.ones((1, 6)))
Ejemplo n.º 14
0
    def test_iterative_updates_wrong_feature_shapes_exception(self):
        dhnet = algorithms.DiscreteHopfieldNetwork()
        dhnet.train(np.ones((1, 10)))

        with self.assertRaisesRegexp(ValueError, "invalid number of features"):
            dhnet.train(np.ones((1, 7)))
Ejemplo n.º 15
0
 def test_train_different_inputs(self):
     self.assertInvalidVectorTrain(
         algorithms.DiscreteHopfieldNetwork(check_limit=False),
         np.array([1, 0, 0, 1]),
         row1d=True)
Ejemplo n.º 16
0
    1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1,
    0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0
])

# In[4]:

draw_bin_image(nine.reshape((8, 8)))

# In[5]:

data = np.concatenate(
    [zero, one, two, three, four, five, six, seven, eight, nine], axis=0)

# In[6]:

dhnet = algorithms.DiscreteHopfieldNetwork(mode='async', check_limit=False)

# In[7]:

dhnet.train(data)

# In[8]:

half_zero = np.matrix([
    0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0
])

half_one = np.matrix([
    0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    dim = 0

    # For each day train the patterns
    for day in sorted_days[0:3]:

        file_nm = day + '.txt'

        value = user_bin_dict[day]
        pat_hop_net = {}

        # Get each folder access pattern
        for pat in value:

            # Create a hopfield network
            hop_net = algorithms.DiscreteHopfieldNetwork(mode='async',
                                                         check_limit=False,
                                                         n_times=300)

            # Train the network
            hop_net.train(pat)

            # Get the weight matrix
            hop_wt_mat = hop_net.get_stored_patterns()

            # Get the dimensions of the matrix
            dim = hop_wt_mat.shape[0]

            if tuple(pat) not in pat_hop_net:

                pat_hop_net[tuple(pat)] = hop_wt_mat
Ejemplo n.º 18
0
def main():
    draw_bin_image(zero.reshape((6, 5)))
    print("\n")
    draw_bin_image(one.reshape((6, 5)))
    print("\n")
    draw_bin_image(two.reshape((6, 5)))
    print("\n")
    draw_bin_image(three.reshape((6, 5)))
    print("\n")
    draw_bin_image(four.reshape((6, 5)))
    print("\n")
    draw_bin_image(five.reshape((6, 5)))
    print("\n")
    draw_bin_image(six.reshape((6, 5)))
    print("\n")
    draw_bin_image(seven.reshape((6, 5)))
    print("\n")
    draw_bin_image(eight.reshape((6, 5)))
    print("\n")
    draw_bin_image(nine.reshape((6, 5)))
    print("\n")

    data = np.concatenate([zero, one, two, three, four], axis=0)
    dhnet = algorithms.DiscreteHopfieldNetwork(mode='sync')
    dhnet.train(data)
    '''
    half_zero = np.matrix([
        0, 1, 1, 1, 0,
        1, 0, 0, 0, 1,
        1, 0, 0, 0, 1,
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
    ])'''
    ran_zero = addnoise(zero)
    print("对数字0进行随机添加噪声")
    draw_bin_image(ran_zero.reshape((6, 5)))
    print("\n")
    result = dhnet.predict(ran_zero)
    print("对数字0进行联想记忆得到结果")
    draw_bin_image(result.reshape((6, 5)))
    print("\n")

    ran_one = addnoise(one)
    print("对数字1进行随机添加噪声")
    draw_bin_image(ran_one.reshape((6, 5)))
    print("\n")
    result = dhnet.predict(ran_one)
    print("对数字1进行联想记忆得到结果")
    draw_bin_image(result.reshape((6, 5)))
    print("\n")

    ran_two = addnoise(two)
    print("对数字2进行随机添加噪声")
    draw_bin_image(ran_two.reshape((6, 5)))
    print("\n")
    result = dhnet.predict(ran_two)
    print("对数字2进行联想记忆得到结果")
    draw_bin_image(result.reshape((6, 5)))
    print("\n")

    ran_three = addnoise(three)
    print("对数字3进行随机添加噪声")
    draw_bin_image(ran_three.reshape((6, 5)))
    print("\n")
    result = dhnet.predict(ran_three)
    print("对数字3进行联想记忆得到结果")
    draw_bin_image(result.reshape((6, 5)))
    print("\n")

    ran_four = addnoise(four)
    print("对数字4进行随机添加噪声")
    draw_bin_image(ran_four.reshape((6, 5)))
    print("\n")
    result = dhnet.predict(ran_four)
    print("对数字4进行联想记忆得到结果")
    draw_bin_image(result.reshape((6, 5)))
    print("\n")

    data = np.concatenate([five, six, seven, eight, nine], axis=0)
    dhnet = algorithms.DiscreteHopfieldNetwork(mode='sync')
    dhnet.train(data)
    '''
    from neupy import utils
    utils.reproducible()

    dhnet.n_times = 400
    '''
    ran_five = addnoise(five)
    print("对数字5进行随机添加噪声")
    draw_bin_image(ran_five.reshape((6, 5)))
    print("\n")
    result = dhnet.predict(ran_five)
    print("对数字5进行联想记忆得到结果")
    draw_bin_image(result.reshape((6, 5)))
    print("\n")

    ran_six = addnoise(six)
    print("对数字6进行随机添加噪声")
    draw_bin_image(ran_six.reshape((6, 5)))
    print("\n")
    result = dhnet.predict(ran_six)
    print("对数字6进行联想记忆得到结果")
    draw_bin_image(result.reshape((6, 5)))
    print("\n")

    ran_seven = addnoise(seven)
    print("对数字7进行随机添加噪声")
    draw_bin_image(ran_seven.reshape((6, 5)))
    print("\n")
    result = dhnet.predict(ran_seven)
    print("对数字7进行联想记忆得到结果")
    draw_bin_image(result.reshape((6, 5)))
    print("\n")

    ran_eight = addnoise(eight)
    print("对数字8进行随机添加噪声")
    draw_bin_image(ran_eight.reshape((6, 5)))
    print("\n")
    result = dhnet.predict(ran_eight)
    print("对数字8进行联想记忆得到结果")
    draw_bin_image(result.reshape((6, 5)))
    print("\n")

    ran_nine = addnoise(nine)
    print("对数字9进行随机添加噪声")
    draw_bin_image(ran_nine.reshape((6, 5)))
    print("\n")
    result = dhnet.predict(ran_nine)
    print("对数字9进行联想记忆得到结果")
    draw_bin_image(result.reshape((6, 5)))
    print("\n")
Ejemplo n.º 19
0
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    1,
    1,
    0,
    0,
    1,
    0,
    0,
    0,
    0,
    1,
    1,
    1,
    1,
    1,
])

data = np.concatenate([zero, one, two], axis=0)

dhnet = algorithms.DiscreteHopfieldNetwork(mode='sync')
dhnet.train(data)

result = dhnet.predict(half_zero)
draw_bin_image(result.reshape((6, 5)))