当前位置:网站首页>How to understand metrics in keras
How to understand metrics in keras
2022-07-25 12:59:00 【51CTO】
Keras The library provides a method to calculate and report a set of standard indicators when training deep learning models . In addition to providing standard indicators for classification and regression problems ,Keras It also allows you to define and report your own custom metrics during in-depth learning . If you want to better capture the performance metrics of the model during training , This will be particularly useful .
In this tutorial , You will learn how to use built-in metrics , And how to Keras Define and use your own indicators when training the deep learning model . After completing this tutorial , You will learn :
- Keras How indicators work and how to use them when training models .
- How to use working examples in Keras Regression and classification indicators are used in .
- How to use working examples in Keras Define and use your own custom metrics .
1 Keras indicators
Keras Allows you to list indicators to be monitored during model training . You can specify by “metrics” Parameters are... On the model compile() Function provide function name ( Or function name alias ) List to do this . for example :
The specific indicators you list can be Keras Name of function ( Such as mean_squared_error) Or string aliases of these functions ( Such as “mse”).
The measurements are recorded on each of the training data sets epoch At the end of . If a validation data set is also provided , The recorded metrics are also calculated for the validation dataset .
All indicators are output and called in detail fit() Report in the history object returned by the function . In both cases , The name of the measurement function is used as the key of the measurement . For the indicators of the validation data set ,“val_” The prefix is added to the key .
Loss function and clearly defined Keras Indicators can be used as training indicators .
2 Keras Regression index
Here is what you can do in Keras List of indicators used for regression problems in .
- Mean square error :mean_squared_error、MSE or
- Mean absolute error :mean_absolute_error, MAE, mae
- Mean absolute percentage error :mean_absolute_percentage_error、MAPE、mape
- Cosine proximity :cosine_proximity, cosine
The above four indicators , The smaller the value. , The better the fitting degree of the model , But it does not mean that the prediction effect of the model is better , It can only be assumed that the model with good fitting effect has better prediction effect . In other words, it's not too rigorous but easy to remember : The smaller the above index value , The better the model predicts .
The following example demonstrates this of a simple artificial regression problem 4 Built in regression indicators .
Be careful : Your results may vary due to the randomness of the algorithm or evaluation process or differences in numerical accuracy . Consider running the example several times and comparing the average results .
Running this example will run on each epoch Print measurements at the end .
Then create during training 4 A line chart of indicators .

Please note that , The indicator is to use the string alias value [‘mse‘, ‘mae‘, ‘mape‘, ‘cosine‘] designated , And use their extension function names as history Key value reference on object .
We can also use the extended name to specify the indicator , As shown below :
If the function name is imported into the script , We can also specify them directly .
You can also use the loss function as a measure . for example , You can put the mean square logarithmic error (mean_squared_logarithmic_error,MSLE or msle) The loss function is used as a measure , As shown below :
3 Keras Classification index
Here is what you can do in Keras List of indicators used to classify problems in .
- Binary precision :binary_accuracy,acc
- Classification accuracy :categorical_accuracy, acc
- Sparse classification accuracy :sparse_categorical_accuracy
- Top k Categorical Accuracy:top_k_categorical_accuracy( You need to specify one k Parameters )
- sparse Top k Classification accuracy :sparse_top_k_categorical_accuracy( You need to specify k Parameters )
Accuracy is special . Whether your problem is a two class problem or a multi class problem , You can specify “accuracy” Indicators to report accuracy .
The following is an example of a binary classification problem , It demonstrates the built-in accuracy index .
Be careful : Your results may vary due to the randomness of the algorithm or evaluation process or differences in numerical accuracy . Consider running the example several times and comparing the average results .
Running this example will report the accuracy at the end of each training period .

4 stay Keras Custom indicators in
You can also call compile() Function and define your own indicators in “metrics” Specify the function name in the function list of the parameter .
I haven't used custom indicators yet , I haven't even seen others use . Therefore, there are few guesses about the application of custom indicators . When you choose Custom indicators , Ask yourself at least three questions : Can common indicators really not meet the demand ? Are the commonly used indicators used to measure the model selected correctly , Is there a classification index to measure the regression problem ? My math is good enough to understand correctly “metrics” What functions in the function list of parameters are used for ? At least I was blocked by the first question . In the future, I will add some custom indicators .^_^
边栏推荐
- Maskgae: masked graph modeling meets graph autoencoders
- 【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12
- Plus SBOM: assembly line BOM pbom
- massCode 一款优秀的开源代码片段管理器
- 如何用因果推断和实验驱动用户增长? | 7月28日TF67
- 【AI4Code】《GraphCodeBERT: Pre-Training Code Representations With DataFlow》 ICLR 2021
- 使用vsftpd服务传输文件(匿名用户认证、本地用户认证、虚拟用户认证)
- 业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
- Detailed explanation of switch link aggregation [Huawei ENSP]
- Synergetic process
猜你喜欢

word样式和多级列表设置技巧(二)

艰辛的旅程

2022.07.24(LC_6124_第一个出现两次的字母)

Eccv2022 | transclassp class level grab posture migration

Mid 2022 review | latest progress of large model technology Lanzhou Technology

Detailed explanation of flex box

感动中国人物刘盛兰

Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?
![[fluent -- example] case 1: comprehensive example of basic components and layout components](/img/d5/2392d9cb8550aa2692c8b41303d507.png)
[fluent -- example] case 1: comprehensive example of basic components and layout components

The programmer's father made his own AI breast feeding detector to predict that the baby is hungry and not let the crying affect his wife's sleep
随机推荐
Cmake learning notes (II) generation and use of Library
【OpenCV 例程 300篇】239. Harris 角点检测之精确定位(cornerSubPix)
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
部署Apache网站服务以及访问控制的实现
Software testing interview question: Please list the testing methods of several items?
I register the absolutely deleted data in the source sqlserver, send it to maxcomputer, and write the absolute data when cleaning the data
Selenium uses -- XPath and analog input and analog click collaboration
MLX90640 红外热成像仪测温传感器模块开发笔记(五)
What is ci/cd?
[advanced C language] dynamic memory management
程序员奶爸自制AI喂奶检测仪,预判宝宝饿点,不让哭声影响老婆睡眠
网络空间安全 渗透攻防9(PKI)
什么是CI/CD?
2022.07.24 (lc_6124_the first letter that appears twice)
Use of hystrix
ECCV 2022 | climb to the top semantickitti! Semantic segmentation of LIDAR point cloud based on two-dimensional prior assistance
Kyligence was selected into Gartner 2022 data management technology maturity curve report
Implementation of recommendation system collaborative filtering in spark
[operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+
2022.07.24(LC_6125_相等行列对)