0

我有二進制輸出 - '1'或'0'。是否有必要將標籤編碼爲tersorflow中的一個熱點?

我不會把它編碼爲一熱(因爲我最初認爲沒有目的),當我運行我的網絡模型時,我得到了奇怪的結果 - 所有輸出都是'1',精度是〜 57%。

我覺得有什麼不對。

所以我的問題是:我們是否總是需要將標籤編碼爲一熱?如果是這樣,爲什麼(在二進制的情況下)

我的代碼:

{'class': tf.argmax(prediction, 1)建議,應該有多種輸出(如矢量),然後我們把一個元素與最大概率的載體 - 是這種理解是否正確?所以這讓我想,我也許應該輸出2個標籤二進制輸出...

而且,我試圖輸出的實際概率在該行

return {'class': prediction, 'prob': prediction}, loss, train_op 

,但似乎並沒有工作,和所有我得到最終爲[1 1 1 ... 1]

我CONV型號:

def my_conv_model(x, y): 

# 1. form a 4d tensor of shape N x 1 x N_FEATURES x 1 
x = tf.reshape(x, [-1, 1, N_FEATURES, 1]) 

########################################################################## 
##### Conv layer 1 ##### 
conv1 = tf.contrib.layers.convolution2d(inputs=x, 
             num_outputs=N_FILTERS, 
             kernel_size=[1, 7], 
             stride=[1, 1], 
             padding='VALID') 

# 3. Add a RELU for non linearity. 
conv1 = tf.nn.relu(conv1) 

# 4. Max pooling across output of Convolution+Relu. 
pool1 = tf.nn.max_pool(conv1, 
         ksize=[1, 1, 3, 1], 
         strides=[1, 1, 3, 1], 
         padding='SAME') 

########################################################################## 
##### Conv layer 2 ##### 
conv2 = tf.contrib.layers.convolution2d(inputs=pool1, 
             num_outputs=N_FILTERS, 
             kernel_size=[1, 7], 
             padding='VALID') 

pool2 = tf.nn.max_pool(conv2, 
         ksize=[1, 1, 2, 1], 
         strides=[1, 1, 2, 1], 
         padding='SAME') 

last_pool_layer = pool2 
last_pool_layer_shape = last_pool_layer.get_shape() 
n_cols = (last_pool_layer_shape[2] * last_pool_layer_shape[3]).value 
last_pool_layer = tf.reshape(last_pool_layer, [-1, n_cols]) 
fc_layer = tf.contrib.layers.fully_connected(inputs=pool2, 
            num_outputs=10, 
            activation_fn=tf.nn.relu) 

last_layer = fc_layer 
    try: 
     last_layer_shape = last_layer.get_shape() 
     print("last_layer_shape", last_layer_shape) 
     last_layer = tf.reshape(last_layer, [-1, (last_layer_shape[2] * last_layer_shape[3]).value]) 
     print("last_layer_shape", last_layer.get_shape()) 

     exc_info = sys.exc_info() 

     y = tf.expand_dims(y, 1) 

     prediction, loss = learn.models.logistic_regression(last_layer, y) 
     print("prediction", prediction) 
     prediction = tf.Print(prediction, [prediction], message="This is a: ") 
     #print(prediction.eval()) 

     train_op = tf.contrib.layers.optimize_loss(
        loss=loss, 
        global_step=tf.contrib.framework.get_global_step(), 
        optimizer='SGD', 
        learning_rate=0.001) 

     #return {'class': tf.argmax(prediction, 1), 'prob': prediction}, loss, train_op 
     return {'class': prediction, 'prob': prediction}, loss, train_op 

回答

0

你可能在一個錯誤的輸入您的FC是輸入= POOL2我認爲他們應該是last_pool_layer。

並回答你的是tensorflow期望一個熱門編碼。您可以通過查看logistic_regression的實施並根據它來確認這一點。

如果你這樣做,你會最終here你會發現它預計標籤是這種形式。

labels: Tensor, [batch_size, n_classes], labels of the output classes. 

這意味着是一個熱。如果它不是一個熱門,它也會混淆你試圖通過加權某些類比其他更多的數學。可能有一些函數可以將類標籤隱式轉換爲單獨標籤,但我不知道它。

相關問題