python - 如何将 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)
解决方案
推荐阅读
- arrays - 在elasticsearch中轻松检索数组最后一个元素的信息
- java - 如何序列化函数返回的 rx.Single 对象?
- flask - WTFforms 字段值在不需要时初始化
- javascript - 替换字符串的最后一部分,javascript
- spring-boot - Glassfish 中的 Spring Websockets - WebSocket 握手期间出错:发送非空“Sec-WebSocket-Protocol”标头但未收到响应
- django-channels - 在 Django Channels 中的 Django Rest 框架中获取会话集
- java - 如何修复使用#2nd 记录而不是#1st(#3rd 而不是#2nd 等)的游标?
- gradle - 如何在 gradle 5+ 中为 shadowJar 插件指定归档分类器
- curl - 使用 LexikJWTAuthenticationBundle 和 Symfony 3.4 创建身份验证令牌
- r - x$data[i, j] <- value 中的错误:要替换的项目数不是替换长度的倍数