【问题标题】:Tensorflow tutorial + How to extract classes and probability from generator objectTensorflow 教程 + 如何从生成器对象中提取类和概率
【发布时间】:2018-06-14 09:40:51
【问题描述】:

MNIST tensorflow tutorial 中的代码创建了一个包含类和概率的字典,并返回一个 EstimatorSpec 对象,

predictions = {
    "classes": tf.argmax(input=logits, axis=1),
    "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}
if mode == tf.estimator.ModeKeys.PREDICT:
  return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

输出格式如下:

....
{'class_ids': 1, 'logits': array([-32976400., -30171870.], dtype=float32)}
{'class_ids': 1, 'logits': array([-32958380., -30386898.], dtype=float32)}
{'class_ids': 1, 'logits': array([-32940332., -30601930.], dtype=float32)}
{'class_ids': 1, 'logits': array([-32922300., -30816956.], dtype=float32)}
....

问题:如何在循环中访问和检索这些 class_id 值? 我试图通过将这些预测的类 ID 与它们的原始值相等来计算正确分类的类的数量。我怎样才能做到这一点?

这就是我正在尝试的。

 ##pred_results contains the EstimatorSpec object
  for i in range(0,len(FileList)):
    x=FileList[i].split('\\')[5]        # This gives me the actual class ID
    print(x+"\nProbability:")
    print(next(pred_results))
    if(int(x)==int(pred_results["classes"])): #POINT OF ERROR
        c+=1                                            ######
    print("\n")
  print(c)  
                ###

这个想法是将预测的“1”存储在“类”中,并将其与实际标签进行比较。

【问题讨论】:

    标签: python tensorflow deep-learning mnist


    【解决方案1】:

    我将 EstimatorSpec 对象转换为字符串,然后对其进行拆分以获得整数 class_id

    for i in range(0,len(FileList)):
        x=FileList[i].split('\\')[5]        
        print(FileList[i]+"\nProbability:")
        y=str(next(pred_results)).split(',')[0].split(":")[1].strip()
        print(y)
        if(int(x)==int(y)):#####
            c+=1                                            ######
        print("\n")
    

    我正在运行这个 sn-p 代码来找出有多少类被正确分类。可能不是最佳的,但现在可以。

    【讨论】:

      猜你喜欢
      • 2019-04-21
      • 2018-04-04
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 2014-04-27
      相关资源
      最近更新 更多