1. 程式人生 > 實用技巧 >『原始碼閱讀』Warp CTC

『原始碼閱讀』Warp CTC

tensorflow_binding\src\warpctc_op.cc:

WarpCTCOpBase::Compute方法

compute_ctc_loss(activations_t.data(), //[max_time,batch_size,num_classes_raw]
grads_t.data(), //[max_time,batch_size,num_classes_raw]
         flat_labels_t.data(), //[batch_size,max_time]
         label_lengths_t.data(), //[batch_size]

         input_lengths_t.data(), //[batch_size]
         alphabet_size,batch_size, // int, int
         costs_t.data(), //[batch_size]
         workspace_t.data(),options); // 記憶體/視訊記憶體申請尺寸

/** Compute the connectionist temporal classification loss between a sequence
 *  of probabilities and a ground truth labeling.  Optionally compute the
 *  gradient with respect to the inputs.
 * \param [in] activations pointer to the activations in either CPU or GPU
 *             addressable memory, depending on info.  We assume a fixed
 *             memory layout for this 3 dimensional tensor, which has dimension
 *             (t, n, p), where t is the time index, n is the minibatch index,
 *             and p indexes over probabilities of each symbol in the alphabet.
 *             The memory layout is (t, n, p) in C order (slowest to fastest changing
 *             index, aka row-major), or (p, n, t) in Fortran order (fastest to slowest
 *             changing index, aka column-major). We also assume strides are equal to
 *             dimensions - there is no padding between dimensions.
 *             More precisely, element (t, n, p), for a problem with mini_batch examples
 *             in the mini batch, and alphabet_size symbols in the alphabet, is located at:
 *             activations[(t * mini_batch + n) * alphabet_size + p]
 * \param [out] gradients if not NULL, then gradients are computed.  Should be
 *              allocated in the same memory space as probs and memory
 *              ordering is identical.
 * \param [in]  flat_labels Always in CPU memory.  A concatenation
 *              of all the labels for the minibatch.
 * \param [in]  label_lengths Always in CPU memory. The length of each label
 *              for each example in the minibatch.
 * \param [in]  input_lengths Always in CPU memory.  The number of time steps
 *              for each sequence in the minibatch.
 * \param [in]  alphabet_size The number of possible output symbols.  There
 *              should be this many probabilities for each time step.
 * \param [in]  mini_batch How many examples in a minibatch.
 * \param [out] costs Always in CPU memory.  The cost of each example in the
 *              minibatch.
 * \param [in,out] workspace In same memory space as probs. Should be of
 *                 size requested by get_workspace_size.
 * \param [in]  options see struct ctcOptions
 *
 *  \return Status information
 *
 * */
API_REFERENCE ctcStatus_t compute_ctc_loss(const float* const activations,
                             float* gradients,
                             const int* const flat_labels,
                             const int* const label_lengths,
                             const int* const input_lengths,
                             int alphabet_size,
                             int minibatch,
                             float *costs,
                             void *workspace,
                             ctcOptions options);

src\ctc_entrypoint.cpp:

compute_ctc_loss函式

cost_and_grad方法:計算梯度

引數:activations,gradients,costs,flat_labels,label_lengths,input_lengths

score_forward方法:單純forward

include\detail\cpu_ctc.h:

CpuCTC<ProbT>::cost_and_grad方法

利用OpenMP並行迴圈batch

const int T = input_lengths[mb];
const int L = label_lengths[mb];

const int S = 2*L + 1; // Number of labels with blanks
CpuCTC<ProbT>::cost_and_grad_kernel CpuCTC<ProbT>::CpuCTC_metadata CpuCTC<ProbT>::CpuCTC_metadata::setup_labels if(L+ctcm.repeats>T) return 0; // 不明白這個報錯 CpuCTC<ProbT>::compute_alphas CpuCTC<ProbT>::compute_betas_and_grad