double GeneralPathSuffStatMultipleMatrixMixtureProfileProcess::ProfileSuffStatLogProb(int cat, int l)	{

	double total = 0;
	SubMatrix* mat = matrixarray[cat][l];
	if (! mat)	{
		cerr << "error : null matrix\n";
		cerr << cat << '\t' << Ncomponent << '\n';
		cerr << occupancy[cat] << '\n';
		exit(1);
	}
	const double* stat = mat->GetStationary();
	for (map<int,int>::iterator i = profilerootcount[cat][l].begin(); i!= profilerootcount[cat][l].end(); i++)	{
		total += i->second * log(stat[i->first]);
	}
	for (map<int,double>::iterator i = profilewaitingtime[cat][l].begin(); i!= profilewaitingtime[cat][l].end(); i++)	{
		total += i->second * (*mat)(i->first,i->first);
	}
	for (map<pair<int,int>, int>::iterator i = profilepaircount[cat][l].begin(); i!= profilepaircount[cat][l].end(); i++)	{
		total += i->second * log((*mat)(i->first.first, i->first.second));
	}
	if (isnan(total))	{
		cerr << "error: in GPSSMultipleMat ProfileSuffStatLogProb: nan\n";
		exit(1);
	}
	return total;
}
Exemplo n.º 2
0
 SpdMatrix::SpdMatrix(const SubMatrix &rhs, bool check)
 {
   if(check){
     if(rhs.nrow() != rhs.ncol()){
       report_error("SpdMatrix constructor was supplied a non-square"
                    "SubMatrix argument");
     }
   }
   operator=(rhs);
 }
void LMAT::add_to(SubMatrix block)const {
    if(block.nrow() != 3 || block.ncol() !=3) {
        report_error("block is the wrong size in LMAT::add_to");
    }
    double phi = phi_->value();
    block(0,0) += 1;
    block(0,1) += 1;
    block(1,1) += phi;
    block(1,2) += 1-phi;
    block(2,2) += 1;
}
Exemplo n.º 4
0
	BorderMatrix matrix2DFinder(int** matrix, int sizeX,int sizeY, int ignValue, int radius)
	{
		BorderMatrix bm;
		VectorQueue vctQ,tmpQ;
		SubMatrix sb;
		sb.radius=radius;
		int xMax,yMax,xMin,yMin;
		vector<SubMatrix> subMatrixVect;
		int i,j,k;
		i=j=k=0;
		while(i<sizeY)
		{
			while(j<sizeX)
			{
				if((matrix[j][i]!= ignValue) && (matrix[j][i]>0))
				{
					while(!vctQ.isEmpty())
						vctQ.pop();
					vctQ.push(j,i,0);

					matrix[j][i]-=2*matrix[j][i];
					tmpQ=neighbour2D(matrix,ignValue, radius,vctQ, sizeX,sizeY);
					xMax=sb.getMax(tmpQ.x);
					yMax=sb.getMax(tmpQ.y);
					xMin=sb.getMin(tmpQ.x);
					yMin=sb.getMin(tmpQ.y);
					bm.push(xMax,xMin,yMax,yMin,0,0);	
					k++;
				}
				++j;
			}
			j=0;
			++i;
		}
		BorderMatrix tmp;
		for(int l=0;l<k;l++)
		{
			tmp.push(bm.xMax.front(),bm.xMin.front(),bm.yMax.front(),bm.yMin.front(),0,0);
			matrixAbs2D(matrix,bm.xMax.front(),bm.xMin.front(),bm.yMax.front(),bm.yMin.front(), 2*radius);
			bm.pop();

		}
		return tmp;
	}
double GeneralPathSuffStatMultipleMatrixMixtureProfileProcess::LogStatProb(int site, int cat)	{

	double total = 0;
	int l = GetSubAlloc(site);
	SubMatrix* mat = matrixarray[cat][l];
	const double* stat = mat->GetStationary();
	int rootstate = GetSiteRootState(site);
	if (rootstate != -1)	{
		total += log(stat[GetSiteRootState(site)]);

		map<int,double>& waitingtime = GetSiteWaitingTime(site);
		for (map<int,double>::iterator i = waitingtime.begin(); i!= waitingtime.end(); i++)	{
			total += i->second * (*mat)(i->first,i->first);
		}

		map<pair<int,int>, int>& paircount = GetSitePairCount(site);
		for (map<pair<int,int>, int>::iterator i = paircount.begin(); i!= paircount.end(); i++)	{
			total += i->second * log((*mat)(i->first.first, i->first.second));
		}
	}
	return total;
}
Exemplo n.º 6
0
	vector<SubMatrix> matrix3DFinder(int*** matrix, int sizeX,int sizeY, int sizeZ, int ignValue, int radius)
	{
		VectorQueue vctQ,tmpQ;
		SubMatrix sb;
		int xMax,yMax,zMax,xMin,yMin,zMin;
		vector<SubMatrix> subMatrixVect;
		int i,j,k;
		i=j=k=0;
		int subSizeX;
		int subSizeY;
		int subSizeZ;
		while (k<sizeZ)
		{
			while(i<sizeY)
			{
				while(j<sizeX)
				{
					if((matrix[j][i][k]!= ignValue) && (matrix[j][i][k]>0))
					{
						while(!vctQ.isEmpty())
							vctQ.pop();
						vctQ.push(j,i,k);

						matrix[j][i][k]-=2*matrix[j][i][k];
						tmpQ=neighbour3D(matrix,ignValue, radius,vctQ);
						xMax=sb.getMax(tmpQ.x);
						yMax=sb.getMax(tmpQ.y);
						zMax=sb.getMax(tmpQ.z);
						xMin=sb.getMin(tmpQ.x);
						yMin=sb.getMin(tmpQ.y);
						zMin=sb.getMin(tmpQ.z);
						sb.xPos=xMin;
						sb.yPos=yMin;
						sb.zPos=zMin;
						subSizeX=xMax-xMin+1+2*radius;
						subSizeY=yMax-yMin+1+2*radius;
						subSizeZ=zMax-zMin+1+2*radius;
						sb.size=subSizeX*subSizeY*subSizeZ;
						sb.xSize=subSizeX;
						sb.ySize=subSizeY;
						sb.zSize=subSizeZ;
						sb.subMatrixCreation3D(matrix,subSizeX,subSizeY,subSizeZ,xMin,yMin,zMin,tmpQ,ignValue,radius);
						subMatrixVect.push_back(sb);
					}
					++j;
				}
				j=0;
				++i;
			}
			i=0;
			k++;
		}
		return subMatrixVect;
	}
Exemplo n.º 7
0
int main( int argc, char** argv ) {

    if ( argc == 6 || argc == 7 ) {
        const char* config = argv[1];
        const char* model = argv[2];
        const char* data = argv[3];
        int order = atoi( argv[4] );
        int batch = atoi( argv[5] );

        float lnZ = 0.0f;
        if ( argc > 6 ) {
            lnZ = atof( argv[6] );
            ASSERT( lnZ > 0.0f );
        }

        BatchConstructor bc( data, order, batch, 0, false );
        Network network( config );
        network.LoadParam( model );

        Matrix placeholder;
        Matrix buffer;
        double loss = 0.0;
        int nExample = 0;

        while( bc.HasNext() ) {
            bc.PrepareNext();

            const SubMatrix input = bc.GetInput();
            const SubMatrix target = bc.GetTarget();

            ExtraInfo info( input.Rows(),
                            bc.GetSentenceLength(),
                            false,
                            target,            // actually not used
                            placeholder );    // not used either
            network.Prepare( info );

            const MatrixBase& output = network.Compute( input );

            if ( lnZ == 0.0f ) {
                loss += output.Xent( target );
                nExample += output.Rows();
            }
            else {
                buffer.Reshape( output.Rows(), output.Columns() );
                buffer.Copy( output );
                buffer.Shift( -lnZ );
                buffer.Exp( buffer );
                loss += buffer.Xent( target );
                nExample += output.Rows();
            }
        }

        double avgLoss = loss / (double)nExample;
        double ppl = exp(avgLoss);
        cout << right
             << CurrentTime() << ") average cross-entropy loss of " << nExample
             << " examples: " << KGRN << avgLoss << KNRM << " or "
             << KGRN << ppl << KNRM << " in PPL " << endl;

        return EXIT_SUCCESS;
    }

    else {
        cerr << KRED;
        cerr << "Usag: " << argv[0] << " <config> <model> <numeric-data> <order> <batch-size> [lnZ]" << endl;
        cerr << "    <config>       : network configuration" << endl;
        cerr << "    <model>        : trained model corresponding to config" << endl;
        cerr << "    <numeric-data> : data set to be evaluated, in numeric form" << endl;
        cerr << "    <order>        : length of context window" << endl;
        cerr << "    <batch-size>   : mini-batch size" << endl;
        cerr << "    [lnZ]          : optional, if set, softmax is not applied" << endl;
        cerr << KNRM;
        exit( EXIT_FAILURE );
    }
}