“具有dtype float32的张量的张量转换请求dtype float64”

时间:2018-07-23 15:22:07

标签: tensorflow keras backend loss-function

我正在尝试使用keras后端实现自己的损失功能。 我的损失函数受本文启发:https://www.sciencedirect.com/science/article/pii/S0167865517301666并计算科恩Kappa得分

    eye = K.eye(self.number_of_classes)
    n=2
    class_id = 0
    N = K.sum(((K.argmax(y_true)-class_id)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id])
    class_id = 1
    N += K.sum(((K.argmax(y_true)-class_id)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id])
    class_id = 2
    N += K.sum(((K.argmax(y_true)-class_id)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id])
    # class_id = 3
    # N += K.sum(((K.argmax(y_true)-class_id)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id])

    class_id = 0
    N_hat = K.sum(y_true*eye[class_id])/K.sum(y_true)
    class_id_2 = 0
    D = K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    class_id_2 = 1
    D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    class_id_2 = 2
    D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    # class_id_2 = 3
    # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])

    class_id = 1
    N_hat = K.sum(y_true*eye[class_id])/K.sum(y_true)
    class_id_2 = 0
    D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    class_id_2 = 1
    D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    class_id_2 = 2
    D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    # class_id_2 = 3
    # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])

    class_id = 2
    N_hat = K.sum(y_true*eye[class_id])/K.sum(y_true)
    class_id_2 = 0
    D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    class_id_2 = 1
    D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    class_id_2 = 2
    D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    # class_id_2 = 3
    # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])

    # class_id = 3
    # N_hat = K.sum(y_true*eye[class_id])/K.sum(y_true)
    # class_id_2 = 0
    # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    # class_id_2 = 1
    # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    # class_id_2 = 2
    # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
    # # class_id_2 = 3
    # # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])

    return(K.log(K.mean(N/D)))

在使用由LSTM层和两个Dense层构建的简单LSTM架构时,我的功能运行良好。

但是当我尝试添加输入卷积层以减少特征时,我遇到了这个错误:

  

ValueError:Tensor转换请求的dtype float32的Tensor为dtype float64:'Tensor(“ loss / dense_2_loss / strided_slice:0”,shape =(?,),dtype = float32)'

顺便指出了第四行。如Keras custom loss function dtype error中所建议 我尝试将y_truey_pred强制转换为float64-并没有改变。 然后,我尝试将其强制转换为float64 y_pred[:, class_id],但它只是更改了错误所指向的行。 每次我更正错误时,都会出现另一个错误,直到它指向此行:

  

/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in weighted(y_true,y_pred,weights,mask)451#如果权重不是,则应用样本权重452无:- > 453 score_array * =权重454 score_array / = K.mean(K.cast(K.not_equal(weights,0),K.floatx()))455 return K.mean(score_array)

有人有想法吗?我强调这样一个事实,我的损失函数在添加Conv2D之前可以正常工作。

谢谢!

PS:如果您有任何线索可以使我的功能更加清晰,而无需后端支持任何循环。

0 个答案:

没有答案