@@ -282,6 +282,16 @@ class decoder
282282 std::vector<std::vector<uint32_t >> D_sparse;
283283};
284284
285+ // / @brief Convert a single soft probability to a hard 0/1 decision.
286+ // / @param in Soft probability input in range [0.0, 1.0]
287+ // / @param thresh Values >= thresh return true; all others return false.
288+ template <typename t_soft,
289+ typename std::enable_if<std::is_floating_point<t_soft>::value,
290+ int >::type = 0 >
291+ constexpr inline bool convert_soft_to_hard (t_soft in, t_soft thresh = 0.5 ) {
292+ return in >= thresh;
293+ }
294+
285295// / @brief Convert a vector of soft probabilities to a vector of hard
286296// / probabilities.
287297// / @param in Soft probability input vector in range [0.0, 1.0]
@@ -298,7 +308,7 @@ inline void convert_vec_soft_to_hard(const std::vector<t_soft> &in,
298308 t_soft thresh = 0.5 ) {
299309 out.resize (in.size ());
300310 for (std::size_t i = 0 ; i < in.size (); i++)
301- out[i] = static_cast <t_hard>(in[i] >= thresh ? 1 : 0 );
311+ out[i] = static_cast <t_hard>(convert_soft_to_hard ( in[i], thresh) );
302312}
303313
304314// / @brief Convert a vector of soft probabilities to a tensor<uint8_t> of hard
@@ -326,7 +336,7 @@ inline void convert_vec_soft_to_tensor_hard(const std::vector<t_soft> &in,
326336 " Vector to tensor conversion requires tensor dim == vector length" );
327337 auto raw_ptr = out.data ();
328338 for (size_t i = 0 ; i < in.size (); ++i)
329- raw_ptr[i] = static_cast <t_hard>(in[i] >= thresh ? 1 : 0 );
339+ raw_ptr[i] = static_cast <t_hard>(convert_soft_to_hard ( in[i], thresh) );
330340}
331341
332342// / @brief Convert a vector of hard probabilities to a vector of soft
@@ -392,7 +402,7 @@ inline void convert_vec_soft_to_hard(const std::vector<std::vector<t_soft>> &in,
392402 auto &out_row = out[row_index++];
393403 out_row.resize (r.size ());
394404 for (std::size_t c = 0 ; c < r.size (); c++)
395- out_row[c] = static_cast <t_hard>(r[c] >= thresh ? 1 : 0 );
405+ out_row[c] = static_cast <t_hard>(convert_soft_to_hard ( r[c], thresh) );
396406 }
397407}
398408
0 commit comments