/usr/include/shark/Models/ConvexCombination.h is in libshark-dev 3.1.3+ds1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | /*!
*
*
* \brief Implements a Model using a linear function.
*
*
*
* \author T. Glasmachers, O. Krause
* \date 2010-2011
*
*
* \par Copyright 1995-2015 Shark Development Team
*
* <BR><HR>
* This file is part of Shark.
* <http://image.diku.dk/shark/>
*
* Shark is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Shark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef SHARK_MODELS_ConvexCombination_H
#define SHARK_MODELS_ConvexCombination_H
#include <shark/Models/AbstractModel.h>
namespace shark {
///
/// \brief Models a convex combination of inputs
///
/// For a given input vector x, the convex combination returns \f$ f_i(x) = sum_j w_{ij} x_j \f$,
/// where \f$ w_i > 0 \f$ and \f$ sum_j w_{ij} = 1\f$, that is the outputs of
/// the model are a convex combination of the inputs.
///
/// To ensure that the constraints are fulfilled, the model uses a different
/// set of weights q_i and \f$ w_{ij} = exp(q_{ij})/sum_j exp(q_{ik}) \f$. As usual, this
/// encoding is only used for the derivatives and the parameter vectors, not
/// when the weights are explicitely set. In the latter case, the user must provide
/// a set of suitable \f$ w_{ij} \f$.
class ConvexCombination : public AbstractModel<RealVector,RealVector>
{
private:
RealMatrix m_w; ///< the convex comination weights. it holds sum(row(w_i)) = 1
public:
/// CDefault Constructor; use setStructure later
ConvexCombination(){
m_features |= HAS_FIRST_PARAMETER_DERIVATIVE;
m_features |= HAS_FIRST_INPUT_DERIVATIVE;
}
/// Constructor creating a model with given dimnsionalities and optional offset term.
ConvexCombination(std::size_t inputs, std::size_t outputs = 1)
: m_w(outputs,inputs,0.0){
m_features |= HAS_FIRST_PARAMETER_DERIVATIVE;
m_features |= HAS_FIRST_INPUT_DERIVATIVE;
}
/// Construction from matrix
ConvexCombination(RealMatrix const& matrix):m_w(matrix){
m_features |= HAS_FIRST_PARAMETER_DERIVATIVE;
m_features |= HAS_FIRST_INPUT_DERIVATIVE;
}
/// \brief From INameable: return the class name.
std::string name() const
{ return "ConvexCombination"; }
///swap
friend void swap(ConvexCombination& model1,ConvexCombination& model2){
swap(model1.m_w,model2.m_w);
}
///operator =
ConvexCombination& operator=(ConvexCombination const& model){
ConvexCombination tempModel(model);
swap(*this,tempModel);
return *this;
}
/// obtain the input dimension
std::size_t inputSize() const{
return m_w.size2();
}
/// obtain the output dimension
std::size_t outputSize() const{
return m_w.size1();
}
/// obtain the parameter vector
RealVector parameterVector() const{
RealVector ret(numberOfParameters());
init(ret) << toVector(log(m_w));
return ret;
}
/// overwrite the parameter vector
void setParameterVector(RealVector const& newParameters)
{
init(newParameters) >> toVector(m_w);
noalias(m_w) = exp(m_w);
for(std::size_t i = 0; i != outputSize(); ++i){
row(m_w,i) /= sum(row(m_w,i));
}
}
/// return the number of parameter
std::size_t numberOfParameters() const{
return m_w.size1()*m_w.size2();
}
/// overwrite structure and parameters
void setStructure(std::size_t inputs, std::size_t outputs = 1){
ConvexCombination model(inputs,outputs);
swap(*this,model);
}
RealMatrix const& weights() const{
return m_w;
}
RealMatrix& weights(){
return m_w;
}
boost::shared_ptr<State> createState()const{
return boost::shared_ptr<State>(new EmptyState());
}
/// Evaluate the model: output = w * input
void eval(BatchInputType const& inputs, BatchOutputType& outputs)const{
outputs.resize(inputs.size1(),m_w.size1());
noalias(outputs) = prod(inputs,trans(m_w));
}
/// Evaluate the model: output = w *input
void eval(BatchInputType const& inputs, BatchOutputType& outputs, State& state)const{
eval(inputs,outputs);
}
///\brief Calculates the first derivative w.r.t the parameters and summing them up over all patterns of the last computed batch
void weightedParameterDerivative(
BatchInputType const& patterns, RealMatrix const& coefficients, State const& state, RealVector& gradient
)const{
SIZE_CHECK(coefficients.size2()==outputSize());
SIZE_CHECK(coefficients.size1()==patterns.size1());
gradient.resize(numberOfParameters());
blas::dense_matrix_adaptor<double> weightGradient = blas::adapt_matrix(outputSize(),inputSize(),gradient.storage());
//derivative is
//sum_i sum_j c_ij sum_k x_ik grad_q w_jk= sum_k sum_j grad_q w_jk (sum_i c_ij x_ik)
//and we set d_jk=sum_i c_ij x_ik => d = C^TX
RealMatrix d = prod(trans(coefficients), patterns);
//use the same drivative as in the softmax model
for(std::size_t i = 0; i != outputSize(); ++i){
double mass=inner_prod(row(d,i),row(m_w,i));
noalias(row(weightGradient,i)) = element_prod(
row(d,i) - mass,
row(m_w,i)
);
}
}
///\brief Calculates the first derivative w.r.t the inputs and summs them up over all patterns of the last computed batch
void weightedInputDerivative(
BatchInputType const & patterns,
BatchOutputType const & coefficients,
State const& state,
BatchInputType& derivative
)const{
SIZE_CHECK(coefficients.size2() == outputSize());
SIZE_CHECK(coefficients.size1() == patterns.size1());
derivative.resize(patterns.size1(),inputSize());
noalias(derivative) = prod(coefficients,m_w);
}
/// From ISerializable
void read(InArchive& archive){
archive >> m_w;
}
/// From ISerializable
void write(OutArchive& archive) const{
archive << m_w;
}
};
}
#endif
|