本文介绍了Tensorflow 自伴随特征分解不成功,输入可能无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在带有 Tensorflow 后端的 Keras 中定义一个自定义损失函数,该函数仅使用预测的 y 值,而不管真实值.图编译成功,但在训练开始时返回异常:InvalidArgumentError(回溯见上文):自伴随特征分解不成功.输入可能无效. 我尝试用随机虚拟数据替换我的数据,但它产生相同的异常.

I want to define a custom loss function in Keras with Tensorflow backend which uses only the predicted y values, regardless of the true ones. The graph compiles successfully, but at the start of the training it returns an exception: InvalidArgumentError (see above for traceback): Self-adjoint eigen decomposition was not successful. The input might not be valid. I have tried replacing my data with random dummy data, but it produces the same exception.

我的损失定义的完整代码可以在下面找到.为什么输入到tf.self_adjoint_eig 无效?

My full code of the loss definition can be found below. Why is the input to the tf.self_adjoint_eig not valid?

def model_correlation_loss(representation_size, k_singular_values):
    global batch_size

    def keras_loss(y_true, y_pred):
        global batch_size

        regularization_constant_1 = regularization_constant_2 = 1e-4
        epsilon = 1e-12

        o1 = o2 = int(y_pred.shape[1] // 2)

        h_1 = y_pred[:, 0:o1]
        h_2 = y_pred[:, o1:o1+o2]

        h_1 = tf.transpose(h_1)
        h_2 = tf.transpose(h_2)

        m = tf.shape(h_1)[1]

        centered_h_1 = h_1 - tf.cast(tf.divide(1, m),  tf.float32) * tf.matmul(h_1, tf.ones(shape=(m, m)))
        centered_h_2 = h_2 - tf.cast(tf.divide(1, m),  tf.float32) * tf.matmul(h_2, tf.ones(shape=(m, m)))

        sigma_hat_12 = tf.cast(tf.divide(1, m - 1),  tf.float32) * tf.matmul(centered_h_1, tf.transpose(centered_h_2))
        sigma_hat_11 = tf.cast(tf.divide(1, m - 1),  tf.float32) * tf.matmul(centered_h_1, tf.transpose(centered_h_1)) + regularization_constant_1 * tf.eye(num_rows=o1)
        sigma_hat_22 = tf.cast(tf.divide(1, m - 1),  tf.float32) * tf.matmul(centered_h_2, tf.transpose(centered_h_2)) + regularization_constant_2 * tf.eye(num_rows=o2)

        w_1, v_1 = tf.self_adjoint_eig(sigma_hat_11)
        w_2, v_2 = tf.self_adjoint_eig(sigma_hat_22)

        zero = tf.constant(False, dtype=tf.bool)

        idx_pos_entries_1 = tf.where(tf.equal(tf.greater(w_1, epsilon), True))
        idx_pos_entries_1 = tf.reshape(idx_pos_entries_1, [-1, tf.shape(idx_pos_entries_1)[0]])[0]

        w_1 = tf.gather(w_1, idx_pos_entries_1)
        v_1 = tf.gather(v_1, idx_pos_entries_1)

        idx_pos_entries_2 = tf.where(tf.equal(tf.greater(w_2, epsilon), True))
        idx_pos_entries_2 = tf.reshape(idx_pos_entries_2, [-1, tf.shape(idx_pos_entries_2)[0]])[0]
        w_2 = tf.gather(w_2, idx_pos_entries_2)
        v_2 = tf.gather(v_2, idx_pos_entries_2)

        sigma_hat_rootinvert_11 = tf.matmul(tf.matmul(v_1, tf.diag(tf.sqrt(w_1))), tf.transpose(v_1))
        sigma_hat_rootinvert_22 = tf.matmul(tf.matmul(v_2, tf.diag(tf.sqrt(w_2))), tf.transpose(v_2))

        t_matrix = tf.matmul(tf.matmul(sigma_hat_rootinvert_11, sigma_hat_12), sigma_hat_rootinvert_22)

        if k_singular_values == representation_size:    # use all
            correlation = tf.sqrt(tf.trace(tf.matmul(K.transpose(t_matrix), t_matrix)))

        return correlation

    return keras_loss

推荐答案

下面是 Wang 在他的网站上提供的计算损失函数的 tf 代码:

Here's the tf code provided by Wang on his website for computing the loss function:

def CCA_loss(H1, H2, N, d1, d2, dim, rcov1, rcov2):
    # Remove mean.
    m1 = tf.reduce_mean(H1, axis=0, keep_dims=True)
    H1 = tf.subtract(H1, m1)

    m2 = tf.reduce_mean(H2, axis=0, keep_dims=True)
    H2 = tf.subtract(H2, m2)

    S11 = tf.matmul(tf.transpose(H1), H1) / (N-1) + rcov1 * tf.eye(d1)
    S22 = tf.matmul(tf.transpose(H2), H2) / (N-1) + rcov2 * tf.eye(d2)
    S12 = tf.matmul(tf.transpose(H1), H2) / (N-1)

    E1, V1 = tf.self_adjoint_eig(S11)
    E2, V2 = tf.self_adjoint_eig(S22)

    # For numerical stability.
    idx1 = tf.where(E1>eps_eig)[:,0]
    E1 = tf.gather(E1, idx1)
    V1 = tf.gather(V1, idx1, axis=1)

    idx2 = tf.where(E2>eps_eig)[:,0]
    E2 = tf.gather(E2, idx2)
    V2 = tf.gather(V2, idx2, axis=1)

    K11 = tf.matmul( tf.matmul(V1, tf.diag(tf.reciprocal(tf.sqrt(E1)))), tf.transpose(V1))
    K22 = tf.matmul( tf.matmul(V2, tf.diag(tf.reciprocal(tf.sqrt(E2)))), tf.transpose(V2))
    T = tf.matmul( tf.matmul(K11, S12), K22)

    # Eigenvalues are sorted in increasing order.
    E2, U = tf.self_adjoint_eig(tf.matmul(T, tf.transpose(T)))

    return tf.reduce_sum(tf.sqrt(E2[-dim:]))

这篇关于Tensorflow 自伴随特征分解不成功,输入可能无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 15:29