Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用估计器获得用于混淆矩阵的课堂分数?_Python_Tensorflow - Fatal编程技术网

Python 如何使用估计器获得用于混淆矩阵的课堂分数?

Python 如何使用估计器获得用于混淆矩阵的课堂分数?,python,tensorflow,Python,Tensorflow,我目前正在尝试训练谷歌的草图识别模型,只是链接中的一个。但我最近遇到了困扰我很长时间的问题 问题如下: 我使用了链接中的代码和quickdraw中的数据来完成培训。我现在有一个包含三个文件(.meta、.index、.data)的训练模型,现在我想计算345个类别的训练模型的混淆矩阵。但由于我从未使用过tensorflow的“估计器”,我不知道如何将经过训练的模型文件加载到代码中并进行测试(无训练),以及如何在softmax层之后获得分类分数(用于计算混淆矩阵) “估计器”API真的让我困惑了很

我目前正在尝试训练谷歌的草图识别模型,只是链接中的一个。但我最近遇到了困扰我很长时间的问题

问题如下: 我使用了链接中的代码和quickdraw中的数据来完成培训。我现在有一个包含三个文件(.meta、.index、.data)的训练模型,现在我想计算345个类别的训练模型的混淆矩阵。但由于我从未使用过tensorflow的“估计器”,我不知道如何将经过训练的模型文件加载到代码中并进行测试(无训练),以及如何在softmax层之后获得分类分数(用于计算混淆矩阵)

“估计器”API真的让我困惑了很长一段时间。请在链接中的代码下解决我的问题:

def create_estimator_and_specs(run_config):
    """Creates an Experiment configuration based on the estimator and input fn."""
    model_params = tf.contrib.training.HParams(
        num_layers=FLAGS.num_layers,
        num_nodes=FLAGS.num_nodes,
        batch_size=FLAGS.batch_size,
        num_conv=ast.literal_eval(FLAGS.num_conv),
        conv_len=ast.literal_eval(FLAGS.conv_len),
        num_classes=get_num_classes(),
        learning_rate=FLAGS.learning_rate,
        gradient_clipping_norm=FLAGS.gradient_clipping_norm,
        cell_type=FLAGS.cell_type,
        batch_norm=FLAGS.batch_norm,
        dropout=FLAGS.dropout)
    estimator = tf.estimator.Estimator(
        model_fn=model_fn,
        config=run_config,
        params=model_params)
    train_spec = tf.estimator.TrainSpec(
        input_fn=get_input_fn(
            mode=tf.estimator.ModeKeys.TRAIN,
            tfrecord_pattern=FLAGS.training_data,
            batch_size=FLAGS.batch_size),
        max_steps=FLAGS.steps)
    eval_spec = tf.estimator.EvalSpec(
        input_fn=get_input_fn(
            mode=tf.estimator.ModeKeys.EVAL,
            tfrecord_pattern=FLAGS.eval_data,
            batch_size=FLAGS.batch_size)
        )
    return estimator, train_spec, eval_spec

def main(unused_args):
    estimator, train_spec, eval_spec = create_estimator_and_specs(
        run_config=tf.estimator.RunConfig(
            model_dir=FLAGS.model_dir,
            save_checkpoints_secs=300,
            save_summary_steps=100)
        )
    tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

我想将我训练过的模型加载到上述代码中,并计算345个类别的混淆矩阵。

您可以使用库函数

根据预测和标签计算混淆矩阵

tf.confusion_matrix([1, 2, 4], [2, 2, 4]) ==>
      [[0 0 0 0 0]
       [0 0 1 0 0]
       [0 0 1 0 0]
       [0 0 0 0 0]
       [0 0 0 0 1]]
对于您的情况,以下代码可能会对您有所帮助:

标签=列表(测试集目标) 预测=列表(估计器预测(输入=测试输入) 混淆矩阵=tf.混淆矩阵(标签、预测) 我不知道如何将经过训练的模型文件加载到代码和 测试一下

使用数据集进行估算

tf.data
模块包含一组类,允许您轻松加载数据、操作数据并将其导入模型

  • 从numpy阵列读取内存中的数据
  • 从csv文件中读取行
如何在softmax层之后获得分类分数(用于 计算混淆矩阵)

使用
tf.keras
,一种高级API在TensorFlow中构建和训练模型

test_dataset = keras.datasets.test_dataset

(train_images, train_labels), (test_images, test_labels) = test_dataset.load_data()

感谢您提供的示例,尤其是上下文(+1)中的示例。为了完整起见,请发布的修改版本,以计算和打印混淆矩阵。
test_dataset = keras.datasets.test_dataset

(train_images, train_labels), (test_images, test_labels) = test_dataset.load_data()