예제 #1
0
void FFNet_Eigen_drawIntersection (FFNet me, Eigen eigen, Graphics g, long pcx, long pcy, double xmin, double xmax, double ymin, double ymax) {
	long ix = labs (pcx), iy = labs (pcy);
	long numberOfEigenvalues = eigen -> numberOfEigenvalues;
	long dimension = eigen -> dimension;

	if (ix > numberOfEigenvalues || iy > numberOfEigenvalues || my nInputs != dimension) {
		return;
	}
	Melder_assert (ix > 0 && iy > 0);
	double x1, x2, y1, y2;
	if (xmax <= xmin || ymax <= ymin) {
		Graphics_inqWindow (g, & x1, & x2, & y1, & y2);
	}
	if (xmax <= xmin) {
		xmin = x1;
		xmax = x2;
	}
	if (ymax <= ymin) {
		ymin = y1;
		ymax = y2;
	}
	Graphics_setInner (g);
	Graphics_setWindow (g, xmin, xmax, ymin, ymax);
	for (long i = 1; i <= my nUnitsInLayer[1]; i++) {
		long unitOffset = my nInputs + 1;
		double c1 = 0.0, c2 = 0.0, bias = my w[ my wLast[unitOffset + i] ];
		double x[6], y[6], xs[3], ys[3]; int ns = 0;
		for (long j = 1; j <= my nInputs; j++) {
			c1 += my w[ my wFirst[unitOffset + i] + j - 1 ] * eigen->eigenvectors[ix][j];
			c2 += my w[ my wFirst[unitOffset + i] + j - 1 ] * eigen->eigenvectors[iy][j];
		}
		x[1] = x[2] = x[5] = xmin; x[3] = x[4] = xmax;
		y[1] = y[4] = y[5] = ymin; y[2] = y[3] = ymax;
		for (long j = 1; j <= 4; j++) {
			double p1 = c1 * x[j  ] + c2 * y[j  ] + bias;
			double p2 = c1 * x[j + 1] + c2 * y[j + 1] + bias;
			double r = fabs (p1) / (fabs (p1) + fabs (p2));
			if (p1 *p2 > 0 || r == 0.0) {
				continue;
			}
			if (++ns > 2) {
				break;
			}
			xs[ns] = x[j] + (x[j + 1] - x[j]) * r;
			ys[ns] = y[j] + (y[j + 1] - y[j]) * r;
		}
		if (ns < 2) {
			Melder_casual (U"Intersection for unit ", i, U" outside range");
		} else {
			Graphics_line (g, xs[1], ys[1], xs[2], ys[2]);
		}
	}
	Graphics_unsetInner (g);
}
예제 #2
0
void Graphics_matrixAsSquares (Graphics g, double **matrix, long numberOfRows, long numberOfColumns, double zmin, double zmax, double cellSizeFactor, int randomFillOrder) {
	long numberOfCells = numberOfRows * numberOfColumns;
	autoPermutation p = Permutation_create (numberOfCells);
	if (randomFillOrder) {
		Permutation_permuteRandomly_inline (p.peek(), 1, numberOfCells);
	}
	double zAbsMax = fabs (zmax) > fabs (zmin) ? fabs (zmax) : fabs (zmin);
	Graphics_Colour colour = Graphics_inqColour (g);
	double x1WC, x2WC, y1WC, y2WC;
	Graphics_inqWindow (g, &x1WC, &x2WC, &y1WC, &y2WC);
	double dx = fabs (x2WC - x1WC) / numberOfColumns;
	double dy = fabs (y2WC - y1WC) / numberOfRows;
	for (long i = 1; i <= numberOfCells; i++) {
		long index = Permutation_getValueAtIndex (p.peek(), i);
		long irow = (index - 1) / numberOfColumns + 1;
		long icol = (index - 1) % numberOfColumns + 1;
		double z = matrix[irow][icol];
		z = z < zmin ? zmin : z;
		z = z > zmax ? zmax : z;
		double zweight = sqrt (fabs (z) / zAbsMax); // Area length^2)
		double xcenter = (icol - 0.5) * dx;
		double ycenter = (irow - 0.5) * dy;
		double x1 = x1WC + xcenter - zweight * 0.5 * dx * cellSizeFactor;
		x1 = x1 < x1WC ? x1WC : x1;
		double x2 = x1WC + xcenter + zweight * 0.5 * dx * cellSizeFactor;
		x2 = x2 > x2WC ? x2WC : x2;
		double y1 = y1WC + ycenter - zweight * 0.5 * dy * cellSizeFactor;
		y1 = y1 < y1WC ? y1WC : y1;
		double y2 = y1WC + ycenter + zweight * 0.5 * dy * cellSizeFactor;
		y2 = y2 > y2WC ? y2WC : y2;
		if (z > 0) {
			Graphics_setColour (g, Graphics_WHITE);
		}
		Graphics_fillRectangle (g, x1, x2, y1, y2);
		Graphics_setColour (g, colour);
		Graphics_rectangle (g, x1, x2 , y1, y2);
	}
}
예제 #3
0
파일: FFNet.cpp 프로젝트: ffostertw/praat
/* This routine is deprecated since praat-4.2.4 20040422 and will be removed in the future. */
void FFNet_drawWeightsToLayer (FFNet me, Graphics g, int layer, int scaling, int garnish) {
	if (layer < 1 || layer > my nLayers) {
		Melder_throw (U"Layer must be in [1,", my nLayers, U"].");
	}
	autoMatrix weights = FFNet_weightsToMatrix (me, layer, 0);
	Matrix_scale (weights.peek(), scaling);
	Matrix_drawAsSquares (weights.peek(), g, 0.0, 0.0, 0.0, 0.0, 0);
	if (garnish) {
		double x1WC, x2WC, y1WC, y2WC;
		Graphics_inqWindow (g, & x1WC, & x2WC, & y1WC, & y2WC);
		Graphics_textBottom (g, false, Melder_cat (U"Units in layer ", layer, U" ->"));
		if (layer == 1) {
			Graphics_textLeft (g, false, U"Input units ->");
		} else {
			Graphics_textLeft (g, false, Melder_cat (U"Units in layer ", layer - 1, U" ->"));
		}
		/* how do I find out the current settings ??? */
		Graphics_setTextAlignment (g, Graphics_RIGHT, Graphics_HALF);
		Graphics_setInner (g);
		Graphics_text (g, 0.5, weights->ny, U"bias");
		Graphics_unsetInner (g);
	}
}
예제 #4
0
/*
	Draw the intersection line of the decision hyperplane 'w.e-b' of the weights of unit i
	from layer j with the plane spanned by eigenvectors pcx and pcy.
*/
void FFNet_Eigen_drawDecisionPlaneInEigenspace (FFNet me, Eigen thee, Graphics g, long unit, long layer,
	long pcx, long pcy, double xmin, double xmax, double ymin, double ymax)
{
	if (layer < 1 || layer > my nLayers) {
		return;
	}
	if (unit < 1 || unit > my nUnitsInLayer[layer]) {
		return;
	}
	if (pcx > thy numberOfEigenvalues || pcy > thy numberOfEigenvalues) {
		return;
	}
	if (my nUnitsInLayer[layer - 1] != thy dimension) {
		return;
	}

	double x1, x2, y1, y2;
	Graphics_inqWindow (g, & x1, & x2, & y1, & y2);
	if (xmax <= xmin) {
		xmin = x1; xmax = x2;
	}
	if (ymax <= ymin) {
		ymin = y1; ymax = y2;
	}
	Graphics_setInner (g);
	Graphics_setWindow (g, xmin, xmax, ymin, ymax);

	long node = FFNet_getNodeNumberFromUnitNumber (me, unit, layer);
	if (node < 1) {
		return;
	}

	/*
		Suppose p1 and p2 are the two points in the eigenplane, spanned by the eigenvectors
		e1 and e2, where the neural net decision hyperplane intersects these eigenvectors.
		Their coordinates in the eigenplane will be (x0*e1, 0) and (0,y0*e2).
		At the same time, the two points are part of the decision hyperplane of the
		chosen unit. The hyperplane equation is:
			w.e+bias = 0,
		where 'w' is the weight vector, 'e' is the input vector and 'b' is the bias.
		This results in two equations for the unknown x0 and y0:
			w.(x0*e1)+bias = 0
			w.(y0*e2)+bias = 0
		This suggests the solution for x0 and y0:

			x0 = -bias / (w.e1)
			y0 = -bias / (w.e2)

		If w.e1 != 0 && w.e2 != 0
		 	p1 = (x0, 0) and p2 = (0, y0)
		If w.e1 == 0 && w.e2 != 0
			The line is parallel to e1 and intersects e2 at y0.
		If w.e2 == 0 && w.e1 != 0
			The line is parallel to e2 and intersects e1 at x0.
		If w.e1 == 0 && w.e2 == 0
			Both planes are parallel, no intersection.
	*/

	long iw = my wFirst[node] - 1;
	double we1 = 0.0, we2 = 0.0;
	for (long i = 1; i <= my nUnitsInLayer[layer - 1]; i++) {
		we1 += my w[iw + i] * thy eigenvectors[pcx][i];
		we2 += my w[iw + i] * thy eigenvectors[pcy][i];
	}

	double bias = my w[my wLast[node]];
	x1 = xmin; x2 = xmax;
	y1 = ymin; y2 = ymax;
	if (we1 != 0.0) {
		x1 = -bias / we1; y1 = 0.0;
	}
	if (we2 != 0.0) {
		x2 = 0.0; y2 = -bias / we2;
	}
	if (we1 == 0.0 && we2 == 0.0) {
		Melder_warning (U"We cannot draw the intersection of the neural net decision plane\n"
		    "for unit ", unit, U" in layer ", layer, U" with the plane spanned by the eigenvectors because \nboth planes are parallel.");
		return;
	}
	double xi[3], yi[3]; /* Intersections */
	double ni = NUMgetIntersectionsWithRectangle (x1, y1, x2, y2, xmin, ymin, xmax, ymax, xi, yi);
	if (ni == 2) {
		Graphics_line (g, xi[1], yi[1], xi[2], yi[2]);
	} else {
		Melder_warning (U"There were no intersections in the drawing area.\nPlease enlarge the drawing area.");
	}
	Graphics_unsetInner (g);
}