// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq 0x400(%rdi), %rcx
movq 0x140(%rdi), %rax
subq 0x138(%rdi), %rax
movq 0x28(%rcx), %r12
movq %rcx, 0x38(%rsp)
movq 0x30(%rcx), %rcx
movq %rcx, %rdx
subq %r12, %rdx
cmpq %rdx, %rax
jne 0x1136c
movq %rdi, %r14
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
cmpq %r12, %rcx
je 0x11198
xorpd %xmm1, %xmm1
xorl %r15d, %r15d
leaq 0x40(%rsp), %rbp
xorl %ebx, %ebx
movq %r14, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
movq %rbp, %rbx
movsd %xmm1, (%rsp)
addq %r15, %r12
movq 0x138(%r14), %rax
movq (%rax,%r15), %rbp
movq %rbp, %rdi
callq 0x1abb8
movq %rax, %r13
movq %rdx, %r14
movq %r12, %rdi
callq 0x1abb8
movq %rdx, %rcx
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x1a5e0
testb %al, %al
je 0x1134d
cmpq $0x1, %r14
jne 0x1132e
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movl $0x1, %r8d
movq %rbp, %rdi
xorl %esi, %esi
movq %r13, %rdx
xorl %ecx, %ecx
leaq 0x10(%rsp), %r9
callq 0x1d568
ucomisd 0x1652a(%rip), %xmm0    # 0x275f0
jbe 0x11152
movl $0x1, %edx
xorpd %xmm0, %xmm0
movq %rbx, %rbp
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1a92c
movq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x1adb2
movq 0x18(%rsp), %rcx
movq (%rax), %rax
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%rax,%rcx,8)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x1c00e
movsd (%rsp), %xmm0
movapd %xmm0, %xmm1
addsd 0x1645e(%rip), %xmm1    # 0x27580
testb %al, %al
movq 0x28(%rsp), %r14
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
movq 0x30(%rsp), %rbx
jne 0x1113e
movapd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq %rbp, %rdi
callq 0x1abf2
movsd (%rsp), %xmm1
jmp 0x1116e
movq 0x28(%rsp), %r14
movsd (%rsp), %xmm1
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
movq %rbx, %rbp
movq 0x30(%rsp), %rbx
incq %rbx
movq 0x38(%rsp), %rax
movq 0x28(%rax), %r12
movq 0x30(%rax), %rax
subq %r12, %rax
sarq $0x3, %rax
imulq %r13, %rax
addq $0x18, %r15
cmpq %rax, %rbx
jb 0x11046
jmp 0x111a4
sarq $0x3, %rax
imulq %rdx, %rax
xorpd %xmm1, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
movq 0x418(%r14), %rsi
cmpq 0x420(%r14), %rsi
je 0x111d6
movsd %xmm1, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x418(%r14)
jmp 0x111e7
leaq 0x410(%r14), %rdi
leaq 0x40(%rsp), %rdx
callq 0x17694
movq 0x1f8(%r14), %rcx
movq 0x200(%r14), %rax
xorpd %xmm2, %xmm2
cmpq %rax, %rcx
je 0x1120e
movq %rcx, %rdx
addsd (%rdx), %xmm2
addq $0x8, %rdx
cmpq %rax, %rdx
jne 0x11201
subq %rcx, %rax
sarq $0x3, %rax
movq %rax, %xmm0
punpckldq 0x15e1e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x15e26(%rip), %xmm0    # 0x27050
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorpd 0x15e52(%rip), %xmm2    # 0x27090
divsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
movq 0x430(%r14), %rsi
cmpq 0x438(%r14), %rsi
movapd %xmm2, (%rsp)
je 0x1126e
movsd %xmm2, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x430(%r14)
jmp 0x11282
addq $0x428, %r14            # imm = 0x428
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x18eb6
movq 0x25d4f(%rip), %rbx     # 0x36fd8
leaq 0x17e0b(%rip), %rsi     # 0x2909b
movl $0x2a, %edx
movq %rbx, %rdi
callq 0x4280
movsd 0x40(%rsp), %xmm0
movq %rbx, %rdi
callq 0x4340
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4250
movsbl %al, %esi
movq %r14, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4190
leaq 0x17dea(%rip), %rsi     # 0x290c6
movl $0x2a, %edx
movq %rbx, %rdi
callq 0x4280
movq %rbx, %rdi
movaps (%rsp), %xmm0
callq 0x4340
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4250
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4190
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16d46(%rip), %rdi     # 0x2807b
leaq 0x1640e(%rip), %rsi     # 0x2774a
leaq 0x17cfd(%rip), %rcx     # 0x29040
movl $0xf6e, %edx            # imm = 0xF6E
callq 0x4160
leaq 0x17d27(%rip), %rdi     # 0x2907b
leaq 0x163ef(%rip), %rsi     # 0x2774a
leaq 0x17cde(%rip), %rcx     # 0x29040
movl $0xf6d, %edx            # imm = 0xF6D
callq 0x4160
leaq 0x17ca6(%rip), %rdi     # 0x29019
leaq 0x163d0(%rip), %rsi     # 0x2774a
leaq 0x17cbf(%rip), %rcx     # 0x29040
movl $0xf64, %edx            # imm = 0xF64
callq 0x4160
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x1abf2
movq %rbx, %rdi
callq 0x43e0