def test_cmac(self): input_train = np.reshape(np.linspace(0, 2 * np.pi, 100), (100, 1)) input_train_before = input_train.copy() input_test = np.reshape(np.linspace(np.pi, 2 * np.pi, 50), (50, 1)) target_train = np.sin(input_train) target_train_before = target_train.copy() target_test = np.sin(input_test) cmac = algorithms.CMAC( quantization=100, associative_unit_size=32, step=0.2, verbose=False, ) cmac.train(input_train, target_train, epochs=100) predicted_test = cmac.predict(input_test) predicted_test = predicted_test.reshape((len(predicted_test), 1)) error = metrics.mean_absolute_error(target_test, predicted_test) self.assertAlmostEqual(error, 0.0024, places=4) # Test that algorithm didn't modify data samples np.testing.assert_array_equal(input_train, input_train_before) np.testing.assert_array_equal(input_train, input_train_before) np.testing.assert_array_equal(target_train, target_train_before) self.assertPickledNetwork(cmac, input_train)
def test_cmac(self): input_train = np.reshape(np.linspace(0, 2 * np.pi, 100), (100, 1)) input_train_before = input_train.copy() input_test = np.reshape(np.linspace(np.pi, 2 * np.pi, 50), (50, 1)) input_test_before = input_test.copy() target_train = np.sin(input_train) target_train_before = target_train.copy() target_test = np.sin(input_test) cmac = algorithms.CMAC( quantization=100, associative_unit_size=32, step=0.2, verbose=False, ) cmac.train(input_train, target_train, epochs=100) predicted_test = cmac.predict(input_test) error = errors.mae(target_test, predicted_test) self.assertEqual(round(error, 4), 0.0024) np.testing.assert_array_equal(input_train, input_train_before) np.testing.assert_array_equal(input_train, input_train_before) np.testing.assert_array_equal(target_train, target_train_before)
def test_cmac_training_exceptions(self): cmac = algorithms.CMAC( quantization=100, associative_unit_size=32, step=0.2, ) with self.assertRaises(ValueError): cmac.train(X_train=True, y_train=True, X_test=None, y_test=True)
def test_predict_different_inputs(self): cmac = algorithms.CMAC() data = np.array([[1, 2, 3]]).T target = np.array([[1, 2, 3]]).T cmac.train(data, target, epochs=100) self.assertInvalidVectorPred(network=cmac, input_vector=np.array([1, 2, 3]), target=target, decimal=2)
def test_cmac_multi_output(self): input_train = np.linspace(0, 2 * np.pi, 100) input_train = np.vstack([input_train, input_train]) input_test = np.linspace(0, 2 * np.pi, 100) input_test = np.vstack([input_test, input_test]) target_train = np.sin(input_train) target_test = np.sin(input_test) cmac = algorithms.CMAC( quantization=100, associative_unit_size=32, step=0.2, ) cmac.train(input_train, target_train, epochs=100) predicted_test = cmac.predict(input_test) error = errors.mae(target_test, predicted_test) self.assertEqual(round(error, 6), 0)
def test_cmac_multi_output(self): X_train = np.linspace(0, 2 * np.pi, 100) X_train = np.vstack([X_train, X_train]) X_test = np.linspace(0, 2 * np.pi, 100) X_test = np.vstack([X_test, X_test]) y_train = np.sin(X_train) y_test = np.sin(X_test) cmac = algorithms.CMAC( quantization=100, associative_unit_size=32, step=0.2, ) cmac.train(X_train, y_train, X_test, y_test, epochs=100) predicted_test = cmac.predict(X_test) error = metrics.mean_absolute_error(y_test, predicted_test) self.assertAlmostEqual(error, 0, places=6)
import numpy as np import matplotlib.pyplot as plt from neupy import algorithms, utils utils.reproducible() plt.style.use('ggplot') X_train = np.reshape(np.linspace(0, 2 * np.pi, 100), (100, 1)) X_test = np.reshape(np.sort(2 * np.pi * np.random.random(50)), (50, 1)) y_train = np.sin(X_train) y_test = np.sin(X_test) cmac = algorithms.CMAC( quantization=100, associative_unit_size=10, step=0.2, verbose=True, show_epoch=100, ) cmac.train(X_train, y_train, epochs=100) predicted_test = cmac.predict(X_test) plt.plot(X_train, y_train) plt.plot(X_test, predicted_test) plt.show()
def test_train_different_inputs(self): self.assertInvalidVectorTrain(network=algorithms.CMAC(), input_vector=np.array([1, 2, 3]), target=np.array([1, 2, 3]))
def test_train_different_inputs(self): self.assertInvalidVectorTrain(algorithms.CMAC(), np.array([1, 2, 3]), np.array([1, 2, 3]))