From c2fe68a8ddd26e1f7b122e7d85ec63e0e9e8b2d8 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Fri, 21 Jan 2022 09:52:53 +0000 Subject: [PATCH] - added void sample_one_hot() git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@856 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- source/RnnStack.hpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/source/RnnStack.hpp b/source/RnnStack.hpp index 9a1eeeb..a9751df 100644 --- a/source/RnnStack.hpp +++ b/source/RnnStack.hpp @@ -15,6 +15,7 @@ #define RNNSTACK_HPP #include "AStack.hpp" +#include "matutils.hpp" class RnnStack : public AStack { @@ -33,6 +34,27 @@ public: arma::mat step_forward(arma::mat &state, const arma::mat &v); + void sample_one_hot(arma::mat &srcDst) + { + double k = arma::accu(srcDst); + + if (k != 0) + { + srcDst = srcDst / k; + } + + arma::mat ps = Matutils::sample(srcDst); + if (arma::accu(ps) > 1) + { + arma::uvec q1 = find(ps > 0); + int winner = (q1.n_elem-1) * arma::randu(1)[0]; + int wix = q1(winner); + + srcDst = zeros(arma::size(srcDst)); + srcDst[wix] = 1; + } + } + void clamp_one_hot(arma::mat &srcDst) { int index = srcDst.index_max();