commit e9d03fd360944cd588d5f1b0d6da5ae86e3fd32f
parent 3c5fa282b5ec8210a8b9f9ba11beff8965c2cce3
Author: David Freifeld <freifeld.david@gmail.com>
Date: Sun, 6 Sep 2020 20:56:20 -0700
Beginning RNN implementation
Diffstat:
| A | src/rnn.cpp | | | 43 | +++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 43 insertions(+), 0 deletions(-)
diff --git a/src/rnn.cpp b/src/rnn.cpp
@@ -0,0 +1,43 @@
+#include "bpnn.hpp"
+
+class RecurrentLayer : public Layer {
+public:
+ Eigen::MatrixXf* rec_weights;
+ void init_weights(RecurrentLayer next);
+ RecurrentLayer(int rows, int columns, float a=0);
+};
+
+RecurrentLayer::RecurrentLayer(int rows, int columns, float a)
+ :Layer(rows, columns, a)
+{
+ rec_weights = new Eigen::MatrixXf(contents->cols(), contents->cols());
+ for (int i = 0; i < (rec_weights->rows()*rec_weights->cols()); i++) {
+ std::random_device rd;
+ std::mt19937 gen(rd());
+ (*rec_weights)(static_cast<int>(i / columns), i%columns) = d(gen);
+ }
+}
+
+void init_weights(RecurrentLayer next)
+{
+ v = new Eigen::MatrixXf (contents->cols(), next.contents->cols());
+ m = new Eigen::MatrixXf (contents->cols(), next.contents->cols());
+ weights = new Eigen::MatrixXf (contents->cols(), next.contents->cols());
+ int nodes = weights->cols();
+ int n = contents->cols() + next.contents->cols();
+ std::normal_distribution<float> d(0,sqrt(1.0/n));
+ for (int i = 0; i < (weights->rows()*weights->cols()); i++) {
+ std::random_device rd;
+ std::mt19937 gen(rd());
+ (*weights)(static_cast<int>(i / nodes), i%nodes) = d(gen);
+ (*v)(static_cast<int>(i / nodes), i%nodes) = 0;
+ (*m)(static_cast<int>(i / nodes), i%nodes) = 0;
+ }
+}
+
+class RNN : public Network {
+public:
+ void feedforward();
+ void backpropagate();
+ RNN();
+};