|
<html><head><title>dlib C++ Library - cpu_dlib.h</title></head><body bgcolor='white'><pre> |
|
<font color='#009900'>// Copyright (C) 2015 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_CPU_H_ |
|
<font color='#0000FF'>#define</font> DLIB_DNN_CPU_H_ |
|
|
|
<font color='#009900'>// This file contains CPU implementations of the GPU based functions in cuda_dlib.h |
|
</font><font color='#009900'>// and cudnn_dlibapi.h |
|
</font> |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='tensor.h.html'>tensor.h</a>" |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../geometry/rectangle.h.html'>../geometry/rectangle.h</a>" |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../dnn/misc.h.html'>../dnn/misc.h</a>" |
|
|
|
<font color='#0000FF'>namespace</font> dlib |
|
<b>{</b> |
|
<font color='#0000FF'>namespace</font> cpu |
|
<b>{</b> |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='multiply'></a>multiply</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>bool</u></font> add_to, |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src1, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src2 |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='multiply_conv'></a>multiply_conv</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>bool</u></font> add_to, |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src1, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src2 |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='multiply_zero_padded'></a>multiply_zero_padded</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>bool</u></font> add_to, |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src1, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src2 |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='scale_channels'></a>scale_channels</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>bool</u></font> add_to, |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> scales |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='add'></a>add</b><font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>float</u></font> beta, |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'><u>float</u></font> alpha, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='assign_bias_gradient'></a>assign_bias_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='add'></a>add</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src1, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src2 |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='assign_conv_bias_gradient'></a>assign_conv_bias_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='affine_transform'></a>affine_transform</b><font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> A, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> B |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='affine_transform'></a>affine_transform</b><font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src1, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src2, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> A, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> B, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> C |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='affine_transform'></a>affine_transform</b><font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src1, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src2, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src3, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> A, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> B, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> C, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> D |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='affine_transform_range'></a>affine_transform_range</b><font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>size_t</u></font> begin, |
|
<font color='#0000FF'><u>size_t</u></font> end, |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src1, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src2, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src3, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> A, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> B, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> C |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='affine_transform'></a>affine_transform</b><font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> A, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> B |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='affine_transform_conv'></a>affine_transform_conv</b><font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> A, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> B |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='affine_transform'></a>affine_transform</b><font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> rectangle<font color='#5555FF'>&</font> rect, |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src1, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src2, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src3, |
|
<font color='#0000FF'><u>float</u></font> A, |
|
<font color='#0000FF'><u>float</u></font> B, |
|
<font color='#0000FF'><u>float</u></font> C |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='compute_adam_update'></a>compute_adam_update</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>size_t</u></font> begin, |
|
<font color='#0000FF'><u>size_t</u></font> end, |
|
tensor<font color='#5555FF'>&</font> s, |
|
tensor<font color='#5555FF'>&</font> m, |
|
tensor<font color='#5555FF'>&</font> v, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> t, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> learning_rate, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> weight_decay, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> momentum1, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> momentum2, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> params, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> params_grad |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='batch_normalize_inference'></a>batch_normalize_inference</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> eps, |
|
resizable_tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gamma, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> beta, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> running_means, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> running_variances |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='batch_normalize'></a>batch_normalize</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> eps, |
|
resizable_tensor<font color='#5555FF'>&</font> dest, |
|
resizable_tensor<font color='#5555FF'>&</font> means, |
|
resizable_tensor<font color='#5555FF'>&</font> invstds, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> averaging_factor, |
|
resizable_tensor<font color='#5555FF'>&</font> running_means, |
|
resizable_tensor<font color='#5555FF'>&</font> running_variances, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gamma, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> beta |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='batch_normalize_gradient'></a>batch_normalize_gradient</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> eps, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> means, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> invstds, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gamma, |
|
tensor<font color='#5555FF'>&</font> src_grad, |
|
tensor<font color='#5555FF'>&</font> gamma_grad, |
|
tensor<font color='#5555FF'>&</font> beta_grad |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='batch_normalize_conv_inference'></a>batch_normalize_conv_inference</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> eps, |
|
resizable_tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gamma, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> beta, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> running_means, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> running_variances |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='batch_normalize_conv'></a>batch_normalize_conv</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> eps, |
|
resizable_tensor<font color='#5555FF'>&</font> dest, |
|
resizable_tensor<font color='#5555FF'>&</font> means, |
|
resizable_tensor<font color='#5555FF'>&</font> invstds, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> averaging_factor, |
|
resizable_tensor<font color='#5555FF'>&</font> running_means, |
|
resizable_tensor<font color='#5555FF'>&</font> running_variances, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gamma, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> beta |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='batch_normalize_conv_gradient'></a>batch_normalize_conv_gradient</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> eps, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> means, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> invstds, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gamma, |
|
tensor<font color='#5555FF'>&</font> src_grad, |
|
tensor<font color='#5555FF'>&</font> gamma_grad, |
|
tensor<font color='#5555FF'>&</font> beta_grad |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='layer_normalize'></a>layer_normalize</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> eps, |
|
resizable_tensor<font color='#5555FF'>&</font> dest, |
|
resizable_tensor<font color='#5555FF'>&</font> means, |
|
resizable_tensor<font color='#5555FF'>&</font> invstds, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gamma, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> beta |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='layer_normalize_gradient'></a>layer_normalize_gradient</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> eps, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> means, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> invstds, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gamma, |
|
tensor<font color='#5555FF'>&</font> src_grad, |
|
tensor<font color='#5555FF'>&</font> gamma_grad, |
|
tensor<font color='#5555FF'>&</font> beta_grad |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='threshold'></a>threshold</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> data, |
|
<font color='#0000FF'><u>float</u></font> thresh |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='dot'></a>dot</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> a, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> b, |
|
tensor<font color='#5555FF'>&</font> result, |
|
<font color='#0000FF'><u>size_t</u></font> idx |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='softmax'></a>softmax</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='softmax_gradient'></a>softmax_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ------------------------------------------------------------------------------------ |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='softmax_all'></a>softmax_all</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='softmax_all_gradient'></a>softmax_all_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ------------------------------------------------------------------------------------ |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='sigmoid'></a>sigmoid</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='sigmoid_gradient'></a>sigmoid_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ------------------------------------------------------------------------------------ |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='mish'></a>mish</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='mish_gradient'></a>mish_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ------------------------------------------------------------------------------------ |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='relu'></a>relu</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='relu_gradient'></a>relu_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='prelu'></a>prelu</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> param |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='prelu_gradient'></a>prelu_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> param, |
|
tensor<font color='#5555FF'>&</font> params_grad |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ------------------------------------------------------------------------------------ |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='leaky_relu'></a>leaky_relu</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> alpha |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='leaky_relu_gradient'></a>leaky_relu_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input, |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> alpha |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ------------------------------------------------------------------------------------ |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='tanh'></a>tanh</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='tanh_gradient'></a>tanh_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='gelu'></a>gelu</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='gelu_gradient'></a>gelu_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='resize_bilinear'></a>resize_bilinear</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'><u>long</u></font> dest_row_stride, |
|
<font color='#0000FF'><u>long</u></font> dest_channel_stride, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'><u>long</u></font> src_row_stride, |
|
<font color='#0000FF'><u>long</u></font> src_channel_stride |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='resize_bilinear_gradient'></a>resize_bilinear_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'><u>long</u></font> grad_row_stride, |
|
<font color='#0000FF'><u>long</u></font> grad_channel_stride, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input, |
|
<font color='#0000FF'><u>long</u></font> gradient_input_row_stride, |
|
<font color='#0000FF'><u>long</u></font> gradient_input_channel_stride |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'>inline</font> <font color='#0000FF'><u>void</u></font> <b><a name='resize_bilinear'></a>resize_bilinear</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src |
|
<font face='Lucida Console'>)</font> <b>{</b> <font color='#BB00BB'>resize_bilinear</font><font face='Lucida Console'>(</font>dest, dest.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, dest.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font>dest.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, src, src.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, src.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font>src.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; <b>}</b> |
|
|
|
<font color='#0000FF'>inline</font> <font color='#0000FF'><u>void</u></font> <b><a name='resize_bilinear_gradient'></a>resize_bilinear_gradient</b> <font face='Lucida Console'>(</font> |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input |
|
<font face='Lucida Console'>)</font> <b>{</b> <font color='#BB00BB'>resize_bilinear_gradient</font><font face='Lucida Console'>(</font>grad, grad.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, grad.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font>grad.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, gradient_input, gradient_input.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, gradient_input.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font>gradient_input.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; <b>}</b> |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>class</font> <b><a name='pooling'></a>pooling</b> |
|
<b>{</b> |
|
<font color='#0000FF'>public</font>: |
|
|
|
<b><a name='pooling'></a>pooling</b><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> pooling<font color='#5555FF'>&</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#0000FF'>delete</font>; |
|
pooling<font color='#5555FF'>&</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> pooling<font color='#5555FF'>&</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#0000FF'>delete</font>; |
|
|
|
<b><a name='pooling'></a>pooling</b> <font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='clear'></a>clear</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='setup_max_pooling'></a>setup_max_pooling</b><font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>int</u></font> window_height, |
|
<font color='#0000FF'><u>int</u></font> window_width, |
|
<font color='#0000FF'><u>int</u></font> stride_y, |
|
<font color='#0000FF'><u>int</u></font> stride_x, |
|
<font color='#0000FF'><u>int</u></font> padding_y, |
|
<font color='#0000FF'><u>int</u></font> padding_x |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='setup_avg_pooling'></a>setup_avg_pooling</b><font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>int</u></font> window_height, |
|
<font color='#0000FF'><u>int</u></font> window_width, |
|
<font color='#0000FF'><u>int</u></font> stride_y, |
|
<font color='#0000FF'><u>int</u></font> stride_x, |
|
<font color='#0000FF'><u>int</u></font> padding_y, |
|
<font color='#0000FF'><u>int</u></font> padding_x |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>bool</u></font> <b><a name='does_max_pooling'></a>does_max_pooling</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> do_max_pooling; <b>}</b> |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font> |
|
resizable_tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='get_gradient'></a>get_gradient</b><font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
tensor<font color='#5555FF'>&</font> grad |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'>private</font>: |
|
<font color='#0000FF'><u>int</u></font> window_height; |
|
<font color='#0000FF'><u>int</u></font> window_width; |
|
<font color='#0000FF'><u>int</u></font> stride_y; |
|
<font color='#0000FF'><u>int</u></font> stride_x; |
|
<font color='#0000FF'><u>int</u></font> padding_y; |
|
<font color='#0000FF'><u>int</u></font> padding_x; |
|
<font color='#0000FF'><u>bool</u></font> do_max_pooling; |
|
|
|
<b>}</b>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>class</font> <b><a name='tensor_conv'></a>tensor_conv</b> |
|
<b>{</b> |
|
<font color='#0000FF'>public</font>: |
|
<b><a name='tensor_conv'></a>tensor_conv</b><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> tensor_conv<font color='#5555FF'>&</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#0000FF'>delete</font>; |
|
tensor_conv<font color='#5555FF'>&</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> tensor_conv<font color='#5555FF'>&</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#0000FF'>delete</font>; |
|
|
|
<b><a name='tensor_conv'></a>tensor_conv</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b><b>}</b> |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='clear'></a>clear</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> <b>{</b><b>}</b> |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='setup'></a>setup</b><font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> data, <font color='#009900'>/* not used but required for interface */</font> |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> filters, <font color='#009900'>/* not used but required for interface */</font> |
|
<font color='#0000FF'><u>int</u></font> stride_y, |
|
<font color='#0000FF'><u>int</u></font> stride_x, |
|
<font color='#0000FF'><u>int</u></font> padding_y, |
|
<font color='#0000FF'><u>int</u></font> padding_x |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font face='Lucida Console'>(</font><font color='#0000FF'><u>void</u></font><font face='Lucida Console'>)</font>data; <font color='#009900'>/* silence compiler */</font> |
|
<font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font>stride_y <font color='#5555FF'>></font> <font color='#979000'>0</font> <font color='#5555FF'>&</font><font color='#5555FF'>&</font> stride_x <font color='#5555FF'>></font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>; |
|
<font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font><font color='#979000'>0</font> <font color='#5555FF'><</font><font color='#5555FF'>=</font> padding_y <font color='#5555FF'>&</font><font color='#5555FF'>&</font> padding_y <font color='#5555FF'><</font> filters.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font><font color='#979000'>0</font> <font color='#5555FF'><</font><font color='#5555FF'>=</font> padding_x <font color='#5555FF'>&</font><font color='#5555FF'>&</font> padding_x <font color='#5555FF'><</font> filters.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
last_stride_y <font color='#5555FF'>=</font> stride_y; |
|
last_stride_x <font color='#5555FF'>=</font> stride_x; |
|
last_padding_y <font color='#5555FF'>=</font> padding_y; |
|
last_padding_x <font color='#5555FF'>=</font> padding_x; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>bool</u></font> add_to_output, |
|
resizable_tensor<font color='#5555FF'>&</font> output, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> data, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> filters |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>bool</u></font> add_to_output, |
|
tensor<font color='#5555FF'>&</font> output, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> data, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> filters |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='get_gradient_for_data'></a>get_gradient_for_data</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>bool</u></font> add_to_output, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> filters, |
|
tensor<font color='#5555FF'>&</font> data_gradient |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='get_gradient_for_filters'></a>get_gradient_for_filters</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>bool</u></font> add_to_output, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> gradient_input, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> data, |
|
tensor<font color='#5555FF'>&</font> filters_gradient |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'>private</font>: |
|
|
|
<font color='#0000FF'><u>long</u></font> last_stride_y <font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<font color='#0000FF'><u>long</u></font> last_stride_x <font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<font color='#0000FF'><u>long</u></font> last_padding_y <font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<font color='#0000FF'><u>long</u></font> last_padding_x <font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<b>}</b>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='copy_tensor'></a>copy_tensor</b><font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>bool</u></font> add_to, |
|
tensor<font color='#5555FF'>&</font> dest, |
|
<font color='#0000FF'><u>size_t</u></font> dest_k_offset, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> src, |
|
<font color='#0000FF'><u>size_t</u></font> src_k_offset, |
|
<font color='#0000FF'><u>size_t</u></font> count_k |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>class</font> <b><a name='compute_loss_binary_log_per_pixel'></a>compute_loss_binary_log_per_pixel</b> |
|
<b>{</b> |
|
|
|
<font color='#009900'>/*! The point of this class is to compute the loss for loss_binary_log_per_pixel_ |
|
on the cpu to provide an analogous implementation of the cuda version |
|
!*/</font> |
|
<font color='#0000FF'>public</font>: |
|
<b><a name='compute_loss_binary_log_per_pixel'></a>compute_loss_binary_log_per_pixel</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font> |
|
<font color='#0000FF'>typename</font> const_label_iterator |
|
<font color='#5555FF'>></font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>(</font> |
|
const_label_iterator truth, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> output_tensor, |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'><u>double</u></font><font color='#5555FF'>&</font> loss |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>sigmoid</font><font face='Lucida Console'>(</font>grad, output_tensor<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>// The loss we output is the average loss over the mini-batch, and also over each element of the matrix output. |
|
</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> scale <font color='#5555FF'>=</font> <font color='#979000'>1.0</font><font color='#5555FF'>/</font><font face='Lucida Console'>(</font>output_tensor.<font color='#BB00BB'>num_samples</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font>output_tensor.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font>output_tensor.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
loss <font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<font color='#0000FF'><u>float</u></font><font color='#5555FF'>*</font> <font color='#0000FF'>const</font> g <font color='#5555FF'>=</font> grad.<font color='#BB00BB'>host</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font><font color='#5555FF'>*</font> <font color='#0000FF'>const</font> out_data <font color='#5555FF'>=</font> output_tensor.<font color='#BB00BB'>host</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>num_samples</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i, <font color='#5555FF'>+</font><font color='#5555FF'>+</font>truth<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> r <font color='#5555FF'>=</font> <font color='#979000'>0</font>; r <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>r<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> c <font color='#5555FF'>=</font> <font color='#979000'>0</font>; c <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>c<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> y <font color='#5555FF'>=</font> truth<font color='#5555FF'>-</font><font color='#5555FF'>></font><font color='#0000FF'>operator</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>(</font>r, c<font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>size_t</u></font> idx <font color='#5555FF'>=</font> <font color='#BB00BB'>tensor_index</font><font face='Lucida Console'>(</font>output_tensor, i, <font color='#979000'>0</font>, r, c<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>y <font color='#5555FF'>></font> <font color='#979000'>0.f</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> temp <font color='#5555FF'>=</font> <font color='#BB00BB'>log1pexp</font><font face='Lucida Console'>(</font><font color='#5555FF'>-</font>out_data[idx]<font face='Lucida Console'>)</font>; |
|
loss <font color='#5555FF'>+</font><font color='#5555FF'>=</font> y<font color='#5555FF'>*</font>scale<font color='#5555FF'>*</font>temp; |
|
g[idx] <font color='#5555FF'>=</font> y<font color='#5555FF'>*</font>scale<font color='#5555FF'>*</font><font face='Lucida Console'>(</font>g[idx]<font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>y <font color='#5555FF'><</font> <font color='#979000'>0.f</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> temp <font color='#5555FF'>=</font> <font color='#5555FF'>-</font><font face='Lucida Console'>(</font><font color='#5555FF'>-</font>out_data[idx]<font color='#5555FF'>-</font><font color='#BB00BB'>log1pexp</font><font face='Lucida Console'>(</font><font color='#5555FF'>-</font>out_data[idx]<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
loss <font color='#5555FF'>+</font><font color='#5555FF'>=</font> <font color='#5555FF'>-</font>y<font color='#5555FF'>*</font>scale<font color='#5555FF'>*</font>temp; |
|
g[idx] <font color='#5555FF'>=</font> <font color='#5555FF'>-</font>y<font color='#5555FF'>*</font>scale<font color='#5555FF'>*</font>g[idx]; |
|
<b>}</b> |
|
<font color='#0000FF'>else</font> |
|
<b>{</b> |
|
g[idx] <font color='#5555FF'>=</font> <font color='#979000'>0.f</font>; |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>class</font> <b><a name='compute_loss_multiclass_log_per_pixel'></a>compute_loss_multiclass_log_per_pixel</b> |
|
<b>{</b> |
|
|
|
<font color='#009900'>/*! The point of this class is to compute the loss for loss_multiclass_log_per_pixel_ |
|
on the cpu to provide an analogous implementation of the cuda version |
|
!*/</font> |
|
<font color='#0000FF'>public</font>: |
|
<b><a name='compute_loss_multiclass_log_per_pixel'></a>compute_loss_multiclass_log_per_pixel</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font> |
|
<font color='#0000FF'>typename</font> const_label_iterator |
|
<font color='#5555FF'>></font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>(</font> |
|
const_label_iterator truth, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> output_tensor, |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'><u>double</u></font><font color='#5555FF'>&</font> loss |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>softmax</font><font face='Lucida Console'>(</font>grad, output_tensor<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>// The loss we output is the average loss over the mini-batch, and also over each element of the matrix output. |
|
</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> scale <font color='#5555FF'>=</font> <font color='#979000'>1.0</font> <font color='#5555FF'>/</font> <font face='Lucida Console'>(</font>output_tensor.<font color='#BB00BB'>num_samples</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>*</font> output_tensor.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>*</font> output_tensor.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
loss <font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<font color='#0000FF'><u>float</u></font><font color='#5555FF'>*</font> <font color='#0000FF'>const</font> g <font color='#5555FF'>=</font> grad.<font color='#BB00BB'>host</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>num_samples</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i, <font color='#5555FF'>+</font><font color='#5555FF'>+</font>truth<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> r <font color='#5555FF'>=</font> <font color='#979000'>0</font>; r <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>r<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> c <font color='#5555FF'>=</font> <font color='#979000'>0</font>; c <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>c<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>const</font> uint16_t y <font color='#5555FF'>=</font> truth<font color='#5555FF'>-</font><font color='#5555FF'>></font><font color='#0000FF'>operator</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>(</font>r, c<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>// The network must produce a number of outputs that is equal to the number |
|
</font> <font color='#009900'>// of labels when using this type of loss. |
|
</font> <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font><font color='#0000FF'>static_cast</font><font color='#5555FF'><</font><font color='#0000FF'><u>long</u></font><font color='#5555FF'>></font><font face='Lucida Console'>(</font>y<font face='Lucida Console'>)</font> <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>k</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>|</font><font color='#5555FF'>|</font> y <font color='#5555FF'>=</font><font color='#5555FF'>=</font> label_to_ignore, |
|
"<font color='#CC0000'>y: </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> y <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>, output_tensor.k(): </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>k</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> k <font color='#5555FF'>=</font> <font color='#979000'>0</font>; k <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>k</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>k<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>size_t</u></font> idx <font color='#5555FF'>=</font> <font color='#BB00BB'>tensor_index</font><font face='Lucida Console'>(</font>output_tensor, i, k, r, c<font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>k <font color='#5555FF'>=</font><font color='#5555FF'>=</font> y<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
loss <font color='#5555FF'>+</font><font color='#5555FF'>=</font> scale<font color='#5555FF'>*</font><font color='#5555FF'>-</font><font color='#BB00BB'>safe_log</font><font face='Lucida Console'>(</font>g[idx]<font face='Lucida Console'>)</font>; |
|
g[idx] <font color='#5555FF'>=</font> scale<font color='#5555FF'>*</font><font face='Lucida Console'>(</font>g[idx] <font color='#5555FF'>-</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>y <font color='#5555FF'>=</font><font color='#5555FF'>=</font> label_to_ignore<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
g[idx] <font color='#5555FF'>=</font> <font color='#979000'>0.f</font>; |
|
<b>}</b> |
|
<font color='#0000FF'>else</font> |
|
<b>{</b> |
|
g[idx] <font color='#5555FF'>=</font> scale<font color='#5555FF'>*</font>g[idx]; |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<font color='#0000FF'>private</font>: |
|
<font color='#0000FF'>static</font> <font color='#0000FF'>const</font> uint16_t label_to_ignore <font color='#5555FF'>=</font> std::numeric_limits<font color='#5555FF'><</font>uint16_t<font color='#5555FF'>></font>::<b><a name='max'></a>max</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<b>}</b>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>class</font> <b><a name='compute_loss_multiclass_log_per_pixel_weighted'></a>compute_loss_multiclass_log_per_pixel_weighted</b> |
|
<b>{</b> |
|
|
|
<font color='#009900'>/*! The point of this class is to compute the loss for loss_multiclass_log_per_pixel_weighted_ |
|
on the cpu to provide an analogous implementation of the cuda version |
|
!*/</font> |
|
<font color='#0000FF'>public</font>: |
|
<b><a name='compute_loss_multiclass_log_per_pixel_weighted'></a>compute_loss_multiclass_log_per_pixel_weighted</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font> |
|
<font color='#0000FF'>typename</font> const_label_iterator |
|
<font color='#5555FF'>></font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>(</font> |
|
const_label_iterator truth, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> output_tensor, |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'><u>double</u></font><font color='#5555FF'>&</font> loss |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>softmax</font><font face='Lucida Console'>(</font>grad, output_tensor<font face='Lucida Console'>)</font>; |
|
<font color='#009900'>// The loss we output is the weighted average loss over the mini-batch, and also over each element of the matrix output. |
|
</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> scale <font color='#5555FF'>=</font> <font color='#979000'>1.0</font> <font color='#5555FF'>/</font> <font face='Lucida Console'>(</font>output_tensor.<font color='#BB00BB'>num_samples</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>*</font> output_tensor.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>*</font> output_tensor.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
loss <font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<font color='#0000FF'><u>float</u></font><font color='#5555FF'>*</font> <font color='#0000FF'>const</font> g <font color='#5555FF'>=</font> grad.<font color='#BB00BB'>host</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>num_samples</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i, <font color='#5555FF'>+</font><font color='#5555FF'>+</font>truth<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> r <font color='#5555FF'>=</font> <font color='#979000'>0</font>; r <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>r<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> c <font color='#5555FF'>=</font> <font color='#979000'>0</font>; c <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>c<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>const</font> weighted_label<font color='#5555FF'><</font>uint16_t<font color='#5555FF'>></font><font color='#5555FF'>&</font> weighted_label <font color='#5555FF'>=</font> truth<font color='#5555FF'>-</font><font color='#5555FF'>></font><font color='#0000FF'>operator</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>(</font>r, c<font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>const</font> uint16_t y <font color='#5555FF'>=</font> weighted_label.label; |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> weight <font color='#5555FF'>=</font> weighted_label.weight; |
|
<font color='#009900'>// The network must produce a number of outputs that is equal to the number |
|
</font> <font color='#009900'>// of labels when using this type of loss. |
|
</font> <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font><font color='#0000FF'>static_cast</font><font color='#5555FF'><</font><font color='#0000FF'><u>long</u></font><font color='#5555FF'>></font><font face='Lucida Console'>(</font>y<font face='Lucida Console'>)</font> <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>k</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>|</font><font color='#5555FF'>|</font> weight <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0.f</font>, |
|
"<font color='#CC0000'>y: </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> y <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>, output_tensor.k(): </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>k</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> k <font color='#5555FF'>=</font> <font color='#979000'>0</font>; k <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>k</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>k<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>size_t</u></font> idx <font color='#5555FF'>=</font> <font color='#BB00BB'>tensor_index</font><font face='Lucida Console'>(</font>output_tensor, i, k, r, c<font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>k <font color='#5555FF'>=</font><font color='#5555FF'>=</font> y<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
loss <font color='#5555FF'>+</font><font color='#5555FF'>=</font> weight<font color='#5555FF'>*</font>scale<font color='#5555FF'>*</font><font color='#5555FF'>-</font><font color='#BB00BB'>safe_log</font><font face='Lucida Console'>(</font>g[idx]<font face='Lucida Console'>)</font>; |
|
g[idx] <font color='#5555FF'>=</font> weight<font color='#5555FF'>*</font>scale<font color='#5555FF'>*</font><font face='Lucida Console'>(</font>g[idx] <font color='#5555FF'>-</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<font color='#0000FF'>else</font> |
|
<b>{</b> |
|
g[idx] <font color='#5555FF'>=</font> weight<font color='#5555FF'>*</font>scale<font color='#5555FF'>*</font>g[idx]; |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
|
|
<b>}</b>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>class</font> <b><a name='compute_loss_mean_squared_per_channel_and_pixel'></a>compute_loss_mean_squared_per_channel_and_pixel</b> |
|
<b>{</b> |
|
<font color='#009900'>/*! The point of this class is to compute the loss for loss_mean_squared_per_channel_and_pixel_ |
|
on the cpu to provide an analogous implementation of the cuda version |
|
!*/</font> |
|
<font color='#0000FF'>public</font>: |
|
|
|
<b><a name='compute_loss_mean_squared_per_channel_and_pixel'></a>compute_loss_mean_squared_per_channel_and_pixel</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font> |
|
<font color='#0000FF'>typename</font> const_label_iterator |
|
<font color='#5555FF'>></font> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>(</font> |
|
const_label_iterator truth, |
|
<font color='#0000FF'>const</font> tensor<font color='#5555FF'>&</font> output_tensor, |
|
tensor<font color='#5555FF'>&</font> grad, |
|
<font color='#0000FF'><u>double</u></font><font color='#5555FF'>&</font> loss |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#009900'>// The loss we output is the average loss over the mini-batch, and also over each element of the matrix output. |
|
</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> scale <font color='#5555FF'>=</font> <font color='#979000'>1.0</font> <font color='#5555FF'>/</font> <font face='Lucida Console'>(</font>output_tensor.<font color='#BB00BB'>num_samples</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>*</font> output_tensor.<font color='#BB00BB'>k</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>*</font> output_tensor.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>*</font> output_tensor.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
loss <font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<font color='#0000FF'><u>float</u></font><font color='#5555FF'>*</font> <font color='#0000FF'>const</font> g <font color='#5555FF'>=</font> grad.<font color='#BB00BB'>host</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font><font color='#5555FF'>*</font> out_data <font color='#5555FF'>=</font> output_tensor.<font color='#BB00BB'>host</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>num_samples</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i, <font color='#5555FF'>+</font><font color='#5555FF'>+</font>truth<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> k <font color='#5555FF'>=</font> <font color='#979000'>0</font>; k <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>k</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>k<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> r <font color='#5555FF'>=</font> <font color='#979000'>0</font>; r <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>r<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> c <font color='#5555FF'>=</font> <font color='#979000'>0</font>; c <font color='#5555FF'><</font> output_tensor.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>c<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> y <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font>truth<font face='Lucida Console'>)</font>[k].<font color='#0000FF'>operator</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>(</font>r, c<font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>size_t</u></font> idx <font color='#5555FF'>=</font> <font color='#BB00BB'>tensor_index</font><font face='Lucida Console'>(</font>output_tensor, i, k, r, c<font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> temp1 <font color='#5555FF'>=</font> y <font color='#5555FF'>-</font> out_data[idx]; |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> temp2 <font color='#5555FF'>=</font> scale<font color='#5555FF'>*</font>temp1; |
|
loss <font color='#5555FF'>+</font><font color='#5555FF'>=</font> temp2<font color='#5555FF'>*</font>temp1; |
|
g[idx] <font color='#5555FF'>=</font> <font color='#5555FF'>-</font>temp2; |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
<b>}</b> |
|
|
|
<b>}</b>; |
|
|
|
<font color='#009900'>// ----------------------------------------------------------------------------------- |
|
</font> |
|
<b>}</b> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>#ifdef</font> NO_MAKEFILE |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='cpu_dlib.cpp.html'>cpu_dlib.cpp</a>" |
|
<font color='#0000FF'>#endif</font> |
|
|
|
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_DNN_CPU_H_ |
|
</font> |
|
|
|
|
|
</pre></body></html> |