Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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
Python 使用一个损失函数预训练权重,然后将损失函数更改为自定义函数_Python_Python 3.x_Machine Learning_Tensorflow_Neural Network - Fatal编程技术网

Python 使用一个损失函数预训练权重,然后将损失函数更改为自定义函数

Python 使用一个损失函数预训练权重,然后将损失函数更改为自定义函数,python,python-3.x,machine-learning,tensorflow,neural-network,Python,Python 3.x,Machine Learning,Tensorflow,Neural Network,我正在使用layers API,我想预先训练我的权重,以便为损失函数创建更好的初始化值,该函数对网络的输出值有一些要求 我正在对atm进行一些生成的数据的预训练,这些数据的随机值在我希望输出给我的范围内。它仅通过使用MSE损耗进行训练 但是,在我对网络进行预培训后,我希望更改损失函数,当然还有标签,但是标签不是问题。当我更改te loss函数并尝试再次运行网络时,我收到一个错误: InvalidArgumentError: TensorArray has inconsistent shapes.

我正在使用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)]]

更改前后的损失函数是什么?更改前后的MSE和基于YOLO对象分割的自定义损失如何更改损失函数?看起来您的图形可能无法正确构建。我使用新函数覆盖loss函数,并使用新的loss重新运行优化器。也许我应该为另一个损失函数创建另一条管道,而不是覆盖旧的管道?我不认为更改损失函数会导致错误,您应该正确检查新定义的损失,如果您在这里发布,我们可以提供更多帮助。更改前后的损失函数是什么?更改前后的MSE和基于YOLO对象分割的自定义损失如何更改损失函数?看起来您的图形可能无法正确构建。我使用新函数覆盖loss函数,并使用新的loss重新运行优化器。也许我应该为另一个损失函数创建另一个管道,而不是覆盖旧的管道?我不认为更改损失函数会导致错误,您应该正确检查新定义的损失,如果您将其发布在此处,我们可以提供更多帮助。
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