首页 > 解决方案 > ValueError:在回归问题上训练 VGG16 模型以生成边界框坐标时,使用序列设置数组元素

问题描述

我正在尝试对我的回归问题使用自定义 Huber 损失函数并训练 VGG16 模型来生成边界框坐标。代码如下:

def huber(true, pred, delta=1.0):
    loss = np.where(np.abs(true-pred) < delta , 0.5*((true-pred)**2), delta*np.abs(true - pred) - 0.5*(delta**2))
    return np.sum(loss)

...
...
...
model.compile(optimizer=Adam(lr=0.001), loss=huber)

运行脚本时,使用自定义 Huber 损失函数时出现错误:

model.compile(optimizer=Adam(lr=0.001), loss=huber)

  File "C:\Users\...\lib\site-packages\keras\engine\training.py", line 342, in compile
    sample_weight, mask)

  File "C:\Users\...\lib\site-packages\keras\engine\training_utils.py", line 404, in weighted
    score_array = fn(y_true, y_pred)

  File "C:\Users\...\train_code.py", line 157, in huber
    loss = np.where(np.abs(true-pred) < delta , 0.5*((true-pred)**2), delta*np.abs(true - pred) - 0.5*(delta**2))

  File "<__array_function__ internals>", line 6, in where

ValueError: setting an array element with a sequence.

标签: pythontensorflowkerasloss-function

解决方案


推荐阅读