// File: /workspace/github_cpp_makefile_star2_O2/lipiji[P]PG_DEEP/deep.cpp
// Function: RBM::train(double*, double, int)
void RBM::train(double *x, double gamma, int cd_k)
{
    double *pos_h_prob = new double[n_hidden];
    int *pos_h_state = new int[n_hidden];
    double *neg_v_prob = new double[n_visible];
    int *neg_v_state = new int[n_visible];
    double *neg_h_prob = new double[n_hidden];
    int *neg_h_state = new int[n_hidden];
    // use prob or state?
    // many tricks in 
    // A Practical Guide to Training Restricted Boltzmann Machines

    // postive phase
    activate_hidden(x, pos_h_prob, pos_h_state, n_visible, n_hidden);
    // negative phase
    for(int k=1; k<=cd_k; k++)
    {
        //h0->v1
        activate_visible(pos_h_state, neg_v_prob, neg_v_state, n_hidden, n_visible);
        //v1->h1	
        activate_hidden(neg_v_prob, neg_h_prob, neg_h_state, n_visible, n_hidden);

    }
    // update parameters
    for(int i=0; i<n_hidden; i++)
    {
        for(int j=0; j<n_visible; j++)
        {
            W[i][j] += momentum * 0 + gamma * ((x[j] * pos_h_prob[i] - neg_v_prob[j] * neg_h_prob[i]) / 1 - lamda * W[i][j]); 
        }
        hbias[i] += momentum * 0 + gamma * (pos_h_prob[i] - neg_h_prob[i]) / 1;
    }
    for(int i=0; i<n_visible; i++)
    {
        vbias[i] += momentum * 0 + gamma * (x[i] - neg_v_prob[i]) / 1; // 1 is #samples
    }

    error = square_error(x, neg_v_prob, n_visible);

    delete[] pos_h_prob;
    delete[] pos_h_state;
    delete[] neg_v_prob;
    delete[] neg_v_state;
    delete[] neg_h_prob;
    delete[] neg_h_state;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, 0x14(%rsp)
movsd %xmm0, 0x30(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movslq 0x8(%rdi), %rax
movq %rax, 0x28(%rsp)
leaq (,%rax,8), %rdi
testq %rax, %rax
pushq $-0x1
popq %r13
cmovsq %r13, %rdi
movq %rdi, (%rsp)
leaq (,%rax,4), %rbp
cmovsq %r13, %rbp
callq 0x2030
movq %rax, %r15
movq %rbp, %rdi
callq 0x2030
movq %rax, 0x8(%rsp)
movslq 0x4(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq (,%rax,8), %rdi
testq %rax, %rax
cmovsq %r13, %rdi
leaq (,%rax,4), %r12
cmovsq %r13, %r12
callq 0x2030
movq %rax, %r13
movq %r12, %rdi
callq 0x2030
movq %rax, 0x18(%rsp)
movq (%rsp), %rdi
callq 0x2030
movq %rax, %r12
movq %rbp, %rdi
callq 0x2030
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %r9
callq 0x3dac
xorl %ebp, %ebp
movl 0x14(%rsp), %eax
testl %eax, %eax
cmovgl %eax, %ebp
movl 0x4(%rbx), %r9d
movl 0x8(%rbx), %r8d
subl $0x1, %ebp
jb 0x3ff2
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
movq 0x18(%rsp), %rcx
callq 0x3e46
movl 0x4(%rbx), %r8d
movl 0x8(%rbx), %r9d
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq (%rsp), %rcx
callq 0x3dac
jmp 0x3fb4
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
testl %r9d, %r9d
cmovlel %ecx, %r9d
testl %r8d, %r8d
cmovlel %ecx, %r8d
xorpd %xmm0, %xmm0
movsd 0x30(%rsp), %xmm5
cmpq %r8, %rcx
je 0x409c
movq 0x10(%rbx), %rdx
xorl %esi, %esi
movsd 0x38(%rbx), %xmm1
mulsd %xmm0, %xmm1
cmpq %rsi, %r9
je 0x4076
movsd (%r14,%rsi,8), %xmm2
movsd (%r12,%rcx,8), %xmm3
mulsd (%r13,%rsi,8), %xmm3
mulsd (%r15,%rcx,8), %xmm2
subsd %xmm3, %xmm2
movq (%rdx,%rcx,8), %rdi
movsd (%rdi,%rsi,8), %xmm3
movsd 0x30(%rbx), %xmm4
mulsd %xmm3, %xmm4
subsd %xmm4, %xmm2
mulsd %xmm5, %xmm2
addsd %xmm2, %xmm1
addsd %xmm3, %xmm1
movsd %xmm1, (%rdi,%rsi,8)
incq %rsi
jmp 0x401f
movsd (%r15,%rcx,8), %xmm2
subsd (%r12,%rcx,8), %xmm2
mulsd %xmm5, %xmm2
addsd %xmm2, %xmm1
addsd (%rax,%rcx,8), %xmm1
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
jmp 0x4010
movl 0x4(%rbx), %edx
movq 0x20(%rbx), %rax
xorl %ecx, %ecx
testl %edx, %edx
movl $0x0, %esi
cmovgl %edx, %esi
xorpd %xmm0, %xmm0
cmpq %rcx, %rsi
je 0x40e5
movsd (%r14,%rcx,8), %xmm1
subsd (%r13,%rcx,8), %xmm1
mulsd %xmm5, %xmm1
movsd 0x38(%rbx), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
addsd (%rax,%rcx,8), %xmm2
movsd %xmm2, (%rax,%rcx,8)
incq %rcx
jmp 0x40b3
movq %r14, %rdi
movq %r13, %rsi
callq 0x31c1
movsd %xmm0, 0x28(%rbx)
movq %r15, %rdi
callq 0x2200
movq 0x8(%rsp), %rdi
callq 0x2200
movq %r13, %rdi
callq 0x2200
movq 0x18(%rsp), %rdi
callq 0x2200
movq %r12, %rdi
callq 0x2200
movq (%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2200