在許多類的分類問題中,tensorflow文檔建議使用sampled_softmax_loss而不是簡單的softmax來減少訓練運行時間。Tensorflow採樣Softmax丟失正確用法
按照docs和source(線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
調用模式的預期投入無差錯運行示例。如果你這樣做,它會產生一個可訓練的模型,它對預測的準確性沒有影響(如你所期望的那樣)。
我可能是錯的,但嘗試用形狀'[num_hidden_1,num_input]'和'[num_classes,num_hidden_1]'定義您的權重。它看起來好像你的數據沒有像你完全連接的層所預期的那樣被轉換... – Engineero
@Engineero感謝稱重。模型的連續層之間的隱藏權重被相乘在一起,使得將是不兼容的(256,784)和(10,256)。 爲了確保其餘代碼正確,將n_hidden_1設置爲10(與n_classes相同)將會成功。當然,由於它在錯誤的標準上進行了優化,因此這沒有任何成效!只是一個小測試。 –
有趣。我正在考慮操作:'h = W1 * x','y = W2 * h',在這種情況下,您需要使用建議尺寸的加權矩陣。我想它實現了相反的結果:'h = x * W1'和'y = h * W2'。 **編輯:**現在我看到您實際定義您的網絡的位置。我的錯。 – Engineero