neuron.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 #include <random>
5 #include <vector>
6 
7 #include "../variable/variable.h"
8 
13 class Neuron
14 {
15 private:
16  std::vector<Variable> _weights; // The weights of the neuron.
17  Variable _bias; // The bias of the neuron.
18  std::string _activate_function; // The activation function of the neuron.
19  std::vector<Variable> _parameters; // All parameters of the neuron.
20 
21 public:
27  Neuron(size_t n_in, std::string activate_function = "tanh")
28  : _weights(n_in), _activate_function(activate_function)
29  {
30  _parameters.reserve(n_in + 1);
31  unsigned seed = static_cast<unsigned>(
32  std::chrono::system_clock::now().time_since_epoch().count());
33  std::default_random_engine generator(seed);
34  std::uniform_real_distribution<double> distribution(-1.0, 1.0);
35  distribution.reset();
36  for (size_t i = 0; i < n_in; i++)
37  {
38  _weights[i] = Variable(distribution(generator), 0.0, "", "weights");
39  _parameters.push_back(_weights[i]);
40  }
41  _bias = Variable(distribution(generator), 0, "", "bias");
42  _parameters.push_back(_bias);
43  }
44 
49  Neuron(const Neuron &other)
50  : _weights(other._weights), _bias(other._bias),
51  _activate_function(other._activate_function){};
52 
58  Neuron &operator=(const Neuron &other)
59  {
60  _weights = other._weights;
61  _bias = other._bias;
62  _activate_function = other._activate_function;
63  return *this;
64  }
65 
70  Neuron(Neuron &&other) noexcept
71  : _weights(other._weights), _bias(other._bias),
72  _activate_function(other._activate_function)
73  {
74  for (auto &weight : other._weights)
75  {
76  weight.set_ref(nullptr);
77  }
78  other._weights.clear();
79  other._bias.set_ref(nullptr);
80  for (auto &weight : _weights)
81  {
82  weight.set_ref(&weight);
83  }
84  _bias.set_ref(&_bias);
85  }
86 
92  Neuron &operator=(Neuron &&other) noexcept
93  {
94  _weights = other._weights;
95  _bias = other._bias;
96  _activate_function = other._activate_function;
97  for (auto &weight : other._weights)
98  {
99  weight.set_ref(nullptr);
100  }
101  other._weights.clear();
102  other._bias.set_ref(nullptr);
103 
104  for (auto &weight : _weights)
105  {
106  weight.set_ref(&weight);
107  }
108  _bias.set_ref(&_bias);
109  return *this;
110  }
111 
116  {
117  for (auto &weight : _weights)
118  {
119  weight.set_ref(nullptr);
120  }
121  _bias.set_ref(nullptr);
122  }
123 
128  const std::vector<Variable> &weights() const
129  {
130  return _weights;
131  }
132 
137  const Variable &bias() const
138  {
139  return _bias;
140  }
141 
146  const std::vector<Variable> &parameters() const
147  {
148  return _parameters;
149  }
150 
156  Variable forward(const std::vector<double> &inputs);
157 
163  Variable forward(const std::vector<Variable> &variables);
164 };
Definition: neuron.h:14
Neuron(size_t n_in, std::string activate_function="tanh")
Definition: neuron.h:27
Neuron & operator=(Neuron &&other) noexcept
Definition: neuron.h:92
Variable forward(const std::vector< double > &inputs)
Definition: neuron.cc:5
Neuron(Neuron &&other) noexcept
Definition: neuron.h:70
Neuron(const Neuron &other)
Definition: neuron.h:49
const std::vector< Variable > & weights() const
Definition: neuron.h:128
const Variable & bias() const
Definition: neuron.h:137
~Neuron()
Definition: neuron.h:115
Neuron & operator=(const Neuron &other)
Definition: neuron.h:58
const std::vector< Variable > & parameters() const
Definition: neuron.h:146
Definition: variable.h:13
void set_ref(Variable *reference)
Definition: variable.h:200