// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movslq 0x8(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0x28(%rbp)
movslq 0x8(%rax), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0x30(%rbp)
movslq 0x4(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0x38(%rbp)
movslq 0x4(%rax), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0x40(%rbp)
movslq 0x8(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0x48(%rbp)
movslq 0x8(%rax), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq -0x68(%rbp), %rdi
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl 0x4(%rdi), %r8d
movl 0x8(%rdi), %r9d
callq 0x5e60
movl $0x1, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jg 0x621d
movq -0x68(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movl 0x8(%rdi), %r8d
movl 0x4(%rdi), %r9d
callq 0x5f70
movq -0x68(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movl 0x4(%rdi), %r8d
movl 0x8(%rdi), %r9d
callq 0x5e60
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x61d0
movl $0x0, -0x58(%rbp)
movq -0x68(%rbp), %rcx
movl -0x58(%rbp), %eax
cmpl 0x8(%rcx), %eax
jge 0x638b
movl $0x0, -0x5c(%rbp)
movq -0x68(%rbp), %rcx
movl -0x5c(%rbp), %eax
cmpl 0x4(%rcx), %eax
jge 0x6328
movq -0x68(%rbp), %rax
movsd 0x38(%rax), %xmm0
movsd -0x18(%rbp), %xmm1
movq -0x10(%rbp), %rcx
movslq -0x5c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm3
movq -0x28(%rbp), %rcx
movslq -0x58(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm4
movq -0x38(%rbp), %rcx
movslq -0x5c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm2
movq -0x48(%rbp), %rcx
movslq -0x58(%rbp), %rdx
mulsd (%rcx,%rdx,8), %xmm2
movq %xmm2, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
xorq %rdx, %rcx
movq %rcx, %xmm2
mulsd %xmm4, %xmm3
addsd %xmm2, %xmm3
movsd 0x4d54(%rip), %xmm2     # 0xb008
divsd %xmm2, %xmm3
movsd 0x30(%rax), %xmm2
movq 0x10(%rax), %rcx
movslq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x5c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm4
movq %xmm2, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
xorq %rdx, %rcx
movq %rcx, %xmm2
mulsd %xmm4, %xmm2
addsd %xmm3, %xmm2
mulsd %xmm2, %xmm1
xorps %xmm2, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x10(%rax), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x5c(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x623b
movq -0x68(%rbp), %rax
movsd 0x38(%rax), %xmm0
movsd -0x18(%rbp), %xmm1
movq -0x28(%rbp), %rcx
movslq -0x58(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm2
movq -0x48(%rbp), %rcx
movslq -0x58(%rbp), %rdx
subsd (%rcx,%rdx,8), %xmm2
mulsd %xmm2, %xmm1
movsd 0x4cac(%rip), %xmm2     # 0xb008
divsd %xmm2, %xmm1
xorps %xmm2, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x18(%rax), %rax
movslq -0x58(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x6224
movl $0x0, -0x60(%rbp)
movq -0x68(%rbp), %rcx
movl -0x60(%rbp), %eax
cmpl 0x4(%rcx), %eax
jge 0x63fe
movq -0x68(%rbp), %rax
movsd 0x38(%rax), %xmm0
movsd -0x18(%rbp), %xmm1
movq -0x10(%rbp), %rcx
movslq -0x60(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm2
movq -0x38(%rbp), %rcx
movslq -0x60(%rbp), %rdx
subsd (%rcx,%rdx,8), %xmm2
mulsd %xmm2, %xmm1
movsd 0x4c36(%rip), %xmm2     # 0xb008
divsd %xmm2, %xmm1
xorps %xmm2, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x20(%rax), %rax
movslq -0x60(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x6392
movq -0x68(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl 0x4(%rax), %edx
callq 0x4a80
movq -0x68(%rbp), %rax
movsd %xmm0, 0x28(%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x6432
movq -0x70(%rbp), %rdi
callq 0x22e0
movq -0x30(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, %rax
je 0x6449
movq -0x78(%rbp), %rdi
callq 0x22e0
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x6460
movq -0x80(%rbp), %rdi
callq 0x22e0
movq -0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x647d
movq -0x88(%rbp), %rdi
callq 0x22e0
movq -0x48(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x649a
movq -0x90(%rbp), %rdi
callq 0x22e0
movq -0x50(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x64b7
movq -0x98(%rbp), %rdi
callq 0x22e0
addq $0xa0, %rsp
popq %rbp
retq