template<class ST> bool CDenseFeatures<ST>::apply_preprocessor(bool force_preprocessing) { if (m_subset_stack->has_subsets()) SG_ERROR("A subset is set, cannot call apply_preproc\n") SG_DEBUG("force: %d\n", force_preprocessing) if (feature_matrix.matrix && get_num_preprocessors()) { for (int32_t i = 0; i < get_num_preprocessors(); i++) { if ((!is_preprocessed(i) || force_preprocessing)) { set_preprocessed(i); CDensePreprocessor<ST>* p = (CDensePreprocessor<ST>*) get_preprocessor(i); SG_INFO("preprocessing using preproc %s\n", p->get_name()) if (p->apply_to_feature_matrix(this).matrix == NULL) { SG_UNREF(p); return false; } SG_UNREF(p); } } return true; }
template<class ST> ST* CDenseFeatures<ST>::get_feature_vector(int32_t num, int32_t& len, bool& dofree) { /* index conversion for subset, only for array access */ int32_t real_num=m_subset_stack->subset_idx_conversion(num); len = num_features; if (feature_matrix.matrix) { dofree = false; return &feature_matrix.matrix[real_num * int64_t(num_features)]; } ST* feat = NULL; dofree = false; if (feature_cache) { feat = feature_cache->lock_entry(real_num); if (feat) return feat; else feat = feature_cache->set_entry(real_num); } if (!feat) dofree = true; feat = compute_feature_vector(num, len, feat); if (get_num_preprocessors()) { int32_t tmp_len = len; ST* tmp_feat_before = feat; ST* tmp_feat_after = NULL; for (int32_t i = 0; i < get_num_preprocessors(); i++) { CDensePreprocessor<ST>* p = (CDensePreprocessor<ST>*) get_preprocessor(i); // temporary hack SGVector<ST> applied = p->apply_to_feature_vector( SGVector<ST>(tmp_feat_before, tmp_len)); tmp_feat_after = applied.vector; SG_UNREF(p); if (i != 0) // delete feature vector, except for the the first one, i.e., feat SG_FREE(tmp_feat_before); tmp_feat_before = tmp_feat_after; } memcpy(feat, tmp_feat_after, sizeof(ST) * tmp_len); SG_FREE(tmp_feat_after); len = tmp_len; } return feat; }
template<class ST> SGSparseVector<ST> CSparseFeatures<ST>::get_sparse_feature_vector(int32_t num) { REQUIRE(num>=0 && num<get_num_vectors(), "get_sparse_feature_vector(num=%d): num exceeds [0;%d]\n", num, get_num_vectors()-1); index_t real_num=m_subset_stack->subset_idx_conversion(num); if (sparse_feature_matrix.sparse_matrix) { return sparse_feature_matrix[real_num]; } else { SGSparseVector<ST> result; if (feature_cache) { result.features=feature_cache->lock_entry(num); if (result.features) return result; else { result.features=feature_cache->set_entry(num); } } //if (!result.features) // result.do_free=true; result.features=compute_sparse_feature_vector(num, result.num_feat_entries, result.features); if (get_num_preprocessors()) { int32_t tmp_len=result.num_feat_entries; SGSparseVectorEntry<ST>* tmp_feat_before=result.features; SGSparseVectorEntry<ST>* tmp_feat_after = NULL; for (int32_t i=0; i<get_num_preprocessors(); i++) { //tmp_feat_after=((CSparsePreprocessor<ST>*) get_preproc(i))->apply_to_feature_vector(tmp_feat_before, tmp_len); if (i!=0) // delete feature vector, except for the the first one, i.e., feat SG_FREE(tmp_feat_before); tmp_feat_before=tmp_feat_after; } if (tmp_feat_after) { memcpy(result.features, tmp_feat_after, sizeof(SGSparseVectorEntry<ST>)*tmp_len); SG_FREE(tmp_feat_after); result.num_feat_entries=tmp_len; } SG_DEBUG("len: %d len2: %d\n", result.num_feat_entries, get_num_features()) } return result ; } }