2017-06-20 77 views
1

我想用Tensorflow來分類一些對象表示。我用同樣的結構如在Tensorflow CIFAR-10例如,具有被定義爲最後一個層:從logits獲取概率 - logits和標籤大小不一

with tf.variable_scope('sigmoid_linear') as scope: 
    weights = _variable_with_weight_decay('weights', [192, num_classes], 
               stddev=1/192.0, wd=0.0) 
    biases = _variable_on_cpu('biases', [num_classes], 
            initializer) 
    sigmoid_linear = tf.add(tf.matmul(local4, weights), biases, name=scope.name) 
    _activation_summary(sigmoid_linear) 

return sigmoid_linear 

在我的情況,num_classes2,並供給到神經網絡的通道中的代表性量爲8.此外,我目前只用5個例子進行調試。最後一層的輸出形狀爲[40,2]。我預計第一維是由於5 examples * 8 channels,第二維由於類的數量。

爲了使用比較使用的例如logits和標籤, tensorflow.nn.SparseSoftmaxCrossEntropyWithLogits我需要他們有一個共同的形狀。如何解釋當前形狀中的當前logits內容,以及如何將logits的第一維減至num_classes

編輯:推理函數的輸入形狀的形狀爲[5,101,1008,8]。推理功能定義爲:

def inference(representations): 
    """Build the model. 
    Args: 
    STFT spectra: spectra returned from distorted_inputs() or inputs(). 
    Returns: 
    Logits. 
    """  
    # conv1 
    with tf.variable_scope('conv1') as scope: 
     kernel = _variable_with_weight_decay('weights', 
                shape=[5, 5, nChannels, 64], 
                stddev=5e-2, 
                wd=0.0) 
     conv = tf.nn.conv2d(representations, kernel, [1, 1, 1, 1], padding='SAME') 
     biases = _variable_on_cpu('biases', [64], initializer, 
           ) 
     pre_activation = tf.nn.bias_add(conv, biases) 
     conv1 = tf.nn.relu(pre_activation, name=scope.name) 
     _activation_summary(conv1) 

    # pool1 
    pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], 
          padding='SAME', name='pool1') 
    # norm1 
    norm1 = tf.nn.lrn(pool1, 4, bias=1.0, alpha=0.001/9.0, beta=0.75, 
         name='norm1') 

    # conv2 
    with tf.variable_scope('conv2') as scope: 
     kernel = _variable_with_weight_decay('weights', 
                shape=[5, 5, 64, 64], 
                stddev=5e-2, 
                wd=0.0) 
     conv = tf.nn.conv2d(norm1, kernel, [1, 1, 1, 1], padding='SAME') 
     biases = _variable_on_cpu('biases', [64], initializer) 
     pre_activation = tf.nn.bias_add(conv, biases) 
     conv2 = tf.nn.relu(pre_activation, name=scope.name) 
     _activation_summary(conv2) 

    # norm2 
    norm2 = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.001/9.0, beta=0.75, 
         name='norm2') 
    # pool2 
    pool2 = tf.nn.max_pool(norm2, ksize=[1, 3, 3, 1], 
          strides=[1, 2, 2, 1], padding='SAME', name='pool2') 

    # local3 
    with tf.variable_scope('local3') as scope: 
     # Move everything into depth so we can perform a single matrix multiply. 
     reshape = tf.reshape(pool2, [batch_size, -1]) 
     dim = reshape.get_shape()[1].value 
     weights = _variable_with_weight_decay('weights', shape=[dim, 384], 
                stddev=0.04, wd=0.004) 
     biases = _variable_on_cpu('biases', [384], initializer) 
     local3 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name=scope.name) 
     _activation_summary(local3) 

    # local4 
    with tf.variable_scope('local4') as scope: 
     weights = _variable_with_weight_decay('weights', shape=[384, 192], 
                stddev=0.04, wd=0.004) 
     biases = _variable_on_cpu('biases', [192], initializer) 
     local4 = tf.nn.relu(tf.matmul(local3, weights) + biases, name=scope.name) 
     _activation_summary(local4) 


    with tf.variable_scope('sigmoid_linear') as scope: 
     weights = _variable_with_weight_decay('weights', [192, num_classes], 
                stddev=1/192.0, wd=0.0) 
     biases = _variable_on_cpu('biases', [num_classes], 
             initializer) 
     sigmoid_linear = tf.add(tf.matmul(local4, weights), biases, name=scope.name) 
     _activation_summary(sigmoid_linear) 

    return sigmoid_linear 

回答

0

經過更多的調試後,我發現問題。最初來自Tensorflow教程的帶有圖層的發佈代碼運行良好(當然是這樣)。我在每一層之後打印所有形狀,並且發現編號40不是由於5 examples * 8 channels,而是我之前設置的batch_size = 40,因此也高於訓練實例的數量。在local layer 3重塑後開始不匹配。這個問題現在可以結束了。

相關問題