当前位置: 首页>>代码示例>>C++>>正文


C++ NDArray::bufferWithOffset方法代码示例

本文整理汇总了C++中NDArray::bufferWithOffset方法的典型用法代码示例。如果您正苦于以下问题:C++ NDArray::bufferWithOffset方法的具体用法?C++ NDArray::bufferWithOffset怎么用?C++ NDArray::bufferWithOffset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NDArray的用法示例。


在下文中一共展示了NDArray::bufferWithOffset方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: skipgramBatchExec_

            void skipgramBatchExec_(NDArray &s0, NDArray &s1, NDArray &s1n, void *vexpTable, void *vnegTable, void *vinfVector, NDArray &targets, NDArray &negStarters, NDArray &indices, NDArray &codes, NDArray &lr, NDArray &nextRandom, const int nsRounds, const int vocabSize, const int vectorLength, const int expLength, const int negLength, const bool preciseMode, const int numThreads) {
                //auto syn0 = reinterpret_cast<T*>(vsyn0);
                //auto syn1 = reinterpret_cast<T*>(vsyn1);
                //auto syn1Neg = reinterpret_cast<T*>(vsyn1Neg);
                const auto expTable = reinterpret_cast<T*>(vexpTable);
                const auto negTable = reinterpret_cast<T*>(vnegTable);
                const auto infVector = reinterpret_cast<T*>(vinfVector);

                T sneu1e[600];

                //const auto numThreads = omp_get_max_threads();
                const auto idxShift = indices.isEmpty() ? 0 : indices.sizeAt(1);
                const auto hsRounds = codes.isEmpty() ? 0 : codes.sizeAt(1);

                    // regular mode provides 0 guarantees for reproducibility
                    auto numTargets = targets.lengthOf();
                    auto bTarget = targets.bufferAsT<int>();
                    auto bIndices = indices.bufferAsT<int>();
                    auto bCodes = codes.bufferAsT<int8_t>();

#pragma omp parallel for num_threads(numThreads) private(sneu1e) default(shared) schedule(static)
                    for (int t = 0; t < numTargets; t++) {
                        T* neu1e = vectorLength <= 600 ? sneu1e : new T[vectorLength];
                        memset(neu1e, 0, vectorLength * sizeof(T));

                        auto target = bTarget[t];
                        auto alpha = lr.e<double>(t);
                        unsigned long long randomValue = nextRandom.e<Nd4jLong>(t);

                        auto syn0row = reinterpret_cast<T*>(s0.bufferWithOffset(target * vectorLength));

                        if (hsRounds > 0) {
                            int irow = 0;
                            auto cShift = t * idxShift;

                            for (int e = 0; e < hsRounds; e++) {
                                irow = bIndices[e + cShift];
                                if (irow < 0 || irow >= vocabSize)
                                    continue;

                                auto syn1row = s1.bufferWithOffset(irow * vectorLength);
                                auto code = bCodes[e + cShift];

                                    //nd4j_printf("syn0: [%i]; syn1: [%i]; code: [%i]\n", target, irow, code);
                                hSoftmax_<T>(syn0row, syn1row, expTable, neu1e, alpha, vectorLength, code, expLength, false);
                            }
                        }


                        if (nsRounds > 0) {
                            int irow = negStarters.e<int>(t);
                            int nsStarter = irow;
                            for (int r = 0; r < nsRounds + 1; r++) {
                                if (r == 0) {
                                    // target is known in advance
                                } else {
                                    randomValue = randomValue * (unsigned long long) 25214903917 + 11;
                                    auto idx = nd4j::math::nd4j_abs<Nd4jLong >((randomValue >> 16) % negLength);
                                    irow = idx >= negLength ? -1 : static_cast<int>(negTable[idx]);

                                    if (irow < 0 || irow >= vocabSize)
                                        irow = randomValue % (vocabSize - 1) + 1;

                                    if (irow == nsStarter)
                                        continue;
                                }

                                nSampling_<T>(syn0row, s1n.bufferWithOffset(irow * vectorLength), expTable, neu1e, alpha, vectorLength, r == 0 ? 1 : 0, expLength, infVector != nullptr);
                            }
                        }

                        #pragma omp simd
                        for (int e = 0; e < vectorLength; e++)
                            syn0row[e] += neu1e[e];


                        // optionally release temp arrays
                        if (vectorLength > 600)
                            delete[] neu1e;
                    }
开发者ID:akhodakivskiy,项目名称:deeplearning4j,代码行数:80,代码来源:sg_cb.cpp


注:本文中的NDArray::bufferWithOffset方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。