XACC
contrastive_divergence.hpp
1 /*******************************************************************************
2  * Copyright (c) 2019 UT-Battelle, LLC.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and Eclipse Distribution License v1.0 which accompanies this
6  * distribution. The Eclipse Public License is available at
7  * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
8  *License is available at https://eclipse.org/org/documents/edl-v10.php
9  *
10  * Contributors:
11  * Alexander J. McCaskey - initial API and implementation
12  *******************************************************************************/
13 #ifndef XACC_ALGORITHM_RBM_CLASSIFICATION_CD_HPP_
14 #define XACC_ALGORITHM_RBM_CLASSIFICATION_CD_HPP_
15 
16 #include "rbm_classification.hpp"
17 
18 namespace xacc {
19 namespace algorithm {
20 
22 protected:
23 
24  Eigen::VectorXd sample(Eigen::VectorXd& probs) {
25  Eigen::VectorXd tmp = probs + Eigen::VectorXd::Random(probs.size());
26  Eigen::VectorXd tmp2 = tmp.array().floor();
27  return tmp2;
28  }
29 public:
30 const std::string name() const override { return "cd"; }
31  const std::string description() const override { return ""; }
32  std::tuple<Eigen::MatrixXd, Eigen::VectorXd, Eigen::VectorXd>
33  compute(Eigen::MatrixXd &features, Eigen::MatrixXd &w, Eigen::VectorXd &v,
34  Eigen::VectorXd &h, HeterogeneousMap options = {}) override {
35 
36  Eigen::VectorXd exp_h = options.get<Eigen::VectorXd>("dataexp_h");
37  Eigen::VectorXd h_sample = sample(exp_h);
38 
39  Eigen::MatrixXd tmp = h_sample * w.transpose();
40  for (int col = 0; col < tmp.cols(); col++) {
41  for (int row = 0; row < tmp.rows(); row++) {
42  tmp(row, col) += v(col);
43  }
44  }
45 
46  // tmp.unaryExpr([](double& x) {return 1./(1.+std::exp(-x));});
47 
48  // Eigen::MatrixXd h_probs = tmp.array().exp();//unaryExpr(s);
49  // h_probs has to be bs x n_v
50 
51  Eigen::MatrixXd w_expectation;
52 
53  Eigen::VectorXd v_expectation = Eigen::VectorXd::Zero(v.size());
54  Eigen::VectorXd h_expectation = Eigen::VectorXd::Zero(v.size());
55 
56  // for (int i = 0; i < features.rows(); i++) {
57  // v_expectation(i) = features.row(i).sum() / features.row(i).size();
58  // h_expectation(i) = h_probs.row(i).sum() / h_probs.row(i).size();
59  // }
60 
61  return std::make_tuple(w_expectation, v_expectation, h_expectation);
62  }
63 };
64 } // namespace algorithm
65 } // namespace xacc
66 #endif
const std::string description() const override
Definition: contrastive_divergence.hpp:31
const std::string name() const override
Definition: contrastive_divergence.hpp:30
Definition: Accelerator.hpp:25
Definition: rbm_classification.hpp:24
Definition: contrastive_divergence.hpp:21
Definition: heterogeneous.hpp:45