MUQ  0.4.3
MultiLogisticLikelihood.cpp
Go to the documentation of this file.
2 
3 using namespace muq::Modeling;
4 
6  Eigen::VectorXi const& dataIn) : ModPiece(numClassesIn*dataIn.size()*Eigen::VectorXi::Ones(1),
7  Eigen::VectorXi::Ones(1)),
8  numClasses(numClassesIn),
9  data(dataIn)
10 {
11 }
12 
13 
15 {
16  // Construct the scores matrix
17  Eigen::VectorXd const& scoresVec = inputs.at(0).get();
18  Eigen::Map<const Eigen::MatrixXd> scores(scoresVec.data(), numClasses, data.size());
19 
20  // Compute the partition function at each point
21  Eigen::RowVectorXd logPartition = scores.array().exp().colwise().sum().log();
22 
23  Eigen::VectorXd logP(data.size());
24  for(int i=0; i<data.size(); ++i)
25  logP(i) = scores(data(i),i) - logPartition(i);
26 
27  outputs.resize(1);
28  outputs.at(0).resize(1);
29  outputs.at(0)(0) = logP.sum();
30 }
31 
32 void MultiLogisticLikelihood::GradientImpl(unsigned int const outputDimWrt,
33  unsigned int const inputDimWrt,
34  ref_vector<Eigen::VectorXd> const& inputs,
35  Eigen::VectorXd const& sensitivity)
36 {
37  Eigen::VectorXd const& scoresVec = inputs.at(0).get();
38  Eigen::Map<const Eigen::MatrixXd> scores(scoresVec.data(), numClasses, data.size());
39 
40  // resize the gradient and create a matrix wrapper
41  gradient.resize(inputSizes(0));
42  Eigen::Map<Eigen::MatrixXd> gradMat(gradient.data(), numClasses, data.size());
43 
44  // Compute the partition function at each point
45  Eigen::RowVectorXd logPartition = scores.array().exp().colwise().sum().log();
46 
47  // Compute the log probabilities
48  Eigen::VectorXd logP(data.size());
49  for(int i=0; i<data.size(); ++i)
50  logP(i) = scores(data(i),i) - logPartition(i);
51 
52  double logLikely = logP.sum();
53 
54  // First, add the derivatives of the logP terms wrt the scores
55  gradMat = Eigen::MatrixXd::Zero(numClasses, data.size());
56  for(int i=0; i<data.size(); ++i){
57  gradMat(data(i),i) += 1.0;
58  gradMat.col(i) -= (1.0/exp(logPartition(i)))*scores.col(i).array().exp().matrix();
59  }
60 
61  gradMat *= sensitivity(0);
62 }
63 
64 void MultiLogisticLikelihood::JacobianImpl(unsigned int const outputDimWrt,
65  unsigned int const inputDimWrt,
66  ref_vector<Eigen::VectorXd> const& input)
67 {
68  jacobian = Gradient(outputDimWrt, inputDimWrt, input, Eigen::VectorXd::Ones(1).eval()).transpose();
69 }
70 
71 void MultiLogisticLikelihood::ApplyJacobianImpl(unsigned int const outputDimWrt,
72  unsigned int const inputDimWrt,
73  ref_vector<Eigen::VectorXd> const& input,
74  Eigen::VectorXd const& vec)
75 {
76  jacobianAction = Jacobian(outputDimWrt, inputDimWrt, input) * vec;
77 }
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
Eigen::MatrixXd jacobian
Definition: ModPiece.h:506
virtual Eigen::VectorXd const & Gradient(unsigned int const outputDimWrt, unsigned int const inputDimWrt, std::vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &sensitivity)
Compute the Gradient .
Definition: ModPiece.cpp:83
const Eigen::VectorXi inputSizes
Definition: ModPiece.h:469
virtual Eigen::MatrixXd const & Jacobian(unsigned int const outputDimWrt, unsigned int const inputDimWrt, std::vector< Eigen::VectorXd > const &input)
Compute the Jacobian of this ModPiece.
Definition: ModPiece.cpp:128
std::vector< Eigen::VectorXd > outputs
Definition: ModPiece.h:503
Eigen::VectorXd gradient
Definition: ModPiece.h:504
Eigen::VectorXd jacobianAction
Definition: ModPiece.h:505
virtual void GradientImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &sensitivity) override
virtual void EvaluateImpl(muq::Modeling::ref_vector< Eigen::VectorXd > const &inputs) override
MultiLogisticLikelihood(unsigned int numClasses, Eigen::VectorXi const &data)
virtual void JacobianImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input) override
virtual void ApplyJacobianImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &vec) override
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37