【问题标题】:Using Tensorflow sampled_softmax_loss使用 TensorFlow sampled_softmax_loss
【发布时间】:2017-06-18 13:37:42
【问题描述】:

我正在研究基于二元组的 LSTM。

自从我介绍了嵌入,我必须选择正确的损失函数。这里 是我的选择:

    loss=tf.reduce_mean(tf.nn.sampled_softmax_loss(weights=softmax_weights,\
                biases=softmax_biases, \
                labels=tf.concat(train_labels,0),\
                 inputs=logits,\
                num_sampled=num_sampled,\
                num_classes=vocabulary_size))

我面临标签张量维度问题错误:

logits 的形状是这样的:(640, 13)
标签具有这种形状 Tensor("concat_2:0", shape=(640, 27), dtype=float32)
我也试过了

   labels==tf.reshape(tf.concat(train_labels,0),[-1])

对于这两种情况,我都会收到错误:

对于第一种情况,错误是:

             Dimension must be 1 but is 27 for 
              'sampled_softmax_loss/ComputeAccidentalHits' (op: 
               'ComputeAccidentalHits') with input shapes: [640,27], [20]. 

对于第二种情况,错误是:

           Shape must be rank 2 but is rank 1 for 
           'sampled_softmax_loss/LogUniformCandidateSampler' (op: 
           'LogUniformCandidateSampler') with input shapes: [17280].

这是我的参数:

           640 = batch_size *num_enrollings =64*10
           27 = vocabulary_size (I am implementing first Embedding  on single character as vocabulary.
           20 is num_sampled of the loss function.
           13 = embedding_size

为什么 tf.nn.sampled_softmax_loss 不接受一维标签?

tf 版本为 1.0.1 蟒蛇版本:2.7

【问题讨论】:

    标签: tensorflow deep-learning


    【解决方案1】:

    找到解决方案。

    我给 sampled_softmax_loss 提供了 one-hot 格式的标签。 将简单的 argmax 函数应用于标签后一切顺利

    【讨论】:

    • 您能详细说明一下吗?为什么它会起作用?我试过了,但现在我得到了错误:形状必须是 2 级,但对于 'sampled_softmax_loss_2/LogUniformCandidateSampler'(操作:'LogUniformCandidateSampler')是 1 级,输入形状:[500]。
    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 2016-05-16
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 2017-09-16
    相关资源
    最近更新 更多