autoConfusion Categories_to_Confusion (Categories me, Categories thee) { try { if (my size != thy size) { Melder_throw (U"Categories_to_Confusion: dimensions do not agree."); } autoCategories ul1 = Categories_selectUniqueItems (me); autoCategories ul2 = Categories_selectUniqueItems (thee); autoConfusion him = Confusion_create (ul1->size, ul2->size); for (long i = 1; i <= ul1->size; i ++) { SimpleString s = ul1->at [i]; TableOfReal_setRowLabel (him.get(), i, s -> string); } for (long i = 1; i <= ul2->size; i ++) { SimpleString s = ul2->at [i]; TableOfReal_setColumnLabel (him.get(), i, s -> string); } for (long i = 1; i <= my size; i ++) { SimpleString myi = my at [i], thyi = thy at [i]; Confusion_increase (him.get(), SimpleString_c (myi), SimpleString_c (thyi)); } return him; } catch (MelderError) { Melder_throw (me, U": no Confusion created."); } }
autoActivation FFNet_Categories_to_Activation (FFNet me, Categories thee) { try { autoCategories uniq = Categories_selectUniqueItems (thee); if (! my outputCategories) { Melder_throw (U"The FFNet does not have categories."); } long nl = OrderedOfString_isSubsetOf (uniq.get(), my outputCategories.get(), 0); if (nl == 0) { Melder_throw (U"The Categories do not match the categories of the FFNet."); } autoActivation him = Activation_create (thy size, my nOutputs); for (long i = 1; i <= thy size; i ++) { const char32 *citem = OrderedOfString_itemAtIndex_c (thee, i); long pos = OrderedOfString_indexOfItem_c (my outputCategories.get(), citem); if (pos < 1) { Melder_throw (U"The FFNet doesn't know the category ", citem, U"."); } his z [i] [pos] = 1.0; } return him; } catch (MelderError) { Melder_throw (me, U": no Activation created."); } }
int FFNet_setOutputCategories (FFNet me, Categories thee) { Categories uniq = Categories_selectUniqueItems (thee, 1); if (uniq -> size == thy size) { my outputCategories = uniq; return 1; } forget (uniq); return 0; }
Activation FFNet_Categories_to_Activation (FFNet me, Categories thee) { Activation him = NULL; Categories uniq; long i, nl, cSize = thy size, hasCategories = 1; uniq = Categories_selectUniqueItems (thee, 1); if (uniq == NULL) Melder_error1 (L"There is not enough memory to create a Categories."); if (my outputCategories == NULL) { if (my nUnitsInLayer[my nLayers] == uniq -> size) { my outputCategories = uniq; hasCategories = 0; } else { (void) Melder_error1 (L""); goto end; } } else if (! ( (nl = OrderedOfString_isSubsetOf (uniq, my outputCategories, NULL)) && nl == uniq -> size && my nOutputs >= uniq -> size)) { (void) Melder_error1 (L"The Categories do not match the categories of the FFNet."); goto end; } him = Activation_create (cSize, my nOutputs); if (him == NULL) goto end; for (i=1; i <= cSize; i++) { long pos = OrderedOfString_indexOfItem_c (my outputCategories, OrderedOfString_itemAtIndex_c (thee, i)); if (pos < 1) { (void) Melder_error3 (L"The FFNet doesn't know the category ", OrderedOfString_itemAtIndex_c (thee, i), L" from Categories."); goto end; } his z[i][pos] = 1.0; } end: if (hasCategories) forget (uniq); if (Melder_hasError ()) forget (him); return him; }
long KNN_prune_prune ( KNN me, // the classifier to be pruned double n, // pruning degree: noise, 0 <= n <= 1 double r, // pruning redundancy: noise, 0 <= n <= 1 long k // k(!) ) { autoCategories uniqueCategories = Categories_selectUniqueItems (my output.get()); if (Categories_getSize (uniqueCategories.get()) == my nInstances) return 0; long removals = 0; long ncandidates = 0; autoNUMvector <long> candidates (0L, my nInstances - 1); if (my nInstances <= 1) return 0; for (long y = 1; y <= my nInstances; y ++) { if (KNN_prune_noisy (my input.get(), my output.get(), y, k)) { if (n == 1 || NUMrandomUniform (0, 1) <= n) { KNN_removeInstance (me, y); ++ removals; } } } for (long y = 1; y <= my nInstances; ++ y) { if (KNN_prune_superfluous (my input.get(), my output.get(), y, k, 0) && ! KNN_prune_critical (my input.get(), my output.get(), y, k)) candidates [ncandidates ++] = y; } KNN_prune_sort (my input.get(), my output.get(), k, candidates.peek(), ncandidates); for (long y = 0; y < ncandidates; ++ y) { if (KNN_prune_superfluous (my input.get(), my output.get(), candidates [y], k, 0) && ! KNN_prune_critical (my input.get(), my output.get(), candidates [y], k)) { if (r == 1.0 || NUMrandomUniform (0.0, 1.0) <= r) { KNN_removeInstance (me, candidates[y]); for (long i = y + 1; i < ncandidates; ++ i) { if(candidates[i] > candidates[y]) -- candidates[i]; } ++ removals; } } } return removals; }
void FFNet_setOutputCategories (FFNet me, Categories thee) { autoCategories uniq = Categories_selectUniqueItems (thee, 1); if (uniq -> size == thy size) { my outputCategories = uniq.transfer(); } }
long KNN_prune_prune ( /////////////////////////////// // Parameters // /////////////////////////////// KNN me, // the classifier to be pruned // double n, // pruning degree: noise, 0 <= n <= 1 // double r, // pruning redundancy: noise, 0 <= n <= 1 // long k // k(!) // ) { Categories uniqueCategories = Categories_selectUniqueItems(my output, 1); if(Categories_getSize(uniqueCategories) == my nInstances) return(0); long removals = 0; long ncandidates = 0; long *candidates = NUMlvector (0, my nInstances - 1); double progress = 1 / (double) my nInstances; if(my nInstances <= 1) return(0); for (long y = 1; y <= my nInstances; y++) { if (!Melder_progress1(1 - (double) y * progress, L"Pruning noisy instances")) return(removals); if (KNN_prune_noisy(my input, my output, y, k)) { if (n == 1 || NUMrandomUniform(0, 1) <= n) { KNN_removeInstance(me, y); ++removals; } } } Melder_progress1(1.0, NULL); for (long y = 1; y <= my nInstances; ++y) { if (!Melder_progress1(1 - (double) y * progress, L"Identifying superfluous and critical instances")) return(removals); if (KNN_prune_superfluous(my input, my output, y, k, 0) && !KNN_prune_critical(my input, my output, y, k)) candidates[ncandidates++] = y; } Melder_progress1(1.0, NULL); KNN_prune_sort(my input, my output, k, candidates, ncandidates); progress = 1 / ncandidates; for (long y = 0; y < ncandidates; ++y) { if (!Melder_progress1(1 - (double) y * progress, L"Pruning superfluous non-critical instances")) return(removals); if (KNN_prune_superfluous(my input, my output, candidates[y], k, 0) && !KNN_prune_critical(my input, my output, candidates[y], k)) { if (r == 1 || NUMrandomUniform(0, 1) <= r) { KNN_removeInstance(me, candidates[y]); for(long i = y + 1; i < ncandidates; ++i) if(candidates[i] > candidates[y]) --candidates[i]; ++removals; } } } Melder_progress1(1.0, NULL); NUMlvector_free (candidates, 0); return(removals); }