我正在编写一个自定义损失函数,其中损失= y_true-conv2D(y_pred,filter_matrix)。滤镜矩阵的大小与y_pred相同,我想将y_pred中的第一幅图像与矩阵中的第一个滤镜进行卷积。我需要帮助弄清楚如何传递此矩阵并正确执行卷积(图像卷积而不是图层)。
我发现这篇帖子conv2d in custom Keras loss function中,用户仅对所有图像使用一个内核。
我正在使用MNIST数据集,因此是数据的宽度和高度的大小。这是我的损失函数。文件train1只有一行,但理想情况下,我希望它的大小与y_true和y_pred相同。
<div class="content-wrapper list-page">
<div class="content">
<div class="section-wrapper page-full-width">
<div class="section page-centered">
<div class="filter-bar"><span class="medium-utility-label filter-by-label">Filter by:</span>
<div role="button" class="filter-button-wrapper" tabindex="0" aria-label="Filter by City: All" aria-haspopup="true" aria-expanded="false">
<div class="filter-button">City
<svg class="filter-button-caret icon icon-caret-down" width="16px" height="16px" viewBox="0 0 16 16">
<path d="M7.6741598,11.3413318 L8.03952616,11.7324251 L8.40489252,11.3413318 L14.8653664,4.42595055 L14.1346336,3.74328687 L7.6741598,10.6586682 L8.40489252,10.6586682 L1.86536636,3.65866816 L1.13463364,4.34133184 L7.6741598,11.3413318 Z M7.6741598,11.3413318" fill="#979797"></path>
</svg>
</div>
<div class="filter-popup">
<ul>
<li>
<a class="category-link selected" href="?" rel="nofollow">
<svg class="selected-filter-checkmark icon icon-checkmark" width="16px" height="16px" viewBox="0 0 16 16">
<path d="M6.2,14.4L0,8.2l2.5-2.5l3.5,3.5c0.1,0.1,0.2,0.1,0.2,0L13.5,2L16,4.5L6.2,14.4z"></path>
</svg>All</a>
</li>
<li><a class="category-link" href="?location=Chicago%2C%20Illinois" rel="nofollow">Chicago, Illinois</a>
</li>
<li><a class="category-link" href="?location=Edmonton%2C%20Alberta%2C%20Canada" rel="nofollow">Edmonton, Alberta, Canada</a>
</li>
当然,当我选择包含更多条目的文件时,就会出现此错误。
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
如果您知道更好的方法,请告诉我。