在一个有很多类别的分类问题中,TensorFlow文档建议使用sampled_softmax_loss而不是简单的softmax来减少训练时间。
根据文档和源代码(第1180行),sampled_softmax_loss的调用模式为:
最后一行触发了一个 ValueError,声明不能将形状为 (?,10) 和 (?,256) 的张量相乘。作为一般规则,我同意这个说法。完整的错误信息如下所示:
如果 tensorflow 文档中的 'dim' 值应该是常数,那么传入损失函数的 'weights' 或者 'inputs' 变量是不正确的。
关于如何正确使用这个损失函数,如果有任何想法都很好,我完全被难住了。对于我们正在使用它的模型的训练时间会产生巨大的影响(500k类)。谢谢!
---编辑---
通过调整参数并忽略样本 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]
定义您的权重。这似乎表明您的数据没有按预期通过完全连接的层进行转换... - Engineeron_hidden_1
设置为 10(与n_classes
相同)将成功。当然,这对优化错误的标准没有任何生产力!只是一个小测试。 - Oliver Riceh = W1 * x
,y = W2 * h
,在这种情况下,您需要具有建议维度的权重矩阵。我想它实现了相反的操作:h = x * W1
和y = h * W2
。编辑:现在我看到您实际定义网络的地方了。我的错误。 - Engineero