Tensorflow 在keras损失函数中使用点函数时的不同形状
我试图在keras中使用自定义损失:Tensorflow 在keras损失函数中使用点函数时的不同形状,tensorflow,keras,loss-function,Tensorflow,Keras,Loss Function,我试图在keras中使用自定义损失: from tensorflow.keras import backend as K def IoULoss(targets, inputs, smooth=1e-6): #flatten label and prediction tensors inputs = K.flatten(inputs) targets = K.flatten(targets) intersection = K.sum(K.dot(targets,
from tensorflow.keras import backend as K
def IoULoss(targets, inputs, smooth=1e-6):
#flatten label and prediction tensors
inputs = K.flatten(inputs)
targets = K.flatten(targets)
intersection = K.sum(K.dot(targets, inputs))
total = K.sum(targets) + K.sum(inputs)
union = total - intersection
IoU = (intersection + smooth) / (union + smooth)
return 1 - IoU
...
model.compile(loss=IoULoss, optimizer=Adam())
...
model.fit(img_train, img_gt, batch_size=2, epochs=epochs)
img_train
是一个具有形状(N,512,512,3)
的numpy数组img\u gt
是一个具有形状(N,512,512,1)
的numpy数组。有了标准的categorical\u crossentropy
loss,一切都正常运行,没有崩溃。但当我尝试使用自定义IoULoss时,我犯了一个错误:
ValueError: Shape must be rank 2 but is rank 1 for '{{node IoULoss/MatMul}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false](IoULoss/Reshape_1, IoULoss/Reshape)' with input shapes: [?], [?].
IoULoss
中的目标和输入开始时具有形状(无、512、512、1)
这里可能有什么问题