我需要定义自己的损失函数,我使用的是 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