首页 > 解决方案 > 如何将 f1 度量添加到 tensorflow 多类 DNN 分类器

问题描述

我正在尝试将 f1 分数添加到 tensorflow 上的罐装 DNNClassifier 中。在尝试使用 tf.contrib.estimator.add_metrics 函数添加 f1_score 指标时,我认为有 3 个标签会导致问题。

我收到错误形状 (?, 3) 和 (?, 1) 不兼容。我不知所措,将不胜感激任何帮助。

文件“C:\Python36\lib\site-packages\tensorflow\python\ops\metrics_impl.py”,第 116 行,_remove_squeezable_dimensions predictions.get_shape().assert_is_compatible_with(labels.get_shape()) 文件“C:\Python36\ lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 848, in assert_is_compatible_with raise ValueError("Shapes %s and %s are incompatible" % (self, other)) ValueError: Shapes (?, 3)和 (?, 1) 不兼容

我有以下代码:

   def my_f1(features, labels, predictions):
         return {'auc': tf.contrib.metrics.f1_score(
                 labels, predictions['logits'], weights=features['weight'])}






    est = tf.estimator.DNNClassifier(feature_columns=feature_columns,
                                 n_classes=3,
                                 config=tf.estimator.RunConfig(
                                     save_checkpoints_steps=100,
                                     keep_checkpoint_max=5000,
                                 ),
                                 activation_fn=tf.nn.leaky_relu,
                                 optimizer=tf.train.FtrlOptimizer(
                                     learning_rate=0.01,
                                     l1_regularization_strength=0.005
                                 ),
                                 weight_column='weight',
                                 dropout=0.5,
                                 hidden_units=[800, 450, 200, 100, 50],
                                 model_dir="D:/Stock Data/NeuralNetwork2"
                                 )
    est = tf.contrib.estimator.add_metrics(est, my_f1)

标签: pythontensorflowmachine-learningneural-network

解决方案


推荐阅读