// File: 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:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x120(%rsp)
movq 0x120(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x400(%rax), %rdi
callq 0x1fd90
movq 0x28(%rsp), %rdi
movq %rax, 0x118(%rsp)
addq $0x138, %rdi            # imm = 0x138
callq 0x1ff40
movq %rax, 0x30(%rsp)
movq 0x118(%rsp), %rdi
callq 0x1f9c0
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
jne 0x1a582
jmp 0x1a5a1
leaq 0x39557(%rip), %rdi     # 0x53ae0
leaq 0x37c42(%rip), %rsi     # 0x521d2
movl $0xf64, %edx            # imm = 0xF64
leaq 0x3956b(%rip), %rcx     # 0x53b07
callq 0x4170
xorps %xmm0, %xmm0
movsd %xmm0, 0x110(%rsp)
movq $0x0, 0x108(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x118(%rsp), %rdi
callq 0x1f9c0
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0x1a861
movq 0x118(%rsp), %rdi
movq 0x108(%rsp), %rsi
callq 0x1f9e0
movq 0x28(%rsp), %rdi
movq %rax, 0x100(%rsp)
addq $0x138, %rdi            # imm = 0x138
movq 0x108(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f9e0
movq %rax, 0xf8(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x3bae0
movq %rax, 0xe8(%rsp)
movq %rdx, 0xf0(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0x100(%rsp), %rdi
callq 0x3bae0
movq %rax, 0xc8(%rsp)
movq %rdx, 0xd0(%rsp)
movq 0xd8(%rsp), %rdi
movq 0xe0(%rsp), %rsi
movq 0xc8(%rsp), %rdx
movq 0xd0(%rsp), %rcx
callq 0x3b180
testb $0x1, %al
jne 0x1a6b3
jmp 0x1a6b5
jmp 0x1a6d4
leaq 0x39486(%rip), %rdi     # 0x53b42
leaq 0x37b0f(%rip), %rsi     # 0x521d2
movl $0xf6d, %edx            # imm = 0xF6D
leaq 0x39438(%rip), %rcx     # 0x53b07
callq 0x4170
cmpq $0x1, 0xf0(%rsp)
jne 0x1a6e1
jmp 0x1a700
leaq 0x3845a(%rip), %rdi     # 0x52b42
leaq 0x37ae3(%rip), %rsi     # 0x521d2
movl $0xf6e, %edx            # imm = 0xF6E
leaq 0x3940c(%rip), %rcx     # 0x53b07
callq 0x4170
leaq 0xb8(%rsp), %rdi
callq 0x1f020
movq 0xf8(%rsp), %rdi
movq 0xe8(%rsp), %rdx
movq 0xf0(%rsp), %r8
xorl %eax, %eax
movl %eax, %ecx
leaq 0xb8(%rsp), %r9
movq %rcx, %rsi
callq 0x40310
movsd %xmm0, 0xb0(%rsp)
movsd 0x3790e(%rip), %xmm0    # 0x52058
movsd %xmm0, 0xa8(%rsp)
movsd 0xb0(%rsp), %xmm0
ucomisd 0xa8(%rsp), %xmm0
jbe 0x1a846
movups 0xe8(%rsp), %xmm0
movaps %xmm0, 0x80(%rsp)
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
leaq 0x90(%rsp), %rdi
movq %rdi, 0x10(%rsp)
xorps %xmm0, %xmm0
callq 0x3b680
movq 0x10(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x3be50
movq %rax, 0x18(%rsp)
jmp 0x1a7b9
movq 0x18(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x1fd60
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
movq 0x100(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x3de00
movb %al, 0xf(%rsp)
jmp 0x1a7f3
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x1a7fd
jmp 0x1a839
movsd 0x37803(%rip), %xmm0    # 0x52008
addsd 0x110(%rsp), %xmm0
movsd %xmm0, 0x110(%rsp)
jmp 0x1a839
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x3bb80
jmp 0x1aa21
leaq 0x90(%rsp), %rdi
callq 0x3bb80
jmp 0x1a848
movq 0x108(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x108(%rsp)
jmp 0x1a5b9
movsd 0x110(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x118(%rsp), %rdi
callq 0x1f9c0
movsd (%rsp), %xmm0
movq 0x28(%rsp), %rdi
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
addq $0x410, %rdi            # imm = 0x410
leaq 0x68(%rsp), %rsi
callq 0x21ce0
movq 0x28(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x60(%rsp)
addq $0x1f8, %rax            # imm = 0x1F8
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rdi
callq 0x21d50
movq %rax, 0x50(%rsp)
movq 0x58(%rsp), %rdi
callq 0x21d80
movq %rax, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x21db0
testb $0x1, %al
jne 0x1a8f1
jmp 0x1a921
leaq 0x50(%rsp), %rdi
callq 0x21df0
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
movsd (%rax), %xmm0
addsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x21e00
jmp 0x1a8dc
movq 0x28(%rsp), %rdi
addq $0x1f8, %rdi            # imm = 0x1F8
callq 0x21e20
movq 0x28(%rsp), %rdi
movq %rax, %xmm1
movaps 0x3771d(%rip), %xmm0    # 0x52060
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x37721(%rip), %xmm0    # 0x52070
subpd %xmm0, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movsd 0x60(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
addq $0x428, %rdi            # imm = 0x428
movsd 0x60(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x38(%rsp)
leaq 0x38(%rsp), %rsi
callq 0x21e40
movq 0x5062f(%rip), %rdi     # 0x6afd8
leaq 0x391b2(%rip), %rsi     # 0x53b62
callq 0x4230
movq %rax, %rdi
movsd 0x68(%rsp), %xmm0
callq 0x4440
movq %rax, %rdi
movq 0x505e3(%rip), %rsi     # 0x6afb0
callq 0x4260
movq 0x505ff(%rip), %rdi     # 0x6afd8
leaq 0x391ad(%rip), %rsi     # 0x53b8d
callq 0x4230
movq %rax, %rdi
movsd 0x60(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
callq 0x4440
movq %rax, %rdi
movq 0x5059c(%rip), %rsi     # 0x6afb0
callq 0x4260
addq $0x128, %rsp            # imm = 0x128
retq
movq 0x78(%rsp), %rdi
callq 0x43f0
nopl (%rax,%rax)