// File: /workspace/github_cpp_makefile_star2_O1/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, %ebp
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
movq $-0x1, %r13
cmovsq %r13, %rdi
movq %rdi, 0x20(%rsp)
leaq (,%rax,4), %r12
cmovsq %r13, %r12
movq %r12, (%rsp)
callq 0x2030
movq %rax, %r15
movq %r12, %rdi
callq 0x2030
movq %rax, 0x8(%rsp)
movslq 0x4(%rbx), %rax
movq %rax, 0x18(%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, 0x10(%rsp)
movq 0x20(%rsp), %rdi
callq 0x2030
movq %rax, %r12
movq (%rsp), %rdi
callq 0x2030
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %r8
movq 0x28(%rsp), %r9
callq 0x44f6
testl %ebp, %ebp
jle 0x479c
movl 0x4(%rbx), %r9d
movl 0x8(%rbx), %r8d
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
movq 0x10(%rsp), %rcx
callq 0x45be
movl 0x4(%rbx), %r8d
movl 0x8(%rbx), %r9d
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq (%rsp), %rcx
callq 0x44f6
decl %ebp
jne 0x4761
movslq 0x8(%rbx), %rax
testq %rax, %rax
movq 0x8(%rsp), %rbp
movsd 0x30(%rsp), %xmm4
jle 0x4856
movl 0x4(%rbx), %ecx
movq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rsi
xorl %edi, %edi
xorpd %xmm0, %xmm0
testl %ecx, %ecx
jle 0x4823
movq (%rdx,%rdi,8), %r8
xorl %r9d, %r9d
movsd (%r12,%rdi,8), %xmm1
mulsd (%r13,%r9,8), %xmm1
movsd (%r14,%r9,8), %xmm2
mulsd (%r15,%rdi,8), %xmm2
subsd %xmm1, %xmm2
movsd (%r8,%r9,8), %xmm1
movsd 0x30(%rbx), %xmm3
mulsd %xmm1, %xmm3
subsd %xmm3, %xmm2
mulsd %xmm4, %xmm2
movsd 0x38(%rbx), %xmm3
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
addsd %xmm1, %xmm3
movsd %xmm3, (%r8,%r9,8)
incq %r9
cmpq %r9, %rcx
jne 0x47d0
movsd (%r15,%rdi,8), %xmm1
subsd (%r12,%rdi,8), %xmm1
mulsd %xmm4, %xmm1
movsd 0x38(%rbx), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
addsd (%rsi,%rdi,8), %xmm2
movsd %xmm2, (%rsi,%rdi,8)
incq %rdi
cmpq %rax, %rdi
jne 0x47c5
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x4899
movq 0x20(%rbx), %rcx
xorl %edx, %edx
xorpd %xmm0, %xmm0
movsd (%r14,%rdx,8), %xmm1
subsd (%r13,%rdx,8), %xmm1
mulsd %xmm4, %xmm1
movsd 0x38(%rbx), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
addsd (%rcx,%rdx,8), %xmm2
movsd %xmm2, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x4869
xorpd %xmm0, %xmm0
testl %eax, %eax
jle 0x48c0
xorl %ecx, %ecx
movsd (%r14,%rcx,8), %xmm1
subsd (%r13,%rcx,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x48a3
movsd %xmm0, 0x28(%rbx)
movq %r15, %rdi
callq 0x2210
movq %rbp, %rdi
callq 0x2210
movq %r13, %rdi
callq 0x2210
movq 0x10(%rsp), %rdi
callq 0x2210
movq %r12, %rdi
callq 0x2210
movq (%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2210