VectorFactorsExtractor::VectorFactorsExtractor(const CRF& i_crf, vector<double>* o_parameters)
{
    o_parameters->clear();

    const Factors& factors = i_crf.GetParameters();
    const vector<double>& initialWeights = factors.GetInitialWeights();
    const vector<double>& finalWeights = factors.GetFinalWeights();
    const vector<double>& defaultWeights = factors.GetDefaultWeights();

    numberOfLabels = i_crf.GetOutputAlphabet().Size();
    numberOfFeatures = i_crf.GetInputAlphabet().Size();
    numberOfCliques = i_crf.GetWeightsAlphabet().Size();

    // Insert intial, final and default weights
    dimension = 2 * numberOfLabels + numberOfCliques;
    o_parameters->resize(dimension);
    std::copy(initialWeights.begin(), initialWeights.end(),
        o_parameters->begin());
    std::copy(finalWeights.begin(), finalWeights.end(),
        o_parameters->begin() + numberOfLabels);
    std::copy(defaultWeights.begin(), defaultWeights.end(),
        o_parameters->begin() + 2 * numberOfLabels);

    cliqueFeatureConverter.resize(numberOfCliques);
    const vector<unordered_map<int, double> >& cliquesData = factors.GetWeights();
    // Iterate over cliques
    for (size_t cliqueIndex = 0; cliqueIndex < cliquesData.size();
        ++cliqueIndex)
    {
        unordered_map<int, int> featureToPositionInVector;
        const unordered_map<int, double>& currentCliquesData = cliquesData[cliqueIndex];
        for (auto iter = currentCliquesData.begin(); iter != currentCliquesData.end(); ++iter)
        {
            o_parameters->push_back(iter->second);
            featureToPositionInVector[iter->first] = dimension;
            ++dimension;
        }
        cliqueFeatureConverter[cliqueIndex] = std::move(featureToPositionInVector);
        printf("\rVectorFactorsExtractor evaluated %zu cliques", cliqueIndex);
    }
}
示例#2
0
void CRFBinaryWriter::Write(const CRF& crf) const
{
	std::ofstream out(file, std::ios::binary);
	Tools::SetLocale(out);
    if (!out)
    {
        std::wcout << L"Can't write to "
                   << Tools::ConvertStringToWstring(file)
                   << L"    " << file.size()
                   << std::endl;
        exit(1);
    }
	// Printing alphabets;
	crf.GetInputAlphabet().Print(out);
	crf.GetOutputAlphabet().Print(out);
	crf.GetWeightsAlphabet().Print(out);
	// Printing States;
	int stateSize = crf.GetNumStates();
	out.write((char*)&stateSize, sizeof(int));
	for (int stateIndex = 0; stateIndex < stateSize; stateIndex++)
	{
		const State& state = crf.GetStates()[stateIndex];
		double initialWeight = crf.GetInitialWeights()[stateIndex];
		out.write((char*)&initialWeight, sizeof(double));
		double finalWeight = crf.GetFinalWeights()[stateIndex];
		out.write((char*)&finalWeight, sizeof(double));
		int numDestinations = state.GetNumDestinations();
		out.write((char*)&numDestinations, sizeof(int));
		for (int destIndex = 0; destIndex < numDestinations; ++destIndex)
		{
			out.write((char*)&state.GetDestinations()[destIndex], sizeof(int));
			int weightIndexesLength = state.GetWeightsIndexes()[destIndex].size();
			out.write((char*)&weightIndexesLength, sizeof(int));
			for (int weightIndex = 0; weightIndex < weightIndexesLength; ++weightIndex)
			{
				out.write((char*)&state.GetWeightsIndexes()[destIndex][weightIndex], 
					sizeof(int));
			}
		}
	}
	// Printing weights;
	const Factors& parameters = crf.GetParameters();
	for (int widx = 0; widx < parameters.GetWeightAlphabet().Size(); ++widx)
	{
		double defaultWeight = parameters.GetDefaultWeights()[widx];
		out.write((char*)&defaultWeight, sizeof(double));
        const unordered_map<int, double>& transitionWeights = parameters.GetWeights()[widx];
        int size = transitionWeights.size();
        out.write((char*)&size, sizeof(int));
        for (auto item : transitionWeights)
        {
            int index = item.first;
            double value = item.second;
            out.write((char*)&index, sizeof(int));
            out.write((char*)&value, sizeof(double));
        }
	}
	out.close();
}