Python 3.x 通过迁移学习为《盗梦空间》增加两个附加层

Python 3.x 通过迁移学习为《盗梦空间》增加两个附加层,python-3.x,tensorflow,deep-learning,Python 3.x,Tensorflow,Deep Learning,我试图通过编辑retain.py脚本,在softmax层之前向inception模型添加另一个FC层。(tensorflow\examples\image\u retaining\retain.py) 因此,输入将从2048变为1024,而不仅仅是2048 因此,权重应为: (20481024) (1024,类的数量) 下面是我添加额外图层的尝试。我无法测试它 目前,我只想确保我做得正确。 我特别不确定交叉熵部分,因为我希望它使用 logits在其自身范围内。 我对tensorflow的工作原理

我试图通过编辑retain.py脚本,在softmax层之前向inception模型添加另一个FC层。(tensorflow\examples\image\u retaining\retain.py)

因此,输入将从2048变为1024,而不仅仅是2048

因此,权重应为:

(20481024) (1024,类的数量)

下面是我添加额外图层的尝试。我无法测试它 目前,我只想确保我做得正确。 我特别不确定交叉熵部分,因为我希望它使用
logits
在其自身范围内。 我对tensorflow的工作原理还不是很熟悉

def add_final_training_ops(class_count, final_tensor_name, bottleneck_tensor,
                           bottleneck_tensor_size):
 """

  Args:
    class_count: Integer of how many categories of things we're trying to
    recognize.
    final_tensor_name: Name string for the new final node that produces results.
    bottleneck_tensor: The output of the main CNN graph.
    bottleneck_tensor_size: How many entries in the bottleneck vector.

  Returns:
    The tensors for the training and cross entropy results, and tensors for the
    bottleneck input and ground truth input.
  """
  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 = 'second_to_final_fC_layer_ops'
  with tf.name_scope(layer_name):
    with tf.name_scope('weights'):
      initial_value = tf.truncated_normal(
          [bottleneck_tensor_size, 1024], stddev=0.001)

      layer_weights = tf.Variable(initial_value, name='final_weights')

      variable_summaries(layer_weights)
    with tf.name_scope('biases'):
      layer_biases = tf.Variable(tf.zeros([1024]), 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)
    with tf.name_scope('Relu_activation'):
      relu_activiated =tf.nn.relu(logits, name= 'Relu')
      tf.summary.histogram('final_relu_activation')


  layer_name = 'final_training_ops'
  with tf.name_scope(layer_name):
    with tf.name_scope('weights'):
      initial_value = tf.truncated_normal(
          [1024, class_count], stddev=0.001)

      layer_weights = tf.Variable(initial_value, 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(
        labels=ground_truth_input, logits=logits)
    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'):
    optimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate)
    train_step = optimizer.minimize(cross_entropy_mean)

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

谢谢(以防万一@mrry)。

通过快速检查代码,我没有看到任何看起来疯狂的东西,但如果遇到问题,请务必更新此内容。@PeteWarden它似乎工作正常(只需要一些调整)。但是,我似乎找不到日志文本文件(甚至在添加图层之前)。我甚至在这里发了一条帖子,但我想我读到了windows系统上可能有一个bug?通过对代码的快速检查,我没有看到任何看起来疯狂的东西,但是如果遇到问题,请更新它。@PeteWarden它似乎工作得很好(只需要一些调整)。但是,我似乎找不到日志文本文件(甚至在添加图层之前)。我甚至在这里发了一条帖子,但我想我读到了windows系统上可能有一个bug?