自定义Keras损失函数中多滤波器的conv2d

2024-09-29 23:28:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个自定义的loss函数,其中loss=y\u true-conv2D(y\u pred,filter\u matrix)。滤波器矩阵的大小与y\u pred相同,我想用矩阵中的第一个滤波器卷积y\u pred中的第一个图像。我需要帮助弄清楚如何通过这个矩阵和执行适当的卷积(图像卷积不是层)。你知道吗

我发现这篇文章conv2d in custom Keras loss function,其中用户对所有图像只使用一个内核。你知道吗

我使用的是MNIST数据集,因此是数据宽度和高度的大小。这是我的损失函数。文件列1只有一行,但理想的情况下,我希望它是相同的大小作为你真和你pred。你知道吗

def custom_loss(y_true, y_pred):
    filename = 'train1.csv'
    raw_data = open(filename, 'rt')
    filters_train = np.loadtxt(raw_data, delimiter=",")
    filters_train = filters_train.astype('float32')
    filters_train = np.reshape(filters_train, (28, 28, 1, 1))
    predlap = k.conv2d(y_pred, filters_train)
    predlap = (predlap-k.min(predlap))/(k.max(predlap)-k.min(predlap))
    loss = losses.mean_squared_error(y_true, predlap)
    return loss

当然,当我选择包含更多条目的文件时,会出现这个错误。你知道吗

ValueError: cannot reshape array of size 47040000 into shape (28,28,1,1)

如果你知道更好的方法,请告诉我。你知道吗


Tags: 文件数据函数图像truecustomtrain矩阵

热门问题