// File: /workspace/github_cpp_makefile_star2_O3/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, 0x8(%rsp)
leaq (,%rax,8), %rdi
testq %rax, %rax
movq $-0x1, %r13
cmovsq %r13, %rdi
movq %rdi, (%rsp)
leaq (,%rax,4), %r12
cmovsq %r13, %r12
movq %r12, 0x10(%rsp)
callq 0x2030
movq %rax, %r15
movq %r12, %rdi
callq 0x2030
movq %rax, 0x28(%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, (%rsp)
movq 0x10(%rsp), %rdi
callq 0x2030
movq %rax, %r12
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, 0x10(%rsp)
movq %r15, %rdx
movq 0x28(%rsp), %rcx
movq %rcx, %r15
movq 0x20(%rsp), %r8
movq 0x8(%rsp), %r9
callq 0x4562
testl %ebp, %ebp
jle 0x480e
movl 0x4(%rbx), %r9d
movl 0x8(%rbx), %r8d
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq 0x18(%rsp), %rcx
callq 0x462a
movl 0x4(%rbx), %r8d
movl 0x8(%rbx), %r9d
movq %rbx, %rdi
movq %r13, %rsi
movq (%rsp), %rdx
movq %r12, %rcx
callq 0x4562
decl %ebp
jne 0x47d5
movq %r12, 0x8(%rsp)
movslq 0x8(%rbx), %rcx
movl 0x4(%rbx), %eax
testq %rcx, %rcx
movq 0x10(%rsp), %rdi
movq %r15, %rbp
movq 0x18(%rsp), %r12
movq (%rsp), %r15
movsd 0x30(%rsp), %xmm6
jle 0x48ea
movq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rsi
xorl %r10d, %r10d
xorpd %xmm0, %xmm0
testl %eax, %eax
jle 0x48b1
movq (%rdx,%r10,8), %r8
movsd (%rdi,%r10,8), %xmm1
movsd (%r15,%r10,8), %xmm2
xorl %r9d, %r9d
movsd (%r13,%r9,8), %xmm3
mulsd %xmm2, %xmm3
movsd (%r14,%r9,8), %xmm4
mulsd %xmm1, %xmm4
subsd %xmm3, %xmm4
movsd (%r8,%r9,8), %xmm3
movsd 0x30(%rbx), %xmm5
mulsd %xmm3, %xmm5
subsd %xmm5, %xmm4
mulsd %xmm6, %xmm4
movsd 0x38(%rbx), %xmm5
mulsd %xmm0, %xmm5
addsd %xmm4, %xmm5
addsd %xmm3, %xmm5
movsd %xmm5, (%r8,%r9,8)
incq %r9
cmpq %r9, %rax
jne 0x4860
jmp 0x48bd
movsd (%rdi,%r10,8), %xmm1
movsd (%r15,%r10,8), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm6, %xmm1
movsd 0x38(%rbx), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
addsd (%rsi,%r10,8), %xmm2
movsd %xmm2, (%rsi,%r10,8)
incq %r10
cmpq %rcx, %r10
jne 0x4849
testl %eax, %eax
jle 0x4951
movq 0x20(%rbx), %rcx
xorl %edx, %edx
xorpd %xmm0, %xmm0
movsd (%r14,%rdx,8), %xmm1
subsd (%r13,%rdx,8), %xmm1
mulsd %xmm6, %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 0x48f8
testl %eax, %eax
jle 0x4951
xorpd %xmm0, %xmm0
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 0x4932
jmp 0x4955
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rbx)
callq 0x2210
movq %rbp, %rdi
callq 0x2210
movq %r13, %rdi
callq 0x2210
movq %r12, %rdi
callq 0x2210
movq %r15, %rdi
callq 0x2210
movq 0x8(%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2210
nop