backprop.cpp 6.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/**
 * @date Mon Jul 18 18:11:22 2011 +0200
 * @author Andre Anjos <andre.anjos@idiap.ch>
 * @author Laurent El Shafey <Laurent.El-Shafey@idiap.ch>
 *
 * @brief Implementation of the BackProp algorithm for MLP training.
 *
 * Copyright (C) 2011-2014 Idiap Research Institute, Martigny, Switzerland
 */

#include <algorithm>
12
13
#include <bob.core/check.h>
#include <bob.math/linear.h>
14

André Anjos's avatar
André Anjos committed
15
#include <bob.learn.mlp/backprop.h>
16
17
18

bob::learn::mlp::BackProp::BackProp(size_t batch_size,
    boost::shared_ptr<bob::learn::mlp::Cost> cost):
19
  bob::learn::mlp::Trainer(batch_size, cost),
20
21
22
23
24
25
26
27
28
29
30
  m_learning_rate(0.1),
  m_momentum(0.0),
  m_prev_deriv(numberOfHiddenLayers() + 1),
  m_prev_deriv_bias(numberOfHiddenLayers() + 1)
{
  reset();
}

bob::learn::mlp::BackProp::BackProp(size_t batch_size,
    boost::shared_ptr<bob::learn::mlp::Cost> cost,
    const bob::learn::mlp::Machine& machine):
31
  bob::learn::mlp::Trainer(batch_size, cost, machine),
32
33
34
35
36
37
38
39
40
41
42
  m_learning_rate(0.1),
  m_momentum(0.0),
  m_prev_deriv(numberOfHiddenLayers() + 1),
  m_prev_deriv_bias(numberOfHiddenLayers() + 1)
{
  initialize(machine);
}

bob::learn::mlp::BackProp::BackProp(size_t batch_size,
    boost::shared_ptr<bob::learn::mlp::Cost> cost,
    const bob::learn::mlp::Machine& machine, bool train_biases):
43
  bob::learn::mlp::Trainer(batch_size, cost, machine, train_biases),
44
45
46
47
48
49
50
51
52
53
54
  m_learning_rate(0.1),
  m_momentum(0.0),
  m_prev_deriv(numberOfHiddenLayers() + 1),
  m_prev_deriv_bias(numberOfHiddenLayers() + 1)
{
  initialize(machine);
}

bob::learn::mlp::BackProp::~BackProp() { }

bob::learn::mlp::BackProp::BackProp(const BackProp& other):
55
  bob::learn::mlp::Trainer(other),
56
57
58
59
60
61
62
63
64
65
66
  m_learning_rate(other.m_learning_rate),
  m_momentum(other.m_momentum)
{
  bob::core::array::ccopy(other.m_prev_deriv, m_prev_deriv);
  bob::core::array::ccopy(other.m_prev_deriv_bias, m_prev_deriv_bias);
}

bob::learn::mlp::BackProp& bob::learn::mlp::BackProp::operator=
(const bob::learn::mlp::BackProp& other) {
  if (this != &other)
  {
67
    bob::learn::mlp::Trainer::operator=(other);
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
    m_learning_rate = other.m_learning_rate;
    m_momentum = other.m_momentum;

    bob::core::array::ccopy(other.m_prev_deriv, m_prev_deriv);
    bob::core::array::ccopy(other.m_prev_deriv_bias, m_prev_deriv_bias);
  }
  return *this;
}

void bob::learn::mlp::BackProp::reset() {
  for (size_t k=0; k<(numberOfHiddenLayers() + 1); ++k) {
    m_prev_deriv[k] = 0;
    m_prev_deriv_bias[k] = 0;
  }
}

void bob::learn::mlp::BackProp::backprop_weight_update(bob::learn::mlp::Machine& machine,
  const blitz::Array<double,2>& input)
{
  std::vector<blitz::Array<double,2> >& machine_weight =
    machine.updateWeights();
  std::vector<blitz::Array<double,1> >& machine_bias =
    machine.updateBiases();
  const std::vector<blitz::Array<double,2> >& deriv = getDerivatives();
  for (size_t k=0; k<machine_weight.size(); ++k) { //for all layers
    machine_weight[k] -= (((1-m_momentum)*m_learning_rate*deriv[k]) +
      (m_momentum*m_prev_deriv[k]));
    m_prev_deriv[k] = m_learning_rate*deriv[k];

    // Here we decide if we should train the biases or not
    if (!getTrainBiases()) continue;

    const std::vector<blitz::Array<double,1> >& deriv_bias = getBiasDerivatives();
    // We do the same for the biases, with the exception that biases can be
    // considered as input neurons connecting the respective layers, with a
    // fixed input = +1. This means we only need to probe for the error at
    // layer k.
    machine_bias[k] -= (((1-m_momentum)*m_learning_rate*deriv_bias[k]) +
      (m_momentum*m_prev_deriv_bias[k]));
    m_prev_deriv_bias[k] = m_learning_rate*deriv_bias[k];
  }
}

void bob::learn::mlp::BackProp::setPreviousDerivatives(const std::vector<blitz::Array<double,2> >& v) {
  bob::core::array::assertSameDimensionLength(v.size(), m_prev_deriv.size());
  for (size_t k=0; k<v.size(); ++k) {
    bob::core::array::assertSameShape(v[k], m_prev_deriv[k]);
    m_prev_deriv[k] = v[k];
  }
}

void bob::learn::mlp::BackProp::setPreviousDerivative(const blitz::Array<double,2>& v, const size_t k) {
  if (k >= m_prev_deriv.size()) {
    boost::format m("MLPRPropTrainer: index for setting previous derivative array %lu is not on the expected range of [0, %lu]");
    m % k % (m_prev_deriv.size()-1);
    throw std::runtime_error(m.str());
  }
  bob::core::array::assertSameShape(v, m_prev_deriv[k]);
  m_prev_deriv[k] = v;
}

void bob::learn::mlp::BackProp::setPreviousBiasDerivatives(const std::vector<blitz::Array<double,1> >& v) {
  bob::core::array::assertSameDimensionLength(v.size(), m_prev_deriv_bias.size());
  for (size_t k=0; k<v.size(); ++k)
  {
    bob::core::array::assertSameShape(v[k], m_prev_deriv_bias[k]);
    m_prev_deriv_bias[k] = v[k];
  }
}

void bob::learn::mlp::BackProp::setPreviousBiasDerivative(const blitz::Array<double,1>& v, const size_t k) {
  if (k >= m_prev_deriv_bias.size()) {
    boost::format m("MLPRPropTrainer: index for setting previous bias derivative array %lu is not on the expected range of [0, %lu]");
    m % k % (m_prev_deriv_bias.size()-1);
    throw std::runtime_error(m.str());
  }
  bob::core::array::assertSameShape(v, m_prev_deriv_bias[k]);
  m_prev_deriv_bias[k] = v;
}

void bob::learn::mlp::BackProp::initialize(const bob::learn::mlp::Machine& machine)
{
150
  bob::learn::mlp::Trainer::initialize(machine);
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

  const std::vector<blitz::Array<double,2> >& machine_weight =
    machine.getWeights();
  const std::vector<blitz::Array<double,1> >& machine_bias =
    machine.getBiases();

  m_prev_deriv.resize(numberOfHiddenLayers() + 1);
  m_prev_deriv_bias.resize(numberOfHiddenLayers() + 1);
  for (size_t k=0; k<(numberOfHiddenLayers() + 1); ++k) {
    m_prev_deriv[k].reference(blitz::Array<double,2>(machine_weight[k].shape()));
    m_prev_deriv_bias[k].reference(blitz::Array<double,1>(machine_bias[k].shape()));
  }

  reset();
}

void bob::learn::mlp::BackProp::train(bob::learn::mlp::Machine& machine,
    const blitz::Array<double,2>& input,
    const blitz::Array<double,2>& target) {
  if (!isCompatible(machine)) {
    throw std::runtime_error("input machine is incompatible with this trainer");
  }
  bob::core::array::assertSameDimensionLength(getBatchSize(), input.extent(0));
  bob::core::array::assertSameDimensionLength(getBatchSize(), target.extent(0));
  train_(machine, input, target);
}

void bob::learn::mlp::BackProp::train_(bob::learn::mlp::Machine& machine,
    const blitz::Array<double,2>& input,
    const blitz::Array<double,2>& target) {
  // To be called in this sequence for a general backprop algorithm
  forward_step(machine, input);
  backward_step(machine, input, target);
  backprop_weight_update(machine, input);
}