|
|
|
@ -2208,7 +2208,6 @@ void testCollectSharedBias(int numSamples, int dim, int channel) {
|
|
|
|
|
MatrixCheckErr(*cpuBias, *check);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Matrix, sharedBias) {
|
|
|
|
|
for (auto numSamples : {1, 100, 520}) {
|
|
|
|
|
for (auto dim : {100 * 16, 100 * 32}) {
|
|
|
|
@ -2222,6 +2221,60 @@ TEST(Matrix, sharedBias) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testMultiBinaryLabelCrossEntropy(int numSamples, int dim) {
|
|
|
|
|
MatrixPtr output = std::make_shared<CpuMatrix>(numSamples, dim);
|
|
|
|
|
MatrixPtr cpuOutput = std::make_shared<CpuMatrix>(numSamples, dim);
|
|
|
|
|
MatrixPtr gpuOutput = std::make_shared<GpuMatrix>(numSamples, dim);
|
|
|
|
|
|
|
|
|
|
MatrixPtr cpuEntropy = std::make_shared<CpuMatrix>(numSamples, 1);
|
|
|
|
|
MatrixPtr gpuEntropy = std::make_shared<GpuMatrix>(numSamples, 1);
|
|
|
|
|
|
|
|
|
|
MatrixPtr cpuGrad = std::make_shared<CpuMatrix>(numSamples, dim);
|
|
|
|
|
MatrixPtr gpuGrad = std::make_shared<GpuMatrix>(numSamples, dim);
|
|
|
|
|
|
|
|
|
|
MatrixPtr cpuLabel = std::make_shared<CpuSparseMatrix>
|
|
|
|
|
(numSamples, dim, numSamples, NO_VALUE, SPARSE_CSR, false);
|
|
|
|
|
MatrixPtr gpuLabel = std::make_shared<GpuSparseMatrix>
|
|
|
|
|
(numSamples, dim, numSamples, NO_VALUE, SPARSE_CSR, false);
|
|
|
|
|
for (int i = 0; i < numSamples; i ++) {
|
|
|
|
|
const unsigned int id = rand() % dim; // NOLINT
|
|
|
|
|
cpuLabel->setRow(i, 1, &id, nullptr);
|
|
|
|
|
gpuLabel->setRow(i, 1, &id, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output->randomizeUniform();
|
|
|
|
|
cpuOutput->zeroMem();
|
|
|
|
|
output->softmax(*cpuOutput);
|
|
|
|
|
gpuOutput->copyFrom(*cpuOutput);
|
|
|
|
|
|
|
|
|
|
cpuEntropy->zeroMem();
|
|
|
|
|
gpuEntropy->zeroMem();
|
|
|
|
|
cpuEntropy->multiBinaryLabelCrossEntropy(*cpuOutput, *cpuLabel);
|
|
|
|
|
gpuEntropy->multiBinaryLabelCrossEntropy(*gpuOutput, *gpuLabel);
|
|
|
|
|
|
|
|
|
|
MatrixPtr check1 = std::make_shared<CpuMatrix>(numSamples, 1);
|
|
|
|
|
check1->copyFrom(*gpuEntropy);
|
|
|
|
|
MatrixCheckErr(*cpuEntropy, *check1);
|
|
|
|
|
|
|
|
|
|
cpuGrad->zeroMem();
|
|
|
|
|
gpuGrad->zeroMem();
|
|
|
|
|
cpuGrad->multiBinaryLabelCrossEntropyBp(*cpuOutput, *cpuLabel);
|
|
|
|
|
gpuGrad->multiBinaryLabelCrossEntropyBp(*gpuOutput, *gpuLabel);
|
|
|
|
|
|
|
|
|
|
MatrixPtr check2 = std::make_shared<CpuMatrix>(numSamples, dim);
|
|
|
|
|
check2->copyFrom(*gpuGrad);
|
|
|
|
|
MatrixCheckErr(*cpuGrad, *check2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Matrix, multiBinaryCrossEntropy) {
|
|
|
|
|
for (auto numSamples : {100, 1000, 10000}) {
|
|
|
|
|
for (auto dim : {100, 1000, 10000}) {
|
|
|
|
|
VLOG(3) << " numSamples=" << numSamples << " dim=" << dim;
|
|
|
|
|
testMultiBinaryLabelCrossEntropy(numSamples, dim);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
|
testing::InitGoogleTest(&argc, argv);
|
|
|
|
|
initMain(argc, argv);
|
|
|
|
|