都知道四舍五入,向上取整,向下取整,居然还有这种方式,也算是一种特殊的量化吧。。
matlab与python对应的这些甚至个python的不同的包,例如tensorflow或者numpy居然规则还不一样。。
一、 tf.math.round
按元素将张量的值四舍五入到最接近的整数。
import tensorflow as tf
tf.math.round(x, name=None)
四舍五入到偶数。也被称为银行家四舍五入。如果你想根据当前的系统舍入模式进行四舍五入,请使用tf::cint。例如:
x = tf.constant([0.9, 2.5, 2.3, 1.5, -4.5])
tf.round(x) # [ 1.0, 2.0, 2.0, 2.0, -4.0 ]
大白话:小数部分小于或大于0.5,取最近的整数(例如:2.3取2、2.6取3)。小数部分等于0.5时取偶整数(例如:2.5取2、3.5取4)。
二、其他几种取整方式:
1、向上取整
tf.ceil(x, name=None) # 向上取整
2、向下取整
tf.floor(x, name=None) # 向下取整
3、取最接近的整数(实际使用中和tf.round()的取整方式一样)
tf.rint(x, name=None) # 取最接近的整数
数据的精度与操作的精度有时候也不一样,有的包可以接受可变类型,可变个数的变量输入,但有的就很鸡肋了。。
参考:
https://blog.csdn.net/qq_40108803/article/details/110939839
https://blog.csdn.net/weixin_39805387/article/details/110496802
https://blog.csdn.net/thl789/article/details/109018926
https://blog.csdn.net/a362682954/article/details/104774539/
http://www.voidcn.com/article/p-usmplytw-bvq.html
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-cnvw2in5.html
https://codingdict.com/sources/py/tensorflow/3477.html