我正在使用图层API,我想预先训练我的权重,以便为损失函数创建更好的初始值,该函数对网络的输出值有一些要求。
我预先训练了一些生成的数据,这些数据在我希望输出给我的范围内具有随机值。它仅通过使用MSE损失进行训练。
然而,在我预先训练网络之后,我想要改变丢失功能,并且还要导致标签,但是标签不是问题。当我更改te loss功能并尝试再次运行网络时出现错误:
InvalidArgumentError: TensorArray has inconsistent shapes.
Index 0 has shape: [1] but index 1 has shape: []
[[Node: map/TensorArrayStack/TensorArrayGatherV3 =
TensorArrayGatherV3[_class=["loc:@map/TensorArray_1"],
dtype=DT_FLOAT, element_shape=<unknown>, _device="/job:localhost/replica:0/task:0/cpu:0"]
(map/TensorArray_1, map/TensorArrayStack/range, map/while/Exit_1)]]
我不知道由于亚当的某些张量而无法切换损失函数,可能需要另外的形状或其他东西。
如果有人知道请赐教。
自定义丢失:
def total_loss(y_true, y_pred):
def single_loss(total_index):
def iou_fn(box, y_true_x, y_true_w):
box_x, box_w = box[0], box[1]
left = tf.maximum(y_true_x-y_true_w/2., box_x-box_w/2.)
right = tf.minimum(y_true_x+y_true_w/2., box_x+box_w/2.)
overlap = right - left
intersection = tf.maximum(overlap*1., 0.)
union = y_true_w*1. + box_w*1. - intersection
return (intersection*1.) / (union*1.)
cell_size = 16
box_count = 5
cell_count = 10
y_true_single = y_true[total_index]
y_pred_single = y_pred[total_index]
coord = tf.constant(5., name='coord')
noobj = tf.constant(0.5, name='noobj')
x_error = 0.
w_error = 0.
c_error = 0.
c_noobj_error = 0.
class_error = 0.
y_true_x, y_true_w, y_true_class = tf.split(y_true_single[1:4], 3, axis=0)
for cell_index in range(cell_count):
cell = y_pred_single[cell_index*cell_size:(cell_index+1)*cell_size]
boxes = cell[0:-1]
_class = cell[-1]
boxes_split = tf.split(boxes, box_count, axis=0)
ious = []
for box_index in range(box_count):
ious.append(iou_fn(boxes_split[box_index], y_true_x, y_true_w))
index = tf.argmax(ious, output_type=tf.int32)
x_error = tf.cond((tf.gather(ious, tf.argmax(ious, output_type=tf.int32))[0]>0.)[0],
lambda: tf.add(x_error, (tf.square(tf.gather(boxes_split, index)[0][0]-y_true_x[0]))),
lambda: x_error)
w_error = tf.cond((tf.gather(ious, tf.argmax(ious, output_type=tf.int32))[0]>0.)[0],
lambda: tf.add(w_error, (tf.square(tf.gather(boxes_split, index)[0][1]-y_true_w[0]))),
lambda: w_error)
c_error = tf.cond((tf.gather(ious, tf.argmax(ious, output_type=tf.int32))[0]>0.)[0],
lambda: tf.add(c_error, (tf.square(tf.gather(ious, index)[0][0]))),
lambda: c_error)
class_error = tf.cond((tf.gather(ious, index)[0]>0.)[0],
lambda: tf.add(class_error, tf.square(_class-y_true_class)),
lambda: class_error)
for box_index in range(box_count):
c_noobj_error = tf.cond((tf.gather(ious, index)[0]>0.)[0],
lambda: tf.cond(tf.equal(box_index, index[0]),
lambda: c_noobj_error,
lambda: tf.add(c_noobj_error, tf.square(tf.gather(ious, index)[0]))),
lambda: c_noobj_error)
loss = x_error*coord + w_error*coord + c_error + c_noobj_error*noobj + class_error
return loss
full_return = tf.map_fn(single_loss, tf.range(tf.shape(y_pred)[0]), dtype=tf.float32)
return full_return