XACC
classical_data_expectations.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_CLASSICAL_DATAEXP_HPP_
14 #define XACC_ALGORITHM_RBM_CLASSIFICATION_CLASSICAL_DATAEXP_HPP_
15 
16 #include "rbm_classification.hpp"
17 
18 namespace xacc {
19 namespace algorithm {
20 
21 template<typename T>
22 class sigmoid {
23  public:
24  T type;
25 
26  T operator()(T& x) {
27  return 1./(1.+std::exp(-x));
28  }
29 };
31 public:
32 const std::string name() const override { return "data-exp"; }
33  const std::string description() const override { return ""; }
34  std::tuple<Eigen::MatrixXd, Eigen::VectorXd, Eigen::VectorXd>
35  compute(Eigen::MatrixXd &features, Eigen::MatrixXd &w, Eigen::VectorXd &v,
36  Eigen::VectorXd &h, HeterogeneousMap options = {}) override {
37  int batch_size = features.rows();
38 
39  // features is bs x n_v
40  // w is n_v x n_v
41  // h is 1 x n_v
42  // features * w + h means add h[0,col] to features*x[I, col]
43  Eigen::MatrixXd tmp = features * w;
44  for (int col = 0; col < tmp.cols(); col++) {
45  for (int row = 0; row < tmp.rows(); row++) {
46  tmp(row, col) += h(col);
47  }
48  }
49 
50  for (int col = 0; col < tmp.cols(); col++) {
51  for (int row = 0; row < tmp.rows(); row++) {
52  tmp(row, col) = 1./(1.+std::exp(-tmp(row,col)));
53  }
54  }
55 
56  Eigen::MatrixXd h_probs = tmp;
57  // h_probs has to be bs x n_v
58 
59  Eigen::MatrixXd w_expectation =
60  (features.transpose() * h_probs) / batch_size;
61 
62  Eigen::VectorXd v_expectation = Eigen::VectorXd::Zero(v.size());
63  Eigen::VectorXd h_expectation = Eigen::VectorXd::Zero(v.size());
64 
65  for (int i = 0; i < features.rows(); i++) {
66  v_expectation(i) = features.row(i).sum() / features.row(i).size();
67  h_expectation(i) = h_probs.row(i).sum() / h_probs.row(i).size();
68  }
69 
70  return std::make_tuple(w_expectation, v_expectation, h_expectation);
71  }
72 };
73 } // namespace algorithm
74 } // namespace xacc
75 #endif
const std::string description() const override
Definition: classical_data_expectations.hpp:33
Definition: Accelerator.hpp:25
Definition: rbm_classification.hpp:24
Definition: heterogeneous.hpp:45
Definition: classical_data_expectations.hpp:22
const std::string name() const override
Definition: classical_data_expectations.hpp:32
Definition: classical_data_expectations.hpp:30