// File: /workspace/github_cpp_makefile_star2_O2/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::SaveAccuracyAndCostForBatch()
void ConstructLayer::SaveAccuracyAndCostForBatch()
{
    // accuracy
    // get labels for current batch
    std::vector<Matrix> & batch_labels =  __p_data_interface -> GetCurrentBatchLabel();
    assert(__imageA.size() == batch_labels.size());

    double correct_prediction = 0.;
    for(size_t i=0;i<batch_labels.size();i++)
    {
        Matrix & label_sample = batch_labels[i];
        Matrix & output_sample = __imageA[i].OutputImageFromKernel[0]; // output layer is 1D

        auto dim = output_sample.Dimension();
        assert(dim == label_sample.Dimension());
        assert(dim.second == 1);

        std::pair<size_t, size_t> max_coord;
        double max = output_sample.MaxInSection(0, dim.first, 0, dim.second, max_coord);

        double confidence_threshold = 0.8;
        if (max > confidence_threshold)
        {
            Matrix tmp(dim, 0);
            tmp[max_coord.first][max_coord.second] = 1.;

            if(tmp == label_sample)
                correct_prediction += 1.0;
        }

    }

    double accuracy_for_this_batch = correct_prediction / (int)batch_labels.size();
    __accuracyForBatches.push_back(accuracy_for_this_batch);

    // cost
    double cost = 0.;
    for(auto &i: __outputLayerCost)
        cost += i;
    cost /= (double)__outputLayerCost.size();

    __lossForBatches.push_back(-cost);

    std::cout<<"............ accuracy for batch training: "<<accuracy_for_this_batch<<std::endl;
    std::cout<<"............    losss for batch training: "<<-cost<<std::endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq 0x400(%rdi), %rax
movq 0x140(%rdi), %rcx
movq %rdi, 0x8(%rsp)
subq 0x138(%rdi), %rcx
movq 0x28(%rax), %r13
movq %rax, 0x38(%rsp)
movq 0x30(%rax), %rax
movq %rax, %rdx
subq %r13, %rdx
cmpq %rdx, %rcx
jne 0xf882
xorpd %xmm1, %xmm1
xorl %esi, %esi
pushq $0x1
popq %r14
leaq 0x50(%rsp), %r12
xorl %edi, %edi
subq %r13, %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %rax, %rdi
jae 0xf74b
movq %rdi, 0x40(%rsp)
movq %r14, %rbx
movsd %xmm1, 0x10(%rsp)
addq %rsi, %r13
movq 0x8(%rsp), %rax
movq 0x138(%rax), %rax
movq %rsi, 0x48(%rsp)
movq (%rax,%rsi), %r15
movq %r15, %rdi
callq 0x17a6a
movq %rax, %rbp
movq %rdx, %r14
movq %r13, %rdi
callq 0x17a6a
movq %rdx, %rcx
movq %rbp, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x175e8
testb %al, %al
je 0xf844
cmpq $0x1, %r14
jne 0xf863
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %rbp, %rdx
xorl %ecx, %ecx
movq %rbx, %r14
movq %rbx, %r8
leaq 0x20(%rsp), %r9
callq 0x19904
ucomisd 0x11ec8(%rip), %xmm0    # 0x21580
jbe 0xf722
xorpd %xmm0, %xmm0
movq %r12, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1787a
movq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x17c46
movq 0x28(%rsp), %rcx
movq (%rax), %rax
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%rax,%rcx,8)
movq %r12, %rdi
movq %r13, %rsi
callq 0x18984
movsd 0x10(%rsp), %xmm0
movapd %xmm0, %xmm1
addsd 0x11e04(%rip), %xmm1    # 0x21510
testb %al, %al
jne 0xf714
movapd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq %r12, %rdi
callq 0x17a9a
movsd 0x10(%rsp), %xmm1
movq 0x40(%rsp), %rdi
incq %rdi
movq 0x38(%rsp), %rax
movq 0x28(%rax), %r13
movq 0x30(%rax), %rax
movq 0x48(%rsp), %rsi
addq $0x18, %rsi
jmp 0xf619
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
leaq 0x50(%rsp), %rsi
movsd %xmm1, (%rsi)
movq 0x8(%rsp), %rbx
leaq 0x410(%rbx), %rdi
callq 0x11d08
movq 0x1f8(%rbx), %rcx
movq 0x200(%rbx), %rax
xorpd %xmm2, %xmm2
movq %rcx, %rdx
cmpq %rax, %rdx
je 0xf791
addsd (%rdx), %xmm2
addq $0x8, %rdx
jmp 0xf782
subq %rcx, %rax
sarq $0x3, %rax
movq %rax, %xmm0
punpckldq 0x1187b(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x11883(%rip), %xmm0    # 0x21030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorpd 0x118af(%rip), %xmm2    # 0x21070
movq 0x8(%rsp), %rdi
addq $0x428, %rdi            # imm = 0x428
divsd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
leaq 0x20(%rsp), %rsi
movsd %xmm2, (%rsi)
callq 0x16266
movq 0x237ec(%rip), %rbx     # 0x32fd8
leaq 0x13838(%rip), %rsi     # 0x2302b
movq %rbx, %rdi
callq 0x4240
movsd 0x50(%rsp), %xmm0
movq %rax, %rdi
callq 0x4320
movq %rax, %rdi
callq 0x4130
leaq 0x1383e(%rip), %rsi     # 0x23056
movq %rbx, %rdi
callq 0x4240
movq %rax, %rdi
movaps 0x10(%rsp), %xmm0
callq 0x4320
movq %rax, %rdi
callq 0x4130
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x137c0(%rip), %rdi     # 0x2300b
leaq 0x11e88(%rip), %rsi     # 0x216da
leaq 0x13777(%rip), %rcx     # 0x22fd0
movl $0xf6d, %edx            # imm = 0xF6D
callq 0x4180
leaq 0x127a1(%rip), %rdi     # 0x2200b
leaq 0x11e69(%rip), %rsi     # 0x216da
leaq 0x13758(%rip), %rcx     # 0x22fd0
movl $0xf6e, %edx            # imm = 0xF6E
callq 0x4180
leaq 0x13720(%rip), %rdi     # 0x22fa9
leaq 0x11e4a(%rip), %rsi     # 0x216da
leaq 0x13739(%rip), %rcx     # 0x22fd0
movl $0xf64, %edx            # imm = 0xF64
callq 0x4180
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x17a9a
movq %rbx, %rdi
callq 0x43b0