// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r15
movq 0x5045(%rip), %r14      # 0x9fd8
leaq 0x21f0(%rip), %rsi      # 0x718a
movl $0x1f, %edx
movq %r14, %rdi
callq 0x21e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2110
movapd 0x225a(%rip), %xmm0     # 0x7230
movapd %xmm0, 0x70(%rsp)
movb $0x0, 0x80(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
cmpl $0x0, 0x8(%r15)
jle 0x551a
leaq 0x4(%r15), %rax
movq %rax, 0x68(%rsp)
movq 0x4fbe(%rip), %rax      # 0x9fd0
movq (%rax), %rcx
movq %rcx, 0x48(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x4fae(%rip), %rbx      # 0x9fd8
leaq 0x340(%rsp), %r12
xorl %r13d, %r13d
testq %r13, %r13
je 0x5069
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0x2f5c
movq 0x10(%rsp), %rdi
movq %r12, %rsi
leaq 0x3f0(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq 0x3d7e
movq %r12, %rdi
callq 0x3778
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x2132(%rip), %rsi      # 0x71aa
callq 0x21e0
leaq 0x1(%r13), %rsi
movq %rbx, %rdi
movq %rsi, 0x18(%rsp)
callq 0x22a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2110
movq 0x8(%rsp), %rax
cmpl $0x0, 0x80(%rax)
jle 0x52af
xorl %ebp, %ebp
xorl %eax, %eax
cmpl $0x5, %ebp
setb %al
leaq 0x2045(%rip), %rcx      # 0x7120
movsd (%rcx,%rax,8), %xmm0
movq 0x28(%r15), %rax
movq (%rax,%r13,8), %rax
movsd %xmm0, 0x38(%rax)
cmpl $0x0, (%r15)
jle 0x51fc
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %ebx, %ebx
testq %r13, %r13
je 0x515c
movq 0x10(%r15), %r12
movslq -0x4(%r12,%r13,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r14
movslq -0x4(%r12,%r13,4), %rax
testq %rax, %rax
jle 0x51a9
movq 0x20(%rsp), %rcx
leaq (%rbx,%rbx,2), %rdx
movq (%rcx,%rdx,8), %rcx
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%r14,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x5147
jmp 0x51a9
movslq 0x4(%r15), %r12
leaq (,%r12,8), %rdi
testq %r12, %r12
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r14
testl %r12d, %r12d
jle 0x51a9
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
leaq (%rbx,%rbx,2), %rcx
movq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%r14,%rcx,8)
incq %rcx
cmpq %rcx, %r12
jne 0x5196
movq 0x28(%r15), %rax
movq (%rax,%r13,8), %rdi
movq 0x8(%rsp), %rax
movsd 0x98(%rax), %xmm0
movl 0x90(%rax), %edx
movq %r14, %rsi
callq 0x4688
movq %r14, %rdi
callq 0x2210
movq 0x28(%r15), %rax
movq (%rax,%r13,8), %rax
movsd (%rsp), %xmm0
addsd 0x28(%rax), %xmm0
movsd %xmm0, (%rsp)
incq %rbx
movslq (%r15), %rax
cmpq %rax, %rbx
jl 0x5102
jmp 0x5205
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movl $0x7, %edx
movq 0x4dc7(%rip), %rbx      # 0x9fd8
movq %rbx, %rdi
leaq 0x1f8f(%rip), %rsi      # 0x71aa
callq 0x21e0
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x22a0
movq %rax, %r14
movl $0x9, %edx
movq %rax, %rdi
leaq 0x1f92(%rip), %rsi      # 0x71d1
callq 0x21e0
movq %r14, %rdi
movl %ebp, %esi
callq 0x22a0
movq %rax, %r14
movl $0x9, %edx
movq %rax, %rdi
leaq 0x1f52(%rip), %rsi      # 0x71b2
callq 0x21e0
movq %r14, %rdi
movsd (%rsp), %xmm0
callq 0x2270
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2110
incl %ebp
movq 0x8(%rsp), %rax
cmpl 0x80(%rax), %ebp
jl 0x50cc
movb $0x0, 0x3f0(%rsp)
leal 0x2(%r13), %ecx
leaq 0x3f0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1d81(%rip), %rsi      # 0x704e
leaq 0x70(%rsp), %rdx
xorl %eax, %eax
callq 0x2050
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x21d0
testq %r13, %r13
movq 0x10(%r15), %rax
leaq -0x4(%rax,%r13,4), %rax
cmoveq 0x68(%rsp), %rax
movslq (%rax), %rbx
leaq (,%rbx,8), %rdi
testq %rbx, %rbx
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
leaq 0x340(%rsp), %r12
movq %rax, %rdi
cmpl $0x0, (%r15)
jle 0x5458
movl %ebx, %r14d
xorl %ebx, %ebx
movq %rdi, (%rsp)
testl %r14d, %r14d
jle 0x5373
movq 0x20(%rsp), %rax
xorl %ecx, %ecx
movq %rax, %rdx
testq %r13, %r13
jne 0x5359
movq 0x10(%rsp), %rdx
movq 0x8(%rdx), %rdx
leaq (%rbx,%rbx,2), %rsi
movq (%rdx,%rsi,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%rdi,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x5348
movq 0x10(%r15), %rbp
movslq (%rbp,%r13,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r12
movq 0x28(%r15), %rax
movq (%rax,%r13,8), %rdi
movl (%rbp,%r13,4), %r9d
movq (%rsp), %rsi
movq %r12, %rdx
xorl %ecx, %ecx
movl %r14d, %r8d
callq 0x44f6
movq 0x10(%r15), %rax
cmpl $0x0, (%rax,%r13,4)
jle 0x53fc
xorl %ebp, %ebp
movsd (%r12,%rbp,8), %xmm0
leaq 0x90(%rsp), %rdi
callq 0x2270
movl $0x1, %edx
movq %rax, %rdi
leaq 0x1df2(%rip), %rsi      # 0x71d9
callq 0x21e0
incq %rbp
movq 0x10(%r15), %rax
movslq (%rax,%r13,4), %rax
cmpq %rax, %rbp
jl 0x53c5
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rdi
leaq 0x90(%rsp), %rax
addq %rax, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
leaq 0x90(%rsp), %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2110
movq %r12, %rdi
callq 0x2210
incq %rbx
movslq (%r15), %rax
cmpq %rax, %rbx
leaq 0x340(%rsp), %r12
movq (%rsp), %rdi
jl 0x533c
callq 0x2210
leaq 0x90(%rsp), %rdi
callq 0x2110
leaq 0x90(%rsp), %rdi
callq 0x2220
testq %r13, %r13
je 0x54c6
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
xorl %esi, %esi
movq %rsi, 0x60(%rsp)
movq 0x58(%rsp), %rax
movq 0x60(%rsp), %rcx
movq 0x20(%rsp), %rdx
movq %rdx, 0x50(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, 0x58(%rsp)
movq %rsi, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq %rcx, 0x30(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x318e
movq 0x48(%rsp), %rax
movq %rax, 0x90(%rsp)
movq -0x18(%rax), %rax
movq 0x40(%rsp), %rcx
movq %rcx, 0x90(%rsp,%rax)
leaq 0x98(%rsp), %rdi
callq 0x2170
leaq 0x188(%rsp), %rdi
callq 0x20a0
movslq 0x8(%r15), %rax
movq 0x18(%rsp), %rcx
movq %rcx, %r13
cmpq %rax, %rcx
movq 0x4ac4(%rip), %rbx      # 0x9fd8
jl 0x5035
leaq 0x290(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2f5c
leaq 0x290(%rsp), %rsi
leaq 0x3f0(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq 0x10(%rsp), %rdi
callq 0x3d7e
leaq 0x290(%rsp), %rdi
callq 0x3778
movq 0x30(%r15), %rax
movslq 0x4(%rax), %rbx
leaq (,%rbx,8), %rax
testq %rbx, %rbx
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, %r12
movq 0x8(%rsp), %rax
cmpl $0x0, 0x80(%rax)
jle 0x56c1
movl %ebx, %ebx
xorl %r14d, %r14d
movq %r14, 0x18(%rsp)
cmpl $0x0, (%r15)
jle 0x5657
xorl %ebp, %ebp
movq 0x30(%r15), %rax
movq %rax, (%rsp)
movslq 0x8(%rax), %r13
leaq (,%r13,4), %r14
testq %r13, %r13
movq %r14, %rdi
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %rdx
testl %r13d, %r13d
jle 0x55eb
movq %rdx, %rdi
xorl %esi, %esi
movq %rdx, %r13
movq %r14, %rdx
callq 0x2100
movq %r13, %rdx
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
cvttsd2si (%rax,%rbp,8), %eax
cltq
movl $0x1, (%rdx,%rax,4)
testl %ebx, %ebx
jle 0x562f
movq 0x20(%rsp), %rax
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%r12,%rcx,8)
incq %rcx
cmpq %rcx, %rbx
jne 0x561c
movq 0x8(%rsp), %rax
movsd 0x98(%rax), %xmm0
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x4b68
incq %rbp
movslq (%r15), %rax
cmpq %rax, %rbp
jl 0x55a6
movl $0x1e, %edx
movq 0x4975(%rip), %r13      # 0x9fd8
movq %r13, %rdi
leaq 0x1b4f(%rip), %rsi      # 0x71bc
callq 0x21e0
movq 0x18(%rsp), %r14
movq %r13, %rdi
movl %r14d, %esi
callq 0x22a0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2110
incl %r14d
movq 0x8(%rsp), %rax
cmpl 0x80(%rax), %r14d
jl 0x5595
movq %r12, %rdi
callq 0x2210
leaq 0x20(%rsp), %rdi
callq 0x318e
addq $0x4f8, %rsp            # imm = 0x4F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x290(%rsp), %rdi
jmp 0x56ff
jmp 0x574d
movq %rax, %rbx
leaq 0x340(%rsp), %rdi
callq 0x3778
jmp 0x5750
jmp 0x574d
jmp 0x5714
jmp 0x574d
jmp 0x574d
jmp 0x574d
jmp 0x5714
jmp 0x574d
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
movq 0x48(%rsp), %rax
movq %rax, -0x8(%rdi)
movq -0x18(%rax), %rax
movq 0x40(%rsp), %rcx
movq %rcx, 0x90(%rsp,%rax)
callq 0x2170
leaq 0x188(%rsp), %rdi
callq 0x20a0
jmp 0x5750
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x318e
movq %rbx, %rdi
callq 0x22b0