// File: /workspace/github_cpp_makefile_star2_O1/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), %r13
movq 0x140(%rdi), %rax
subq 0x138(%rdi), %rax
movq 0x30(%r13), %rcx
subq 0x28(%r13), %rcx
cmpq %rcx, %rax
jne 0x114b3
movq %rdi, %r14
movq 0x28(%r13), %r12
movq 0x30(%r13), %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
subq %r12, %rax
je 0x112df
xorpd %xmm1, %xmm1
xorl %r15d, %r15d
leaq 0x40(%rsp), %rbp
xorl %ebx, %ebx
movq %r14, 0x30(%rsp)
movq %r13, 0x28(%rsp)
movq %rbx, 0x38(%rsp)
movq %rbp, %rbx
movsd %xmm1, (%rsp)
addq %r15, %r12
movq 0x138(%r14), %rax
movq (%rax,%r15), %rbp
movq %rbp, %rdi
callq 0x1ac7c
movq %rax, %r13
movq %rdx, %r14
movq %r12, %rdi
callq 0x1ac7c
movq %rdx, %rcx
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x1a680
testb %al, %al
je 0x11494
cmpq $0x1, %r14
jne 0x11475
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 0x1d660
ucomisd 0x163de(%rip), %xmm0    # 0x275f0
jbe 0x11299
movl $0x1, %edx
xorpd %xmm0, %xmm0
movq %rbx, %rbp
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1a9ec
movq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x1ae76
movq 0x18(%rsp), %rcx
movq (%rax), %rax
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%rax,%rcx,8)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x1c100
movsd (%rsp), %xmm0
movapd %xmm0, %xmm1
addsd 0x16312(%rip), %xmm1    # 0x27580
testb %al, %al
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %r13
movq 0x38(%rsp), %rbx
jne 0x11285
movapd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq %rbp, %rdi
callq 0x1acb6
movsd (%rsp), %xmm1
jmp 0x112b0
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %r13
movsd (%rsp), %xmm1
movq %rbx, %rbp
movq 0x38(%rsp), %rbx
incq %rbx
movq 0x28(%r13), %r12
movq 0x30(%r13), %rax
subq %r12, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
addq $0x18, %r15
cmpq %rax, %rbx
jb 0x11192
jmp 0x112eb
sarq $0x3, %rax
imulq %rcx, %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 0x1131d
movsd %xmm1, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x418(%r14)
jmp 0x1132e
leaq 0x410(%r14), %rdi
leaq 0x40(%rsp), %rdx
callq 0x17770
movq 0x1f8(%r14), %rcx
movq 0x200(%r14), %rax
xorpd %xmm2, %xmm2
cmpq %rax, %rcx
je 0x11355
movq %rcx, %rdx
addsd (%rdx), %xmm2
addq $0x8, %rdx
cmpq %rax, %rdx
jne 0x11348
subq %rcx, %rax
sarq $0x3, %rax
movq %rax, %xmm0
punpckldq 0x15cd7(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x15cdf(%rip), %xmm0    # 0x27050
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorpd 0x15d0b(%rip), %xmm2    # 0x27090
divsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
movq 0x430(%r14), %rsi
cmpq 0x438(%r14), %rsi
movapd %xmm2, (%rsp)
je 0x113b5
movsd %xmm2, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x430(%r14)
jmp 0x113c9
addq $0x428, %r14            # imm = 0x428
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x18f58
movq 0x25c08(%rip), %rbx     # 0x36fd8
leaq 0x17d03(%rip), %rsi     # 0x290da
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 0x17ce2(%rip), %rsi     # 0x29105
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 0x16c3e(%rip), %rdi     # 0x280ba
leaq 0x162c7(%rip), %rsi     # 0x2774a
leaq 0x17bf5(%rip), %rcx     # 0x2907f
movl $0xf6e, %edx            # imm = 0xF6E
callq 0x4160
leaq 0x17c1f(%rip), %rdi     # 0x290ba
leaq 0x162a8(%rip), %rsi     # 0x2774a
leaq 0x17bd6(%rip), %rcx     # 0x2907f
movl $0xf6d, %edx            # imm = 0xF6D
callq 0x4160
leaq 0x17b9e(%rip), %rdi     # 0x29058
leaq 0x16289(%rip), %rsi     # 0x2774a
leaq 0x17bb7(%rip), %rcx     # 0x2907f
movl $0xf64, %edx            # imm = 0xF64
callq 0x4160
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x1acb6
movq %rbx, %rdi
callq 0x43e0
nop