0
votes

After I define my loss function with keras+tensorflow ,the error occurs in the cource of training data.

I strongly suspect that the error caused by the shape of my matrixs

import theano.tensor as T import numpy as np import tensorflow as tf

def cca_loss(outdim_size, use_all_singular_values): """ The main loss function (inner_cca_objective) is wrapped in this function due to the constraints imposed by Keras on objective functions """ def inner_cca_objective(y_true, y_pred): """ It is the loss function of CCA as introduced in the original paper. There can be other formulations. It is implemented by Tensorflow tensor operations, and does not work on theano backend y_true is just ignored """ r1 = 1e-5 r2 = 1e-5 eps = 1e-10 o1 = o2 = y_pred.shape[1]//2

    H1 = y_pred[:, 0:o1]
    H2 = y_pred[:, o1:o1+o2]
    #o1=int(o1)
    #o2=int(o2)

    H1=tf.transpose(H1)
    H2=tf.transpose(H2)
    m=H1.shape[1]
    print(m)
    m=20
    print(H1.shape)
    H1bar = H1 - (1.0 / m) * tf.matmul(H1, tf.ones([m, m]))
    H2bar = H2 - (1.0 / m) * tf.matmul(H2, tf.ones([m, m]))

    SigmaHat12 = (1.0 / (m - 1)) * tf.matmul(H1bar, tf.transpose(H2bar))
    SigmaHat11 = (1.0 / (m - 1)) * tf.matmul(H1bar, tf.transpose(H1bar)) + r1 * tf.matrix_diag(tf.constant(value=np.ones([1,o1])[0],dtype=tf.float32))
    SigmaHat22 = (1.0 / (m - 1)) * tf.matmul(H2bar, tf.transpose(H2bar)) + r2 * tf.matrix_diag(tf.constant(value=np.ones([1,o2])[0],dtype=tf.float32))

    print(SigmaHat22.shape)
    # Calculating the root inverse of covariance matrices by using eigen decomposition

    D1, V1 = tf.linalg.eigh(SigmaHat11)
    D2, V2 = tf.linalg.eigh(SigmaHat22)


    posInd1 = tf.where(tf.greater(D1, eps))
    D1 = tf.gather_nd(D1, tf.where(tf.greater(D1, eps)))
    V1 = tf.transpose(tf.gather(tf.transpose(V1), tf.squeeze(posInd1, [1])))

    posInd2 = tf.where(tf.greater(D2, eps))
    D2 = tf.gather_nd(D2, tf.where(tf.greater(D2, eps)))
    V2 = tf.transpose(tf.gather(tf.transpose(V2), tf.squeeze(posInd2, [1])))

    SigmaHat11RootInv = tf.matmul(tf.matmul(V1, tf.matrix_diag(D1 ** -0.5)), tf.transpose(V1))
    SigmaHat22RootInv = tf.matmul(tf.matmul(V2, tf.matrix_diag(D2 ** -0.5)), tf.transpose(V1))

    Tval = tf.matmul(tf.matmul(SigmaHat11RootInv, SigmaHat12), SigmaHat22RootInv)

    if use_all_singular_values:
        print("true")
        # all singular values are used to calculate the correlation
        corr = tf.sqrt(tf.trace((tf.matmul(tf.transpose(Tval), Tval))))
    else:
        #just the top outdim_size singular values are used
        U, V = tf.linalg.eigh(tf.matmul(tf.transpose(Tval), Tval))
        U = tf.gather_nd(U, tf.where(tf.greater(U, eps)))
        print(U.shape)
        U = tf.nn.top_k(U).values
        U = tf.reverse(U, axis=[0])
        corr = tf.reduce_sum(tf.sqrt(U[0:outdim_size]))
    return -corr

return inner_cca_objective

tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found. (0) Invalid argument: Got info = 19 for batch index 0, expected info = 0. Debug_info = heevd [[{{node loss/concatenate_1_loss/SelfAdjointEigV2}}]] [[loss/add_5/_77]] (1) Invalid argument: Got info = 19 for batch index 0, expected info = 0. Debug_info = heevd [[{{node loss/concatenate_1_loss/SelfAdjointEigV2}}]] 0 successful operations. 0 derived errors ignored.

1

1 Answers

0
votes

Try reverting the tf version to 1.2. Let me know.