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; }
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; } }
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]; } } }
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); } }
void IndexEncoding::SolveOptimized(const double* pz, const SMatrix<double>& matDictionary, short* prepresentation) const { int m_nDimension = matDictionary.Cols(); Vector<double> vec_residual(m_nDimension); memcpy(vec_residual.Ptr(), pz, sizeof(double) * m_nDimension); double pre_error = vec_residual.Norm2Squared(); int num_center = matDictionary.Rows(); Vector<bool> vec_representation(num_center); if (m_nNumberDictionaryEachPartition >= 0) { vec_representation.SetValueZeros(); } double best_error; short best_idx; int m_nSubDictionarySize = num_center / m_nNumberDictionaryEachPartition; int idx_start = 0; for (int i = 0; i < m_nNumberDictionaryEachPartition; i++) { idx_start = i * m_nSubDictionarySize; //BestNextWord(vec_residual.Ptr(), matDictionary, vec_representation.Ptr(), //best_error, best_idx); BestNextWord(vec_residual.Ptr(), matDictionary, idx_start, idx_start + m_nSubDictionarySize, best_error, best_idx); if (pre_error > best_error) { vec_residual -= matDictionary[best_idx]; pre_error = best_error; //int mask_start = best_idx / m_nSubDictionarySize; //memset(vec_representation.Ptr() + mask_start, 1, sizeof(bool) * m_nSubDictionarySize); prepresentation[i] = best_idx; } else { break; } } }
int IndexEncoding::SolveOptimizedAdv3( const double* pz, const SMatrix<double> &matDictionary, int num_dic_each_partition, const SMatrix<double> &matPrecomputed, short* prepresentation) const { int num_centers = matDictionary.Rows(); int dim = matDictionary.Cols(); Vector<double> vec_x_map(num_centers); for (int i = 0; i < num_centers; i++) { vec_x_map[i] = dot(pz, matDictionary[i], dim); } return SolveOptimizedAdv3(vec_x_map, matPrecomputed, num_dic_each_partition, prepresentation); }
void IndexEncoding::BestNextWord(const double* p_residual, const SMatrix<double>& matDictionary, double& best_error, int& best_idx) const { best_error = DBL_MAX; best_idx = -1; int dim = matDictionary.Cols(); int num_center = matDictionary.Rows(); for(int i = 0; i < num_center; i++) { double err = squared_distance(p_residual, matDictionary[i], dim); if (err < best_error) { best_error = err;; best_idx = i; } } }
int IndexEncoding::SolveOptimizedAdv2(const double* pz, const SMatrix<double>& matDictionary, const SMatrix<double>& matInnerProduct, short* prepresentation) const { double e = 0; int num_centers = matDictionary.Rows(); Vector<double> vec_x_map(num_centers); for (int i = 0; i < num_centers; i++) { vec_x_map[i] = dot(pz, matDictionary[i], matDictionary.Cols()); } Vector<short> vec_pre(m_nNumberDictionaryEachPartition); if (!m_isInitialize) { memcpy(vec_pre.Ptr(), prepresentation, sizeof(short) * m_nNumberDictionaryEachPartition); } SolveOptimizedAdv2Recursive(vec_x_map, matInnerProduct, 0, prepresentation, e); int is_not_changed = 0; if (!m_isInitialize) { Vector<double> vec_residual(matDictionary.Cols()); memcpy(vec_residual.Ptr(), pz, sizeof(double) * matDictionary.Cols()); for (int i = 0; i < m_nNumberDictionaryEachPartition; i++) { VectorMinus(vec_residual.Ptr(), matDictionary[vec_pre[i]], matDictionary.Cols()); } double pre_value = vec_residual.Norm2Squared(); memcpy(vec_residual.Ptr(), pz, sizeof(double) * matDictionary.Cols()); for (int i = 0; i < m_nNumberDictionaryEachPartition; i++) { VectorMinus(vec_residual.Ptr(), matDictionary[prepresentation[i]], matDictionary.Cols()); } double after_value = vec_residual.Norm2Squared(); if (after_value > pre_value) { is_not_changed = 1; memcpy(prepresentation, vec_pre.Ptr(), sizeof(short) * m_nNumberDictionaryEachPartition); } } return is_not_changed; }
void IndexEncoding::BestNextWord(const double* p_residual, const SMatrix<double>& matDictionary, int idx_start, int idx_end, double& best_error, short& best_idx) const { best_error = DBL_MAX; best_idx = -1; int dim = matDictionary.Cols(); for(int i = idx_start; i < idx_end; i++) { double err = squared_distance(p_residual, matDictionary[i], dim); if (err < best_error) { best_error = err;; best_idx = i; } } //best_idx -= idx_start; }
void IndexEncoding::BestNextWord(double* p_residual, const SMatrix<double>& matDictionary, const bool* p_rest_dict, double& best_error, int& best_idx) const { best_error = DBL_MAX; best_idx = -1; int m_nDimension = matDictionary.Cols(); int num_words = matDictionary.Rows(); for(int i = 0; i < num_words; i++) { if (!p_rest_dict[i]) { double err = squared_distance(p_residual, matDictionary[i], m_nDimension); if (err < best_error) { best_error = err;; best_idx = i; } } } }