python文本分类评价指标 top1如何计算_使用paddlehub自定义任务,具体是自定义文本分类任务中的评价指标时按照官方文档的方法做了之后报错了,请问应该如何解决?...
### 我的版本、环境信息
1)PaddleHub 1.8.2和PaddlePaddle版本1.8.4
2)系统环境:在Windows的AiStudio网页端跑的,python版本为3.7
复现信息:
执行的操作:
参考这里https://github.com/PaddlePaddle/PaddleHub/wiki/%E5%A6%82%E4%BD%95%E4%BF%AE%E6%94%B9Task%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95%EF%BC%9F意图修改paddlehub中的hub.TextClassifierTask的默认的评价指标**(目前是计划先按照文档把默认指标”acc"改成“f1",进而学会怎么改成macro_f1、microf1、召回率)**,这部分代码如下:
`import numpy as np
def calculate_f1_np(preds, labels):
# 计算F1分数
# preds:预测label
# labels: 真实labels
# 返回F1分数
preds = np.array(preds)
labels = np.array(labels)
tp = np.sum((labels == 1) & (preds == 1))
tn = np.sum((labels == 0) & (preds == 0))
fp = np.sum((labels == 0) & (preds == 1))
fn = np.sum((labels == 1) & (preds == 0))
p = tp / (tp + fp) if (tp + fp) else 0
r = tp / (tp + fn) if (tp + fn) else 0
f1 = (2 * p * r) / (p + r) if p + r else 0
return f1
def calculate_metrics(self, run_states):
loss_sum = acc_sum = run_examples = 0
run_step = run_time_used = 0
all_labels = np.array([])
all_infers = np.array([])
for run_state in run_states:
run_examples += run_state.run_examples
run_step += run_state.run_step
loss_sum += np.mean(
run_state.run_results[-1]) * run_state.run_examples
acc_sum += np.mean(
run_state.run_results[2]) * run_state.run_examples
np_labels = run_state.run_results[0]
np_infers = run_state.run_results[1]
all_labels = np.hstack((all_labels, np_labels.reshape([-1])))
all_infers = np.hstack((all_infers, np_infers.reshape([-1])))
run_time_used = time.time() - run_states[0].run_time_begin
avg_loss = loss_sum / run_examples
run_speed = run_step / run_time_used
scores = OrderedDict()
f1 = calculate_f1_np(all_infers, all_labels)
scores["f1"] = f1
return scores, avg_loss, run_speed
from tb_paddle import SummaryWriter
tb_writer = SummaryWriter("PATH/TO/LOG")
def record_value(evaluation_scores, loss, s):
tb_writer.add_scalar(
tag="Loss_{}".format(self.phase),
scalar_value=loss,
global_step=self.envs['train'].current_step)
log_scores = ""
for metric in evaluation_scores:
self.tb_writer.add_scalar(
tag="{}{}".format(metric, self.phase),
scalar_value=scores[metric],
global_step=self._envs['train'].current_step)
log_scores += "%s=%.5f " % (metric, scores[metric])
print("step %d / %d: loss=%.5f %s[step/sec: %.2f]" %
(self.current_step, self.max_train_steps, avg_loss,
log_scores, run_speed))
def new_log_interval_event(self, run_states):
print("This is the new log_interval_event!")
scores, avg_loss, run_speed = calculate_metrics(run_states)
record_value(scores, avg_loss, run_speed)
cls_task.delete_hook(hook_type="log_interval_event", name="default")
cls_task.add_hook(hook_type="log_interval_event", name="new_log_interval_event", func=new_log_interval_event)
cls_task.hook_info()`
### 报错信息如下
This is the new log_interval_event!
---------------------------------------------------------------------------TypeError Traceback (most recent call last) in
1 #run_states = cls_task.finetune_and_eval()
----> 2 cls_task.finetune_and_eval()
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py in finetune_and_eval(self)
943
944 def finetune_and_eval(self):
--> 945 return self.finetune(do_eval=True)
946
947 def finetune(self, do_eval=False):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py in finetune(self, do_eval)
964 while self.current_epoch <= self.config.num_epoch:
965 self.config.strategy.step()
--> 966 run_states = self._run(do_eval=do_eval)
967 self.env.current_epoch += 1
968
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py in _run(self, do_eval)
1222 if self.is_train_phase:
1223 if self.current_step % self.config.log_interval == 0:
-> 1224 self._log_interval_event(period_run_states)
1225 global_run_states += period_run_states
1226 period_run_states = []
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py in hook_function(self, *args)
708 func(*args)
709 else:
--> 710 partial(func, self)(*args)
711
712 return hook_function
in new_log_interval_event(self, run_states)
73 # 改写的事件方法,参数列表务必与PaddleHub内置的相应方法保持一致
74 print("This is the new log_interval_event!")
---> 75 scores, avg_loss, run_speed = calculate_metrics(run_states)
76 record_value(scores, avg_loss, run_speed)
77
TypeError: calculate_metrics() missing 1 required positional argument: 'run_states'
python文本分类评价指标 top1如何计算_使用paddlehub自定义任务,具体是自定义文本分类任务中的评价指标时按照官方文档的方法做了之后报错了,请问应该如何解决?...相关推荐
- re python 引擎_转 python内置正则表达式(re)模块官方文档简要中文版
学习正则表达式,最好的教材是<精通正则表达式>,而要精通NFA正则表达式,使用了NFA引擎的python正则模块官方文档就是最好的教材,大部分的功能同样在其他使用传统NFA引擎的正则包里受 ...
- python读取docx中表格 图片_python 解析docx文档的方法,以及提取插入的文本对象和图片...
首先安装docx模块,通过pip install docx或者在docx官方链接上下载安装都可以 下面来看下如何解析docx文档:文档格式如下 有3个部分组成 1 正文:text文档 2 一个表格. ...
- python怎么选取不连续的列_用pandas中的DataFrame时选取行或列的方法
如下所示: import numpy as np import pandas as pd from pandas import Sereis, DataFrame ser = Series(np.ar ...
- Python格式化字符串字面值 | 被官方文档称之为『漂亮』的输出格式
Python格式化字符串字面值 | 被官方文档称之为『漂亮』的输出格式 本文参考输入输出 - Python 3.7.10 文档.首先声明咱的实验环境. ❯ python --version Pytho ...
- Python Turtle 海龟绘图详解官方文档中文版
Python Turtle 海龟绘图详解 (官方文档中文版)-安徽省太湖中学陈晓中整理 概述 海龟绘图很适合用来引导孩子学习编程. 最初来自于 Wally Feurzeig, Seymour Pape ...
- python笔记6-python官方文档之format()格式化详解
字符串格式化之format() 字符串的格式化是特别特别重要的一个知识点,自己将通过python官方文档来具体总结学习它,整体为如下一篇笔记文章,以备自己后来复习回顾. 一.format() ...
- Python 3.7.1 官方文档 总结
Python 3.7.1 1.一些概念 2.小用法 2.1 迭代器: 2.2 生成器 3.数据结构 3.1 字符串 3.2 列表 3.2.1 列表方法 list.append(x) list.exte ...
- 【Python绘图】turtle库乌龟绘图官方文档详细整理
[函数表格整理在最下方] Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面 ...
- 每天一小时python官方文档学习(七)————模块与包
6. 模块 模块是一个包含Python定义和语句的文件,实际上就是一个正常的.py文件.但是作用和普通需要运行的程序不一样,它实际上有点像函数.如果说函数实现了代码的重用,模块则是实现了函数的重用.我 ...
最新文章
- Java线程池面试必备:核心参数、工作流、监控、调优手段
- 常用的前端辅助开发工具有哪些
- 华为手机权限开启方法8
- python自学步骤-Python入门深度学习完整指南
- 容器资源需求、资源限制(二十二)
- array reduce
- matlab wc wp,数字信号处理MATLAB习题.PDF
- angular点击按钮弹出页面_Win10提示“由于启动计算机时出现了页面文件配置问题”解决方法...
- 【CodeForces - 589F】Gourmet and Banquet (贪心,思维,二分)
- 天正多条线段长度lisp下载_四年级数学上册基础题过关检测+12套思维题,下载给孩子测试!【有电子版】...
- 我平时比较喜欢的几篇技术文章
- ACL在QinQ port 中的应用
- 关于RJ45 网线接线问题
- 中国的程序员工资为什么那么高?
- DNX 版本升级命令
- Poco库使用:文件目录操作
- http/https接口调用
- linux c语言 select函数用法
- Xceed Words for .NET转换和页眉/页脚功能
- 大数据常见英文词汇(二)(待续)