【发布时间】:2017-12-23 17:57:39
【问题描述】:
我需要定义自己的损失函数,我使用的是 GAN 模型,我的损失将包括真实图像和生成图像之间的逆向损失和 L1 损失。
我试图写一个函数但是出现以下错误:
ValueError: ('Could not interpret loss function identifier:', Elemwise{add,no_inplace}.0)
我的损失函数是:
def loss_function(y_true, y_pred, y_true1, y_pred1):
bce=0
for i in range (64):
a = y_pred1[i]
b = y_true1[i]
x = K.log(a)
bce=bce-x
bce/=64
print('bce = ', bce)
for i in zip( y_pred, y_true):
img = i[0]
image = np.zeros((64,64),dtype=y_pred.dtype)
image = img[0,:,:]
image = image*127.5+127.5
imgfinal = Image.fromarray(image.astype(np.uint8))
img1 = i[1]
image1 = np.zeros((64,64), dtype=y_true.dtype)
image1 = img1[0,:,:]
image1 = image1*127.5+127.5
imgfinal1 = Image.fromarray(image1.astype(np.uint8))
diff = ImageChops.difference(imgfinal,imgfinal1)
h = diff.histogram()
sq = (value*((idx%256)**2) for idx, value in enumerate(h))
sum_of_squares = sum(sq)
lossr = math.sqrt(sum_of_squares/float(im1.size[0] * im1.size[1]))
loss = loss+lossr
loss /=(64*127)
print('loss = ', loss)
return x+loss
【问题讨论】:
-
你能把错误追溯到抛出它的那一行吗?
-
discriminator_on_generator.compile(loss = loss_function(y_true ,y_pred ,y_true1 ,y_pred1), optimizer=g_optim) - 这是抛出错误的行,这是我调用这个 loss_function 的地方。跨度>
-
您能否追踪到错误源自您的自定义损失函数内部的哪一行?
-
错误不是源自损失函数内部,但 keras 无法识别我的损失函数。
-
根据您的评论,我添加了一个答案,这也将有助于包含您在答案中提到的那行代码...我要编辑所以
标签: python machine-learning keras loss-function