Ejemplo n.º 1
0
autoPattern Matrix_to_Pattern (Matrix me, int join) {
	try {
		if (join < 1) {
			join = 1;
		}
		if ( (my ny % join) != 0) {
			Melder_throw (U"Number of rows is not a multiple of join factor.");
		}

		autoPattern thee = Pattern_create (my ny / join, join * my nx);

		long r = 0, c = 1;
		for (long i = 1; i <= my ny; i++) {
			if ( (i - 1) % join == 0) {
				r++;
				c = 1;
			}
			for (long j = 1; j <= my nx; j++) {
				thy z[r][c++] = my z[i][j];
			}
		}
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": not converted to Pattern.");
	}
}
Ejemplo n.º 2
0
autoPattern Excitations_to_Pattern (Excitations me, long join) {
	try {
		Melder_assert (my size > 0);
		Matrix m = (Matrix) my item[1];
		if (join < 1) {
			join = 1;
		}
		if ( (my size % join) != 0) {
			Melder_throw (U"Number of rows is not a multiple of join.");
		}
		autoPattern thee = Pattern_create (my size / join, join * m -> nx);
		long r = 0, c = 1;
		for (long i = 1; i <= my size; i++) {
			double *z = ( (Matrix) my item[i])->z[1];
			if ( (i - 1) % join == 0) {
				r++;
				c = 1;
			}
			for (long j = 1; j <= m -> nx; j++) {
				thy z[r][c++] = z[j];
			}
		}
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": no Pattern created.");
	}
}
Ejemplo n.º 3
0
Pattern Matrix_to_Pattern (I, int join)
{
    iam (Matrix);
	long i, j, r = 0, c = 1;
	Pattern thee = NULL;
	
    if (join < 1) join = 1;
	if ((my ny % join) != 0) return Melder_errorp1 (L"Matrix_to_Pattern:"
		"number of rows is not a multiple of join."); 
	if (! (thee = Pattern_create (my ny / join, join * my nx))) return thee;
	for (i = 1; i <= my ny; i++)
	{
		if ((i - 1) % join == 0)
		{
			r++; c = 1;
		}
		for (j = 1; j <= my nx; j++)
		{
			thy z[r][c++] = my z[i][j];
		}
	}
    return thee;
}
Ejemplo n.º 4
0
	LOOP {
		iam (FFNet);
		Melder_information (Melder_integer (my nUnitsInLayer[my nLayers] * (my nUnitsInLayer[my nLayers - 1] + 1)),
		L" weights");
	}
END

/**************** New Pattern ***************************/

FORM (Pattern_create, L"Create Pattern", 0)
	WORD (L"Name", L"1x1")
	NATURAL (L"Dimension of a pattern", L"1")
	NATURAL (L"Number of patterns", L"1")
	OK
DO
	praat_new (Pattern_create (GET_INTEGER (L"Number of patterns"),
		GET_INTEGER (L"Dimension of a pattern")), GET_STRING (L"Name"));
END

/**************** New Categories ***************************/

FORM (Categories_create, L"Create Categories", L"")
	WORD (L"Name", L"empty")
	OK
DO
	praat_new (Categories_create (), GET_STRING (L"Name"));
END

DIRECT (FFNet_help) Melder_help (L"Feedforward neural networks"); END

DIRECT (FFNet_getMinimum)
	LOOP {
Ejemplo n.º 5
0
Categories Pattern_to_Categories_cluster
(
    ///////////////////////////////
    // Parameters                //
    ///////////////////////////////

    Pattern p,              // source
                            //
    FeatureWeights fws,     // feature weights
                            //
    long k,                 // k(!)
                            //
    double s,               // clustersize constraint 0 < s <= 1
                            //
    long m                  // reseed maximum
                            //
)

{
		autoCategories categories = Categories_sequentialNumbers (k);
		if (k == p->ny)
			return categories.transfer();

		autoKNN knn = KNN_create();
		if(p->ny % k) 
			if (s > (double) (p->ny / k) / (double) (p->ny / k + 1)) 
				s = (double) (p->ny / k) / (double) (p->ny / k + 1);

		double progress = m;
		autoNUMvector <double> sizes (0L, k);
		autoNUMvector <long> seeds (0L, k);

		autoPattern centroids = (Pattern) Pattern_create (k, p->nx);
		autoNUMvector <double> beta (0L, centroids->nx);

		do
		{
			double delta;
			long nfriends  = 0;
			Melder_progress (1 - (progress - m) / progress, U"");

			for (long y = 1; y <= centroids->ny; y++)
			{
				int ifriend = 1;
				long ys = (long) lround(NUMrandomUniform(1, p->ny));

				if (nfriends)
				{
					while (ifriend)
					{
						ys = (long) lround(NUMrandomUniform(1, p->ny));
						for (long fc = 0; fc < nfriends; fc++)
						{
							ifriend = 0;
							Melder_assert (fc < k);
							if (seeds [fc] == ys)
							{
								ifriend = 1;
								break;
							}
						}
					}
				}
				Melder_assert (nfriends <= k);
				seeds [nfriends++] = ys;

				for (long x = 1; x <= centroids->nx; x++)
					centroids->z[y][x] = p->z[ys][x];
			}
			do
			{
				delta = 0;
				KNN_learn (knn.peek(), centroids.peek(), categories.peek(), kOla_REPLACE, kOla_SEQUENTIAL);
				autoCategories interim = KNN_classifyToCategories (knn.peek(), p, fws, 1, kOla_FLAT_VOTING);

				for (long x = 1; x <= k; x++)
					sizes [x] = 0;

				for (long yp = 1; yp <= categories->size; yp++)
				{
					double alfa = 1;
					Melder_assert (yp <= centroids->ny);

					for (long x = 1; x <= centroids->nx; x++)
					{
						beta[x] = centroids->z[yp][x];
					}

					for (long ys = 1; ys <= interim->size; ys++)
					{
						if (FeatureWeights_areFriends ((SimpleString) categories->item[yp], (SimpleString) interim->item[ys]))
						{
							for (long x = 1; x <= p->nx; x++)
							{
								Melder_assert (ys <= p->ny);
								if (alfa == 1)
								{
									centroids->z[yp][x] = p->z[ys][x];
								}
								else
								{
									centroids->z[yp][x] += (p->z[ys][x] - centroids->z[yp][x]) / alfa;
								}
							}
							Melder_assert (yp <= k);
							sizes [yp] ++;
							alfa++;
						}
					}

					for (long x = 1; x <= centroids->nx; x++)
					{
						delta += fabs (beta[x] - centroids->z[yp][x]);
					}
				}
			}
			while (delta != 0.0);

			double smax = sizes [1];
			double smin = sizes [1];

			for (long x = 1; x <= k; x++)
			{
				if (smax < sizes [x]) smax = sizes [x];
				if (smin > sizes [x]) smin = sizes [x];
			}

			sizes [0] = smin / smax;
			--m;
		}
		while (sizes[0] < s && m > 0);

		autoCategories output = KNN_classifyToCategories (knn.peek(), p, fws, 1, kOla_FLAT_VOTING);

		return output.transfer();
}
Ejemplo n.º 6
0
Categories Pattern_to_Categories_cluster
(
    ///////////////////////////////
    // Parameters                //
    ///////////////////////////////

    Pattern p,              // source
                            //
    FeatureWeights fws,     // feature weights
                            //
    long k,                 // k(!)
                            //
    double s,               // clustersize constraint 0 < s <= 1
                            //
    long m                  // reseed maximum
                            //
)

{
    Categories categories = NULL, output = NULL;
    KNN knn = NULL;
    Pattern centroids = NULL;
    double *sizes = NULL, *beta = NULL;
    long *seeds = NULL;

	categories = Categories_sequentialNumbers (k); cherror
    if (k == p->ny)
        return categories;

    knn = KNN_create(); cherror
    if(p->ny % k) 
        if (s > (double) (p->ny / k) / (double) (p->ny / k + 1)) 
            s = (double) (p->ny / k) / (double) (p->ny / k + 1);

    double progress = m;
    sizes = NUMdvector (0, k); cherror
    seeds = NUMlvector (0, k); cherror

    centroids = Pattern_create (k, p->nx);
    beta = NUMdvector (0, centroids->nx);

    do
    {
        double delta;
        long nfriends  = 0;
        if (!Melder_progress1(1 - (progress - m) / progress, L"")) break;

        for (long y = 1; y <= centroids->ny; y++)
        {
            int friend = 1;
            long ys = (long) lround(NUMrandomUniform(1, p->ny));

            if (nfriends)
            {
                while (friend)
                {
                    ys = (long) lround(NUMrandomUniform(1, p->ny));
                    for (long fc = 0; fc < nfriends; fc++)
                    {
                        friend = 0;
                        Melder_assert (fc < k);
                        if (seeds [fc] == ys)
                        {
                            friend = 1;
                            break;
                        }
                    }
                }
            }
			Melder_assert (nfriends <= k);
            seeds [nfriends++] = ys;

            for (long x = 1; x <= centroids->nx; x++)
                centroids->z[y][x] = p->z[ys][x];
        }
        do
        {
            delta = 0;
            KNN_learn (knn, centroids, categories, kOla_REPLACE, kOla_SEQUENTIAL);
            Categories interim = KNN_classifyToCategories (knn, p, fws, 1, kOla_FLAT_VOTING);

            for (long x = 1; x <= k; x++)
                sizes [x] = 0;

            for (long yp = 1; yp <= categories->size; yp++)
            {
                double alfa = 1;
                Melder_assert (yp <= centroids->ny);

                for (long x = 1; x <= centroids->nx; x++)
                {
                    beta[x] = centroids->z[yp][x];
                }

                for (long ys = 1; ys <= interim->size; ys++)
                {
                    if (FRIENDS(categories->item[yp], interim->item[ys]))
                    {
                        for (long x = 1; x <= p->nx; x++)
                        {
                        	Melder_assert (ys <= p->ny);
                            if (alfa == 1)
                            {
                                centroids->z[yp][x] = p->z[ys][x];
                            }
                            else
                            {
                                centroids->z[yp][x] += (p->z[ys][x] - centroids->z[yp][x]) / alfa;
                            }
                        }
                        Melder_assert (yp <= k);
                        sizes [yp] ++;
                        alfa++;
                    }
                }

                for (long x = 1; x <= centroids->nx; x++)
                {
                    delta += fabs (beta[x] - centroids->z[yp][x]);
                }
            }
            forget (interim);
        }
        while (delta);

        double smax = sizes [1];
        double smin = sizes [1];

        for (long x = 1; x <= k; x++)
        {
            if (smax < sizes [x]) smax = sizes [x];
            if (smin > sizes [x]) smin = sizes [x];
        }

        sizes [0] = smin / smax;
        --m;
    }
    while (sizes[0] < s && m > 0);

    Melder_progress1(1.0, NULL);

    output = KNN_classifyToCategories (knn, p, fws, 1, kOla_FLAT_VOTING); cherror

end:
    forget (centroids);
    forget (categories);
    forget (knn);
    NUMdvector_free (sizes, 0);
    NUMdvector_free (beta, 0);
    NUMlvector_free (seeds, 0);
    iferror return NULL;
    return output;
}