diff --git a/C++/p003-Dot-Product.cpp b/C++/p003-Dot-Product.cpp index f159b8f..4041124 100644 --- a/C++/p003-Dot-Product.cpp +++ b/C++/p003-Dot-Product.cpp @@ -3,53 +3,40 @@ * * Associated tutorial https://www.youtube.com/watch?v=tMrbN67U9d4 */ -#include #include -#include #include -#include #include -// Simple implementation of built-in python zip -template -std::vector> zip(typename std::vector::iterator firstArrayBegin, - typename std::vector

::iterator secondArrayBegin, - typename std::vector

::const_iterator secondArrayEnd) +template +void print_vec(const std::vector& vec, const std::string title) { - const auto size = std::distance(static_cast(secondArrayBegin), secondArrayEnd); - std::vector> zipped; - zipped.reserve(size); - while (secondArrayBegin != secondArrayEnd) { - zipped.emplace_back(*firstArrayBegin++, *secondArrayBegin++); - } - return zipped; + std::cout << "\n----- " << title << " -----\n"; + for (int i{}; i != vec.size(); ++i) + std::cout << vec[i] << (!((i + 1) % 5) ? "\n" : " "); + std::cout << "\n----- " << title << " -----\n"; } + int main() { // Inputs - std::vector inputs{1.0, 2.0, 3.0, 2.5}; - std::vector> weights{{.2, 0.8, -0.5, 1.0}, - {0.5, -0.91, 0.26, -0.5}, - {-0.26, -0.27, 0.17, 0.87}}; - std::vector biases{2.0, 3.0, 0.5}; + std::vector inputs{ 1.0, 2.0, 3.0, 2.5 }; + std::vector> weights{ + { 0.2, 0.8, -0.5, 1.0 }, + { 0.5, -0.91, 0.26, -0.5 }, + { -0.26, -0.27, 0.17, 0.87 } + }; + std::vector biases{ 2.0, 3.0, 0.5 }; - // Zipping - auto zipped_weights_and_biases = - zip(weights.begin(), biases.begin(), biases.end()); // Calculating - std::vector outputs; - outputs.reserve(std::size(zipped_weights_and_biases)); - std::transform(std::begin(zipped_weights_and_biases), std::end(zipped_weights_and_biases), - std::back_inserter(outputs), [&inputs](const auto &weightAndBias) { - return std::inner_product(std::begin(std::get<0>(weightAndBias)), - std::end(std::get<0>(weightAndBias)), - std::begin(inputs), 0.0)+std::get<1>(weightAndBias); - }); + std::vector outputs(biases.size()); + + // inner product of inputs and weights on every iterate, also init value is biases[i]. + for (size_t i{}; i != weights.size(); ++i) + outputs[i] = std::inner_product(inputs.begin(), inputs.end(), weights[i].begin(), biases[i]); // Ouputing - std::cout << '['; - std::copy(outputs.begin(), outputs.end(), std::ostream_iterator(std::cout, " ")); - std::cout << ']'; + print_vec(outputs, "Output"); + return 0; }