cross_entropy.cpp 1.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
/**
 * @author Andre Anjos <andre.anjos@idiap.ch>
 * @date Fri 31 May 23:52:08 2013 CEST
 *
 * @brief Implementation of the cross entropy loss function
 *
 * Copyright (C) 2011-2014 Idiap Research Institute, Martigny, Switzerland
 */

André Anjos's avatar
André Anjos committed
10
#include <bob.learn.mlp/cross_entropy.h>
11
12
13

namespace bob { namespace learn { namespace mlp {

14
  CrossEntropyLoss::CrossEntropyLoss(boost::shared_ptr<bob::learn::activation::Activation> actfun)
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
    : m_actfun(actfun),
      m_logistic_activation(m_actfun->unique_identifier() == "bob.machine.Activation.Logistic") {}

  CrossEntropyLoss::~CrossEntropyLoss() {}

  double CrossEntropyLoss::f (double output, double target) const {
    return - (target * std::log(output)) - ((1-target)*std::log(1-output));
  }

  double CrossEntropyLoss::f_prime (double output, double target) const {
    return (output-target) / (output * (1-output));
  }

  double CrossEntropyLoss::error (double output, double target) const {
    return m_logistic_activation? (output - target) : m_actfun->f_prime_from_f(output) * f_prime(output, target);
  }

  std::string CrossEntropyLoss::str() const {
    std::string retval = "J = - target*log(output) - (1-target)*log(1-output) (cross-entropy loss)";
    if (m_logistic_activation) retval += " [+ logistic activation]";
    else retval += " [+ unknown activation]";
    return retval;
  }

}}}