// File: /workspace/github_cpp_makefile_star2_O2/lipiji[P]PG_DEEP/deep.cpp
// Function: DBN::finetune(Dataset, Conf)
void DBN::finetune(Dataset data, Conf conf)
{
    cout << "Fine-tuning..." << endl;
    // BP fine-tuning method
    // First, train the LR model without fine-tuning
    double *layer_input = NULL;
    double *pre_layer_input = NULL;
    int pre_size;

    for(int epoch=0; epoch<conf.epoch; epoch++)
    {
        //ofstream fout("./model/x.txt");
        for(int i=0; i<n_samples; i++)
        {
            vector<vector<double> > ai;
            vector<vector<double> > deltai;

            int *train_y = new int[n_labels];
            for(int yi=0; yi<n_labels; yi++)
                train_y[yi] = 0;
            train_y[int(data.Y[i])] = 1;
            // some x may be computed many times
            // which may be replaced by DP algoritham
            for(int j=0; j<n_layers; j++)
            {
                if(j == 0)
                {
                    pre_size = n_features;
                    pre_layer_input = new double[pre_size];
                    for(int f=0; f<pre_size; f++)
                        pre_layer_input[f] = data.X[i][f];
                    
                    //a0 = v0
                    vector<double> al;
                    for(int ia=0; ia<pre_size; ia++)
                        al.push_back(pre_layer_input[ia]);
                    ai.push_back(al);
                    vector<double>().swap(al);
                }
                else
                {
                    pre_size = hidden_layer_size[j-1];
                    pre_layer_input = new double[pre_size];

                    for(int f=0; f<pre_size; f++)
                        pre_layer_input[f] = layer_input[f];
                    delete[] layer_input;

                }
                layer_input = new double[hidden_layer_size[j]];
                rbm_layers[j]->activate_hidden(pre_layer_input, layer_input, NULL, pre_size, hidden_layer_size[j]);
                delete[] pre_layer_input;
                vector<double> al;
                for(int ia=0; ia<hidden_layer_size[j]; ia++)
                    al.push_back(layer_input[ia]);
                ai.push_back(al);
                vector<double>().swap(al);
            }
            ///////////
            // for(int ii=0; ii<hidden_layer_size[n_layers-1]; ii++)
            //   fout << layer_input[ii] << " ";
            //fout << int(data.Y[i]) << endl;
            /////////

            //output layer
            //in http://deeplearning.stanford.edu/wiki/index.php/Fine-tuning_Stacked_AEs
            double *pred_y = new double[lr_layer->n_labels];
            lr_layer->predict(layer_input, pred_y);
            vector<double> di0;
            for(int j=0; j<lr_layer->n_labels; j++)
                di0.push_back(-1*(train_y[j] - pred_y[j]) * pred_y[j] * (1 - pred_y[j]));
            deltai.push_back(di0);
            vector<double>().swap(di0);
            
            // hidden layer
            for(int l=n_layers; l>=0; l--)
            {
                if(l == n_layers)
                {
                    vector<double> di;
                    for(int j=0; j<lr_layer->n_features; j++)
                    {
                        double di_tmp = 0;
                        for(int f=0; f<lr_layer->n_labels; f++)
                        {
                            di_tmp += lr_layer->W[f][j] * deltai[n_layers-l][f];
                        }
                        di_tmp *= ai[l][j] * (1 -  ai[l][j]);
                        di.push_back(di_tmp);
                    }
                    deltai.push_back(di);
                    vector<double>().swap(di);
                }
                else
                {
                    vector<double> di;
                    for(int j=0; j<rbm_layers[l]->n_visible; j++)
                    {
                        double di_tmp = 0;
                        for(int f=0; f<rbm_layers[l]->n_hidden; f++)
                        {
                            di_tmp += rbm_layers[l]->W[f][j] * deltai[n_layers-l][f];
                        }
                        di_tmp *= ai[l][j] * (1 -  ai[l][j]);
                        di.push_back(di_tmp);
                    }
                    deltai.push_back(di);
                    vector<double>().swap(di);
                }
            }// end deltai

            // update hidden layer parameters
            for(int l=0; l<n_layers; l++)
            {
                for(int j=0; j<rbm_layers[l]->n_hidden; j++)
                {
                    for(int f=0; f<rbm_layers[l]->n_visible; f++)
                    {
                        rbm_layers[l]->W[j][f] -= alpha*(deltai[n_layers-l][j]*ai[l][f] + lamda * rbm_layers[l]->W[j][f]); 
                    }
                    rbm_layers[l]->hbias[j] -= alpha * deltai[n_layers-l][j];
                }
            }
            // update the parameters in LR layer
            lr_layer->train(layer_input, train_y, conf.learning_rate);
            
            vector<vector<double> >().swap(ai);
            vector<vector<double> >().swap(deltai);

            delete[] train_y;
            delete[] pred_y;
            delete[] layer_input;
        }//end xi


        ////////////
        //fout << flush;fout.close();
        cout << "Fine-tuning epoch: " << epoch <<endl;
    }//end epoch
    cout << "Fine-tuning done." << endl;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, 0xb0(%rsp)
movq %rsi, 0xc0(%rsp)
movq %rdi, %r15
movq 0x423d(%rip), %rdi      # 0x8fd8
leaq 0x1439(%rip), %rsi      # 0x61db
callq 0x21a0
movq %rax, %rdi
callq 0x20e0
xorl %ebx, %ebx
xorl %r13d, %r13d
movq 0xb0(%rsp), %rax
cmpl 0x80(%rax), %ebx
jge 0x5565
movl %ebx, 0xac(%rsp)
xorl %ecx, %ecx
movslq (%r15), %rax
cmpq %rax, %rcx
jge 0x5532
movq %rcx, 0xa0(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
movapd %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movslq 0xc(%r15), %rbx
leaq (,%rbx,4), %rdi
testq %rbx, %rbx
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, 0x38(%rsp)
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
xorl %eax, %eax
cmpq %rax, %rbx
je 0x4e46
movq 0x38(%rsp), %rcx
andl $0x0, (%rcx,%rax,4)
incq %rax
jmp 0x4e33
movq 0xc0(%rsp), %rax
movq 0x20(%rax), %rax
movq 0xa0(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
cltq
movq 0x38(%rsp), %rcx
movl $0x1, (%rcx,%rax,4)
xorl %ebp, %ebp
movslq 0x8(%r15), %rax
cmpq %rax, %rbp
jge 0x5083
testq %rbp, %rbp
je 0x4ede
movq 0x10(%r15), %rax
movslq -0x4(%rax,%rbp,4), %r14
leaq (,%r14,8), %rdi
testq %r14, %r14
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %rbx
testl %r14d, %r14d
movl $0x0, %eax
cmovgl %r14d, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x4ec8
movsd (%r13,%rcx,8), %xmm0
movsd %xmm0, (%rbx,%rcx,8)
incq %rcx
jmp 0x4eb2
testq %r13, %r13
je 0x4fb2
movq %r13, %rdi
callq 0x2200
jmp 0x4fb2
movslq 0x4(%r15), %r14
leaq (,%r14,8), %rdi
testq %r14, %r14
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %rbx
testl %r14d, %r14d
movl $0x0, %r13d
cmovgl %r14d, %r13d
xorl %eax, %eax
cmpq %rax, %r13
je 0x4f38
movq 0xc0(%rsp), %rcx
movq 0x8(%rcx), %rcx
imulq $0x18, 0xa0(%rsp), %rdx
movq (%rcx,%rdx), %rcx
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rbx,%rax,8)
incq %rax
jmp 0x4f0b
andq $0x0, 0x30(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
shlq $0x3, %r13
xorl %r12d, %r12d
cmpq %r12, %r13
je 0x4f68
leaq (%rbx,%r12), %rsi
leaq 0x20(%rsp), %rdi
callq 0x585e
addq $0x8, %r12
jmp 0x4f4f
leaq 0x80(%rsp), %rdi
leaq 0x20(%rsp), %r12
movq %r12, %rsi
callq 0x5832
movapd 0x20(%rsp), %xmm0
movapd %xmm0, (%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq %rsp, %rdi
callq 0x2ef2
movq %r12, %rdi
callq 0x2ef2
movq 0x10(%r15), %r12
movslq (%r12,%rbp,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r13
movq 0x28(%r15), %rax
movq (%rax,%rbp,8), %rdi
movl (%r12,%rbp,4), %r9d
movq %rbx, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
movl %r14d, %r8d
callq 0x3dac
movq %rbx, %rdi
callq 0x2200
andq $0x0, 0x30(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r13, %rbx
xorl %r14d, %r14d
movq 0x10(%r15), %rax
movslq (%rax,%rbp,4), %rax
cmpq %rax, %r14
jge 0x5031
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x585e
incq %r14
addq $0x8, %rbx
jmp 0x500e
leaq 0x80(%rsp), %rdi
leaq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x5832
movapd 0x20(%rsp), %xmm0
movapd %xmm0, (%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq %rsp, %rdi
callq 0x2ef2
movq %rbx, %rdi
callq 0x2ef2
incq %rbp
jmp 0x4e6f
movq 0x30(%r15), %rbx
movslq 0x8(%rbx), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, 0xb8(%rsp)
movq %rax, %rdx
callq 0x4542
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
xorl %ebx, %ebx
movq %rsp, %r14
movq 0x30(%r15), %rax
movslq 0x8(%rax), %rax
cmpq %rax, %rbx
jge 0x5128
movq 0x38(%rsp), %rax
cvtsi2sdl (%rax,%rbx,4), %xmm0
movq 0xb8(%rsp), %rax
movsd (%rax,%rbx,8), %xmm1
subsd %xmm1, %xmm0
xorpd 0x1003(%rip), %xmm0     # 0x6100
mulsd %xmm1, %xmm0
movsd 0xf0f(%rip), %xmm2      # 0x6018
subsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
movsd %xmm2, (%rsp)
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x5880
incq %rbx
jmp 0x50cd
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x5832
movapd 0x20(%rsp), %xmm0
movapd %xmm0, (%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq %r14, %rdi
callq 0x2ef2
movl 0x8(%r15), %r12d
movslq 0x8(%r15), %rax
testl %r12d, %r12d
js 0x5354
movl %r12d, %r14d
movslq %r12d, %rbp
xorpd %xmm0, %xmm0
cmpl %eax, %r12d
jne 0x523c
movapd %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
xorl %ebx, %ebx
movq 0x30(%r15), %rax
movslq 0x4(%rax), %rcx
cmpq %rcx, %rbx
jge 0x52f4
andq $0x0, 0x60(%rsp)
movl 0x8(%rax), %ecx
movslq 0x8(%r15), %rdx
subq %rbp, %rdx
imulq $0x18, %rdx, %rdx
addq 0x40(%rsp), %rdx
testl %ecx, %ecx
movl $0x0, %esi
cmovlel %esi, %ecx
xorpd %xmm0, %xmm0
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x51f8
movq 0x18(%rax), %rdi
movq (%rdi,%rsi,8), %rdi
movsd (%rdi,%rbx,8), %xmm1
movq (%rdx), %rdi
mulsd (%rdi,%rsi,8), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
incq %rsi
jmp 0x51cf
movq 0x80(%rsp), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %rax
movsd (%rax,%rbx,8), %xmm1
movsd 0xe03(%rip), %xmm2      # 0x6018
subsd %xmm1, %xmm2
mulsd %xmm1, %xmm2
mulsd %xmm2, %xmm0
movsd %xmm0, 0x60(%rsp)
movq %rsp, %rdi
leaq 0x60(%rsp), %rsi
callq 0x585e
incq %rbx
jmp 0x5195
movapd %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
xorl %ebx, %ebx
movq 0x28(%r15), %rax
movq (%rax,%r14,8), %rax
movslq 0x4(%rax), %rcx
cmpq %rcx, %rbx
jge 0x5306
andq $0x0, 0x60(%rsp)
movl 0x8(%rax), %ecx
movslq 0x8(%r15), %rdx
subq %rbp, %rdx
imulq $0x18, %rdx, %rdx
addq 0x40(%rsp), %rdx
testl %ecx, %ecx
movl $0x0, %esi
cmovlel %esi, %ecx
xorpd %xmm0, %xmm0
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x52b0
movq 0x10(%rax), %rdi
movq (%rdi,%rsi,8), %rdi
movsd (%rdi,%rbx,8), %xmm1
movq (%rdx), %rdi
mulsd (%rdi,%rsi,8), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
incq %rsi
jmp 0x5287
movq 0x80(%rsp), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %rax
movsd (%rax,%rbx,8), %xmm1
movsd 0xd4b(%rip), %xmm2      # 0x6018
subsd %xmm1, %xmm2
mulsd %xmm1, %xmm2
mulsd %xmm2, %xmm0
movsd %xmm0, 0x60(%rsp)
movq %rsp, %rdi
leaq 0x60(%rsp), %rsi
callq 0x585e
incq %rbx
jmp 0x5249
leaq 0x40(%rsp), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x5832
jmp 0x5316
leaq 0x40(%rsp), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x5832
movapd (%rsp), %xmm0
movapd %xmm0, 0x60(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x70(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x2ef2
movq %r14, %rdi
callq 0x2ef2
decl %r12d
jmp 0x5168
movq 0x28(%r15), %rcx
movq %rcx, 0xc8(%rsp)
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, 0xd0(%rsp)
xorl %esi, %esi
cmpq 0xd0(%rsp), %rsi
je 0x545a
movq 0xc8(%rsp), %rcx
movq (%rcx,%rsi,8), %rdi
movl 0x8(%rdi), %r8d
movq %rax, %rcx
subq %rsi, %rcx
imulq $0x18, %rcx, %r9
movq 0x40(%rsp), %r10
addq %r9, %r10
testl %r8d, %r8d
movl $0x0, %ecx
cmovlel %ecx, %r8d
xorl %r11d, %r11d
cmpq %r8, %r11
je 0x5452
movl 0x4(%rdi), %ebx
movq 0x40(%rsp), %r14
addq %r9, %r14
imulq $0x18, %rsi, %rbp
addq 0x80(%rsp), %rbp
testl %ebx, %ebx
movl $0x0, %ecx
cmovlel %ecx, %ebx
xorl %ecx, %ecx
movsd 0x20(%r15), %xmm0
cmpq %rcx, %rbx
je 0x542a
movq (%r14), %r12
movsd (%r12,%r11,8), %xmm1
movq (%rbp), %r12
movq 0x10(%rdi), %rdx
movq (%rdx,%r11,8), %rdx
movsd (%rdx,%rcx,8), %xmm2
movsd 0x18(%r15), %xmm3
mulsd %xmm2, %xmm3
mulsd (%r12,%rcx,8), %xmm1
addsd %xmm3, %xmm1
mulsd %xmm1, %xmm0
subsd %xmm0, %xmm2
movsd %xmm2, (%rdx,%rcx,8)
incq %rcx
jmp 0x53df
movq (%r10), %rcx
movq 0x18(%rdi), %rbx
mulsd (%rcx,%r11,8), %xmm0
movsd (%rbx,%r11,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rbx,%r11,8)
incq %r11
movq %rsp, %r14
jmp 0x53b3
incq %rsi
jmp 0x5374
movq 0x30(%r15), %rdi
movq 0xb0(%rsp), %rax
movsd 0x98(%rax), %xmm0
movq %r13, %rsi
movq 0x38(%rsp), %rdx
callq 0x437a
movaps 0x80(%rsp), %xmm0
movaps %xmm0, (%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
movq %r14, %rdi
callq 0x2d9a
movapd 0x40(%rsp), %xmm0
movapd %xmm0, (%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movq %r14, %rdi
callq 0x2d9a
movq 0x38(%rsp), %rdi
callq 0x2200
movq 0xb8(%rsp), %rdi
callq 0x2200
testq %r13, %r13
je 0x5501
movq %r13, %rdi
callq 0x2200
leaq 0x20(%rsp), %rdi
callq 0x2ef2
leaq 0x40(%rsp), %rdi
callq 0x2d9a
leaq 0x80(%rsp), %rdi
callq 0x2d9a
movq 0xa0(%rsp), %rcx
incq %rcx
jmp 0x4dd1
movq 0x3a9f(%rip), %rdi      # 0x8fd8
leaq 0xcaa(%rip), %rsi       # 0x61ea
callq 0x21a0
movq %rax, %rdi
movl 0xac(%rsp), %ebx
movl %ebx, %esi
callq 0x22a0
movq %rax, %rdi
callq 0x20e0
incl %ebx
jmp 0x4db4
movq 0x3a6c(%rip), %rdi      # 0x8fd8
leaq 0xc8b(%rip), %rsi       # 0x61fe
callq 0x21a0
movq %rax, %rdi
callq 0x20e0
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x55a2
jmp 0x55a2
jmp 0x55b8
jmp 0x55b8
jmp 0x55ab
jmp 0x55ab
jmp 0x55b8
jmp 0x55b8
movq %rax, %rbx
jmp 0x55c5
jmp 0x55b8
jmp 0x55ab
movq %rax, %rbx
movq %rsp, %rdi
callq 0x2ef2
jmp 0x55bb
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2ef2
leaq 0x40(%rsp), %rdi
callq 0x2d9a
leaq 0x80(%rsp), %rdi
callq 0x2d9a
movq %rbx, %rdi
callq 0x22b0