| 1 | /* FFNet_Eigen.cpp |
| 2 | * |
| 3 | * Copyright (C) 1994-2019 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | djmw 20020712 GPL header |
| 21 | djmw 20071202 Melder_warning<n> |
| 22 | */ |
| 23 | |
| 24 | #include "FFNet_Eigen.h"cxx . . $ `<file>/FFNet/FFNet_Eigen.h`/ |
| 25 | #include "Graphics.h"cxx . . $ `<file>/sys/Graphics.h`/ |
| 26 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 27 | |
| 28 | void FFNet_Eigen_drawIntersectioncxx . . $ FFNet_Eigen_drawIntersection(e9287a0ac5849478). (FFNetcxx . . $ FFNet# me, Eigencxx . . $ Eigen# eigen, Graphicscxx . . $ Graphics# g, integercxx . . $ integer# pcx, integercxx . . $ integer# pcy, double xmin, double xmax, double ymin, double ymax) { |
| 29 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (pcx > 0 && pcy > 0); |
| 30 | if (pcx > eigen -> numberOfEigenvaluescxx . . $ structEigen#numberOfEigenvalues. || pcy > eigen -> numberOfEigenvaluescxx . . $ structEigen#numberOfEigenvalues. || mycxx . . $ `melder/melder.h:44:9`! numberOfInputscxx . . $ structFFNet#numberOfInputs. != eigen -> dimensioncxx . . $ structEigen#dimension.) |
| 31 | return; |
| 32 | |
| 33 | double x1, x2, y1, y2; |
| 34 | if (xmax <= xmin || ymax <= ymin) |
| 35 | Graphics_inqWindowcxx . . $ Graphics_inqWindow(962850f2031ce0d0). (g, & x1, & x2, & y1, & y2); |
| 36 | |
| 37 | if (xmax <= xmin) { |
| 38 | xmin = x1; |
| 39 | xmax = x2; |
| 40 | } |
| 41 | if (ymax <= ymin) { |
| 42 | ymin = y1; |
| 43 | ymax = y2; |
| 44 | } |
| 45 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g); |
| 46 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (g, xmin, xmax, ymin, ymax); |
| 47 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfUnitsInLayercxx . . $ structFFNet#numberOfUnitsInLayer. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]; i ++) { |
| 48 | const integercxx . . $ integer# unitOffset = mycxx . . $ `melder/melder.h:44:9`! numberOfInputscxx . . $ structFFNet#numberOfInputs. + 1; |
| 49 | double c1 = 0.0, c2 = 0.0; |
| 50 | for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! numberOfInputscxx . . $ structFFNet#numberOfInputs.; j ++) { |
| 51 | c1 += mycxx . . $ `melder/melder.h:44:9`! wcxx . . $ structFFNet#w. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).mycxx . . $ `melder/melder.h:44:9`! wFirstcxx . . $ structFFNet#wFirst. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).unitOffset + i] + j - 1] * eigen -> eigenvectorscxx . . $ structEigen#eigenvectors. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).pcx] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j]; |
| 52 | c2 += mycxx . . $ `melder/melder.h:44:9`! wcxx . . $ structFFNet#w. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).mycxx . . $ `melder/melder.h:44:9`! wFirstcxx . . $ structFFNet#wFirst. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).unitOffset + i] + j - 1] * eigen -> eigenvectorscxx . . $ structEigen#eigenvectors. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).pcy] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j]; |
| 53 | } |
| 54 | double x [6], y [6], xs [3], ys [3]; |
| 55 | x [1] = x [2] = x [5] = xmin; |
| 56 | x [3] = x [4] = xmax; |
| 57 | y [1] = y [4] = y [5] = ymin; |
| 58 | y [2] = y [3] = ymax; |
| 59 | integercxx . . $ integer# ns = 0; |
| 60 | const double bias = mycxx . . $ `melder/melder.h:44:9`! wcxx . . $ structFFNet#w. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).mycxx . . $ `melder/melder.h:44:9`! wLastcxx . . $ structFFNet#wLast. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).unitOffset + i]]; |
| 61 | for (integercxx . . $ integer# j = 1; j <= 4; j++) { |
| 62 | const double p1 = c1 * x [j] + c2 * y [j] + bias; |
| 63 | const double p2 = c1 * x [j + 1] + c2 * y [j + 1] + bias; |
| 64 | const double r = fabscxx . . $ fabs(fee4f1d22bc8e617). (p1) / (fabscxx . . $ fabs(fee4f1d22bc8e617). (p1) + fabscxx . . $ fabs(fee4f1d22bc8e617). (p2)); |
| 65 | if (p1 *p2 > 0 || r == 0.0) |
| 66 | continue; |
| 67 | if (++ ns > 2) |
| 68 | break; |
| 69 | xs [ns] = x [j] + (x [j + 1] - x [j]) * r; |
| 70 | ys [ns] = y [j] + (y [j + 1] - y [j]) * r; |
| 71 | } |
| 72 | if (ns < 2) |
| 73 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"Intersection for unit ", i, U" outside range"); |
| 74 | else |
| 75 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (g, xs [1], ys [1], xs [2], ys [2]); |
| 76 | } |
| 77 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | Draw the intersection line of the decision hyperplane 'w.e-b' of the weights of unit i |
| 82 | from layer j with the plane spanned by eigenvectors pcx and pcy. |
| 83 | */ |
| 84 | void FFNet_Eigen_drawDecisionPlaneInEigenspacecxx . . $ FFNet_Eigen_drawDecisionPlaneInEigenspace(636ce0592cd4cc71). (FFNetcxx . . $ FFNet# me, Eigencxx . . $ Eigen# thee, Graphicscxx . . $ Graphics# g, integercxx . . $ integer# unit, integercxx . . $ integer# layer, |
| 85 | integercxx . . $ integer# pcx, integercxx . . $ integer# pcy, double xmin, double xmax, double ymin, double ymax) { |
| 86 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (layer > 0); |
| 87 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (unit > 0); |
| 88 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (pcx > 0 && pcy > 0); |
| 89 | if (layer > mycxx . . $ `melder/melder.h:44:9`! numberOfLayerscxx . . $ structFFNet#numberOfLayers. || unit > mycxx . . $ `melder/melder.h:44:9`! numberOfUnitsInLayercxx . . $ structFFNet#numberOfUnitsInLayer. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).layer] || pcx > thycxx . . $ `melder/melder.h:45:9`! numberOfEigenvaluescxx . . $ structEigen#numberOfEigenvalues. || pcy > thycxx . . $ `melder/melder.h:45:9`! numberOfEigenvaluescxx . . $ structEigen#numberOfEigenvalues.) |
| 90 | return; |
| 91 | const integercxx . . $ integer# numberOfUnitsInLayer_m1 = ( layer == 1 ? mycxx . . $ `melder/melder.h:44:9`! numberOfInputscxx . . $ structFFNet#numberOfInputs. : mycxx . . $ `melder/melder.h:44:9`! numberOfUnitsInLayercxx . . $ structFFNet#numberOfUnitsInLayer. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).layer - 1] ); |
| 92 | if (numberOfUnitsInLayer_m1 != thycxx . . $ `melder/melder.h:45:9`! dimensioncxx . . $ structEigen#dimension.) |
| 93 | return; |
| 94 | |
| 95 | double x1, x2, y1, y2; |
| 96 | Graphics_inqWindowcxx . . $ Graphics_inqWindow(962850f2031ce0d0). (g, & x1, & x2, & y1, & y2); |
| 97 | if (xmax <= xmin) { |
| 98 | xmin = x1; |
| 99 | xmax = x2; |
| 100 | } |
| 101 | if (ymax <= ymin) { |
| 102 | ymin = y1; |
| 103 | ymax = y2; |
| 104 | } |
| 105 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g); |
| 106 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (g, xmin, xmax, ymin, ymax); |
| 107 | |
| 108 | const integercxx . . $ integer# node = FFNet_getNodeNumberFromUnitNumbercxx . . $ FFNet_getNodeNumberFromUnitNumber(5612a7baadb7dc89). (me, unit, layer); |
| 109 | if (node < 1) |
| 110 | return; |
| 111 | |
| 112 | /* |
| 113 | Suppose p1 and p2 are the two points in the eigenplane, spanned by the eigenvectors |
| 114 | e1 and e2, where the neural net decision hyperplane intersects these eigenvectors. |
| 115 | Their coordinates in the eigenplane will be (x0*e1, 0) and (0,y0*e2). |
| 116 | At the same time, the two points are part of the decision hyperplane of the |
| 117 | chosen unit. The hyperplane equation is: |
| 118 | w.e+bias = 0, |
| 119 | where 'w' is the weight vector, 'e' is the input vector and 'b' is the bias. |
| 120 | This results in two equations for the unknown x0 and y0: |
| 121 | w.(x0*e1)+bias = 0 |
| 122 | w.(y0*e2)+bias = 0 |
| 123 | This suggests the solution for x0 and y0: |
| 124 | |
| 125 | x0 = -bias / (w.e1) |
| 126 | y0 = -bias / (w.e2) |
| 127 | |
| 128 | If w.e1 != 0 && w.e2 != 0 |
| 129 | p1 = (x0, 0) and p2 = (0, y0) |
| 130 | If w.e1 == 0 && w.e2 != 0 |
| 131 | The line is parallel to e1 and intersects e2 at y0. |
| 132 | If w.e2 == 0 && w.e1 != 0 |
| 133 | The line is parallel to e2 and intersects e1 at x0. |
| 134 | If w.e1 == 0 && w.e2 == 0 |
| 135 | Both planes are parallel, no intersection. |
| 136 | */ |
| 137 | |
| 138 | const integercxx . . $ integer# iw = mycxx . . $ `melder/melder.h:44:9`! wFirstcxx . . $ structFFNet#wFirst. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).node] - 1; |
| 139 | double we1 = 0.0, we2 = 0.0; |
| 140 | for (integercxx . . $ integer# i = 1; i <= numberOfUnitsInLayer_m1; i ++) { |
| 141 | we1 += mycxx . . $ `melder/melder.h:44:9`! wcxx . . $ structFFNet#w. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iw + i] * thycxx . . $ `melder/melder.h:45:9`! eigenvectorscxx . . $ structEigen#eigenvectors. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).pcx] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]; |
| 142 | we2 += mycxx . . $ `melder/melder.h:44:9`! wcxx . . $ structFFNet#w. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iw + i] * thycxx . . $ `melder/melder.h:45:9`! eigenvectorscxx . . $ structEigen#eigenvectors. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).pcy] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]; |
| 143 | } |
| 144 | |
| 145 | const double bias = mycxx . . $ `melder/melder.h:44:9`! wcxx . . $ structFFNet#w. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).mycxx . . $ `melder/melder.h:44:9`! wLastcxx . . $ structFFNet#wLast. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).node]]; |
| 146 | x1 = xmin; |
| 147 | x2 = xmax; |
| 148 | y1 = ymin; |
| 149 | y2 = ymax; |
| 150 | if (we1 != 0.0) { |
| 151 | x1 = -bias / we1; |
| 152 | y1 = 0.0; |
| 153 | } |
| 154 | if (we2 != 0.0) { |
| 155 | x2 = 0.0; |
| 156 | y2 = -bias / we2; |
| 157 | } |
| 158 | if (we1 == 0.0 && we2 == 0.0) { |
| 159 | Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"We cannot draw the intersection of the neural net decision plane\n" |
| 160 | "for unit ", unit, U" in layer ", layer, U" with the plane spanned by the eigenvectors because \nboth planes are parallel."); |
| 161 | return; |
| 162 | } |
| 163 | double xi [3], yi [3]; // Intersections |
| 164 | const double ni = NUMgetIntersectionsWithRectanglecxx . . $ NUMgetIntersectionsWithRectangle(427fee5e47058269). (x1, y1, x2, y2, xmin, ymin, xmax, ymax, xi, yi); |
| 165 | if (ni == 2) |
| 166 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (g, xi [1], yi [1], xi [2], yi [2]); |
| 167 | else |
| 168 | Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"There were no intersections in the drawing area.\nPlease enlarge the drawing area."); |
| 169 | |
| 170 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g); |
| 171 | } |
| 172 | |
| 173 | /* End of file FFNet_Eigen.cpp */ |