// File: /workspace/github_cpp_makefile_star2_O3/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 $0x508, %rsp            # imm = 0x508
movq %rdx, 0x8(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r15
movq 0x4f7f(%rip), %r14      # 0x9fd8
leaq 0x212a(%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 0x2194(%rip), %xmm0     # 0x7230
movapd %xmm0, 0x60(%rsp)
movb $0x0, 0x70(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
cmpl $0x0, 0x8(%r15)
movq %r15, 0x18(%rsp)
jle 0x55e4
leaq 0x4(%r15), %rax
movq %rax, 0x28(%rsp)
movq 0x4ef6(%rip), %rax      # 0x9fd0
movq (%rax), %rcx
movq %rcx, 0x50(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x4ee6(%rip), %rbx      # 0x9fd8
leaq 0x350(%rsp), %r14
xorl %r13d, %r13d
testq %r13, %r13
je 0x5131
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x2f66
movq 0x20(%rsp), %rdi
movq %r14, %rsi
leaq 0x400(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x3dbc
movq %r14, %rdi
callq 0x3794
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x206a(%rip), %rsi      # 0x71aa
callq 0x21e0
leaq 0x1(%r13), %rsi
movq %rbx, %rdi
movq %rsi, 0x58(%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 0x5381
xorl %ebp, %ebp
xorl %eax, %eax
cmpl $0x5, %ebp
setb %al
movq 0x28(%r15), %r12
movq (%r12,%r13,8), %rcx
leaq 0x1f75(%rip), %rdx      # 0x7120
movsd (%rdx,%rax,8), %xmm0
movsd %xmm0, 0x38(%rcx)
cmpl $0x0, (%r15)
jle 0x52cc
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %ebx, %ebx
testq %r13, %r13
je 0x5225
movq 0x10(%r15), %r15
movslq -0x4(%r15,%r13,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r14
movslq -0x4(%r15,%r13,4), %rax
testq %rax, %rax
jle 0x5276
movq 0x30(%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 0x5210
jmp 0x5276
movq 0x28(%rsp), %rax
movslq (%rax), %r15
leaq (,%r15,8), %rdi
testq %r15, %r15
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r14
testl %r15d, %r15d
jle 0x5276
movq 0x20(%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, %r15
jne 0x5263
movq (%r12,%r13,8), %rdi
movq 0x8(%rsp), %rax
movsd 0x98(%rax), %xmm0
movl 0x90(%rax), %edx
movq %r14, %rsi
callq 0x46f4
movq %r14, %rdi
callq 0x2210
movq 0x18(%rsp), %r15
movq 0x28(%r15), %r12
movq (%r12,%r13,8), %rax
movsd 0x10(%rsp), %xmm0
addsd 0x28(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
incq %rbx
movslq (%r15), %rax
cmpq %rax, %rbx
jl 0x51cb
jmp 0x52d6
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movl $0x7, %edx
movq 0x4cf6(%rip), %rbx      # 0x9fd8
movq %rbx, %rdi
leaq 0x1ebe(%rip), %rsi      # 0x71aa
callq 0x21e0
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
callq 0x22a0
movq %rax, %r14
movl $0x9, %edx
movq %rax, %rdi
leaq 0x1ec1(%rip), %rsi      # 0x71d1
callq 0x21e0
movq %r14, %rdi
movl %ebp, %esi
callq 0x22a0
movq %rax, %r14
movl $0x9, %edx
movq %rax, %rdi
leaq 0x1e81(%rip), %rsi      # 0x71b2
callq 0x21e0
movq %r14, %rdi
movsd 0x10(%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 0x5194
movb $0x0, 0x400(%rsp)
leal 0x2(%r13), %ecx
leaq 0x400(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1caf(%rip), %rsi      # 0x704e
leaq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x2050
leaq 0xa0(%rsp), %rdi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x21d0
testq %r13, %r13
movq 0x10(%r15), %rax
leaq -0x4(%rax,%r13,4), %rax
cmoveq 0x28(%rsp), %rax
movslq (%rax), %rbx
leaq (,%rbx,8), %rdi
testq %rbx, %rbx
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %rbp
cmpl $0x0, (%r15)
jle 0x5525
movl %ebx, %r14d
xorl %ebx, %ebx
testl %r14d, %r14d
jle 0x543a
movq 0x30(%rsp), %rax
xorl %ecx, %ecx
movq %rax, %rdx
testq %r13, %r13
jne 0x541f
movq 0x20(%rsp), %rdx
movq 0x8(%rdx), %rdx
leaq (%rbx,%rbx,2), %rsi
movq (%rdx,%rsi,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x540e
movq 0x10(%r15), %r15
movslq (%r15,%r13,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r12
movq 0x18(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax,%r13,8), %rdi
movl (%r15,%r13,4), %r9d
movq %rbp, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
movl %r14d, %r8d
callq 0x4562
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
cmpl $0x0, (%rax,%r13,4)
jle 0x54d0
xorl %r15d, %r15d
movsd (%r12,%r15,8), %xmm0
leaq 0xa0(%rsp), %rdi
callq 0x2270
movl $0x1, %edx
movq %rax, %rdi
leaq 0x1d23(%rip), %rsi      # 0x71d9
callq 0x21e0
incq %r15
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movslq (%rax,%r13,4), %rax
cmpq %rax, %r15
jl 0x5494
movq 0xa0(%rsp), %rax
movq -0x18(%rax), %rdi
leaq 0xa0(%rsp), %rax
addq %rax, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
leaq 0xa0(%rsp), %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2110
movq %r12, %rdi
callq 0x2210
incq %rbx
movq 0x18(%rsp), %r15
movslq (%r15), %rax
cmpq %rax, %rbx
jl 0x5402
movq %rbp, %rdi
callq 0x2210
leaq 0xa0(%rsp), %rdi
callq 0x2110
leaq 0xa0(%rsp), %rdi
callq 0x2220
testq %r13, %r13
je 0x5588
movapd 0x30(%rsp), %xmm0
movapd %xmm0, 0x80(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x90(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x3198
movq 0x50(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq -0x18(%rax), %rax
movq 0x48(%rsp), %rcx
movq %rcx, 0xa0(%rsp,%rax)
leaq 0xa8(%rsp), %rdi
callq 0x2170
leaq 0x198(%rsp), %rdi
callq 0x20a0
movslq 0x8(%r15), %rax
movq 0x58(%rsp), %rcx
movq %rcx, %r13
cmpq %rax, %rcx
movq 0x4a02(%rip), %rbx      # 0x9fd8
leaq 0x350(%rsp), %r14
jl 0x50fd
leaq 0x2a0(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2f66
leaq 0x2a0(%rsp), %rsi
leaq 0x400(%rsp), %rdx
leaq 0x30(%rsp), %rcx
movq 0x20(%rsp), %rdi
callq 0x3dbc
leaq 0x2a0(%rsp), %rdi
callq 0x3794
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 0x5791
movl %ebx, %ebx
xorl %r14d, %r14d
movq %r14, 0x28(%rsp)
cmpl $0x0, (%r15)
jle 0x5727
xorl %ebp, %ebp
movq 0x30(%r15), %rax
movq %rax, 0x10(%rsp)
movslq 0x8(%rax), %r15
leaq (,%r15,4), %r14
testq %r15, %r15
movq %r14, %rdi
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r13
testl %r15d, %r15d
jle 0x56b0
movq %r13, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2100
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
cvttsd2si (%rax,%rbp,8), %eax
cltq
movl $0x1, (%r13,%rax,4)
testl %ebx, %ebx
jle 0x56f6
movq 0x30(%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 0x56e3
movq 0x8(%rsp), %rax
movsd 0x98(%rax), %xmm0
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x4c28
incq %rbp
movq 0x18(%rsp), %r15
movslq (%r15), %rax
cmpq %rax, %rbp
jl 0x5670
movl $0x1e, %edx
movq 0x48a5(%rip), %r13      # 0x9fd8
movq %r13, %rdi
leaq 0x1a7f(%rip), %rsi      # 0x71bc
callq 0x21e0
movq 0x28(%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 0x565f
movq %r12, %rdi
callq 0x2210
leaq 0x30(%rsp), %rdi
callq 0x3198
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x2a0(%rsp), %rdi
jmp 0x57cf
jmp 0x5821
movq %rax, %rbx
leaq 0x350(%rsp), %rdi
callq 0x3794
jmp 0x5824
jmp 0x5821
jmp 0x57e4
jmp 0x5821
jmp 0x5821
jmp 0x5821
jmp 0x57e4
jmp 0x5821
movq %rax, %rbx
movq 0x50(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq -0x18(%rax), %rax
movq 0x48(%rsp), %rcx
movq %rcx, 0xa0(%rsp,%rax)
leaq 0xa8(%rsp), %rdi
callq 0x2170
leaq 0x198(%rsp), %rdi
callq 0x20a0
jmp 0x5824
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3198
movq %rbx, %rdi
callq 0x22b0