def reconstruct(rbm_struct, test_image):

    pre_sigmoid_activation_h = np.dot(rbm_struct.weights,
                                      test_image)  #+  rbm_struct.ci
    sigmoid_activation_h = function_logistic(pre_sigmoid_activation_h)

    pre_sigmoid_activation_v = np.dot(rbm_struct.weights[:, 785:795].T,
                                      sigmoid_activation_h)  #+ rbm_struct.bj
    sigmoid_activation_v = function_logistic(pre_sigmoid_activation_v)

    return sigmoid_activation_v
Exemplo n.º 2
0
def function_feed_forward(inputData, backPropStruct):
    #Create activation function for inputNodes
    backPropStruct.Y_i[1:] = inputData

    #Compute hidden activation function from input layer
    backPropStruct.V_j = np.dot(backPropStruct.W_ji, backPropStruct.Y_i)
    backPropStruct.Y_j[1:] = function_logistic(backPropStruct.V_j[1:])

    #Compute output activation function from hidden layer
    backPropStruct.V_k = np.dot(backPropStruct.W_kj, backPropStruct.Y_j)
    backPropStruct.Y_k = function_logistic(backPropStruct.V_k)

    return backPropStruct
def get_reconstruction_cross_entropy(rbm_struct):

    pre_sigmoid_activation_h = np.tensordot(rbm_struct.visible_states.T,
                                            rbm_struct.weights[:, :, :], 1).T
    #pre_sigmoid_activation_h =  np.dot(rbm_struct.weights , rbm_struct.visible_states)
    sigmoid_activation_h = function_logistic(pre_sigmoid_activation_h)

    pre_sigmoid_activation_v = np.dot(rbm_struct.weights.T,
                                      sigmoid_activation_h) + rbm_struct.bj
    sigmoid_activation_v = function_logistic(pre_sigmoid_activation_v)

    cross_entropy = -np.mean(
        np.sum(
            rbm_struct.visible_states * np.log(sigmoid_activation_v) +
            (1 - rbm_struct.visible_states) * np.log(1 - sigmoid_activation_v),
            axis=1))

    return cross_entropy
def get_y_given_h(rbm_struct, x_i_input_image, h0_sample):

    a = np.tensordot(x_i_input_image.T, rbm_struct.weights[:, :, :], 1).T
    b = np.tensordot(h0_sample, a, 1)
    p_v_c_h_k = function_logistic(b + rbm_struct.bj)
    sampled_p_v_c_h_k = rbm_struct.rng.binomial(size=p_v_c_h_k.shape,
                                                n=1,
                                                p=p_v_c_h_k)

    return [p_v_c_h_k, sampled_p_v_c_h_k]
def get_h_given_v(rbm_struct, x_i_input_image, y_j_output_image):

    a = np.tensordot(
        np.tensordot(x_i_input_image.T, rbm_struct.weights[:, :, :], 1).T,
        y_j_output_image)
    a = function_reshape_arr(a, rbm_struct.h_val, 1, 0)
    p_h_c_v_k = function_logistic(a + rbm_struct.ci)
    p_h_c_v_k = function_reshape_arr(p_h_c_v_k, rbm_struct.h_val, 0, 0)
    sampled_p_h_c_v_k = rbm_struct.rng.binomial(size=p_h_c_v_k.shape,
                                                n=1,
                                                p=p_h_c_v_k)

    return [p_h_c_v_k, sampled_p_h_c_v_k]
def get_h_given_v(rbm_struct, x_i_input_image, y_j_output_image):

    a = np.tensordot(
        np.tensordot(x_i_input_image.T, rbm_struct.weights[:, :, :], 1).T,
        y_j_output_image)

    p_h_c_v_k = function_logistic(a)
    sampled_p_h_c_v_k = rbm_struct.rng.binomial(
        size=p_h_c_v_k.shape,  # discrete: binomial
        n=1,
        p=p_h_c_v_k)

    return [p_h_c_v_k, sampled_p_h_c_v_k]