| 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" |
| 25 | #include "Graphics.h" |
| 26 | #include "NUM2.h" |
| 27 | |
| 28 | void FFNet_Eigen_drawIntersection (FFNet me, Eigen eigen, Graphics g, integer pcx, integer pcy, double xmin, double xmax, double ymin, double ymax) { |
| 29 | Melder_assert (pcx > 0 && pcy > 0); |
| 30 | if (pcx > eigen -> numberOfEigenvalues || pcy > eigen -> numberOfEigenvalues || my numberOfInputs != eigen -> dimension) |
| 31 | return; |
| 32 | |
| 33 | double x1, x2, y1, y2; |
| 34 | if (xmax <= xmin || ymax <= ymin) |
| 35 | Graphics_inqWindow (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_setInner (g); |
| 46 | Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 47 | for (integer i = 1; i <= my numberOfUnitsInLayer [1]; i ++) { |
| 48 | const integer unitOffset = my numberOfInputs + 1; |
| 49 | double c1 = 0.0, c2 = 0.0; |
| 50 | for (integer j = 1; j <= my numberOfInputs; j ++) { |
| 51 | c1 += my w [my wFirst [unitOffset + i] + j - 1] * eigen -> eigenvectors [pcx] [j]; |
| 52 | c2 += my w [my wFirst [unitOffset + i] + j - 1] * eigen -> eigenvectors [pcy] [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 | integer ns = 0; |
| 60 | const double bias = my w [my wLast [unitOffset + i]]; |
| 61 | for (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 = fabs (p1) / (fabs (p1) + fabs (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_casual (U"Intersection for unit ", i, U" outside range"); |
| 74 | else |
| 75 | Graphics_line (g, xs [1], ys [1], xs [2], ys [2]); |
| 76 | } |
| 77 | Graphics_unsetInner (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_drawDecisionPlaneInEigenspace (FFNet me, Eigen thee, Graphics g, integer unit, integer layer, |
| 85 | integer pcx, integer pcy, double xmin, double xmax, double ymin, double ymax) { |
| 86 | Melder_assert (layer > 0); |
| 87 | Melder_assert (unit > 0); |
| 88 | Melder_assert (pcx > 0 && pcy > 0); |
| 89 | if (layer > my numberOfLayers || unit > my numberOfUnitsInLayer [layer] || pcx > thy numberOfEigenvalues || pcy > thy numberOfEigenvalues) |
| 90 | return; |
| 91 | const integer numberOfUnitsInLayer_m1 = ( layer == 1 ? my numberOfInputs : my numberOfUnitsInLayer [layer - 1] ); |
| 92 | if (numberOfUnitsInLayer_m1 != thy dimension) |
| 93 | return; |
| 94 | |
| 95 | double x1, x2, y1, y2; |
| 96 | Graphics_inqWindow (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_setInner (g); |
| 106 | Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 107 | |
| 108 | const integer node = FFNet_getNodeNumberFromUnitNumber (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 integer iw = my wFirst [node] - 1; |
| 139 | double we1 = 0.0, we2 = 0.0; |
| 140 | for (integer i = 1; i <= numberOfUnitsInLayer_m1; i ++) { |
| 141 | we1 += my w [iw + i] * thy eigenvectors [pcx] [i]; |
| 142 | we2 += my w [iw + i] * thy eigenvectors [pcy] [i]; |
| 143 | } |
| 144 | |
| 145 | const double bias = my w [my wLast [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_warning (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 = NUMgetIntersectionsWithRectangle (x1, y1, x2, y2, xmin, ymin, xmax, ymax, xi, yi); |
| 165 | if (ni == 2) |
| 166 | Graphics_line (g, xi [1], yi [1], xi [2], yi [2]); |
| 167 | else |
| 168 | Melder_warning (U"There were no intersections in the drawing area.\nPlease enlarge the drawing area."); |
| 169 | |
| 170 | Graphics_unsetInner (g); |
| 171 | } |
| 172 | |
| 173 | /* End of file FFNet_Eigen.cpp */ |