Tensorflow损失函数以发现指标差异

问题描述

因此,我一直在研究卷积神经网络,该网络试图从时间序列数据中选择一行。我以一个密集层结束了我的网络,该层具有与数据行相同数量输出。然后,我的目标是仅使用tf.argmax(x)找出应选择的索引。

请注意,我的Y数据是网络应选择的索引。

我的问题是,在损失函数中使用argmax会破坏其创建渐变的能力,我不确定如何解决这个问题。我具体收到此错误

ValueError: No gradients provided for any variable: ['batch_normalization/gamma:0','batch_normalization/beta:0','conv1d/kernel:0','conv1d/bias:0','batch_normalization_1/gamma:0','batch_normalization_1/beta:0','conv1d_1/kernel:0','conv1d_1/bias:0','batch_normalization_2/gamma:0','batch_normalization_2/beta:0','conv1d_2/kernel:0','conv1d_2/bias:0','batch_normalization_3/gamma:0','batch_normalization_3/beta:0','dense/kernel:0','dense/bias:0','batch_normalization_4/gamma:0','batch_normalization_4/beta:0','dense_1/kernel:0','dense_1/bias:0','batch_normalization_5/gamma:0','batch_normalization_5/beta:0','dense_2/kernel:0','dense_2/bias:0','batch_normalization_6/gamma:0','batch_normalization_6/beta:0','dense_3/kernel:0','dense_3/bias:0'].

这是我的损失函数供参考

def custom_loss_accuracy(y_true,y_pred):
    """This function tracks the overall accuracy of the guess
    aka how close the index of argmax() was vs the index it should produce
    
    Parameters:
    y_true (tensor): tensor of the true,or actual y values for the given protion of the dataset
    y_pred (tensor): tensor of the predicted y values for the given portion of the dataset
    
    Returns:
    float: Mean absolute value between the index of y_true and index of y_pred
    """
    return(tf.math.reduce_mean(tf.math.abs(tf.cast(tf.math.argmax(y_pred),tf.dtypes.float32) - y_true)))

任何人都知道如何在保持渐变的同时执行此操作,以便可以将其用于损失函数吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)