// File: /workspace/github_cpp_makefile_star2_O2/lipiji[P]PG_DEEP/deep.cpp
// Function: DBN::pretrain(Dataset, Conf)
void DBN::pretrain(Dataset data, Conf conf)
{
    cout << "Layer-wise pre-training begin: " <<endl;

    char str[] = "./model/x_layer_";
    char x_l[256];
    vector<vector<double> > xlayer;

    double *rbm_input = NULL;
    double *pre_rbm_input = NULL;
    int pre_size;
    for(int l=0; l<n_layers; l++)
    {
        if(l > 0)
            data.reloadx(conf, x_l, xlayer);
        cout << "Layer: " << (l+1) << endl;
        for(int epoch=0; epoch<conf.epoch; epoch++)
        {
            if(epoch < 5)
                rbm_layers[l]->momentum = 0.5;
            else
                rbm_layers[l]->momentum = 0.9;

            double error = 0;
            for(int i=0; i<n_samples; i++)
            {
                if(l == 0)
                {
                    rbm_input = new double[n_features];
                    for(int f=0; f<n_features; f++)
                        rbm_input[f] = data.X[i][f];
                }
                else
                {
                    rbm_input = new double[hidden_layer_size[l-1]];
                    for(int f=0; f<hidden_layer_size[l-1]; f++)
                        rbm_input[f] = xlayer[i][f];

                }
                rbm_layers[l]->train(rbm_input, conf.learning_rate, conf.cd_k);
                delete[] rbm_input;
                error += rbm_layers[l]->error;
            }
            cout << "Layer: " << (l+1) << ", Epoch: " << epoch << ", Error: " << error << endl;
        }// end one layer

        //save the samples
        x_l[0]='\0';
        sprintf(x_l, "%s%d", str, (l+2));
        ofstream fout(x_l);
        if(l == 0)
            pre_size = n_features;
        else
            pre_size = hidden_layer_size[l-1];
        pre_rbm_input = new double[pre_size];
        for(int i=0; i<n_samples; i++)
        {
            for(int f=0; f<pre_size; f++)
            {
                if(l == 0)
                    pre_rbm_input[f] =  data.X[i][f];
                else
                    pre_rbm_input[f] =  xlayer[i][f];
            }
            rbm_input = new double[hidden_layer_size[l]];
            rbm_layers[l]->activate_hidden(pre_rbm_input, rbm_input, NULL, pre_size, hidden_layer_size[l]);
            for(int f=0; f<hidden_layer_size[l]; f++)
            {
                fout << rbm_input[f] << " ";
            }
            fout << endl;
            delete[] rbm_input;
        }
        delete[] pre_rbm_input;
        fout << flush;
        fout.close();
        if(l > 0)
            vector<vector<double> >().swap(xlayer);
    }//rbm layers end

    // logistic layer training
    data.reloadx(conf, x_l, xlayer);
    pre_size = lr_layer->n_features;
    rbm_input = new double[pre_size];

    for(int epoch=0; epoch<conf.epoch; epoch++)
    {
        for(int i=0; i<n_samples; i++)
        {
            int *train_y = new int[lr_layer->n_labels];
            for(int yi=0; yi<lr_layer->n_labels; yi++)
                train_y[yi] = 0;
            train_y[int(data.Y[i])] = 1;
            for(int f=0; f<pre_size; f++)
                rbm_input[f] = xlayer[i][f];

            //
            lr_layer->train(rbm_input, train_y, conf.learning_rate);
        }
        cout << "Layer: logistic layer, Epoch: " << epoch  << endl;
    }
    delete[] rbm_input;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4f8, %rsp            # imm = 0x4F8
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, %rbp
movq 0x4889(%rip), %rdi      # 0x8fd8
leaq 0x1a34(%rip), %rsi      # 0x618a
callq 0x21a0
movq %rax, %rdi
callq 0x20e0
movapd 0x1ac5(%rip), %xmm0     # 0x6230
leaq 0x70(%rsp), %rax
movapd %xmm0, (%rax)
movb $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rax
andq $0x0, 0x10(%rax)
movapd %xmm0, (%rax)
leaq 0x4(%rbp), %rax
movq %rax, 0x48(%rsp)
leaq 0x140(%rsp), %rbx
xorl %r13d, %r13d
movq %rbp, 0x18(%rsp)
movslq 0x8(%rbp), %rax
cmpq %rax, %r13
jge 0x4ba0
testq %r13, %r13
je 0x47e2
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x2bfe
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x1f0(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq 0x37be
movq %rbx, %rdi
callq 0x334e
movq 0x47ef(%rip), %rdi      # 0x8fd8
leaq 0x19ba(%rip), %rsi      # 0x61aa
callq 0x21a0
leaq 0x1(%r13), %rsi
movq %rax, %rdi
movq %rsi, 0x40(%rsp)
callq 0x22a0
movq %rax, %rdi
callq 0x20e0
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
cmpl 0x80(%rax), %ebx
jge 0x49c1
xorl %eax, %eax
movl %ebx, 0x38(%rsp)
cmpl $0x5, %ebx
setb %al
movq 0x28(%rbp), %rbx
movq (%rbx,%r13,8), %rcx
leaq 0x18e4(%rip), %rdx      # 0x6120
movsd (%rdx,%rax,8), %xmm0
movsd %xmm0, 0x38(%rcx)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r12d, %r12d
movslq (%rbp), %rax
cmpq %rax, %r12
jge 0x4958
testq %r13, %r13
je 0x48ba
movq 0x10(%rbp), %r15
movslq -0x4(%r15,%r13,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %rbp
movl -0x4(%r15,%r13,4), %eax
imulq $0x18, %r12, %rcx
addq 0x20(%rsp), %rcx
testl %eax, %eax
movl $0x0, %edx
cmovlel %edx, %eax
xorl %edx, %edx
cmpq %rdx, %rax
je 0x490b
movq (%rcx), %rsi
movsd (%rsi,%rdx,8), %xmm0
movsd %xmm0, (%rbp,%rdx,8)
incq %rdx
jmp 0x48a2
movq 0x48(%rsp), %rax
movslq (%rax), %r15
leaq (,%r15,8), %rdi
testq %r15, %r15
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %rbp
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
imulq $0x18, %r12, %rax
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x490b
movq 0x8(%r14), %rdx
movq (%rdx,%rax), %rdx
movsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%rbp,%rcx,8)
incq %rcx
jmp 0x48ee
movq (%rbx,%r13,8), %rdi
movq 0x10(%rsp), %rax
movsd 0x98(%rax), %xmm0
movl 0x90(%rax), %edx
movq %rbp, %rsi
callq 0x3edc
movq %rbp, %rdi
callq 0x2200
movq 0x18(%rsp), %rbp
movq 0x28(%rbp), %rbx
movq (%rbx,%r13,8), %rax
movsd 0x8(%rsp), %xmm0
addsd 0x28(%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
incq %r12
jmp 0x4853
movq 0x4679(%rip), %rdi      # 0x8fd8
leaq 0x1844(%rip), %rsi      # 0x61aa
callq 0x21a0
movl 0x38(%rsp), %ebx
movq %rax, %rdi
movq 0x40(%rsp), %rsi
callq 0x22a0
movq %rax, %rdi
leaq 0x184b(%rip), %rsi      # 0x61d1
callq 0x21a0
movq %rax, %rdi
movl %ebx, %esi
callq 0x22a0
movq %rax, %rdi
leaq 0x1813(%rip), %rsi      # 0x61b2
callq 0x21a0
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x2270
movq %rax, %rdi
callq 0x20e0
incl %ebx
jmp 0x4810
movb $0x0, 0x1f0(%rsp)
leal 0x2(%r13), %ecx
leaq 0x1f0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x166f(%rip), %rsi      # 0x604e
leaq 0x70(%rsp), %rdx
xorl %eax, %eax
callq 0x2040
leaq 0x2f8(%rsp), %rdi
movq %rbx, %rsi
pushq $0x10
popq %rdx
callq 0x21d0
testq %r13, %r13
movq 0x10(%rbp), %rax
leaq -0x4(%rax,%r13,4), %rax
cmoveq 0x48(%rsp), %rax
movslq (%rax), %r15
leaq (,%r15,8), %rdi
testq %r15, %r15
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %rdi
testl %r15d, %r15d
movl $0x0, %ebx
movq %r15, 0x38(%rsp)
cmovgl %r15d, %ebx
xorl %r15d, %r15d
movq %rax, 0x8(%rsp)
movslq (%rbp), %rax
cmpq %rax, %r15
jge 0x4b2d
movq 0x20(%rsp), %rax
xorl %ecx, %ecx
cmpq %rcx, %rbx
je 0x4a82
movq %rax, %rdx
testq %r13, %r13
jne 0x4a6b
movq 0x8(%r14), %rdx
imulq $0x18, %r15, %rsi
movq (%rdx,%rsi), %rdx
movsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%rdi,%rcx,8)
incq %rcx
jmp 0x4a5a
movq 0x10(%rbp), %rbp
movslq (%rbp,%r13,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r12
movq 0x18(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax,%r13,8), %rdi
movl (%rbp,%r13,4), %r9d
movq 0x8(%rsp), %rsi
movq %r12, %rdx
xorl %ecx, %ecx
movq 0x38(%rsp), %r8
callq 0x3dac
xorl %ebp, %ebp
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movslq (%rax,%r13,4), %rax
cmpq %rax, %rbp
jge 0x4b06
movsd (%r12,%rbp,8), %xmm0
leaq 0x2f8(%rsp), %rdi
callq 0x2270
movq %rax, %rdi
leaq 0x16dd(%rip), %rsi      # 0x61d9
callq 0x21a0
incq %rbp
jmp 0x4acd
leaq 0x2f8(%rsp), %rdi
callq 0x20e0
movq %r12, %rdi
callq 0x2200
incq %r15
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %rdi
jmp 0x4a46
callq 0x2200
leaq 0x2f8(%rsp), %rdi
callq 0x2110
leaq 0x2f8(%rsp), %rdi
callq 0x2220
testq %r13, %r13
je 0x4b81
movapd 0x20(%rsp), %xmm0
movapd %xmm0, 0x50(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x60(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x2d9a
leaq 0x2f8(%rsp), %rdi
callq 0x2240
movq 0x40(%rsp), %r13
leaq 0x140(%rsp), %rbx
jmp 0x47a3
leaq 0x90(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2bfe
leaq 0x90(%rsp), %rsi
leaq 0x1f0(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r14, %rdi
callq 0x37be
leaq 0x90(%rsp), %rdi
callq 0x334e
movq 0x30(%rbp), %rax
movslq 0x4(%rax), %r13
leaq (,%r13,8), %rdi
testq %r13, %r13
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r12
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
cmpl 0x80(%rax), %ebx
jge 0x4cf6
movl %ebx, 0x8(%rsp)
xorl %ebx, %ebx
movslq (%rbp), %rax
cmpq %rax, %rbx
jge 0x4cc6
movq 0x30(%rbp), %rbp
movslq 0x8(%rbp), %r15
leaq (,%r15,4), %rdi
testq %r15, %r15
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
testl %r15d, %r15d
movl $0x0, %ecx
cmovlel %ecx, %r15d
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x4c68
andl $0x0, (%rax,%rcx,4)
incq %rcx
jmp 0x4c5a
movq 0x20(%r14), %rcx
cvttsd2si (%rcx,%rbx,8), %ecx
movslq %ecx, %rcx
movl $0x1, (%rax,%rcx,4)
imulq $0x18, %rbx, %rcx
addq 0x20(%rsp), %rcx
xorl %edx, %edx
cmpq %rdx, %r13
je 0x4c9e
movq (%rcx), %rsi
movsd (%rsi,%rdx,8), %xmm0
movsd %xmm0, (%r12,%rdx,8)
incq %rdx
jmp 0x4c86
movq 0x10(%rsp), %rcx
movsd 0x98(%rcx), %xmm0
movq %rbp, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x437a
incq %rbx
movq 0x18(%rsp), %rbp
jmp 0x4c20
movq 0x430b(%rip), %rdi      # 0x8fd8
leaq 0x14e8(%rip), %rsi      # 0x61bc
callq 0x21a0
movl 0x8(%rsp), %ebx
movq %rax, %rdi
movl %ebx, %esi
callq 0x22a0
movq %rax, %rdi
callq 0x20e0
incl %ebx
jmp 0x4c09
movq %r12, %rdi
callq 0x2200
leaq 0x20(%rsp), %rdi
callq 0x2d9a
addq $0x4f8, %rsp            # imm = 0x4F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
jmp 0x4d34
jmp 0x4d5b
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x334e
jmp 0x4d5e
jmp 0x4d5b
jmp 0x4d49
jmp 0x4d5b
jmp 0x4d5b
jmp 0x4d49
jmp 0x4d5b
jmp 0x4d5b
movq %rax, %rbx
leaq 0x2f8(%rsp), %rdi
callq 0x2240
jmp 0x4d5e
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2d9a
movq %rbx, %rdi
callq 0x22b0