layer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include "../neuron/neuron.h"
6 
11 class Layer
12 {
13 private:
14  size_t _n_in; // The number of input connections to the layer.
15  size_t _n_out; // The number of output connections from the layer.
16  std::string _activate_function =
17  "tanh"; // The activation function of the layer.
18  std::vector<Neuron> _neurons; // The neurons in the layer.
19  std::vector<Variable> _parameters; // All parameters of the layer.
20 
21 public:
28  Layer(size_t n_in, size_t n_out, std::string activate_function = "tanh")
29  : _n_in(n_in), _n_out(n_out), _activate_function(activate_function)
30  {
31  _parameters.reserve((n_in + 1) * n_out);
32  _neurons.reserve(n_out);
33  for (size_t i = 0; i < n_out; i++)
34  {
35  _neurons.emplace_back(n_in, _activate_function);
36  for (size_t j = 0; j < _neurons[i].parameters().size(); j++)
37  {
38  _parameters.push_back(_neurons[i].parameters()[j]);
39  }
40  }
41  };
42 
47  Layer(const Layer &other)
48  : _n_in(other._n_in), _n_out(other._n_out),
49  _activate_function(other._activate_function),
50  _neurons(other._neurons){};
51 
57  Layer &operator=(const Layer &other)
58  {
59  _n_in = other._n_in;
60  _n_out = other._n_out;
61  _activate_function = other._activate_function;
62  _neurons = other._neurons;
63  return *this;
64  }
65 
70  Layer &operator=(Layer &&other) noexcept
71  {
72  _n_in = other._n_in;
73  _n_out = other._n_out;
74  _activate_function = std::move(other._activate_function);
75  _neurons = std::move(other._neurons);
76  return *this;
77  }
78 
84  Layer(Layer &&other) noexcept
85  {
86  _n_in = other._n_in;
87  _n_out = other._n_out;
88  _activate_function = std::move(other._activate_function);
89  _neurons = std::move(other._neurons);
90  }
91 
96  const std::vector<Variable> &parameters() const
97  {
98  return _parameters;
99  }
100 
105  const std::vector<Neuron> &neurons() const
106  {
107  return _neurons;
108  }
109 
115  std::vector<Variable> forward(const std::vector<double> &inputs);
116 
122  std::vector<Variable> forward(const std::vector<Variable> &variables);
123 };
Definition: layer.h:12
Layer & operator=(const Layer &other)
Definition: layer.h:57
const std::vector< Neuron > & neurons() const
Definition: layer.h:105
std::vector< Variable > forward(const std::vector< double > &inputs)
Definition: layer.cc:4
const std::vector< Variable > & parameters() const
Definition: layer.h:96
Layer(const Layer &other)
Definition: layer.h:47
Layer(Layer &&other) noexcept
Definition: layer.h:84
Layer(size_t n_in, size_t n_out, std::string activate_function="tanh")
Definition: layer.h:28
Layer & operator=(Layer &&other) noexcept
Definition: layer.h:70