回答我自己的问题:
我需要使用我需要的形状的现有张量之一来创建一个[?, len(CATEGORIES)]
基于它们的张量。
为此,我们需要一个张量[?]
as tf.argmax(logits, 1)
用于使用tf.till
超过categories_tensor
和一个张量[?, len(CATEGORIES)]
用于使用tf.reshape
对此的结果。所以
CATEGORIES # => ['dog', 'elephant']
n_classes = len(CATEGORIES) # => 2
categories_tensor = tf.constant(CATEGORIES) # => Shape [2]
pob_tensor = tf.nn.softmax(logits)
# => Shape [?, 2] being ? the number of inputs to predict
class_tensor = tf.argmax(logits, 1)
# => Shape [?, 1]
tiled_categories_tensor = tf.tile(categories_tensor, tf.shape(class_tensor)) # => Shape [2*?]
# => ['dog', 'elephant', 'dog', 'elephant', ... (? times) , 'dog', 'elephant' ]
categories = tf.reshape(tiled_categories_tensor, tf.shape(prob_tensor)) # => Shape [?, 2]
# => [['dog', 'elephant'], ['dog', 'elephant'], ... (? times) , ['dog', 'elephant'] ]
predictions_dict = {
'category': tf.gather(CATEGORIES, tf.argmax(logits, 1)),
'class': class_tensor,
'prob': prob_tensor,
'categories': categories
}
希望它对遇到此问题的任何人有所帮助