Skip to content

Commit

Permalink
Iterating with references
Browse files Browse the repository at this point in the history
  • Loading branch information
fszewczyk committed Nov 8, 2023
1 parent 71bca9b commit e30b15e
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions examples/xor_classification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main() {
auto mlp = SequentialBuilder<Type::float32>::begin()
.add(Linear32::create(2, 15))
.add(ReLU32::create())
.add(Dropout32::create(15, 5, 0.3))
.add(Dropout32::create(15, 5, 0.2))
.add(Tanh32::create())
.add(Linear32::create(5, 2))
.add(Softmax32::create())
Expand All @@ -27,7 +27,7 @@ int main() {
Loss::Function32 lossFunction = Loss::CrossEntropy<Type::float32>;

// ------ TRAINING THE NETWORK ------- //
for (size_t epoch = 0; epoch < 500; epoch++) {
for (size_t epoch = 0; epoch < 100; epoch++) {
auto epochLoss = Val32::create(0);

optimizer.reset();
Expand Down
4 changes: 2 additions & 2 deletions include/nn/Neuron.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ template <typename T> std::vector<ValuePtr<T>> Neuron<T>::parameters() const {
std::vector<ValuePtr<T>> params;
params.reserve(_weights.size() + 1);

for (size_t i = 0; i < _weights.size(); ++i)
params.push_back(_weights[i]);
for (auto &w : _weights)
params.push_back(w);

params.push_back(_bias);

Expand Down
2 changes: 1 addition & 1 deletion include/nn/activation/Exp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ template <typename T> Vector<T> Exp<T>::operator()(const Vector<T> &x) const {
std::vector<ValuePtr<T>> out;
out.reserve(x.size());

for (auto entry : x)
for (auto &entry : x)
out.emplace_back(entry->exp());

return Vector<T>(out);
Expand Down
2 changes: 1 addition & 1 deletion include/nn/activation/ReLU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ template <typename T> Vector<T> ReLU<T>::operator()(const Vector<T> &x) const {
std::vector<ValuePtr<T>> out;
out.reserve(x.size());

for (auto entry : x)
for (auto &entry : x)
out.emplace_back(entry->relu());

return Vector<T>(out);
Expand Down
2 changes: 1 addition & 1 deletion include/nn/activation/Sigmoid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ template <typename T> Vector<T> Sigmoid<T>::operator()(const Vector<T> &x) const
std::vector<ValuePtr<T>> out;
out.reserve(x.size());

for (auto entry : x) {
for (auto &entry : x) {
out.emplace_back(entry->sigmoid());
}

Expand Down
4 changes: 2 additions & 2 deletions include/nn/activation/Softmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ template <typename T> Vector<T> Softmax<T>::operator()(const Vector<T> &x) const
out.reserve(x.size());

auto maxValue = Value<T>::create(x[0]->getValue());
for (auto entry : x)
for (auto &entry : x)
if (entry > maxValue)
maxValue = entry;

auto sumExponentiated = Value<T>::create(0);
for (auto entry : x) {
for (auto &entry : x) {
auto exponentiated = (entry - maxValue)->exp();
out.emplace_back(exponentiated);
sumExponentiated = sumExponentiated + exponentiated;
Expand Down
2 changes: 1 addition & 1 deletion include/nn/activation/Tanh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ template <typename T> Vector<T> Tanh<T>::operator()(const Vector<T> &x) const {
std::vector<ValuePtr<T>> out;
out.reserve(x.size());

for (auto entry : x)
for (auto &entry : x)
out.emplace_back(entry->tanh());

return Vector<T>(out);
Expand Down

0 comments on commit e30b15e

Please sign in to comment.