TensorFlow采样Softmax损失的正确使用方法

5
在一个有很多类别的分类问题中,TensorFlow文档建议使用sampled_softmax_loss而不是简单的softmax来减少训练时间。
根据文档源代码(第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")

对我来说,如何将现实世界的问题转化为需要此损失函数的形状尚不清楚。我认为“inputs”字段是问题所在。

以下是可复制的最小工作示例,调用损失函数时会引发矩阵乘法形状错误。

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 文档中的 'dim' 值应该是常数,那么传入损失函数的 'weights' 或者 'inputs' 变量是不正确的。
关于如何正确使用这个损失函数,如果有任何想法都很好,我完全被难住了。对于我们正在使用它的模型的训练时间会产生巨大的影响(500k类)。谢谢!
---编辑---
通过调整参数并忽略样本 softmax 损失函数调用模式的预期输入,可以得到上述示例而不产生错误。如果这样做,将生成一个可训练模型,对预测准确性没有任何影响(正如你预期的那样)。

我可能错了,但尝试用形状[num_hidden_1, num_input][num_classes, num_hidden_1]定义您的权重。这似乎表明您的数据没有按预期通过完全连接的层进行转换... - Engineero
@Engineero 感谢您的参与。模型中连续层之间的隐藏权重会相乘,因此 (256, 784) 和 (10, 256) 不兼容。为确保代码的其余部分正确,将 n_hidden_1 设置为 10(与 n_classes 相同)将成功。当然,这对优化错误的标准没有任何生产力!只是一个小测试。 - Oliver Rice
有趣。我是从操作的角度考虑的:h = W1 * xy = W2 * h,在这种情况下,您需要具有建议维度的权重矩阵。我想它实现了相反的操作:h = x * W1y = h * W2编辑:现在我看到您实际定义网络的地方了。我的错误。 - Engineero
2个回答

2
在softmax层中,你将具有维度(num_classes,)的网络预测乘以具有维度(num_classes, num_hidden_1)w矩阵,因此你试图将(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。这意味着它将同时调整权重和偏差值来达到此目标。请注意保留HTML标签。

1
如果你更改这个解决方案,使得最后一行使用weights['h1']biases['b1']而不是weights['out]biases['out'],它将通过构建步骤并且甚至可以执行。但是,根据文档,权重的值应该是(num_classes, dim),即(10, ?)。你可以通过训练完整的模型来确认某些地方不太对劲。损失会很好地降低,但准确性是随机的或比随机还要差。我将更新帖子以包括完整的训练代码以说明。 - Oliver Rice
这没有任何意义。我会再仔细看看,但是在这种情况下,尺寸对我来说没有意义。看起来我回答得太早了... - Engineero
谢谢你的帮助。我不理解的部分是sampled_softmax会剥离最后的softmax层,并在损失函数中替换它。 - Oliver Rice

1
重点是传递正确的权重、偏置、输入和标签形状。传递给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])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接