在使用sampled_softmax_loss出现的一个关于‘’nput 'b' of 'MatMul' Op has type float32 that does not match type int32 of argument 'a'.“错误原因以及解决办法

在使用负采样函数的时候出现了该错误,错误代码:

tf.nn.sampled_softmax_loss(softmax_weights, softmax_biases,
                tf.reduce_sum(embeds, 1),
                train_labels,
                num_sampled, vocabulary_size)
原因:参数赋值错误
查看sampled_softmax_loss的源代码可知其参数定义为
def sampled_softmax_loss(weights,
biases,
labels,
inputs,
num_sampled,
num_classes,
num_true=1,
sampled_values=None,
remove_accidental_hits=True,
partition_strategy="mod",
name="sampled_softmax_loss",
seed=None):
因此 代码中的参数赋值顺序错误 labels 对应的应是train_labels inputs对应的应是tf.reduce_sum(embeds, 1)

解决办法:
采用指定赋值的方式:tf.nn.sampled_softmax_loss(weights=softmax_weights,
                 biases=softmax_biases,
                inputs=tf.reduce_sum(embeds, 1),
                labels=train_labels,
                num_sampled=num_sampled,
                num_classes=vocabulary_size
)

或者 更改赋值变量位置:
tf.nn.sampled_softmax_loss(softmax_weights, softmax_biases,
train_labels, tf.reduce_sum(embeds, 1),num_sampled, vocabulary_size)

则可以顺利解决这个问题

同时也是对平时编写程序的一个习惯问题,在参数较多的情况下,最好采用指定复制的方式!

  


猜你喜欢

转载自www.cnblogs.com/deeplearning1/p/11413206.html