将多个softmax分类器添加到TensorFlow示例中

时间:2016-12-12 00:30:52

标签: neural-network tensorflow classification multilabel-classification softmax

我从通用的TensorFlow示例开始。

要对我的数据进行分类,我需要在最后一层使用多个标签(理想情况下是多个softmax分类器),因为我的数据带有多个独立标签(概率之和不是1)。

具体在retrain.py add_final_training_ops()中的这些行添加最终张量

final_tensor = tf.nn.softmax(logits, name=final_tensor_name)

在这里

cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
      logits, ground_truth_input)

TensorFlow中是否已存在通用分类器?如果没有,如何实现多级分类?

来自add_final_training_ops()

tensorflow/examples/image_retraining/retrain.py

def add_final_training_ops(class_count, final_tensor_name, bottleneck_tensor):

  with tf.name_scope('input'):
    bottleneck_input = tf.placeholder_with_default(
        bottleneck_tensor, shape=[None, BOTTLENECK_TENSOR_SIZE],
        name='BottleneckInputPlaceholder')

    ground_truth_input = tf.placeholder(tf.float32,
                                        [None, class_count],
                                        name='GroundTruthInput')

  layer_name = 'final_training_ops'
  with tf.name_scope(layer_name):
    with tf.name_scope('weights'):
      layer_weights = tf.Variable(tf.truncated_normal([BOTTLENECK_TENSOR_SIZE, class_count], stddev=0.001), name='final_weights')
      variable_summaries(layer_weights)
    with tf.name_scope('biases'):
      layer_biases = tf.Variable(tf.zeros([class_count]), name='final_biases')
      variable_summaries(layer_biases)
    with tf.name_scope('Wx_plus_b'):
      logits = tf.matmul(bottleneck_input, layer_weights) + layer_biases
      tf.summary.histogram('pre_activations', logits)

  final_tensor = tf.nn.softmax(logits, name=final_tensor_name)
  tf.summary.histogram('activations', final_tensor)

  with tf.name_scope('cross_entropy'):
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
      logits, ground_truth_input)
    with tf.name_scope('total'):
      cross_entropy_mean = tf.reduce_mean(cross_entropy)
  tf.summary.scalar('cross_entropy', cross_entropy_mean)

  with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(
        cross_entropy_mean)

  return (train_step, cross_entropy_mean, bottleneck_input, ground_truth_input,
          final_tensor)

即使在添加sigmoid分类器并进行再培训后,Tensorboard仍会显示softmax

Tensorboard with softmax

1 个答案:

答案 0 :(得分:0)

TensorFlow具有tf.nn.sigmoid_cross_entropy_with_logits用于独立的多标签分类。