| 24 | namespace embedx { |
| 25 | |
| 26 | class DSSMInstReader : public EmbedInstanceReader { |
| 27 | private: |
| 28 | bool is_train_ = true; |
| 29 | int num_neg_ = 5; |
| 30 | bool add_node_ = true; |
| 31 | |
| 32 | private: |
| 33 | DeepFlow flow_; |
| 34 | // for training/predicting data |
| 35 | vec_int_t pos_items_; |
| 36 | std::vector<vec_pair_t> feats_list_; |
| 37 | std::vector<vec_int_t> neg_items_list_; |
| 38 | |
| 39 | // for item feature |
| 40 | vec_int_t unique_items_; |
| 41 | std::vector<vec_pair_t> item_feats_list_; |
| 42 | |
| 43 | uint16_t ns_id_; |
| 44 | std::unique_ptr<IndexingWrapper> indexing_wrapper_; |
| 45 | |
| 46 | public: |
| 47 | DEFINE_INSTANCE_READER_LIKE(DSSMInstReader); |
| 48 | |
| 49 | protected: |
| 50 | bool InitConfigKV(const std::string& k, const std::string& v) override { |
| 51 | if (InstanceReaderImpl::InitConfigKV(k, v)) { |
| 52 | } else if (k == "is_train") { |
| 53 | auto val = std::stoi(v); |
| 54 | DXCHECK(val == 1 || val == 0); |
| 55 | is_train_ = val; |
| 56 | } else if (k == "num_neg") { |
| 57 | num_neg_ = std::stoi(v); |
| 58 | DXCHECK(num_neg_ > 0); |
| 59 | } else if (k == "add_node") { |
| 60 | auto val = std::stoi(v); |
| 61 | DXCHECK(val == 1 || val == 0); |
| 62 | add_node_ = val; |
| 63 | } else { |
| 64 | DXERROR("Unexpected config: %s = %s.", k.c_str(), v.c_str()); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | DXINFO("Instance reader argument: %s = %s.", k.c_str(), v.c_str()); |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | void PostInit(const std::string& /*node_config*/) override { |
| 73 | ns_id_ = 0; |
| 74 | indexing_wrapper_ = IndexingWrapper::Create(""); |
| 75 | } |
| 76 | |
| 77 | public: |
| 78 | bool GetBatch(Instance* inst) override { |
| 79 | return is_train_ ? GetTrainBatch(inst) : GetPredictBatch(inst); |
| 80 | } |
| 81 | |
| 82 | /************************************************************************/ |
| 83 | /* Read batch data from file for training */ |
nothing calls this directly
no outgoing calls
no test coverage detected