Esempio n. 1
0
void SparseRowCpuMatrix::sgdUpdate(BaseMatrix& value,
                                   IVector& t0,
                                   real learningRate,
                                   int currentTime,
                                   real decayRate,
                                   bool useL1,
                                   bool fini) {
  std::vector<unsigned int>& localIndices = indexDictHandle_->localIndices;

  // t0 and value are vectors
  CHECK_EQ(t0.getSize(), this->height_);
  CHECK_EQ(value.width_, this->height_ * this->width_);

  if (decayRate == 0.0f) {
    if (fini) {
      return;
    }

    for (size_t i = 0; i < localIndices.size(); ++i) {
      real* g = getLocalRow(i);
      real* v = value.rowBuf(localIndices[i]);
      for (size_t j = 0; j < this->width_; ++j) {
        v[j] -= learningRate * g[j];
      }
    }
    return;
  }  // else

  if (useL1) {  // L1 decay
    if (fini) {
      for (size_t i = 0; i < this->height_; ++i) {
        real* v = value.rowBuf(i);
        int* t = t0.getData() + i;
        if (t[0] < currentTime) {
          // W(t0) -> W(t+1)
          int tDiff = currentTime - t[0];
          real delta = tDiff * learningRate * decayRate;
          simd::decayL1(v, v, delta, this->width_);
        }
      }
      return;
    }  // else

    for (size_t i = 0; i < localIndices.size(); ++i) {
      real* g = getLocalRow(i);
      real* v = value.rowBuf(localIndices[i]);
      int* t = t0.getData() + localIndices[i];
      if (t[0] < currentTime) {
        // W(t0) -> W(t)
        int tDiff = currentTime - t[0];
        real delta = tDiff * learningRate * decayRate;
        simd::decayL1(v, v, delta, this->width_);
      }

      // W(t) -> W(t+1)
      for (size_t j = 0; j < this->width_; ++j) {
        v[j] -= learningRate * g[j];
      }
      simd::decayL1(v, v, learningRate * decayRate, this->width_);

      // state update to t+1
      t[0] = currentTime + 1;
    }

  } else {  // L2 decay
    if (fini) {
      for (size_t i = 0; i < this->height_; ++i) {
        real* v = value.rowBuf(i);
        int* t = t0.getData() + i;
        if (t[0] < currentTime) {
          // W(t0) -> W(t+1)
          int tDiff = currentTime - t[0];
          real recip = 1.0f / (1.0f + tDiff * learningRate * decayRate);
          for (size_t j = 0; j < this->width_; ++j) {
            v[j] *= recip;
          }
        }
      }
      return;
    }  // else

    real recipDecay = 1.0f / (1.0f + learningRate * decayRate);

    for (size_t i = 0; i < localIndices.size(); ++i) {
      real* g = getLocalRow(i);
      real* v = value.rowBuf(localIndices[i]);
      int* t = t0.getData() + localIndices[i];
      if (t[0] < currentTime) {
        // W(t0) -> W(t)
        int tDiff = currentTime - t[0];
        real recip = 1.0f / (1.0f + tDiff * learningRate * decayRate);
        for (size_t j = 0; j < this->width_; ++j) {
          v[j] *= recip;
        }
      }

      // W(t) -> W(t+1)
      for (size_t j = 0; j < this->width_; ++j) {
        v[j] = recipDecay * (v[j] - learningRate * g[j]);
      }

      // state update to t+1
      t[0] = currentTime + 1;
    }
  }
}
boolean CAlgorithmClassifierBliffCFIS::classify(const IFeatureVector& rFeatureVector, float64& rf64Class, IVector& rClassificationValues)
{
	bliff::FeatureVector l_oFeatureVector(rFeatureVector.getSize(), 0);
	for(uint32 j=0; j<rFeatureVector.getSize(); j++)
	{
		l_oFeatureVector[j]=rFeatureVector[j];
	}

	FILE* l_pFile=::fopen(_ParameterFile_, "wb");
	::fwrite(m_oConfiguration.getDirectPointer(), m_oConfiguration.getSize(), 1, l_pFile);
	::fclose(l_pFile);

	itpp::Vec<double> l_vResult;
	double l_dResult;
	bliff::CFIS l_oBliffCFISClassifier;

	l_oBliffCFISClassifier.readParams(_ParameterFile_);
	l_vResult=l_oBliffCFISClassifier.classify(l_oFeatureVector);
	l_dResult=l_oBliffCFISClassifier.assign(l_oFeatureVector);

	if(ip_ui64OutputMode == OVP_TypeId_CFISOutputMode_ClassMembership.toUInteger())
	{
		itpp::Vec<double> l_vTmpVec(l_oBliffCFISClassifier.getNbClasses());
		l_vTmpVec.zeros();
		std::vector<double> l_oClassLabels = l_oBliffCFISClassifier.getClassLabels();

		//finding the maximal degree of fulfillment for each class
		for(uint32 j=0; j < l_oBliffCFISClassifier.getNbRules(); j++)
		{
			for(uint32 i=0; i < l_oClassLabels.size(); i++)
			{
				if((l_oBliffCFISClassifier.getRule(j)->getClass() == l_oClassLabels[i]) && (l_vResult[j]>l_vTmpVec[i]))
				{
					l_vTmpVec[i]=l_vResult[j];
				}
			}
		}

		//switching from rule fulfillment to class membership
		l_vResult=l_vTmpVec;
	}

	//converting from BLiFF++ output to OpenViBE output (classification state)
	rf64Class=l_dResult;
	rClassificationValues.setSize(l_vResult.size());
	for(size_t i=0; i<rClassificationValues.getSize(); i++)
	{
		rClassificationValues[i]=l_vResult[i];
	}

	//labelling the element of the output vector
	if(ip_ui64OutputMode == OVP_TypeId_CFISOutputMode_ClassMembership.toUInteger())
	{
		char l_sBuffer[1024];
		std::vector<double> l_vClassLabels = l_oBliffCFISClassifier.getClassLabels();
		for(uint32 i=0; i < rClassificationValues.getSize(); i++)
		{
			sprintf(l_sBuffer, "Class %d membership degree", (uint32)l_vClassLabels[i]);
			rClassificationValues.setElementLabel(i, l_sBuffer);
		}
	}
	else if(ip_ui64OutputMode == OVP_TypeId_CFISOutputMode_RuleFulfillment.toUInteger())
	{
		char l_sBuffer[1024];
		for(uint32 i=0; i < rClassificationValues.getSize(); i++)
		{
			sprintf(l_sBuffer, "Rule %d degree of fulfillment", (uint32)i+1);
			rClassificationValues.setElementLabel(i, l_sBuffer);
		}
	}
	else
	{
		this->getLogManager() << LogLevel_Warning << "Unhandled CFIS output mode " << ip_ui64OutputMode << " (" << this->getTypeManager().getEnumerationEntryNameFromValue(OVP_TypeId_CFISOutputMode, ip_ui64OutputMode) << "\n";
	}

	return true;
}