我正在使用layers API,我想预先训练我的权重,以便为对网络输出值有一定要求的损失函数创建更好的初始化值。你知道吗
我正在对atm进行一些生成的数据的预训练,这些数据的随机值在我想要输出的范围内。它是通过使用一个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)]]
我不知道是不是因为Adam中的张量不可能转换损失函数,可能需要另一个形状或者别的什么。你知道吗
如果有人知道,请告诉我。你知道吗
自定义损失:
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
目前没有回答
相关问题 更多 >
编程相关推荐