Tensorflow计算一个模型的浮点运算数

1、统计模型的浮点运算数和参数量

  • FLOPS:注意全大写,是floating point operations per second的缩写,意指每秒浮点运算次数,理解为计算速度。是一个衡量硬件性能的指标。
  • FLOPs:注意s小写,是floating point operations的缩写(s表复数),意指浮点运算数,理解为计算量。可以用来衡量算法/模型的复杂度。
  • MACCs:是multiply-accumulate operations),也叫MAdds,意指乘-加操作,理解为计算量,MAdds 大约是 FLOPs 的一半。

    当我们使用tensorflow设计好一个神经网络模型之后,我们如何统计这个模型有多少浮点运算数(FLOPs)和多少参数量呢?我们可以使用tensorflow中的一个模块来进行统计,实例如下(以统计VGG为例):

#coding = utf-8

import tensorflow as tf
import tensorflow.contrib.slim as slim
from tensorflow.contrib.slim.nets import vgg

def stats_graph(graph):
	flops = tf.profiler.profile(graph, options=tf.profiler.ProfileOptionBuilder.float_operation())
	params = tf.profiler.profile(graph, options=tf.profiler.ProfileOptionBuilder.trainable_variables_parameter())
	print('FLOPs: {};    Trainable params: {}'.format(flops.total_float_ops, params.total_parameters))


def main():
	with tf.Graph().as_default() as graph:
		inputs = tf.placeholder(dtype = tf.float32, shape = [1, 224, 224, 3])
		with slim.arg_scope(vgg.vgg_arg_scope()):
			_, end_points = vgg.vgg_16(inputs,
			                        num_classes=1000,
			                        is_training=True,
			                        dropout_keep_prob=0.5,
			                        spatial_squeeze=False,
			                        scope='vgg_16')		
		stats_graph(graph)

if __name__ == '__main__':
	main()

    统计的输出为:

FLOPs: 31651968442;    Trainable params: 138357544

   计算方式参考论文:《Pruning Convolutional Neural Networks for Resource Efficient Inference》中的内容:

   

2、常用模型的FLOPs统计 (参考

      

  参考:https://blog.csdn.net/leayc/article/details/81001801    

             https://robertlexis.github.io/2018/08/28/Tensorflow-%E6%A8%A1%E5%9E%8B%E6%B5%AE%E7%82%B9%E6%95%B0%E8%AE%A1%E7%AE%97%E9%87%8F%E5%92%8C%E5%8F%82%E6%95%B0%E9%87%8F%E7%BB%9F%E8%AE%A1/

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值