2017-05-05 40 views
2

在許多類的分類問題中,tensorflow文檔建議使用sampled_softmax_loss而不是簡單的softmax來減少訓練運行時間。Tensorflow採樣Softmax丟失正確用法

按照docssource(線1180),用於sampled_softmax_loss通話模式是:

tf.nn.sampled_softmax_loss(weights, # Shape (num_classes, dim)  - floatXX 
        biases,  # Shape (num_classes)   - floatXX 
        labels,  # Shape (batch_size, num_true) - int64 
        inputs,  # Shape (batch_size, dim)  - floatXX 
        num_sampled, # - int 
        num_classes, # - int 
        num_true=1, 
        sampled_values=None, 
        remove_accidental_hits=True, 
        partition_strategy="mod", 
        name="sampled_softmax_loss") 

目前還不清楚(至少對我來說)如何轉換爲現實世界的問題轉化成形狀,這一損失功能需要。我認爲'投入'領域是問題。

這是一個複製粘貼準備好的最小工作示例,它在調用丟失函數時引發矩陣乘法形狀錯誤。

import tensorflow as tf 

# Network Parameters 
n_hidden_1 = 256 # 1st layer number of features 
n_input = 784  # MNIST data input (img shape: 28*28) 
n_classes = 10 # MNIST total classes (0-9 digits)  

# Dependent & Independent Variable Placeholders 
x = tf.placeholder("float", [None, n_input]) 
y = tf.placeholder("float", [None, n_classes]) # 

# Weights and Biases 
weights = { 
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])), 
    'out': tf.Variable(tf.random_normal([n_hidden_1, n_classes])) 
} 
biases = { 
    'b1': tf.Variable(tf.random_normal([n_hidden_1])), 
    'out': tf.Variable(tf.random_normal([n_classes])) 
} 

# Super simple model builder 
def tiny_perceptron(x, weights, biases): 
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) 
    layer_1 = tf.nn.relu(layer_1) 
    out_layer = tf.matmul(layer_1, weights['out']) + biases['out'] 
    return out_layer 

# Create the model 
pred = tiny_perceptron(x, weights, biases)  

# Set up loss function inputs and inspect their shapes 
w = tf.transpose(weights['out']) 
b = biases['out'] 
labels = tf.reshape(tf.argmax(y, 1), [-1,1]) 
inputs = pred 
num_sampled = 3 
num_true = 1 
num_classes = n_classes 

print('Shapes\n------\nw:\t%s\nb:\t%s\nlabels:\t%s\ninputs:\t%s' % (w.shape, b.shape, labels.shape, inputs.shape)) 
# Shapes 
# ------ 
# w:  (10, 256) # Requires (num_classes, dim)  - CORRECT 
# b:  (10,)  # Requires (num_classes)   - CORRECT 
# labels: (?, 1)  # Requires (batch_size, num_true) - CORRECT 
# inputs: (?, 10) # Requires (batch_size, dim)  - Not sure 

loss_function = tf.reduce_mean(tf.nn.sampled_softmax_loss(
        weights=w, 
        biases=b, 
        labels=labels, 
        inputs=inputs, 
        num_sampled=num_sampled, 
        num_true=num_true, 
        num_classes=num_classes)) 

最後一行觸發器和ValueError異常,說明你不能乘張量與形狀(?,10)和(?,256)。作爲一般規則,我同意這種說法。下面示出完整的錯誤:

ValueError: Dimensions must be equal, but are 10 and 256 for 'sampled_softmax_loss_2/MatMul_1' (op: 'MatMul') with input shapes: [?,10], [?,256]. 

如果從tensorflow文檔的「變暗」值旨在是恆定的,無論是「權重」或「輸入」變量進入損耗函數是不正確的。

任何想法都會很棒,我完全無法正確使用這個損失函數&這對我們使用它的模型(500k類)的訓練時間會產生巨大影響。謝謝!

---編輯---

有可能獲得由以上顯示與參數玩,而忽略sampled_softmax_loss調用模式的預期投入無差錯運行示例。如果你這樣做,它會產生一個可訓練的模型,它對預測的準確性沒有影響(如你所期望的那樣)。

+0

我可能是錯的,但嘗試用形狀'[num_hidden_​​1,num_input]'和'[num_classes,num_hidden_​​1]'定義您的權重。它看起來好像你的數據沒有像你完全連接的層所預期的那樣被轉換... – Engineero

+0

@Engineero感謝稱重。模型的連續層之間的隱藏權重被相乘在一起,使得將是不兼容的(256,784)和(10,256)。 爲了確保其餘代碼正確,將n_hidden_​​1設置爲10(與n_classes相同)將會成功。當然,由於它在錯誤的標準上進行了優化,因此這沒有任何成效!只是一個小測試。 –

+0

有趣。我正在考慮操作:'h = W1 * x','y = W2 * h',在這種情況下,您需要使用建議尺寸的加權矩陣。我想它實現了相反的結果:'h = x * W1'和'y = h * W2'。 **編輯:**現在我看到您實際定義您的網絡的位置。我的錯。 – Engineero

回答

1

在你SOFTMAX層你乘以你的網絡的預測,由你w矩陣具有尺寸(num_classes, num_hidden_1)有尺寸(num_classes,),所以你最終想要的尺寸(num_classes,)的目標標籤比較的東西,現在大小(num_hidden_1,)。改變你的微型感知器,改爲輸出layer_1,然後改變成本的定義。下面的代碼可能會訣竅。

def tiny_perceptron(x, weights, biases): 
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) 
    layer_1 = tf.nn.relu(layer_1) 
    return layer_1 

layer_1 = tiny_perceptron(x, weights, biases) 
loss_function = tf.reduce_mean(tf.nn.sampled_softmax_loss(
        weights=weights['h1'], 
        biases=biases['b1'], 
        labels=labels, 
        inputs=layer_1, 
        num_sampled=num_sampled, 
        num_true=num_true, 
        num_classes=num_classes)) 

當你訓練了一些優化你的網絡,你會告訴它最大限度地減少loss_function,這應該意味着它將調整兩套重量和偏見。

+1

如果你改變了這個解決方案,所以最後一行使用權重['h1']'和'偏向['b1']'而不是'權重['出]'和'偏差['出']'它會通過構建步驟甚至會執行。但是,根據文檔,權重的值應該是'(num_classes,dim)',它將是'(10,?)'。 通過培訓完整的模型,您可以確認某些事情不太正確。損失會很好地下降,但準確性是隨機的或比隨機的更差。 我會更新帖子以包含完整的培訓代碼來說明 –

+0

這沒有任何意義。我會試着再看一遍,但這種情況下的尺寸對我來說沒有意義。這似乎是我過早回答... – Engineero

+0

感謝您的幫助。我不明白的是,sampled_softmax撕掉了最終的softmax圖層,並在損失函數內將其替換。 –

0

關鍵是要通過權重,偏見,輸入和標籤的正確形狀。傳遞給sampled_softmax的重量形狀與一般情況並不相同。 例如,logits = xw + b,請致電sampled_softmax像這樣: sampled_softmax(weight=tf.transpose(w), bias=b, inputs=x),不是sampled_softmax(weight=w, bias=b, inputs=logits) !! 此外,標籤不是一個熱點代表。如果您的標籤是一熱表示,通過labels=tf.reshape(tf.argmax(labels_one_hot, 1), [-1,1])