void som_train_incremental(struct som *ann, struct matrix *x, unsigned int epochs) { clann_size_type s, *mess = malloc(sizeof(clann_size_type) * x->rows); clann_real_type *sample, *winner = NULL; /* * Index vector used to shuffle the input presentation sequence */ for (s = 0; s < x->rows; s++) mess[s] = s; while (ann->epoch <= epochs) { clann_shuffle((clann_int_type *) mess, x->rows); som_adjust_width(ann); som_adjust_learning_rate(ann); /* * For each input sample `s' */ for (s = 0; s < x->rows; s++) { sample = matrix_value(x, mess[s], 0); som_find_winner_neuron(ann, sample, &winner); som_incremental_adjust_of_weights(ann, sample, winner); } #if CLANN_VERBOSE printf("N. [SOM] Width: "CLANN_PRINTF", Rate: "CLANN_PRINTF" (%d).\n", ann->actual_width, ann->actual_learning_rate, ann->epoch); #endif ann->epoch += ann->step; } free((void *) mess); }
clann_void_type kmeans_train(struct kmeans *ann, const clann_matrix_type *x, clann_real_type learning_rate) { clann_size_type s, i, *mess = malloc(sizeof(clann_size_type) * x->rows); clann_real_type e, distance, minimun, *sample, *winner = NULL; ann->old_centers = clann_matrix_copy_new(&ann->centers); /* * Index vector used to shuffle the input presentation sequence */ for (s = 0; s < x->rows; s++) mess[s] = s; do { /* * For each input in the shuffle list */ clann_shuffle((clann_int_type *) mess, x->rows); e = 0; for (s = 0; s < x->rows; s++) { sample = clann_matrix_value(x, mess[s], 0); /* * Find the center most closer to current input sample */ minimun = metric_euclidean(sample, clann_matrix_value(&ann->centers, 0, 0), ann->center_size); winner = clann_matrix_value(&ann->centers, 0, 0); for (i = 1; i < ann->n_centers; i++) { distance = metric_euclidean(sample, clann_matrix_value(&ann->centers, i, 0), ann->center_size); if (distance < minimun) { minimun = distance; winner = clann_matrix_value(&ann->centers, i, 0); } } /* * Adjust winning center positions */ for (i = 0; i < ann->center_size; i++) winner[i] += learning_rate * (sample[i] - winner[i]); } /* * Compute the mean of changes */ for (i = 0; i < ann->n_centers; i++) { e += metric_euclidean(clann_matrix_value(&ann->centers, i, 0), clann_matrix_value(ann->old_centers, i, 0), ann->center_size); } e = e / ann->n_centers; #if CLANN_VERBOSE printf("N. [KMEANS] Mean centers' update: " CLANN_PRINTF ".\n", e); #endif clann_matrix_copy(&ann->centers, ann->old_centers); } while (e > ann->noticeable_change_rate); free((clann_void_type *) ann->old_centers); ann->old_centers = NULL; }