AshanGimhana's picture
Upload folder using huggingface_hub
9375c9a verified
<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - misc.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2020 Davis E. King ([email protected])
</font><font color='#009900'>// License: Boost Software License See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_DNn_MISC_h
<font color='#0000FF'>#define</font> DLIB_DNn_MISC_h
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../cuda/tensor.h.html'>../cuda/tensor.h</a>"
<font color='#0000FF'>namespace</font> dlib
<b>{</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> label_type<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>struct</font> <b><a name='weighted_label'></a>weighted_label</b>
<b>{</b>
<b><a name='weighted_label'></a>weighted_label</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b><b>}</b>
<b><a name='weighted_label'></a>weighted_label</b><font face='Lucida Console'>(</font>label_type label, <font color='#0000FF'><u>float</u></font> weight <font color='#5555FF'>=</font> <font color='#979000'>1.f</font><font face='Lucida Console'>)</font>
: label<font face='Lucida Console'>(</font>label<font face='Lucida Console'>)</font>, weight<font face='Lucida Console'>(</font>weight<font face='Lucida Console'>)</font>
<b>{</b><b>}</b>
label_type label<b>{</b><b>}</b>;
<font color='#0000FF'><u>float</u></font> weight <font color='#5555FF'>=</font> <font color='#979000'>1.f</font>;
<b>}</b>;
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>inline</font> <font color='#0000FF'><u>double</u></font> <b><a name='log1pexp'></a>log1pexp</b><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font> x<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>using</font> std::exp;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std; <font color='#009900'>// Do this instead of using std::log1p because some compilers
</font> <font color='#009900'>// error out otherwise (E.g. gcc 4.9 in cygwin)
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>x <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#5555FF'>-</font><font color='#979000'>37</font><font face='Lucida Console'>)</font>
<font color='#0000FF'>return</font> <font color='#BB00BB'>exp</font><font face='Lucida Console'>(</font>x<font face='Lucida Console'>)</font>;
<font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>-</font><font color='#979000'>37</font> <font color='#5555FF'>&lt;</font> x <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> x <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>18</font><font face='Lucida Console'>)</font>
<font color='#0000FF'>return</font> <font color='#BB00BB'>log1p</font><font face='Lucida Console'>(</font><font color='#BB00BB'>exp</font><font face='Lucida Console'>(</font>x<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#979000'>18</font> <font color='#5555FF'>&lt;</font> x <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> x <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>33.3</font><font face='Lucida Console'>)</font>
<font color='#0000FF'>return</font> x <font color='#5555FF'>+</font> <font color='#BB00BB'>exp</font><font face='Lucida Console'>(</font><font color='#5555FF'>-</font>x<font face='Lucida Console'>)</font>;
<font color='#0000FF'>else</font>
<font color='#0000FF'>return</font> x;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
T <b><a name='safe_log'></a>safe_log</b><font face='Lucida Console'>(</font>T input, T epsilon <font color='#5555FF'>=</font> <font color='#979000'>1e</font><font color='#5555FF'>-</font><font color='#979000'>10</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// Prevent trying to calculate the logarithm of a very small number (let alone zero)
</font> <font color='#0000FF'>return</font> std::<font color='#BB00BB'>log</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>max</font><font face='Lucida Console'>(</font>input, epsilon<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>static</font> <font color='#0000FF'><u>size_t</u></font> <b><a name='tensor_index'></a>tensor_index</b><font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&amp;</font> t,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> sample,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> k,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> r,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> c
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>return</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>sample <font color='#5555FF'>*</font> t.<font color='#BB00BB'>k</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> k<font face='Lucida Console'>)</font> <font color='#5555FF'>*</font> t.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> r<font face='Lucida Console'>)</font> <font color='#5555FF'>*</font> t.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> c;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_DNn_MISC_h
</font>
</pre></body></html>