bool hsclient::query(const char* oper, const char* values[], int num, const char* limit_offset, char mop, const char* to_values[], int to_num) { acl_assert(tbl_curr_); bool retried = false; while (true) { // 创建请求数据 hsproto::build_request(buf_, tbl_curr_->id_, oper, values, num, limit_offset, mop, to_values, to_num); if (debugOn_) printf("%s(%d)>>>send: (%s)\n", __FUNCTION__, __LINE__, buf_.c_str()); // 与数据库通信并从数据库获得结果 if (chat() == true) break; // 如果与数据库通信失败当允许重试时若重试也失败则返回错误 if (retry_enable_ == false || retried) { close_stream(); return (false); } retried = true; // 先缓冲当前表结构中的信息 string dbn(tbl_curr_->dbn_), tbl(tbl_curr_->tbl_); string idx(tbl_curr_->idx_), flds(tbl_curr_->flds_); // 先关闭旧的连接对象及所有的表对象 close_stream(); // 再重新打开连接对象并打开表对象 if (open_tbl(dbn.c_str(), tbl.c_str(), idx.c_str(), flds.c_str(), true) == false) { logger_error("reopen error"); return (false); } } if (debugOn_) printf("%s(%d): gets: (%s)\n", __FUNCTION__, __LINE__, buf_.c_str()); return (proto_.parse_respond(tbl_curr_->nfld_, buf_, error_, serror_)); }
// [ref] ${MOCAPY_HOME}/examples/infenginemm_example.cpp void mixture_model_inference() { // Mocapy++ provides exact methods for doing inference in mixture models (MMs). // [ref] Mocapy++ manual, pp. 27~28. mocapy::mocapy_seed((uint)std::time(NULL)); //--------------------------------------------------------------- std::cout << "******************" << std::endl; std::cout << "Setting up network" << std::endl; std::cout << "******************" << std::endl; // set cpd for in-node const double in_cpd_array[] = { 0.5, 0.5 }; mocapy::CPD in_cpd; in_cpd.set_shape(2); local::copy_flat(in_cpd.flat_iterator(), in_cpd_array); // set cpd for hidde node const double hd_cpd_array[] = { 1.0, 0.0, 0.0, 0.0, 0.25, 0.25, 0.25, 0.25 }; mocapy::CPD hd_cpd; hd_cpd.set_shape(2, 4); local::copy_flat(hd_cpd.flat_iterator(), hd_cpd_array); // set cpd for out node const double out_cpd_array[] = { 0.7, 0.0, 0.0, 0.0, 0.0, 0.7, 0.0, 0.0, 0.0, 0.0, 0.7, 0.0, 0.0, 0.0, 0.0, 0.7 }; mocapy::CPD out_cpd; out_cpd.set_shape(4, 4); local::copy_flat(out_cpd.flat_iterator(), out_cpd_array); // setup nodes mocapy::DiscreteNode *in = new mocapy::DiscreteNode(); in->set_densities(mocapy::DiscreteDensities(2, in_cpd)); mocapy::DiscreteNode *hd = new mocapy::DiscreteNode(); hd->set_densities(mocapy::DiscreteDensities(4, hd_cpd)); mocapy::DiscreteNode *out = new mocapy::DiscreteNode(); out->set_densities(mocapy::DiscreteDensities(4, out_cpd)); //--------------------------------------------------------------- // setup dbn std::vector<mocapy::Node *> nodes = local::vect(in->base(), hd->base(), out->base()); mocapy::DBN dbn(nodes, nodes); dbn.add_intra(0, 1); dbn.add_intra(1, 2); dbn.construct(); // output the CPDs std::cout << "in: \n" << in->get_densities()->getCPD() << std::endl; std::cout << "hd: \n" << hd->get_densities()->getCPD() << std::endl; std::cout << "out: \n" << out->get_densities()->getCPD() << std::endl; std::cout << std::endl; //--------------------------------------------------------------- std::cout << "******************" << std::endl; std::cout << "Setting up dataset" << std::endl; std::cout << "******************" << std::endl; mocapy::Sequence data; data.set_shape(5, 3); const double seq_array[] = { 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 }; local::copy_flat(data.flat_iterator(), seq_array); mocapy::MDArray<mocapy::eMISMASK> mism; mism.set_shape(5, 3); mism.set_wildcard(mocapy::vec(-1, 0), mocapy::MOCAPY_OBSERVED); mism.set_wildcard(mocapy::vec(-1, 1), mocapy::MOCAPY_HIDDEN); mism.set_wildcard(mocapy::vec(-1, 2), mocapy::MOCAPY_OBSERVED); mocapy::MDArray<mocapy::eMISMASK> mism_sample; mism_sample.set_shape(5, 3); mism_sample.set_wildcard(mocapy::vec(-1, 0), mocapy::MOCAPY_OBSERVED); mism_sample.set_wildcard(mocapy::vec(-1, 1), mocapy::MOCAPY_HIDDEN); mism_sample.set_wildcard(mocapy::vec(-1, 2), mocapy::MOCAPY_HIDDEN); std::cout << "Data: \n" << data << std::endl; std::cout << "Mism: \n" << mism << std::endl; std::cout << "Mism_sample: \n" << mism_sample << std::endl; //--------------------------------------------------------------- std::cout << "*************************" << std::endl; std::cout << "Example of SampleInfEngineMM" << std::endl; std::cout << "*************************" << std::endl; // setup the sampler mocapy::SampleInfEngineMM sampler(&dbn, data, mism_sample, 1); mocapy::MDArray<double> sample = sampler.sample_next(); std::cout << "Sample:\n" << sample; std::cout << "ll of sample = " << sampler.calc_ll(mism) << std::endl << std::endl; std::cout << "undo()" << std::endl; sampler.undo(); std::cout << "ll of initial values =" << sampler.calc_ll(mism) << std::endl << std::endl; std::cout << "Setting start=0 and end=1" << std::endl << std::endl; sampler.set_start_end(0, 1); sample = sampler.sample_next(); std::cout << "Sample:\n" << sample; std::cout << "ll of sample = " << sampler.calc_ll(mism) << std::endl << std::endl; std::cout << "undo()" << std::endl; sampler.undo(); // use the other mismask, so that we can just sample the hidden states sampler.set_seq_mismask(data, mism); sampler.set_start_end(); sample = sampler.sample_next(); std::cout << "Sample only hidden states:\n" << sample; std::cout << "ll of sample = " << sampler.calc_ll(mism) << std::endl << std::endl; //--------------------------------------------------------------- std::cout << "********************************" << std::endl; std::cout << "Example of LikelihoodInfEngineMM" << std::endl; std::cout << "********************************" << std::endl; mocapy::LikelihoodInfEngineMM infengine(&dbn, 1); const double ll1 = infengine.calc_ll(data, mism, false); std::cout << "ll of initail values = " << ll1 << std::endl; const double ll2 = infengine.calc_ll(data, mism, 0, -1, true); std::cout << "ll of initail values (including parents) = " << ll2 << std::endl; //--------------------------------------------------------------- // clean up delete in; delete hd; delete out; }
int main(int argc, const char *argv[]) { string ftx = "./data/test_x.txt"; string fty = "./data/test_y.txt"; int epoch = 100; int batch_size = 0; double gamma = 0.1; // learning rate int k = 1; //Contrastive Divergence k int hls[] = {500, 500, 900}; int n_layers = sizeof(hls) / sizeof(hls[0]); int n_lables = 10; double lbd = 0.0002; // weight cost Conf conf(ftx, fty, epoch, batch_size, hls, k, gamma, n_layers, n_lables, lbd); Dataset data(conf); /*// test rbm RBM rbm(data.N, data.n_f, 400, NULL, NULL, NULL, lbd, 0); for(int i=0; i<epoch; i++) { cout << "epoch: " << i << endl; for(int j=0; j<data.N; j++) { double *x = new double[data.n_f]; for(int f=0; f<data.n_f; f++) x[f] = data.X[j][f]; rbm.train(x, gamma, k); delete[] x; } ofstream fout("./model/W1"); for(int j=0; j<rbm.n_visible; j++) { for(int l=0; l<rbm.n_hidden; l++) { fout << rbm.W[l][j] << " "; } fout << endl; } fout << flush; fout.close(); } */ //test lr /* LR lr(data, conf); for(int i=0; i<epoch; i++) { cout << "epoch: " << i << endl; for(int j=0; j<data.N; j++) { double *x = new double[lr.n_features]; for(int f=0; f<lr.n_features; f++) x[f] = data.X[j][f]; int *y = new int[lr.n_labels]; y[int(data.Y[j])] = 1; lr.train(x, y, gamma); delete[] x; delete[] y; } } for(int j=0; j<data.N; j++) { double *x = new double[lr.n_features]; for(int f=0; f<lr.n_features; f++) x[f] = data.X[j][f]; double *y = new double[lr.n_labels]; lr.predict(x, y); cout <<data.Y[j]<<": "; for(int i=0; i<lr.n_labels; i++) cout <<y[i]<<" "; cout<<endl; delete[] y; } */ DBN dbn(data, conf); dbn.pretrain(data, conf); for(int i=0; i<=n_layers; i++) { char str[] = "./model/W"; char W_l[128]; sprintf(W_l, "%s%d", str, (i+1)); ofstream fout(W_l); if(i < n_layers) { for(int j=0; j<dbn.rbm_layers[i]->n_visible; j++) { for(int l=0; l<dbn.rbm_layers[i]->n_hidden; l++) { fout << dbn.rbm_layers[i]->W[l][j] << " "; } fout << endl; } } else { for(int j=0; j<dbn.lr_layer->n_features; j++) { for(int l=0; l<dbn.lr_layer->n_labels; l++) { fout << dbn.lr_layer->W[l][j] << " "; } fout << endl; } } fout << flush; fout.close(); } dbn.finetune(data, conf); ftx = "./data/train_x.txt"; fty = "./data/train_y.txt"; Conf conf_(ftx, fty, epoch, batch_size, hls, k, gamma, n_layers, n_lables, lbd); Dataset data_(conf_); double acc_num = 0; for(int j=0; j<data_.N; j++) { double *x = new double[data_.n_f]; for(int f=0; f<data_.n_f; f++) x[f] = data_.X[j][f]; double *y = new double[conf.n_labels]; int true_label = int(data_.Y[j]); if(dbn.predict(x, y, true_label) == 1) acc_num++; delete[] x; delete[] y; cout << j <<": Accuracy=" << acc_num/(j+1) <<endl; } return 0; }