Tensorflow中用VGG19做二分类-loss出现0.693174解决方法

在Tensorflow下用VGG19 pre-train的model跑一个人脸表情库,做一个二分类。
出现loss除了迭代的第一个值,其余输出均是0.693174
百度解决方法,发现需要在全连接层的权重加权重衰减(l2正则化)
在把权重衰减loss和交叉熵loss添加到总loss里。
Tensorflow中具体实施如下:

    flattened_shape = 8 * 8 * 512
    reshp = tf.reshape(pool5, [-1, flattened_shape],       name='resh1')
    w_fc1 = utils.weight_variable([flattened_shape, 4096])
    # 加正则化
    regularizer = contrib.layers.l2_regularizer(REGULARIZATION_RATE)
    wloss_w1 = regularizer(w_fc1)
    tf.add_to_collection('losses', wloss_w1)

    w_fc2 = utils.weight_variable([4096, 4096])
    wloss_w2 = regularizer(w_fc2)
    tf.add_to_collection('losses', wloss_w2)  
    w_fc3 = utils.weight_variable([4096, NUM_OF_CLASSESS])
    wloss_w3 = regularizer(w_fc3)
    tf.add_to_collection('losses', wloss_w3)

在后面主函数里:

    loss = tf.reduce_mean(
                                tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
                                       labels=annotation,
                                       name="entropy"))
    tf.add_to_collection('losses', loss)
    all_loss = tf.add_n(tf.get_collection('losses'), name='total_loss')

你可能感兴趣的:(Tensorflow中用VGG19做二分类-loss出现0.693174解决方法)