Пример #1
0
int test()
{
	string str_file_name = "E:\\research\\working\\test_data\\test_gk_means.mat";

	SMatrix<double> matZ;
	Vector<SMatrix<double> > vecmatDictionary;
	SMatrix<CodeType> mat_target;
	int num_dic;
	int is_initialize;
	int num_grouped;
	{
		MATFile* fp = matOpen(str_file_name.c_str(), "r");
		SMART_ASSERT(fp).Exit();

		mexConvert(matGetVariable(fp, "Z"), matZ);
		mexConvert(matGetVariable(fp, "all_D"), vecmatDictionary);
		mexConvert(matGetVariable(fp, "num_sub_dic_each_partition"), num_dic);
		mexConvert(matGetVariable(fp, "mat_compact_B"), mat_target);

		mxArray * para_encode = matGetVariable(fp, "para_encode");
		mexConvert(mxGetField(para_encode, 0, "is_initialize"), is_initialize);
		mexConvert(mxGetField(para_encode, 0, "num_grouped"), num_grouped);
		matClose(fp);
	}

	IndexEncoding ie;
	ie.SetIsInitialize(is_initialize);
	ie.SetNumberGroup(num_grouped);
	ie.SetEncodingType(Type_gk_means);
	ie.SetEncodingType(Type_additive_quantization);

	SMatrix<CodeType> matRepresentation;
	matRepresentation.AllocateSpace(mat_target.Rows(), mat_target.Cols());

	int num_selected_rows = 10;
	matRepresentation.AllocateSpace(num_selected_rows, mat_target.Cols());
	ie.Solve(SMatrix<double>(matZ.Ptr(), num_selected_rows, matZ.Cols()), 
		vecmatDictionary, 
		num_dic, 
		matRepresentation);
	PRINT << "good\n";
	for (int i = 0; i < matRepresentation.Rows(); i++)
	{
		for (int j = 0; j < matRepresentation.Cols(); j++)
		{
			cout << (int)(matRepresentation[i][j]) << "\t";
		}
		cout << "\n";
	}

	//for (int i = 0; i < mat_target.Rows(); i++)
	//{
	//	for (int j = 0; j < mat_target.Cols(); j++)
	//	{
	//		SMART_ASSERT(matRepresentation[i][j] == mat_target[i][j]).Exit();
	//	}
	//}

	return 0;
}
Пример #2
0
void IndexEncoding::MultiDictionaryPreprocess(const SMatrix<double> &matDictionary, 
											  int num_dic_each_partition, 
											  SMatrix<double> &matPrecomputed) const
{
	int num_all_words = matDictionary.Rows();
	int dim = matDictionary.Cols();

	matPrecomputed.AllocateSpace(num_all_words, num_all_words);
	for (int i = 0; i < num_all_words; i++)
	{
		for (int j = 0; j <= i; j++)
		{
			matPrecomputed[i][j] = dot(
				matDictionary[i], 
				matDictionary[j], 
				dim);
		}
	}
	for (int i = 0; i < num_all_words; i++)
	{
		for (int j = i + 1; j < num_all_words; j++)
		{
			matPrecomputed[i][j] = matPrecomputed[j][i];
		}
	}
}
Пример #3
0
void IndexEncoding::GKMeansPreprocess(
	const SMatrix<double> &matDictionary, 
	int num_dic_each_partition, 
	SMatrix<double>  &matPrecomputed) const
{
	int num_all_centers = matDictionary.Rows();

	matPrecomputed.AllocateSpace(num_all_centers, num_all_centers);

	int dim = matDictionary.Cols();

	for (int i = 0; i < num_all_centers; i++)
	{
		for (int j = 0; j < num_all_centers; j++)
		{
			const double* pi = matDictionary[i];
			const double* pj = matDictionary[j];

			matPrecomputed[i][j] = dot(pi, pj, dim);
		}
	}

	for (int i = 0; i < num_all_centers; i++)
	{
		matPrecomputed[i][i] = matPrecomputed[i][i] * 0.5;
	}

}
Пример #4
0
void IndexEncoding::BestNextWords(const double* p_residual, 
								  const SMatrix<double>& matDictionary, 
								  int idx_start,
								  int idx_end,
								  SMatrix<double> &mat_diff, 
								  Vector<short>& best_idx) const
{
	int m_nDimension = matDictionary.Cols();

	SMatrix<double> mat_each_diff(idx_end - idx_start, m_nDimension);

	Heap<PAIR<double> > pq;
	pq.Reserve(m_nNumBestCan);

	for (int i = idx_start; i < idx_end; i++)
	{
		const double* p_dic = matDictionary[i];

		double e = 0;
		for (int j = 0; j < m_nDimension; j++)
		{
			double d = p_residual[j] - p_dic[j];
			mat_each_diff[i - idx_start][j] = d;
			e += d * d;
		}

		if (pq.size() >= m_nNumBestCan)
		{
			const PAIR<double> &p = pq.Top();
			if (p.distance > e)
			{
				pq.popMin();

				pq.insert(PAIR<double>(i - idx_start, e));
			}
		}
		else
		{
			pq.insert(PAIR<double>(i - idx_start, e));
		}

	}

	mat_diff.AllocateSpace(m_nNumBestCan, m_nDimension);
	best_idx.AllocateSpace(m_nNumBestCan);
	for (int i = m_nNumBestCan - 1; i >= 0; i--)
	{
		PAIR<double> p;
		pq.popMin(p);
		best_idx[i] = p.index;
		memcpy(mat_diff[i], mat_each_diff[p.index], sizeof(double) * m_nDimension);
	}
}
Пример #5
0
void SearchEngineSCKMeans::ConstructQueryCodewordInnerProduct(
		double* p_query, SMatrix<double> &matQueryCodewordInnerProduct)
{
	matQueryCodewordInnerProduct.AllocateSpace(m_numTables, m_nNumCodeWordEach);

	for (int k = 0; k < m_numTables; k++)
	{
		double* p = matQueryCodewordInnerProduct[k];
		const SMatrix<double>& matCodebook = m_pvecmatCodeBook->operator[](k); 

		for (int i = 0; i < m_nNumCodeWordEach; i++)
		{
			p[i] = dot(p_query, matCodebook[i], m_nSubDimensionsEach);
		}
		p_query += m_nSubDimensionsEach;
	}
}