Computer Vision News - December 2020
13 Loading the Iris dataset Using the scikit-learn Python package, the dataset can be loaded in the memory, as shown earlier. Defining the SOM The SOM is created by using the following statement: The call to random_sample() generates a 40 x 40 matrix where each cell is a vector of size 4 with random values between 0.0 and 1.0. The creation of the SOM starts with statements: The pct_left calculates howmany iteration steps are remaining (%).The curr_range is the maximum Manhattan distance at step s which defines how near is it. Following, a random data item is selected and the best matching unit map node/ cell is determined: Each node/cell of the SOM is examined and the vector in the current node is updated if the current node is defined as “close” to the BMU. Define the U-Matrix The 40 x 40 vectors hold a value that corresponds to one or more data items which helps define the U-Matrix as following: Each 40 x 40 cell of the U-Matrix holds an initial 0.0 value which is updated in every iteration: Self-Organizing Maps (SOMs) with PyTorch print ("Constructing a 40x40 SOM from the iris data") map = np.random.random_sample(size=(rows, cols, dims)) for s in range(steps_max): if s % (steps_max/ 10 ) == 0 : print ("step = ", str(s)) pct_left = 1.0 - ((s * 1.0 ) / steps_max) curr_range = (int)(pct_left * d) curr_rate = pct_left * learn_max t = np.random.randint(len(data_x)) (bmu_row, bmu_col) = closest_node(data_x, t, map, rows, cols) for i in range(Rows): for j in range(Cols): if manhattan_dist(bmu_row, bmu_col, i, j) < curr_range: map[i][j] = map[i][j] + curr_rate * (data_x[t] - map[i][j]) print ("Define U-Matrix using the SOM") u_matrix = np.zeros(shape=(rows, cols), dtype=np.float64) for i in range(rows): for j in range(cols): v = map[i][j] sum_dists = 0.0 ct = 0
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=