def test_simple_som(self): colors = np.array([[0., 0., 0.], [0., 0., 1.], [0., 1., 0.], [1., 0., 0.], [0., 1., 1.], [1., 0., 1.], [1., 1., 0.], [1., 1., 1.]]) distance_measures = (None, lambda x, y:(x ** 3 + y ** 3) ** (1. / 3)) for distance_measure in distance_measures: # only small SOM for speed reasons som = SimpleSOMMapper((10, 5), 200, learning_rate=0.05) # no acces when nothing is there self.assertRaises(RuntimeError, som._access_kohonen) som.train(colors) fmapped = som.forward(colors) self.assertTrue(fmapped.shape == (8, 2)) # reverse mapping rmapped = som.reverse(fmapped) if cfg.getboolean('tests', 'labile', default='yes'): # should approximately restore the input, but could fail # with bad initialisation self.assertTrue((np.round(rmapped) == colors).all())
def test_kohonen_update(self): # before update error occured when learning_rate*number of samples > 1 # here use extreme learning_rate to force bad behaviour som = SimpleSOMMapper((10, 5), 200, learning_rate=1.0) trainer = np.ones([8, 3]) som.train(trainer) # use 10 instead of 4 to allow for some randomness in the training # fail values tend to be closer to 10^30 self.assertTrue((np.abs(som.K) <= 10).all())
def test_kohonen_update(self): # before update error occurred when learning_rate*number of samples > 1 # here use extreme learning_rate to force bad behavior som = SimpleSOMMapper((10, 5), 200, learning_rate=1.0) trainer = np.ones([8,3]) som.train(trainer) # use 10 instead of 4 to allow for some randomness in the training # fail values tend to be closer to 10^30 self.assertTrue((np.abs(som.K) <= 10).all())
def test_periodic_boundaries(self): som = SimpleSOMMapper((10, 5), 200, learning_rate=0.05) test_dqdshape = np.array([5, 2, 5, 3]) # som._dqdshape only defined in newer version # this is not explicitly linked to the periodic boundary conditions, # but had trouble coming up with a simple test for them self.assertTrue((som._dqdshape == test_dqdshape).all())
def test_simple_som(self): colors = np.array([[0., 0., 0.], [0., 0., 1.], [0., 1., 0.], [1., 0., 0.], [0., 1., 1.], [1., 0., 1.], [1., 1., 0.], [1., 1., 1.]]) distance_measures = (None, lambda x, y: (x**3 + y**3)**(1. / 3)) for distance_measure in distance_measures: # only small SOM for speed reasons som = SimpleSOMMapper((10, 5), 200, learning_rate=0.05) # no acces when nothing is there self.assertRaises(RuntimeError, som._access_kohonen) som.train(colors) fmapped = som.forward(colors) self.assertTrue(fmapped.shape == (8, 2)) # reverse mapping rmapped = som.reverse(fmapped) if cfg.getboolean('tests', 'labile', default='yes'): # should approximately restore the input, but could fail # with bad initialisation self.assertTrue((np.round(rmapped) == colors).all())
import numpy as np colors = np.array([[0., 0., 0.], [0., 0., 1.], [0., 0., 0.5], [0.125, 0.529, 1.0], [0.33, 0.4, 0.67], [0.6, 0.5, 1.0], [0., 1., 0.], [1., 0., 0.], [0., 1., 1.], [1., 0., 1.], [1., 1., 0.], [1., 1., 1.], [.33, .33, .33], [.5, .5, .5], [.66, .66, .66]]) # store the names of the colors for visualization later on color_names = \ ['black', 'blue', 'darkblue', 'skyblue', 'greyblue', 'lilac', 'green', 'red', 'cyan', 'violet', 'yellow', 'white', 'darkgrey', 'mediumgrey', 'lightgrey'] som = SimpleSOMMapper((20, 30), 400, learning_rate=0.05) som.train(colors) plt.imshow(som.K, origin='lower') mapped = som(colors) plt.title('Color SOM') # SOM's kshape is (rows x columns), while matplotlib wants (X x Y) for i, m in enumerate(mapped): plt.text(m[1], m[0], color_names[i], ha='center', va='center', bbox=dict(facecolor='white', alpha=0.5, lw=0))