Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 在keras损失函数中使用点函数时的不同形状_Tensorflow_Keras_Loss Function - Fatal编程技术网

Tensorflow 在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,

我试图在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, 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)

这里可能有什么问题