// File: btbujiangjun[P]abcdl/src/fnn/FNN.cpp
// Function: abcdl::fnn::FNN::train(abcdl::algebra::Matrix<float> const&, abcdl::algebra::Matrix<float> const&)
void FNN::train(const abcdl::algebra::Mat& train_data,
                const abcdl::algebra::Mat& train_label){
    LOG(INFO) << "fnn start training...";

    size_t layer_size = _layers.size();
    size_t num_train_data = train_data.rows();

    CHECK(num_train_data == train_label.rows());
    CHECK(train_data.cols() == _layers[0]->get_input_dim());
    CHECK(train_label.cols() == _layers[_layers.size() -1]->get_output_dim());

    abcdl::algebra::Mat data;
    abcdl::algebra::Mat label;
    abcdl::utils::Shuffler shuffler(num_train_data);
    auto now = []{return std::chrono::system_clock::now();};
    auto start_time = now();
    real total_loss = 0;
    std::vector<std::pair<real, real>> auc_train_vec;

    shuffler.shuffle();
    for(size_t j = 0; j != num_train_data; j++){
        train_data.get_row(&data, shuffler.get(j));
        train_label.get_row(&label, shuffler.get(j));

        for(size_t k = 0; k != layer_size; k++){
            if(k == 0){
                ((InputLayer*)_layers[k])->set_x(data);
            }
            _layers[k]->forward(_layers[k-1]);
        }

        for(size_t k = layer_size - 1; k > 0; k--){
            if(k == layer_size - 1){
                ((OutputLayer*)_layers[k])->set_y(label);
                _layers[k]->backward(_layers[k-1], nullptr);
            }else{
                _layers[k]->backward(_layers[k-1], _layers[k+1]);
            }
               
            //mini_batch_update
            if(j % _batch_size == _batch_size - 1 || j == num_train_data - 1){
                _layers[k]->update_gradient(j % _batch_size + 1, _alpha);
            }
        }

        total_loss += _loss->loss(label, _layers[layer_size - 1]->get_activate_data());
        auc_train_vec.push_back(std::make_pair(label.argmax(), _layers[layer_size - 1]->get_activate_data().argmax()));

        if(j % 100 == 0){
            printf(" Train[%ld/%ld]\r", j, num_train_data);
        }
    }
        
    double auc_score = auc(auc_train_vec);
    double avg_loss = total_loss / num_train_data;
    long long int train_time = std::chrono::duration_cast<std::chrono::milliseconds>(now() - start_time).count();

    LOG(INFO) << "auc["<< auc_score <<"] loss["<< avg_loss <<"] training run time:["<< train_time <<"]ms";
    printf("auc[%lf] loss[%lf] training run time:[%lld]ms\n", auc_score, avg_loss, train_time);
}

// Assembly:
subq $0xa48, %rsp            # imm = 0xA48
movq %rdi, 0xa40(%rsp)
movq %rsi, 0xa38(%rsp)
movq %rdx, 0xa30(%rsp)
movq 0xa40(%rsp), %rax
movq %rax, 0x148(%rsp)
leaq 0x67fea(%rip), %rsi     # 0x8ffb0
leaq 0x8a8(%rsp), %rdi
movq %rdi, 0x150(%rsp)
movl $0x14, %edx
xorl %ecx, %ecx
callq 0x89860
movq 0x150(%rsp), %rdi
leaq 0x67fcf(%rip), %rsi     # 0x8ffc0
callq 0x2ba30
jmp 0x27ff8
leaq 0x8a8(%rsp), %rdi
callq 0x89c20
movq 0x148(%rsp), %rdi
addq $0x30, %rdi
callq 0x17250
movq %rax, 0x890(%rsp)
movq 0xa38(%rsp), %rdi
callq 0x1a390
movq %rax, 0x888(%rsp)
movq 0x888(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0xa30(%rsp), %rdi
callq 0x1a390
movq %rax, %rcx
movq 0x140(%rsp), %rax
cmpq %rcx, %rax
sete %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2806c
jmp 0x28103
leaq 0x67f3d(%rip), %rsi     # 0x8ffb0
leaq 0x700(%rsp), %rdi
movq %rdi, 0x138(%rsp)
movl $0x19, %edx
movl $0x3, %ecx
callq 0x89860
movq 0x138(%rsp), %rdi
leaq 0x67f35(%rip), %rsi     # 0x8ffd6
callq 0x27c60
jmp 0x280a8
leaq 0x700(%rsp), %rdi
callq 0x89c20
jmp 0x28103
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x8a0(%rsp)
movl %eax, 0x89c(%rsp)
leaq 0x8a8(%rsp), %rdi
callq 0x89c20
jmp 0x28b90
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x8a0(%rsp)
movl %eax, 0x89c(%rsp)
leaq 0x700(%rsp), %rdi
callq 0x89c20
jmp 0x28b90
movq 0xa38(%rsp), %rdi
callq 0x1a3a0
movq 0x148(%rsp), %rdi
movq %rax, 0x130(%rsp)
addq $0x30, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x17270
movq (%rax), %rdi
callq 0x17310
movq %rax, %rcx
movq 0x130(%rsp), %rax
cmpq %rcx, %rax
sete %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2814e
jmp 0x281bf
leaq 0x67e5b(%rip), %rsi     # 0x8ffb0
leaq 0x578(%rsp), %rdi
movq %rdi, 0x128(%rsp)
movl $0x1a, %edx
movl $0x3, %ecx
callq 0x89860
movq 0x128(%rsp), %rdi
leaq 0x67e86(%rip), %rsi     # 0x90009
callq 0x2ba60
jmp 0x2818a
leaq 0x578(%rsp), %rdi
callq 0x89c20
jmp 0x281bf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x8a0(%rsp)
movl %eax, 0x89c(%rsp)
leaq 0x578(%rsp), %rdi
callq 0x89c20
jmp 0x28b90
movq 0xa30(%rsp), %rdi
callq 0x1a3a0
movq 0x148(%rsp), %rdi
movq %rax, 0x120(%rsp)
movq %rdi, %rax
addq $0x30, %rax
movq %rax, 0x118(%rsp)
addq $0x30, %rdi
callq 0x17250
movq 0x118(%rsp), %rdi
movq %rax, %rsi
subq $0x1, %rsi
callq 0x17270
movq (%rax), %rdi
callq 0x172d0
movq %rax, %rcx
movq 0x120(%rsp), %rax
cmpq %rcx, %rax
sete %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x28229
jmp 0x2829a
leaq 0x67d80(%rip), %rsi     # 0x8ffb0
leaq 0x3f0(%rsp), %rdi
movq %rdi, 0x110(%rsp)
movl $0x1b, %edx
movl $0x3, %ecx
callq 0x89860
movq 0x110(%rsp), %rdi
leaq 0x67dea(%rip), %rsi     # 0x90048
callq 0x2ba90
jmp 0x28265
leaq 0x3f0(%rsp), %rdi
callq 0x89c20
jmp 0x2829a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x8a0(%rsp)
movl %eax, 0x89c(%rsp)
leaq 0x3f0(%rsp), %rdi
callq 0x89c20
jmp 0x28b90
leaq 0x3c8(%rsp), %rdi
callq 0x33a40
leaq 0x3a0(%rsp), %rdi
callq 0x33a40
jmp 0x282b6
movq 0x888(%rsp), %rsi
leaq 0x380(%rsp), %rdi
callq 0x27dc0
jmp 0x282cd
leaq 0x37f(%rsp), %rdi
callq 0x28ba0
movq %rax, 0x108(%rsp)
jmp 0x282e4
movq 0x108(%rsp), %rax
movq %rax, 0x370(%rsp)
movl $0x0, 0x36c(%rsp)
leaq 0x350(%rsp), %rdi
callq 0x2bac0
leaq 0x380(%rsp), %rdi
callq 0x27e60
jmp 0x2831b
movq $0x0, 0x348(%rsp)
movq 0x348(%rsp), %rax
cmpq 0x888(%rsp), %rax
je 0x288dd
movq 0xa38(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq 0x348(%rsp), %rsi
leaq 0x380(%rsp), %rdi
callq 0x2bad0
movq %rax, 0x100(%rsp)
jmp 0x2836c
movq 0x100(%rsp), %rdx
movq 0xf8(%rsp), %rdi
leaq 0x3c8(%rsp), %rsi
movl $0x1, %ecx
callq 0x34470
jmp 0x28390
movq 0xa30(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0x348(%rsp), %rsi
leaq 0x380(%rsp), %rdi
callq 0x2bad0
movq %rax, 0xf0(%rsp)
jmp 0x283bf
movq 0xf0(%rsp), %rdx
movq 0xe8(%rsp), %rdi
leaq 0x3a0(%rsp), %rsi
movl $0x1, %ecx
callq 0x34470
jmp 0x283e3
movq $0x0, 0x340(%rsp)
movq 0x340(%rsp), %rax
cmpq 0x890(%rsp), %rax
je 0x2851a
cmpq $0x0, 0x340(%rsp)
jne 0x284a5
movq 0x148(%rsp), %rdi
addq $0x30, %rdi
movq 0x340(%rsp), %rsi
callq 0x17270
movq (%rax), %rdi
leaq 0x3c8(%rsp), %rsi
callq 0x26f70
jmp 0x2843f
jmp 0x284a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x8a0(%rsp)
movl %eax, 0x89c(%rsp)
jmp 0x28b83
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x8a0(%rsp)
movl %eax, 0x89c(%rsp)
jmp 0x28b76
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x8a0(%rsp)
movl %eax, 0x89c(%rsp)
jmp 0x28b69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x8a0(%rsp)
movl %eax, 0x89c(%rsp)
jmp 0x28b5c
movq 0x148(%rsp), %rdi
addq $0x30, %rdi
movq %rdi, 0xd8(%rsp)
movq 0x340(%rsp), %rsi
callq 0x17270
movq 0xd8(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0xe0(%rsp)
movq 0x340(%rsp), %rsi
decq %rsi
callq 0x17270
movq 0xe0(%rsp), %rdi
movq (%rax), %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
jmp 0x284ff
jmp 0x28501
movq 0x340(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x340(%rsp)
jmp 0x283ef
movq 0x890(%rsp), %rax
subq $0x1, %rax
movq %rax, 0x338(%rsp)
cmpq $0x0, 0x338(%rsp)
jbe 0x2871e
movq 0x338(%rsp), %rax
movq 0x890(%rsp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x285e8
movq 0x148(%rsp), %rdi
addq $0x30, %rdi
movq 0x338(%rsp), %rsi
callq 0x17270
movq (%rax), %rdi
leaq 0x3a0(%rsp), %rsi
callq 0x2bb00
jmp 0x28585
movq 0x148(%rsp), %rdi
addq $0x30, %rdi
movq %rdi, 0xc8(%rsp)
movq 0x338(%rsp), %rsi
callq 0x17270
movq 0xc8(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0xd0(%rsp)
movq 0x338(%rsp), %rsi
decq %rsi
callq 0x17270
movq 0xd0(%rsp), %rdi
movq (%rax), %rsi
movq (%rdi), %rax
movq 0x18(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
jmp 0x285e3
jmp 0x2866f
movq 0x148(%rsp), %rdi
addq $0x30, %rdi
movq %rdi, 0xb0(%rsp)
movq 0x338(%rsp), %rsi
callq 0x17270
movq 0xb0(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0x338(%rsp), %rsi
decq %rsi
callq 0x17270
movq 0xb0(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0xc0(%rsp)
movq 0x338(%rsp), %rsi
incq %rsi
callq 0x17270
movq 0xb8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
movq (%rax), %rdx
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0x2866d
jmp 0x2866f
movq 0x148(%rsp), %rcx
movq 0x348(%rsp), %rax
xorl %edx, %edx
divq 0x8(%rcx)
movq 0x148(%rsp), %rax
movq 0x8(%rax), %rax
subq $0x1, %rax
cmpq %rax, %rdx
je 0x286b3
movq 0x348(%rsp), %rax
movq 0x888(%rsp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x28703
movq 0x148(%rsp), %rdi
addq $0x30, %rdi
movq 0x338(%rsp), %rsi
callq 0x17270
movq 0x148(%rsp), %rcx
movq (%rax), %rdi
movq 0x348(%rsp), %rax
movq 0x8(%rcx), %rcx
xorl %edx, %edx
divq %rcx
movq 0x148(%rsp), %rax
movq %rdx, %rsi
incq %rsi
movss (%rax), %xmm0
callq 0x2bb30
jmp 0x28701
jmp 0x28703
jmp 0x28705
movq 0x338(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x338(%rsp)
jmp 0x2852e
movq 0x148(%rsp), %rdi
movq 0x48(%rdi), %rax
movq %rax, 0xa0(%rsp)
addq $0x30, %rdi
movq 0x890(%rsp), %rsi
decq %rsi
callq 0x17270
movq (%rax), %rdi
callq 0x27c10
movq %rax, 0xa8(%rsp)
jmp 0x28758
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
leaq 0x3a0(%rsp), %rsi
callq *%rax
movss %xmm0, 0x9c(%rsp)
jmp 0x28784
movss 0x9c(%rsp), %xmm1
movss 0x36c(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x36c(%rsp)
leaq 0x3a0(%rsp), %rdi
callq 0x5d440
movq %rax, 0x90(%rsp)
jmp 0x287ba
movq 0x148(%rsp), %rdi
movq 0x90(%rsp), %rax
movq %rax, 0x318(%rsp)
addq $0x30, %rdi
movq 0x890(%rsp), %rsi
decq %rsi
callq 0x17270
movq (%rax), %rdi
callq 0x27c10
movq %rax, 0x88(%rsp)
jmp 0x287f8
movq 0x88(%rsp), %rdi
callq 0x5d440
movq %rax, 0x80(%rsp)
jmp 0x2880f
movq 0x80(%rsp), %rax
movq %rax, 0x310(%rsp)
leaq 0x318(%rsp), %rdi
leaq 0x310(%rsp), %rsi
callq 0x2bd00
movq %rdx, 0x70(%rsp)
movq %rax, 0x78(%rsp)
jmp 0x28840
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %rcx
movq %rcx, 0x320(%rsp)
movq %rax, 0x328(%rsp)
leaq 0x330(%rsp), %rdi
leaq 0x320(%rsp), %rsi
callq 0x2bd40
jmp 0x28871
leaq 0x350(%rsp), %rdi
leaq 0x330(%rsp), %rsi
callq 0x2bcd0
jmp 0x28888
movq 0x348(%rsp), %rax
movl $0x64, %ecx
xorl %edx, %edx
divq %rcx
cmpq $0x0, %rdx
jne 0x288c2
movq 0x348(%rsp), %rsi
movq 0x888(%rsp), %rdx
leaq 0x677e2(%rip), %rdi     # 0x90099
xorl %eax, %eax
callq 0x9060
jmp 0x288c0
jmp 0x288c2
jmp 0x288c4
movq 0x348(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x348(%rsp)
jmp 0x28327
movq 0x148(%rsp), %rdi
leaq 0x350(%rsp), %rsi
callq 0x28bc0
movsd %xmm0, 0x68(%rsp)
jmp 0x288fa
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x308(%rsp)
movss 0x36c(%rsp), %xmm0
movss %xmm0, 0x5c(%rsp)
movq 0x888(%rsp), %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, 0x60(%rsp)
testq %rax, %rax
movss %xmm0, 0x64(%rsp)
js 0x28959
movss 0x60(%rsp), %xmm0
movss %xmm0, 0x64(%rsp)
movss 0x5c(%rsp), %xmm0
movss 0x64(%rsp), %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x300(%rsp)
leaq 0x37f(%rsp), %rdi
callq 0x28ba0
movq %rax, 0x50(%rsp)
jmp 0x2898a
movq 0x50(%rsp), %rax
movq %rax, 0x2e0(%rsp)
leaq 0x2e0(%rsp), %rdi
leaq 0x370(%rsp), %rsi
callq 0x2be30
movq %rax, 0x48(%rsp)
jmp 0x289b3
movq 0x48(%rsp), %rax
movq %rax, 0x2e8(%rsp)
leaq 0x2e8(%rsp), %rdi
callq 0x2be00
movq %rax, 0x40(%rsp)
jmp 0x289d4
movq 0x40(%rsp), %rax
movq %rax, 0x2f0(%rsp)
leaq 0x2f0(%rsp), %rdi
callq 0x2be80
movq %rax, 0x38(%rsp)
jmp 0x289f5
movq 0x38(%rsp), %rax
movq %rax, 0x2f8(%rsp)
leaq 0x675a7(%rip), %rsi     # 0x8ffb0
leaq 0x158(%rsp), %rdi
movl $0x4b, %edx
xorl %ecx, %ecx
callq 0x89860
jmp 0x28a1f
leaq 0x677c4(%rip), %rsi     # 0x901ea
leaq 0x158(%rsp), %rdi
callq 0x2bf50
movq %rax, 0x30(%rsp)
jmp 0x28a3a
movq 0x30(%rsp), %rdi
leaq 0x308(%rsp), %rsi
callq 0x2bef0
movq %rax, 0x28(%rsp)
jmp 0x28a53
movq 0x28(%rsp), %rdi
leaq 0x6764b(%rip), %rsi     # 0x900aa
callq 0x2bf20
movq %rax, 0x20(%rsp)
jmp 0x28a6b
movq 0x20(%rsp), %rdi
leaq 0x300(%rsp), %rsi
callq 0x2bef0
movq %rax, 0x18(%rsp)
jmp 0x28a84
movq 0x18(%rsp), %rdi
leaq 0x67622(%rip), %rsi     # 0x900b2
callq 0x2ba30
movq %rax, 0x10(%rsp)
jmp 0x28a9c
movq 0x10(%rsp), %rdi
leaq 0x2f8(%rsp), %rsi
callq 0x2bec0
movq %rax, 0x8(%rsp)
jmp 0x28ab5
movq 0x8(%rsp), %rdi
leaq 0x67607(%rip), %rsi     # 0x900c8
callq 0x2be90
jmp 0x28ac8
leaq 0x158(%rsp), %rdi
callq 0x89c20
movsd 0x308(%rsp), %xmm0
movsd 0x300(%rsp), %xmm1
movq 0x2f8(%rsp), %rsi
leaq 0x675d6(%rip), %rdi     # 0x900cc
movb $0x2, %al
callq 0x9060
jmp 0x28aff
leaq 0x350(%rsp), %rdi
callq 0x2bf80
leaq 0x380(%rsp), %rdi
callq 0x2bfe0
leaq 0x3a0(%rsp), %rdi
callq 0x33e80
leaq 0x3c8(%rsp), %rdi
callq 0x33e80
addq $0xa48, %rsp            # imm = 0xA48
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x8a0(%rsp)
movl %eax, 0x89c(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x89c20
leaq 0x350(%rsp), %rdi
callq 0x2bf80
leaq 0x380(%rsp), %rdi
callq 0x2bfe0
leaq 0x3a0(%rsp), %rdi
callq 0x33e80
leaq 0x3c8(%rsp), %rdi
callq 0x33e80
movq 0x8a0(%rsp), %rdi
callq 0x9700
nopl (%rax)