Ejemplo n.º 1
0
void EdgeFeatures::getFeatures(featureVector& listFeatures, DataSequence* X,
                               Model* m, int nodeIndex, int prevNodeIndex,
                               int seqLabel)
{
  // These features are only used for adjacent edge in the chain
  int nbNodes = -1;
  
  if(X->getPrecomputedFeatures())
    nbNodes = X->getPrecomputedFeatures()->getWidth();
  else
    nbNodes = (int)X->getPrecomputedFeaturesSparse()->getWidth();
  
  if( ((prevNodeIndex == nodeIndex-1) || prevNodeIndex == nodeIndex-1 + nbNodes)
     && (prevNodeIndex != -1))
  {
    feature* pFeature;
    int nbStateLabels = m->getNumberOfStates();
    for(int s1 = 0; s1 < nbStateLabels;s1++)
    {
      for(int s2 = 0; s2 < nbStateLabels;s2++)
      {
        pFeature = listFeatures.addElement();
        pFeature->id = getIdOffset(seqLabel) + s2 + s1*nbStateLabels ;
        pFeature->globalId = getIdOffset() + s2 + s1*nbStateLabels +
        seqLabel*nbStateLabels*nbStateLabels ;
        pFeature->nodeIndex = nodeIndex;
        pFeature->nodeState = s2;
        pFeature->prevNodeIndex = prevNodeIndex;
        pFeature->prevNodeState = s1;
        pFeature->sequenceLabel = seqLabel;
        pFeature->value = 1.0f;
      }
    }
  }
}
Ejemplo n.º 2
0
void RawFeatures::getFeatures(featureVector& listFeatures, DataSequence* X, Model* m,
                              int nodeIndex, int prevNodeIndex, int seqLabel)
{
  if(X->getPrecomputedFeatures() != NULL && prevNodeIndex == -1)
  {
    dMatrix * preFeatures = X->getPrecomputedFeatures();
    int nbFeatures = preFeatures->getHeight();
    feature* pFeature;
    int idState = 0;
    int nbStateLabels = m->getNumberOfStates();
    
    for(int s = 0; s < nbStateLabels; s++)
    {
      for(int f = 0; f < nbFeatures; f++)
      {
        pFeature = listFeatures.addElement();
        pFeature->id = getIdOffset(seqLabel) + f + idState*nbFeatures;
        pFeature->globalId = getIdOffset() + f + idState*nbFeatures;
        pFeature->nodeIndex = nodeIndex;
        pFeature->nodeState = s;
        pFeature->prevNodeIndex = -1;
        pFeature->prevNodeState = -1;
        pFeature->sequenceLabel = seqLabel;
        pFeature->value = preFeatures->getValue(f,nodeIndex);
      }
      idState ++;
    }
  }
}
Ejemplo n.º 3
0
void EdgeFeatures::getAllFeatures(featureVector& listFeatures, Model* m, int)
{
  int nbStateLabels = m->getNumberOfStates();
  int nbSeqLabels = m->getNumberOfSequenceLabels();
  feature* pFeature;
  if(nbSeqLabels == 0)
    nbSeqLabels = 1;
  for(int seqLabel = 0; seqLabel < nbSeqLabels;seqLabel++)
  {
    for(int s1 = 0; s1 < nbStateLabels;s1++)
    {
      for(int s2 = 0; s2 < nbStateLabels;s2++)
      {
        pFeature = listFeatures.addElement();
        pFeature->id = getIdOffset() + s2 + s1*nbStateLabels + seqLabel*nbStateLabels*nbStateLabels ;
        pFeature->globalId = getIdOffset() + s2 + s1*nbStateLabels + seqLabel*nbStateLabels*nbStateLabels ;
        pFeature->nodeIndex = featureTypeId;
        pFeature->nodeState = s2;
        pFeature->prevNodeIndex = -1;
        pFeature->prevNodeState = s1;
        pFeature->sequenceLabel = seqLabel;
        pFeature->value = 1.0f;
      }
    }
  }
}
Ejemplo n.º 4
0
void LabelEdgeFeatures::getAllFeatures(featureVector& listFeatures, Model* m, int)
{
	// TODO: Should we test for statesPerLabel matrix for authorized features.
	if(m->getNumberOfSequenceLabels() > 0)
	{
		int nbStateLabels = m->getNumberOfStates();
		int nbSeqLabels = m->getNumberOfSequenceLabels();
		feature* pFeature;
		for(int seqLabel = 0; seqLabel < nbSeqLabels; seqLabel++)
		{
			for(int s = 0; s < nbStateLabels; s++)
			{
				pFeature = listFeatures.addElement();
				pFeature->id = getIdOffset() + s + seqLabel*nbStateLabels ;
				pFeature->globalId = getIdOffset() + s + seqLabel*nbStateLabels ;
				pFeature->nodeIndex = featureTypeId;
				pFeature->nodeState = s;
				pFeature->prevNodeIndex = -1;
				pFeature->prevNodeState = -1;
				pFeature->sequenceLabel = seqLabel;
				pFeature->value = 1.0f;
			}
		}
	}
}
Ejemplo n.º 5
0
void WindowRawFeatures::getAllFeatures(featureVector& listFeatures, Model* m,
									   int nbRawFeatures)
{
	int idNode = 0;
	int nbStateLabels = m->getNumberOfStates();
	feature* pFeature;

	for(int n = -WindowSize; n <= WindowSize; n++)
	{
		for(int s = 0; s < nbStateLabels; s++)
		{
			for(int f = 0; f < nbRawFeatures; f++)
			{
				pFeature = listFeatures.addElement();
				pFeature->id = getIdOffset() + f + s*nbRawFeatures + idNode*nbStateLabels*nbRawFeatures;
				pFeature->globalId = getIdOffset() + f + s*nbRawFeatures + idNode*nbStateLabels*nbRawFeatures;
				pFeature->nodeIndex = WINDOW_RAW_FEATURE_ID;
				pFeature->nodeState = s;
				pFeature->prevNodeIndex = -1;
				pFeature->prevNodeState = -1;
				pFeature->sequenceLabel = -1;
				pFeature->value = f; 
			}
		}
		idNode++;
	}
}
Ejemplo n.º 6
0
void GateNodeFeatures::getPreGateFeatures(featureVector& listFeatures,
                                          DataSequence* X, Model* m, int nodeIndex,
                                          int prevNodeIndex, int seqLabel)
{
  if( X->getPrecomputedFeatures()==NULL || prevNodeIndex!=-1 ) return;
  listFeatures.clear();
  
  int f,g,nbNodes,nbStates,nbFeaturesDense,nbSG,gXfpg;
  
  dMatrix* preFeatures = X->getPrecomputedFeatures();
  feature* pFeature;
  
  nbNodes = preFeatures->getWidth();
  nbStates = m->getNumberOfStates();
  nbFeaturesDense = preFeatures->getHeight();
  nbSG = nbStates*nbGates;
  
  for( g=0; g<nbGates; g++ ) {
    gXfpg = g*nbFeaturesPerGate;
    for( f=0; f<nbFeaturesDense; f++ )
    {
      pFeature = listFeatures.addElement();
      pFeature->id = getIdOffset(seqLabel) + nbSG + gXfpg + f;
      pFeature->globalId = getIdOffset() + nbSG + gXfpg + f;
      pFeature->nodeIndex = nodeIndex;
      pFeature->nodeState = -1;
      pFeature->prevNodeIndex = -1;
      pFeature->prevNodeState = g; // this is a quick and dirty fix
      pFeature->sequenceLabel = seqLabel;
      pFeature->value = preFeatures->getValue(f,nodeIndex);
    }
    // Gate bias.
    pFeature = listFeatures.addElement();
    pFeature->id = getIdOffset(seqLabel) + nbSG + gXfpg + nbFeaturesDense;
    pFeature->globalId = getIdOffset() + nbSG + gXfpg + nbFeaturesDense;
    pFeature->nodeIndex = nodeIndex;
    pFeature->nodeState =
    pFeature->prevNodeIndex = -1;
    pFeature->prevNodeState = g; // this is a quick and dirty fix
    pFeature->sequenceLabel = seqLabel;
    pFeature->value = 1.0;
  }
}
Ejemplo n.º 7
0
void GateNodeFeatures::getFeatures(featureVector& listFeatures,
                                   DataSequence* X, Model* m, int nodeIndex,
                                   int prevNodeIndex, int seqLabel)
{
  if( X->getPrecomputedFeatures()==NULL || prevNodeIndex!=-1 ) return;
  
  int idx,s,f,g,nbNodes,nbStates,nbSG,nbFeaturesDense,gXfpg,sXG;
  double gateSum;
  
  dMatrix *preFeatures = X->getPrecomputedFeatures();
  feature* pFeature;
  
  nbStates = m->getNumberOfStates();
  nbSG = nbStates*nbGates;
  nbFeaturesDense = preFeatures->getHeight();
  nbNodes = preFeatures->getWidth();
  
  dVector* lambda = m->getWeights();
  dMatrix* gates = X->getGateMatrix();
  
  // Precompute gate values, (row,col) = (gate,node)
  if( gates->getHeight()!=nbGates || gates->getWidth()!=nbNodes )
    gates->resize(nbNodes,nbGates);
  for( g=0; g<nbGates; g++ ) {
    gXfpg = g*(nbFeaturesDense+1); // 1: bias
    
    // gate bias
    idx = getIdOffset() + nbSG + gXfpg + nbFeaturesDense;
    gateSum = (*lambda)[idx];
    
    // gate sum
    for( f=0; f<nbFeaturesDense; f++ ) {
      idx = getIdOffset() + nbSG + gXfpg + f;
      gateSum += (*lambda)[idx] * preFeatures->getValue(f,nodeIndex);
    }
    gates->setValue(g,nodeIndex,gate(gateSum));
  }
  
  // Then compute node features from gate values
  for( s=0; s<nbStates; s++ ) {
    sXG = s*nbGates;
    for( g=0; g<nbGates; g++ ) {
      pFeature = listFeatures.addElement();
      pFeature->id = getIdOffset(seqLabel) + g + sXG;
      pFeature->globalId = getIdOffset() + g + sXG;
      pFeature->nodeIndex = nodeIndex;
      pFeature->nodeState = s;
      pFeature->prevNodeIndex = -1;
      pFeature->prevNodeState = g; // quick-and-dirty fix
      pFeature->sequenceLabel = seqLabel;
      pFeature->value = gates->getValue(g,nodeIndex);
      pFeature->featureTypeId = GATE_NODE_FEATURE_ID;
    }
  }
}
Ejemplo n.º 8
0
void GateNodeFeatures::getAllFeatures(featureVector& listFeatures, Model* m, int nbRawFeatures)
{
  int s, f, g, nbStates, nbSG, sXG, gXfpg;
  feature* pFeature;
  
  nbStates = m->getNumberOfStates();
  nbSG = nbStates*nbGates;
  
  // Node features.
  for( s=0; s<nbStates; s++ ) {
    sXG = s*nbGates;
    for( g=0; g<nbGates; g++ ) {
      pFeature = listFeatures.addElement();
      pFeature->id = getIdOffset() + g + sXG;
      pFeature->globalId = getIdOffset() + g + sXG;
      pFeature->nodeIndex = GATE_NODE_FEATURE_ID;
      pFeature->nodeState = s;
      pFeature->prevNodeIndex = -1;
      pFeature->prevNodeState = -1;
      pFeature->sequenceLabel = -1;
      pFeature->value = g;
    }
  }
  
  // Gate features.
  for( g=0; g<nbGates; g++ ) {
    gXfpg = g*nbFeaturesPerGate;
    for( f=0; f<nbFeaturesPerGate; f++ ) {
      pFeature = listFeatures.addElement();
      pFeature->id = getIdOffset() + nbSG + gXfpg+ f;
      pFeature->globalId = getIdOffset() + nbSG + gXfpg+ f;
      pFeature->nodeIndex = GATE_NODE_FEATURE_ID;
      pFeature->nodeState = -1;
      pFeature->prevNodeIndex = -1;
      pFeature->prevNodeState = -1;
      pFeature->sequenceLabel = -1;
      pFeature->value = f;
    }
  }
}
Ejemplo n.º 9
0
void LabelEdgeFeatures::getFeatures(featureVector& listFeatures, DataSequence*, Model* m, int nodeIndex, int prevNodeIndex, int seqLabel)
{
	if(seqLabel != -1 && m->getNumberOfSequenceLabels() > 0 && prevNodeIndex == -1)
	{
		feature* pFeature;
		int nbStateLabels = m->getNumberOfStates();
		for(int s = 0; s < nbStateLabels; s++)
		{
			pFeature = listFeatures.addElement();
			pFeature->id = getIdOffset(seqLabel) + s;
			pFeature->globalId = getIdOffset() + s + seqLabel*nbStateLabels ;
			pFeature->nodeIndex = nodeIndex;
			pFeature->nodeState = s;
			pFeature->prevNodeIndex = -1;
			pFeature->prevNodeState = -1;
			pFeature->sequenceLabel = seqLabel;
			pFeature->value = 1.0f;
		}
	}
}
Ejemplo n.º 10
0
void RawFeatures::getAllFeatures(featureVector& listFeatures, Model* m, int NbRawFeatures)
{
  int nbStateLabels = m->getNumberOfStates();
  feature* pFeature;
  
  for(int s = 0; s < nbStateLabels; s++)
  {
    for(int f = 0; f < NbRawFeatures; f++)
    {
      pFeature = listFeatures.addElement();
      pFeature->id = getIdOffset() + f + s*NbRawFeatures;
      pFeature->globalId = getIdOffset() + f + s*NbRawFeatures;
      pFeature->nodeIndex = featureTypeId;
      pFeature->nodeState = s;
      pFeature->prevNodeIndex = -1;
      pFeature->prevNodeState = -1;
      pFeature->sequenceLabel = -1;
      pFeature->value = f;
    }
  }
}
Ejemplo n.º 11
0
void EdgeObservationFeatures::getAllFeatures(featureVector& listFeatures, Model* m, 
								  int)
/* We dont need the number of raw features as the number of edge feature is
 * independant from the size of the windows
 */
{
	int nbStateLabels = m->getNumberOfStates();
	int nbSeqLabels = m->getNumberOfSequenceLabels();
	feature* pFeature;
	if(nbSeqLabels == 0)
		nbSeqLabels = 1;
	for(int seqLabel = 0; seqLabel < nbSeqLabels;seqLabel++)
	{
		for(int s1 = 0; s1 < nbStateLabels;s1++)
		{
			for(int s2 = 0; s2 < nbStateLabels;s2++)
			{
				for(int f1 = 0; f1 < nbObservationFeatures;f1++)
				{
					for(int f2 = 0; f2 < nbObservationFeatures;f2++)
					{
						pFeature = listFeatures.addElement();
						pFeature->id = getIdOffset() + f2+ s2*nbObservationFeatures + f1*nbStateLabels*nbObservationFeatures + s1*nbStateLabels*nbObservationFeatures*nbObservationFeatures + seqLabel*nbStateLabels*nbStateLabels*nbObservationFeatures*nbObservationFeatures ;
						pFeature->globalId = getIdOffset() + + f2+ s2*nbObservationFeatures + f1*nbStateLabels*nbObservationFeatures + s1*nbStateLabels*nbObservationFeatures*nbObservationFeatures + seqLabel*nbStateLabels*nbStateLabels*nbObservationFeatures*nbObservationFeatures ;
						pFeature->nodeIndex = featureTypeId;
						pFeature->nodeState = s2;
						pFeature->prevNodeIndex = -1;
						pFeature->prevNodeState = s1;
						pFeature->sequenceLabel = seqLabel;
						pFeature->value = 1.0f;
					}
				}
			}
		}
	}
}
Ejemplo n.º 12
0
void WindowRawFeatures::getFeatures(featureVector& listFeatures,
									DataSequence* X, Model* m, int nodeIndex,
									int prevNodeIndex, int seqLabel)
{
	int idNode = 0;
	int nbStateLabels = m->getNumberOfStates();
	int nbFeaturesDense = 0; 

	if(X->getPrecomputedFeatures() != NULL && prevNodeIndex == -1)
	{		
		dMatrix * preFeatures = X->getPrecomputedFeatures();
		nbFeaturesDense = preFeatures->getHeight();
		int nbNodes = preFeatures->getWidth();
		feature* pFeature;

		for(int n = nodeIndex - WindowSize; n <= nodeIndex + WindowSize; n++)
		{
			if(n >= 0 && n < nbNodes)
			{					
				for(int s = 0; s < nbStateLabels; s++)
				{
					for(int f = 0; f < nbFeaturesDense; f++)
					{
						pFeature = listFeatures.addElement();
						pFeature->id = getIdOffset(seqLabel) + f + s*nbFeaturesDense + idNode*nbStateLabels*nbFeaturesDense;
						pFeature->globalId = getIdOffset() + f + s*nbFeaturesDense + idNode*nbStateLabels*nbFeaturesDense;
						pFeature->nodeIndex = nodeIndex;
						pFeature->nodeState = s;
						pFeature->prevNodeIndex = -1;
						pFeature->prevNodeState = -1;
						pFeature->sequenceLabel = seqLabel;
						pFeature->value = preFeatures->getValue(f,n); // TODO: Optimize
					}										
				}
			}
			idNode++;
		}
	}
	
	// Load Sparse raw features
	int offsetOfPreFeatures = idNode*nbStateLabels*nbFeaturesDense;

	if(X->getPrecomputedFeaturesSparse() != NULL && prevNodeIndex == -1)
	{
		dMatrixSparse * preFeaturesSparse = X->getPrecomputedFeaturesSparse();		
		int nbFeaturesSparse = (int)preFeaturesSparse->getHeight();
		int nbNodes = (int)preFeaturesSparse->getWidth();
		feature* pFeature;
		idNode = 0;
		
		for(int n = nodeIndex - WindowSize; n <= nodeIndex + WindowSize; n++)
		{
			if(n >= 0 && n < nbNodes)
			{	
				int irIndex = (int)preFeaturesSparse->getJc()->getValue(n);
				size_t numElementsInCol = preFeaturesSparse->getJc()->getValue(n+1) - irIndex;
				for(int s = 0; s < nbStateLabels; s++)
				{
					for(unsigned int i = 0; i < numElementsInCol; i++)
					{						
						int f = (int)preFeaturesSparse->getIr()->getValue(irIndex + i);// feature ID 					
						pFeature = listFeatures.addElement();					
						pFeature->id = getIdOffset(seqLabel) + f + s*nbFeaturesSparse + idNode*nbStateLabels*nbFeaturesSparse + offsetOfPreFeatures;						
						pFeature->globalId = getIdOffset() + f + s*nbFeaturesSparse + idNode*nbStateLabels*nbFeaturesSparse + offsetOfPreFeatures;
						
						pFeature->nodeIndex = nodeIndex;
						pFeature->nodeState = s;
						pFeature->prevNodeIndex = -1;
						pFeature->prevNodeState = -1;
						pFeature->sequenceLabel = seqLabel;
						
						pFeature->value = preFeaturesSparse->getPr()->getValue(irIndex + i);
					}					
				}
			}
			idNode++;
		}
	}
}
Ejemplo n.º 13
0
void EdgeObservationFeatures::getFeatures(featureVector& listFeatures, DataSequence* X, 
							   Model* m, int nodeIndex, int prevNodeIndex, 
							   int seqLabel)
{
	// These features are only used for adjacent edge in the chain	
	int nbNodes = -1;
	
	if(X->getPrecomputedFeatures())
	{
		nbNodes = X->getPrecomputedFeatures()->getWidth();
	
		if( ((prevNodeIndex == nodeIndex-1) || prevNodeIndex == nodeIndex-1 + nbNodes)
			&& (prevNodeIndex != -1))
		{
			dMatrix * preFeatures = X->getPrecomputedFeatures();
			feature* pFeature;
			int nbStateLabels = m->getNumberOfStates();
			for(int f1 = 0; f1 < nbObservationFeatures;f1++)
			{
				for(int f2 = 0; f2 < nbObservationFeatures;f2++)
				{
					if(preFeatures->getValue(f1,prevNodeIndex) && preFeatures->getValue(f2,nodeIndex))
					{
						for(int s1 = 0; s1 < nbStateLabels;s1++)
						{
							for(int s2 = 0; s2 < nbStateLabels;s2++)
							{
								pFeature = listFeatures.addElement();
								pFeature->id = getIdOffset(seqLabel) + f2+ s2*nbObservationFeatures + f1*nbStateLabels*nbObservationFeatures + s1*nbStateLabels*nbObservationFeatures*nbObservationFeatures;
								pFeature->globalId = getIdOffset() + f2+ s2*nbObservationFeatures + f1*nbStateLabels*nbObservationFeatures + s1*nbStateLabels*nbObservationFeatures*nbObservationFeatures+ seqLabel*nbStateLabels*nbStateLabels*nbObservationFeatures*nbObservationFeatures ;
								pFeature->nodeIndex = nodeIndex;
								pFeature->nodeState = s2;
								pFeature->prevNodeIndex = prevNodeIndex;
								pFeature->prevNodeState = s1;
								pFeature->sequenceLabel = seqLabel;
								pFeature->value = 1.0f;
							}
						}
					}
				}
			}
		}
	}
	else
	{
		nbNodes = (int)X->getPrecomputedFeaturesSparse()->getWidth();
		if( ((prevNodeIndex == nodeIndex-1) || prevNodeIndex == nodeIndex-1 + nbNodes)
			&& (prevNodeIndex != -1))
		{
			dMatrixSparse * preFeaturesSparse = X->getPrecomputedFeaturesSparse();		
			int nbFeaturesSparse = (int)preFeaturesSparse->getHeight();
			feature* pFeature;
			int nbStateLabels = m->getNumberOfStates();

			int irIndex1 = (int)preFeaturesSparse->getJc()->getValue(prevNodeIndex);
			size_t numElementsInCol1 = preFeaturesSparse->getJc()->getValue(prevNodeIndex+1) - irIndex1;
			int irIndex2 = (int)preFeaturesSparse->getJc()->getValue(nodeIndex);
			size_t numElementsInCol2 = preFeaturesSparse->getJc()->getValue(nodeIndex+1) - irIndex2;

			for(int i1 = 0; i1 < numElementsInCol1;i1++)
			{
				int f1 = (int)preFeaturesSparse->getIr()->getValue(irIndex1 + i1);
				if (f1 >= nbObservationFeatures)
					break;
				for(int i2 = 0; i2 < numElementsInCol2;i2++)
				{
					int f2 = (int)preFeaturesSparse->getIr()->getValue(irIndex2 + i2);
					if (f2 >= nbObservationFeatures)
						break;
					for(int s1 = 0; s1 < nbStateLabels;s1++)
					{
						for(int s2 = 0; s2 < nbStateLabels;s2++)
						{
							pFeature = listFeatures.addElement();
							pFeature->id = getIdOffset(seqLabel) + f2+ s2*nbObservationFeatures + f1*nbStateLabels*nbObservationFeatures + s1*nbStateLabels*nbObservationFeatures*nbObservationFeatures;
							pFeature->globalId = getIdOffset() + f2+ s2*nbObservationFeatures + f1*nbStateLabels*nbObservationFeatures + s1*nbStateLabels*nbObservationFeatures*nbObservationFeatures+ seqLabel*nbStateLabels*nbStateLabels*nbObservationFeatures*nbObservationFeatures ;
							pFeature->nodeIndex = nodeIndex;
							pFeature->nodeState = s2;
							pFeature->prevNodeIndex = prevNodeIndex;
							pFeature->prevNodeState = s1;
							pFeature->sequenceLabel = seqLabel;
							pFeature->value = 1.0f;
						}
					}
				}
			}
		}
	}

}
Ejemplo n.º 14
0
experienceNet::experienceNet(const featureVector& cVec, const experienceNetSettings& _cSet)
{
	*this = experienceNet(cVec.inputSize(), _cSet);
};