// File: xbai0624[P]dnn/src/UnitTest.cpp
// Function: UnitTest::TestCNNWeightAndBiasEvolution()
void UnitTest::TestCNNWeightAndBiasEvolution()
{
    cout<<"ooooooooo000000000oooooooooo000000000ooooooooooo00000000oooooooooo0000000000000ooooooooooooo"<<endl;
    DataInterface *data_interface = new DataInterface("simulation_data/data_signal_train.dat", "simulation_data/data_cosmic_train.dat", LayerDimension::_2D, std::pair<int, int>(10, 10), 200);

    TrainingType resume_or_new_training = TrainingType::NewTraining;
    double learning_rate = 0.06;
    double regularization_factor = 0.01;

    // 3) input layer   ID=0
    LayerParameterList p_list0(LayerType::input, LayerDimension::_2D, data_interface, 0, 0, 
            std::pair<size_t, size_t>(0, 0), learning_rate, false, 0, 0, Regularization::Undefined, 0, ActuationFuncType::Undefined, resume_or_new_training, false);
    Layer* layer_input = new ConstructLayer(p_list0);
    layer_input->Init();

    // 4) middle layer 3 : cnn layer ID=1
    LayerParameterList p_list1(LayerType::cnn, LayerDimension::_2D, data_interface, 0, 1, 
            std::pair<size_t, size_t>(4, 4), learning_rate, false, 0, 0.5, Regularization::L2, regularization_factor, ActuationFuncType::Relu, resume_or_new_training, false);
    Layer *l1 = new ConstructLayer(p_list1);
    l1->SetPrevLayer(layer_input);
    l1->Init();

    // 4) middle layer 3 : cnn layer ID=3
    LayerParameterList p_list3(LayerType::cnn, LayerDimension::_2D, data_interface, 0, 1, 
            std::pair<size_t, size_t>(4, 4), learning_rate, false, 0, 0.5, Regularization::L2, regularization_factor, ActuationFuncType::Relu, resume_or_new_training, false);
    Layer *l3 = new ConstructLayer(p_list3);
    l3->SetPrevLayer(l1);
    l3->Init();

    // 5) output layer ID = 6
    LayerParameterList p_list6(LayerType::output, LayerDimension::_1D, data_interface, 2, 0, 
            std::pair<size_t, size_t>(0, 0), learning_rate, false, 0, 0., Regularization::L2, regularization_factor, ActuationFuncType::SoftMax, resume_or_new_training, false);
    Layer* layer_output = new ConstructLayer(p_list6);
    layer_output -> SetPrevLayer(l3);
    layer_output -> Init();

    l1->SetNextLayer(l3);
    l3->SetNextLayer(layer_output); // This line is ugly, to be improved

    cout<<"ooooooooo000000000oooooooooo000000000ooooooooooo00000000oooooooooo0000000000000ooooooooooooo"<<endl;
    int numEpoch = 50;
    for(int e=0;e<numEpoch;e++)
    {
        cout<<"...............Epoch: "<<e<<endl;
        data_interface -> Reset();
        data_interface -> Shuffle();

        int nBatch = 1;
        for(int i = 0; i< nBatch;i++)
        {
            l1 -> BatchInit();
            l3 -> BatchInit();
            layer_output -> BatchInit();

            data_interface -> GetNewBatchData();
            data_interface -> GetNewBatchLabel();

            layer_input -> FillBatchDataToInputLayerA();

            int nSample = data_interface -> GetBatchSize();

            // forward propagation
            for(int sample_index = 0; sample_index < nSample;sample_index++)
            {
                l1 -> ForwardPropagate_Z_ForSample(sample_index);
                l1 -> ForwardPropagate_SigmaPrimeAndA_ForSample(sample_index);

                l3 -> ForwardPropagate_Z_ForSample(sample_index);
                l3 -> ForwardPropagate_SigmaPrimeAndA_ForSample(sample_index);

                layer_output -> ForwardPropagate_Z_ForSample(sample_index);
                layer_output -> ForwardPropagate_SigmaPrimeAndA_ForSample(sample_index);
            }

            // backward propagation
            for(int sample_index = 0; sample_index < nSample;sample_index++)
            {
                layer_output -> BackwardPropagateForSample(sample_index);
                l3 -> BackwardPropagateForSample(sample_index);
                l1 -> BackwardPropagateForSample(sample_index);
            }

            // update weights and bias
            l1->UpdateWeightsAndBias();
            l3->UpdateWeightsAndBias();
            layer_output->UpdateWeightsAndBias();
        }
    }
}

// Assembly:
subq $0x4d8, %rsp            # imm = 0x4D8
movq %rdi, 0x4d0(%rsp)
movq 0x1a702(%rip), %rdi     # 0x6afd8
leaq 0x5d08(%rip), %rsi      # 0x565e5
callq 0x4230
movq %rax, %rdi
movq 0x1a6c4(%rip), %rsi     # 0x6afb0
callq 0x4260
movl $0xc8, %edi
callq 0x4240
movq %rax, 0xa8(%rsp)
movl $0xa, 0x4bc(%rsp)
movl $0xa, 0x4b8(%rsp)
leaq 0x4c0(%rsp), %rdi
leaq 0x4bc(%rsp), %rsi
leaq 0x4b8(%rsp), %rdx
callq 0x27b90
jmp 0x50938
movq 0xa8(%rsp), %rdi
movq 0x4c0(%rsp), %r8
leaq 0x4f49(%rip), %rsi      # 0x55898
leaq 0x4f68(%rip), %rdx      # 0x558be
movl $0x1, %ecx
movl $0xc8, %r9d
callq 0x39860
jmp 0x50968
movq 0xa8(%rsp), %rax
movq %rax, 0x4c8(%rsp)
movl $0x0, 0x4a8(%rsp)
movabsq $0x3faeb851eb851eb8, %rax # imm = 0x3FAEB851EB851EB8
movq %rax, 0x4a0(%rsp)
movabsq $0x3f847ae147ae147b, %rax # imm = 0x3F847AE147AE147B
movq %rax, 0x498(%rsp)
movq 0x4c8(%rsp), %rax
movq %rax, 0x98(%rsp)
movl $0x0, 0x41c(%rsp)
movl $0x0, 0x418(%rsp)
leaq 0x420(%rsp), %rdi
leaq 0x41c(%rsp), %rsi
leaq 0x418(%rsp), %rdx
callq 0x279d0
movq 0x98(%rsp), %rcx
movsd 0x4a0(%rsp), %xmm0
movl 0x4a8(%rsp), %edx
movups 0x420(%rsp), %xmm1
movq %rsp, %rax
movups %xmm1, (%rax)
movl %edx, 0x30(%rax)
movl $0x0, 0x38(%rax)
movl $0x4, 0x28(%rax)
movl $0x2, 0x20(%rax)
movl $0x0, 0x18(%rax)
movl $0x0, 0x10(%rax)
xorl %eax, %eax
movl %eax, %r9d
leaq 0x430(%rsp), %rdi
movl $0x3, %esi
movl $0x1, %edx
xorps %xmm2, %xmm2
movq %r9, %r8
movaps %xmm2, %xmm1
callq 0x46830
movl $0x490, %edi            # imm = 0x490
callq 0x4240
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0xa0(%rsp)
movq 0x490(%rsp), %rax
movq %rax, 0x400(%rsp)
movups 0x480(%rsp), %xmm0
movaps %xmm0, 0x3f0(%rsp)
movups 0x470(%rsp), %xmm0
movaps %xmm0, 0x3e0(%rsp)
movups 0x430(%rsp), %xmm0
movups 0x440(%rsp), %xmm1
movups 0x450(%rsp), %xmm2
movups 0x460(%rsp), %xmm3
movaps %xmm3, 0x3d0(%rsp)
movaps %xmm2, 0x3c0(%rsp)
movaps %xmm1, 0x3b0(%rsp)
movaps %xmm0, 0x3a0(%rsp)
movq 0x400(%rsp), %rcx
movq %rsp, %rax
movq %rcx, 0x60(%rax)
movaps 0x3f0(%rsp), %xmm0
movups %xmm0, 0x50(%rax)
movaps 0x3e0(%rsp), %xmm0
movups %xmm0, 0x40(%rax)
movaps 0x3a0(%rsp), %xmm0
movaps 0x3b0(%rsp), %xmm1
movaps 0x3c0(%rsp), %xmm2
movaps 0x3d0(%rsp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x4ea0
jmp 0x50b40
movq 0xa0(%rsp), %rax
movq %rax, 0x410(%rsp)
movq 0x410(%rsp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq 0x4c8(%rsp), %rax
movq %rax, 0x88(%rsp)
movl $0x4, 0x324(%rsp)
movl $0x4, 0x320(%rsp)
leaq 0x328(%rsp), %rdi
leaq 0x324(%rsp), %rsi
leaq 0x320(%rsp), %rdx
callq 0x279d0
movq 0x88(%rsp), %rcx
movsd 0x4a0(%rsp), %xmm0
movsd 0x498(%rsp), %xmm2
movl 0x4a8(%rsp), %edx
movups 0x328(%rsp), %xmm1
movq %rsp, %rax
movups %xmm1, (%rax)
movl %edx, 0x30(%rax)
movl $0x0, 0x38(%rax)
movl $0x3, 0x28(%rax)
movl $0x0, 0x20(%rax)
movl $0x0, 0x18(%rax)
movl $0x0, 0x10(%rax)
xorl %eax, %eax
movl %eax, %r8d
leaq 0x338(%rsp), %rdi
movl $0x1, %edx
movl $0x1, %r9d
movsd 0x13f7(%rip), %xmm1     # 0x52010
movl %edx, %esi
callq 0x46830
movl $0x490, %edi            # imm = 0x490
callq 0x4240
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x90(%rsp)
movq 0x398(%rsp), %rax
movq %rax, 0x310(%rsp)
movups 0x388(%rsp), %xmm0
movaps %xmm0, 0x300(%rsp)
movups 0x378(%rsp), %xmm0
movaps %xmm0, 0x2f0(%rsp)
movups 0x338(%rsp), %xmm0
movups 0x348(%rsp), %xmm1
movups 0x358(%rsp), %xmm2
movups 0x368(%rsp), %xmm3
movaps %xmm3, 0x2e0(%rsp)
movaps %xmm2, 0x2d0(%rsp)
movaps %xmm1, 0x2c0(%rsp)
movaps %xmm0, 0x2b0(%rsp)
movq 0x310(%rsp), %rcx
movq %rsp, %rax
movq %rcx, 0x60(%rax)
movaps 0x300(%rsp), %xmm0
movups %xmm0, 0x50(%rax)
movaps 0x2f0(%rsp), %xmm0
movups %xmm0, 0x40(%rax)
movaps 0x2b0(%rsp), %xmm0
movaps 0x2c0(%rsp), %xmm1
movaps 0x2d0(%rsp), %xmm2
movaps 0x2e0(%rsp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x4ea0
jmp 0x50d05
movq 0x90(%rsp), %rax
movq %rax, 0x318(%rsp)
movq 0x318(%rsp), %rdi
movq 0x410(%rsp), %rsi
movq (%rdi), %rax
movq 0x1e8(%rax), %rax
callq *%rax
movq 0x318(%rsp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq 0x4c8(%rsp), %rax
movq %rax, 0x78(%rsp)
movl $0x4, 0x234(%rsp)
movl $0x4, 0x230(%rsp)
leaq 0x238(%rsp), %rdi
leaq 0x234(%rsp), %rsi
leaq 0x230(%rsp), %rdx
callq 0x279d0
movq 0x78(%rsp), %rcx
movsd 0x4a0(%rsp), %xmm0
movsd 0x498(%rsp), %xmm2
movl 0x4a8(%rsp), %edx
movups 0x238(%rsp), %xmm1
movq %rsp, %rax
movups %xmm1, (%rax)
movl %edx, 0x30(%rax)
movl $0x0, 0x38(%rax)
movl $0x3, 0x28(%rax)
movl $0x0, 0x20(%rax)
movl $0x0, 0x18(%rax)
movl $0x0, 0x10(%rax)
xorl %eax, %eax
movl %eax, %r8d
leaq 0x248(%rsp), %rdi
movl $0x1, %edx
movl $0x1, %r9d
movsd 0x121c(%rip), %xmm1     # 0x52010
movl %edx, %esi
callq 0x46830
movl $0x490, %edi            # imm = 0x490
callq 0x4240
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x80(%rsp)
movq 0x2a8(%rsp), %rax
movq %rax, 0x220(%rsp)
movups 0x298(%rsp), %xmm0
movaps %xmm0, 0x210(%rsp)
movups 0x288(%rsp), %xmm0
movaps %xmm0, 0x200(%rsp)
movups 0x248(%rsp), %xmm0
movups 0x258(%rsp), %xmm1
movups 0x268(%rsp), %xmm2
movups 0x278(%rsp), %xmm3
movaps %xmm3, 0x1f0(%rsp)
movaps %xmm2, 0x1e0(%rsp)
movaps %xmm1, 0x1d0(%rsp)
movaps %xmm0, 0x1c0(%rsp)
movq 0x220(%rsp), %rcx
movq %rsp, %rax
movq %rcx, 0x60(%rax)
movaps 0x210(%rsp), %xmm0
movups %xmm0, 0x50(%rax)
movaps 0x200(%rsp), %xmm0
movups %xmm0, 0x40(%rax)
movaps 0x1c0(%rsp), %xmm0
movaps 0x1d0(%rsp), %xmm1
movaps 0x1e0(%rsp), %xmm2
movaps 0x1f0(%rsp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x4ea0
jmp 0x50ee0
movq 0x80(%rsp), %rax
movq %rax, 0x228(%rsp)
movq 0x228(%rsp), %rdi
movq 0x318(%rsp), %rsi
movq (%rdi), %rax
movq 0x1e8(%rax), %rax
callq *%rax
movq 0x228(%rsp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq 0x4c8(%rsp), %rax
movq %rax, 0x68(%rsp)
movl $0x0, 0x144(%rsp)
movl $0x0, 0x140(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x144(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x279d0
movq 0x68(%rsp), %rcx
movsd 0x4a0(%rsp), %xmm0
movsd 0x498(%rsp), %xmm2
movl 0x4a8(%rsp), %edx
movups 0x148(%rsp), %xmm1
movq %rsp, %rax
movups %xmm1, (%rax)
movl %edx, 0x30(%rax)
movl $0x0, 0x38(%rax)
movl $0x1, 0x28(%rax)
movl $0x0, 0x20(%rax)
movl $0x0, 0x18(%rax)
movl $0x0, 0x10(%rax)
xorl %edx, %edx
movl %edx, %r9d
leaq 0x158(%rsp), %rdi
movl $0x4, %esi
movl $0x2, %r8d
xorps %xmm1, %xmm1
callq 0x46830
movl $0x490, %edi            # imm = 0x490
callq 0x4240
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x70(%rsp)
movq 0x1b8(%rsp), %rax
movq %rax, 0x130(%rsp)
movups 0x1a8(%rsp), %xmm0
movaps %xmm0, 0x120(%rsp)
movups 0x198(%rsp), %xmm0
movaps %xmm0, 0x110(%rsp)
movups 0x158(%rsp), %xmm0
movups 0x168(%rsp), %xmm1
movups 0x178(%rsp), %xmm2
movups 0x188(%rsp), %xmm3
movaps %xmm3, 0x100(%rsp)
movaps %xmm2, 0xf0(%rsp)
movaps %xmm1, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movq 0x130(%rsp), %rcx
movq %rsp, %rax
movq %rcx, 0x60(%rax)
movaps 0x120(%rsp), %xmm0
movups %xmm0, 0x50(%rax)
movaps 0x110(%rsp), %xmm0
movups %xmm0, 0x40(%rax)
movaps 0xd0(%rsp), %xmm0
movaps 0xe0(%rsp), %xmm1
movaps 0xf0(%rsp), %xmm2
movaps 0x100(%rsp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x4ea0
jmp 0x510b1
movq 0x70(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x138(%rsp), %rdi
movq 0x228(%rsp), %rsi
movq (%rdi), %rax
callq *0x1e8(%rax)
movq 0x138(%rsp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq 0x318(%rsp), %rdi
movq 0x228(%rsp), %rsi
movq (%rdi), %rax
callq *0x1f0(%rax)
movq 0x228(%rsp), %rdi
movq 0x138(%rsp), %rsi
movq (%rdi), %rax
callq *0x1f0(%rax)
movq 0x19eba(%rip), %rdi     # 0x6afd8
leaq 0x54c0(%rip), %rsi      # 0x565e5
callq 0x4230
movq %rax, %rdi
movq 0x19e7c(%rip), %rsi     # 0x6afb0
callq 0x4260
movl $0x32, 0xcc(%rsp)
movl $0x0, 0xc8(%rsp)
movl 0xc8(%rsp), %eax
cmpl 0xcc(%rsp), %eax
jge 0x5147d
movq 0x19e6e(%rip), %rdi     # 0x6afd8
leaq 0x54d1(%rip), %rsi      # 0x56642
callq 0x4230
movq %rax, %rdi
movl 0xc8(%rsp), %esi
callq 0x43e0
movq %rax, %rdi
movq 0x19e21(%rip), %rsi     # 0x6afb0
callq 0x4260
movq 0x4c8(%rsp), %rdi
callq 0x3a2a0
movq 0x4c8(%rsp), %rdi
callq 0x3a2c0
movl $0x1, 0xc4(%rsp)
movl $0x0, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
cmpl 0xc4(%rsp), %eax
jge 0x51465
movq 0x318(%rsp), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
movq 0x228(%rsp), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
movq 0x138(%rsp), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
movq 0x4c8(%rsp), %rdi
callq 0x39b20
movq 0x4c8(%rsp), %rdi
callq 0x3a030
movq 0x410(%rsp), %rdi
movq (%rdi), %rax
callq *0x180(%rax)
movq 0x4c8(%rsp), %rdi
callq 0x1f310
movl %eax, 0xbc(%rsp)
movl $0x0, 0xb8(%rsp)
movl 0xb8(%rsp), %eax
cmpl 0xbc(%rsp), %eax
jge 0x513b8
movq 0x318(%rsp), %rdi
movl 0xb8(%rsp), %esi
movq (%rdi), %rax
callq *0x28(%rax)
movq 0x318(%rsp), %rdi
movl 0xb8(%rsp), %esi
movq (%rdi), %rax
callq *0x30(%rax)
movq 0x228(%rsp), %rdi
movl 0xb8(%rsp), %esi
movq (%rdi), %rax
callq *0x28(%rax)
movq 0x228(%rsp), %rdi
movl 0xb8(%rsp), %esi
movq (%rdi), %rax
callq *0x30(%rax)
movq 0x138(%rsp), %rdi
movl 0xb8(%rsp), %esi
movq (%rdi), %rax
callq *0x28(%rax)
movq 0x138(%rsp), %rdi
movl 0xb8(%rsp), %esi
movq (%rdi), %rax
callq *0x30(%rax)
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x51255
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x4b0(%rsp)
movl %eax, 0x4ac(%rsp)
callq 0x4220
jmp 0x51485
movq 0xa0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x4b0(%rsp)
movl %eax, 0x4ac(%rsp)
callq 0x4220
jmp 0x51485
movq 0x90(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x4b0(%rsp)
movl %eax, 0x4ac(%rsp)
callq 0x4220
jmp 0x51485
movq 0x80(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x4b0(%rsp)
movl %eax, 0x4ac(%rsp)
callq 0x4220
jmp 0x51485
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x4b0(%rsp)
movl %eax, 0x4ac(%rsp)
callq 0x4220
jmp 0x51485
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
cmpl 0xbc(%rsp), %eax
jge 0x51425
movq 0x138(%rsp), %rdi
movl 0xb4(%rsp), %esi
movq (%rdi), %rax
callq *0x38(%rax)
movq 0x228(%rsp), %rdi
movl 0xb4(%rsp), %esi
movq (%rdi), %rax
callq *0x38(%rax)
movq 0x318(%rsp), %rdi
movl 0xb4(%rsp), %esi
movq (%rdi), %rax
callq *0x38(%rax)
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x513c3
movq 0x318(%rsp), %rdi
movq (%rdi), %rax
callq *0x50(%rax)
movq 0x228(%rsp), %rdi
movq (%rdi), %rax
callq *0x50(%rax)
movq 0x138(%rsp), %rdi
movq (%rdi), %rax
callq *0x50(%rax)
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x511c4
jmp 0x51467
movl 0xc8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc8(%rsp)
jmp 0x5114f
addq $0x4d8, %rsp            # imm = 0x4D8
retq
movq 0x4b0(%rsp), %rdi
callq 0x43f0
nopw %cs:(%rax,%rax)
nopl (%rax)