Computes the variational autoencoder error function. More...
#include <shark/ObjectiveFunctions/VariationalAutoencoderError.h>
Public Types | |
typedef UnlabeledData< SearchPointType > | DatasetType |
typedef AbstractModel< SearchPointType, SearchPointType, SearchPointType > | ModelType |
Public Types inherited from shark::AbstractObjectiveFunction< SearchPointType, double > | |
enum | Feature |
List of features that are supported by an implementation. More... | |
typedef SearchPointType | SearchPointType |
typedef double | ResultType |
typedef boost::mpl::if_< std::is_arithmetic< double >, SearchPointType, RealMatrix >::type | FirstOrderDerivative |
typedef TypedFlags< Feature > | Features |
This statement declares the member m_features. See Core/Flags.h for details. | |
typedef TypedFeatureNotAvailableException< Feature > | FeatureNotAvailableException |
Public Member Functions | |
VariationalAutoencoderError (DatasetType const &data, ModelType *encoder, ModelType *decoder, AbstractLoss< SearchPointType, SearchPointType > *visible_loss, double lambda=1.0) | |
std::string | name () const |
From INameable: return the class name. | |
SearchPointType | proposeStartingPoint () const |
Proposes a starting point in the feasible search space of the function. | |
std::size_t | numberOfVariables () const |
Accesses the number of variables. | |
MatrixType | sampleZ (SearchPointType const ¶meters, MatrixType const &batch) const |
double | eval (SearchPointType const ¶meters) const |
Evaluates the objective function for the supplied argument. | |
double | evalDerivative (SearchPointType const ¶meters, SearchPointType &derivative) const |
Public Member Functions inherited from shark::AbstractObjectiveFunction< SearchPointType, double > | |
const Features & | features () const |
virtual void | updateFeatures () |
bool | hasValue () const |
returns whether this function can calculate it's function value | |
bool | hasFirstDerivative () const |
returns whether this function can calculate the first derivative | |
bool | hasSecondDerivative () const |
returns whether this function can calculate the second derivative | |
bool | canProposeStartingPoint () const |
returns whether this function can propose a starting point. | |
bool | isConstrained () const |
returns whether this function can return | |
bool | hasConstraintHandler () const |
returns whether this function can return | |
bool | canProvideClosestFeasible () const |
Returns whether this function can calculate thee closest feasible to an infeasible point. | |
bool | isThreadSafe () const |
Returns true, when the function can be usd in parallel threads. | |
bool | isNoisy () const |
Returns true, when the function can be usd in parallel threads. | |
AbstractObjectiveFunction () | |
Default ctor. | |
virtual | ~AbstractObjectiveFunction () |
Virtual destructor. | |
virtual void | init () |
void | setRng (random::rng_type *rng) |
Sets the Rng used by the objective function. | |
virtual bool | hasScalableDimensionality () const |
virtual void | setNumberOfVariables (std::size_t numberOfVariables) |
Adjusts the number of variables if the function is scalable. | |
virtual std::size_t | numberOfObjectives () const |
virtual bool | hasScalableObjectives () const |
virtual void | setNumberOfObjectives (std::size_t numberOfObjectives) |
Adjusts the number of objectives if the function is scalable. | |
std::size_t | evaluationCounter () const |
Accesses the evaluation counter of the function. | |
AbstractConstraintHandler< SearchPointType > const & | getConstraintHandler () const |
Returns the constraint handler of the function if it has one. | |
virtual bool | isFeasible (const SearchPointType &input) const |
Tests whether a point in SearchSpace is feasible, e.g., whether the constraints are fulfilled. | |
virtual void | closestFeasible (SearchPointType &input) const |
If supported, the supplied point is repaired such that it satisfies all of the function's constraints. | |
ResultType | operator() (SearchPointType const &input) const |
Evaluates the function. Useful together with STL-Algorithms like std::transform. | |
virtual ResultType | evalDerivative (SearchPointType const &input, FirstOrderDerivative &derivative) const |
Evaluates the objective function and calculates its gradient. | |
virtual ResultType | evalDerivative (SearchPointType const &input, SecondOrderDerivative &derivative) const |
Evaluates the objective function and calculates its gradient. | |
Public Member Functions inherited from shark::INameable | |
virtual | ~INameable () |
Additional Inherited Members | |
Protected Member Functions inherited from shark::AbstractObjectiveFunction< SearchPointType, double > | |
void | announceConstraintHandler (AbstractConstraintHandler< SearchPointType > const *handler) |
helper function which is called to announce the presence of an constraint handler. | |
Protected Attributes inherited from shark::AbstractObjectiveFunction< SearchPointType, double > | |
Features | m_features |
std::size_t | m_evaluationCounter |
Evaluation counter, default value: 0. | |
AbstractConstraintHandler< SearchPointType > const * | m_constraintHandler |
random::rng_type * | mep_rng |
Computes the variational autoencoder error function.
We want to optimize a model \( p(x) = \int p(x|z) p(z) dz \) where we choose p(z) as a multivariate normal distribution and p(x|z) is an arbitrary model, e.g. a deep neural entwork. The naive solution is sampling from p(z) and then compute the sample average. This will fail when p(z|x) is a very localized distribution and we might need many samples from p(z) to find a sample which is likely under p(z|x). p(z|x) is assumed to be intractable to compute, so we introduce a second model q(z|x), modeling p(z|x) and we want to train it such that it learns the unknown p(z|x). For this a variational lower bound on the likelihood is used and we maximize
\[ log p(x) \leq E_{q(z|x)}[\log p(x|z)] - KL[q(z|x) || p(z)] \]
The first term explains the meaning of variational autoencoder: we first sample z given x using the encoder model q and then decode z to obtain an estimate for x. The only difference to normal autoencoders is that we now have a probabilistic z. The second term ensures that q is learning p(z|x), assuming that we have enough modeling capacity to actually learn it. See https://arxiv.org/abs/1606.05908 for more background.
Implementation notice: we assume q(z|x) to be a set of independent gaussian distributions parameterized as \( q(z| mu(x), \log \sigma^2(x)) \). The provided encoder model q must therefore have twice as many outputs as the decvoder has inputs as the second half of outputs is interpreted as the log of the variance. So if z should be a 100 dimensional variable, q must have 200 outputs. The outputs and loss function used for the encoder p is arbitrary, but a SquaredLoss will work well, however also other losses like pixel probabilities can be used.
Definition at line 63 of file VariationalAutoencoderError.h.
typedef UnlabeledData<SearchPointType> shark::VariationalAutoencoderError< SearchPointType >::DatasetType |
Definition at line 70 of file VariationalAutoencoderError.h.
typedef AbstractModel<SearchPointType,SearchPointType, SearchPointType> shark::VariationalAutoencoderError< SearchPointType >::ModelType |
Definition at line 71 of file VariationalAutoencoderError.h.
|
inline |
Definition at line 73 of file VariationalAutoencoderError.h.
References shark::AbstractObjectiveFunction< SearchPointType, double >::CAN_PROPOSE_STARTING_POINT, shark::AbstractObjectiveFunction< SearchPointType, double >::HAS_FIRST_DERIVATIVE, shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::hasFirstParameterDerivative(), shark::AbstractObjectiveFunction< SearchPointType, double >::IS_NOISY, and shark::AbstractObjectiveFunction< SearchPointType, double >::m_features.
|
inlinevirtual |
Evaluates the objective function for the supplied argument.
[in] | input | The argument for which the function shall be evaluated. |
FeatureNotAvailableException | in the default implementation and if a function does not support this feature. |
Reimplemented from shark::AbstractObjectiveFunction< SearchPointType, double >.
Definition at line 110 of file VariationalAutoencoderError.h.
References shark::Data< Type >::batch(), shark::random::discrete(), shark::AbstractObjectiveFunction< SearchPointType, double >::m_evaluationCounter, shark::AbstractObjectiveFunction< SearchPointType, double >::mep_rng, shark::Data< Type >::numberOfBatches(), shark::IParameterizable< VectorType >::numberOfParameters(), shark::VariationalAutoencoderError< SearchPointType >::numberOfVariables(), shark::IParameterizable< VectorType >::setParameterVector(), SIZE_CHECK, and shark::sqr().
|
inline |
Definition at line 131 of file VariationalAutoencoderError.h.
References shark::Data< Type >::batch(), shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::createState(), shark::random::discrete(), shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::eval(), shark::AbstractLoss< LabelT, OutputT >::evalDerivative(), shark::AbstractObjectiveFunction< SearchPointType, double >::m_evaluationCounter, shark::AbstractObjectiveFunction< SearchPointType, double >::mep_rng, shark::Data< Type >::numberOfBatches(), shark::IParameterizable< VectorType >::numberOfParameters(), shark::VariationalAutoencoderError< SearchPointType >::numberOfVariables(), shark::IParameterizable< VectorType >::setParameterVector(), SIZE_CHECK, shark::sqr(), shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::weightedDerivatives(), and shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::weightedParameterDerivative().
|
inlinevirtual |
From INameable: return the class name.
Reimplemented from shark::INameable.
Definition at line 87 of file VariationalAutoencoderError.h.
|
inlinevirtual |
Accesses the number of variables.
Implements shark::AbstractObjectiveFunction< SearchPointType, double >.
Definition at line 94 of file VariationalAutoencoderError.h.
References shark::IParameterizable< VectorType >::numberOfParameters().
Referenced by shark::VariationalAutoencoderError< SearchPointType >::eval(), shark::VariationalAutoencoderError< SearchPointType >::evalDerivative(), and shark::VariationalAutoencoderError< SearchPointType >::sampleZ().
|
inlinevirtual |
Proposes a starting point in the feasible search space of the function.
FeatureNotAvailableException | in the default implementation and if a function does not support this feature. |
Reimplemented from shark::AbstractObjectiveFunction< SearchPointType, double >.
Definition at line 90 of file VariationalAutoencoderError.h.
References shark::IParameterizable< VectorType >::parameterVector().
|
inline |
Definition at line 98 of file VariationalAutoencoderError.h.
References shark::AbstractObjectiveFunction< SearchPointType, double >::mep_rng, shark::IParameterizable< VectorType >::numberOfParameters(), shark::VariationalAutoencoderError< SearchPointType >::numberOfVariables(), and shark::IParameterizable< VectorType >::setParameterVector().
Referenced by main().