训练时权重不更新只会改变偏差

2024-04-25 17:00:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用经过修改的预先训练的VGG16来对医疗设备进行分类图像。在训练中,权重没有变化,每一步仅偏倚变化0.001左右,损失仅在第一步发生变化,之后维持一个巨大的数字。你知道吗

批次大小=8,标签固定为[1,1,1,1,0,0,0,0]迭代.图像形状:(182182,2)。输入数据被标准化为0-1。我试图将学习率从10^(-5)修改为0.1,没有改变了。全部可训练变量被初始化为非零值,我把中间结果打印出来,没有找到任何非索引或inf价值观输出8的结果总是相同的相当大的,例如[8.2265,8.2265,…,8.2265],并且损失总是在23左右,在第一步之后从不下降。你知道吗

这是我的代码:

一部分训练.py你知道吗

def train():

    x = tf.placeholder(tf.float32, [None, 182, 182, 2], name = 'image_input')
    y_ = tf.placeholder(tf.float32, [None, 8], name='label_input')  
    global_step = tf.Variable(0, trainable=False)  
    learning_rate = tf.train.exponential_decay(learning_rate=0.0001,
                                               decay_rate=0.9,
                                               global_step=TRAINING_STEPS,
                                               decay_steps=50,
                                               staircase=True)
    # read the image dataset
    # pos refers to the image with label '1', neg : label'0'
    pos, neg = get_data.get_image(img_path)
    label_batch = np.reshape(np.array([1.0, 1.0, 1.0, 1.0,  0.0, 0.0, 0.0, 0.0]),[1, 8])

    vgg = vgg16.Vgg16()
    vgg.build(x)

    #see the loss function definition below 
    loss = vgg.side_loss( y_,vgg.output1, vgg.output2, vgg.output3, vgg.output4)

    train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=global_step)
    init_op = tf.global_variables_initializer()

    saver = tf.train.Saver()

    with tf.device('/gpu:0'):
        with tf.Session() as sess:
            sess.run(init_op)

            for i in range(TRAINING_STEPS):

                #batch_size = 4,defined in other area of train.py
                start = i * batch_size
                end = start + batch_size 

                #creating image_batch
                #the first four images are from pos,and then 4 from neg
                image_list = []
                image_list.append(pos[start:end])
                image_list.append(neg[start:end])
                image_batch = np.reshape(np.array(image_list),[-1,182,182,2])

                _,loss_val,step = sess.run([train_step,loss,global_step], feed_dict={x: image_batch,y_:label_batch})

                if i % 50 == 0:
                    print("the step is %d,loss is %f" % (step, loss_val))
                    if loss_val < min_loss:
                        min_loss = loss_val
                        saver.save(sess, 'ckpt/vgg.ckpt', global_step=2000)

损失函数的定义

class Vgg16:

    #a,b,c,d refers to the output in the model
    #my modified vgg model has 4 outputs
    def side_loss(self,yi,a,b,c,d):        
        self.loss1 = self.f_indicator(yi, a)
        self.loss2 = self.f_indicator(yi, b)
        self.loss3 = self.f_indicator(yi, c)
        self.loss4 = self.f_indicator(yi, d)
        self.loss_side = self.loss1 + self.loss2 + self.loss3 + self.loss4
        res_loss = tf.reduce_sum(self.loss_side)

        return res_loss

    #here is the definition of my loss function
    def f_indicator(self,yi,yj):

        b = tf.where(yj>=1,yj*50,tf.abs(tf.log(tf.abs(1 - yj))))
        res=tf.where(tf.equal(yi , 0.0), b,tf.abs(tf.log(tf.clip_by_value(yj, 1e-8, float("inf")))))

        return res

关于如何解决这个问题有什么建议吗?损失函数有什么问题吗?你知道吗


Tags: theimageselftfstepbatchtrainglobal