【问题标题】:Adding multiple softmax classifiers to TensorFlow example向 TensorFlow 示例添加多个 softmax 分类器
【发布时间】:2017-04-26 20:06:18
【问题描述】:

我从通用 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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 由于某种原因它不起作用。我尝试重新训练模型,他们的输出仍然是softmax kind。 Tensorboard 还在图表中不断显示softmax(见截图)
    猜你喜欢
    • 2023-03-16
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多