栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

TensorFlow:有没有一种方法可以测量模型的FLOPS?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

TensorFlow:有没有一种方法可以测量模型的FLOPS?

有点晚了,但也许将来对某些访客有帮助。对于您的示例,我成功测试了以下代码段:

g = tf.Graph()run_meta = tf.Runmetadata()with g.as_default():    A = tf.Variable(tf.random_normal( [25,16] ))    B = tf.Variable(tf.random_normal( [16,9] ))    C = tf.matmul(A,B) # shape=[25,9]    opts = tf.profiler.ProfileOptionBuilder.float_operation()        flops = tf.profiler.profile(g, run_meta=run_meta, cmd='op', options=opts)    if flops is not None:        print('Flops should be ~',2*25*16*9)        print('25 x 25 x 9 would be',2*25*25*9) # ignores internal dim, repeats first        print('TF stats gives',flops.total_float_ops)

也可以将分析器与

Keras
以下代码段结合使用:

import tensorflow as tfimport keras.backend as Kfrom keras.applications.mobilenet import MobileNetrun_meta = tf.Runmetadata()with tf.Session(graph=tf.Graph()) as sess:    K.set_session(sess)    net = MobileNet(alpha=.75, input_tensor=tf.placeholder('float32', shape=(1,32,32,3)))    opts = tf.profiler.ProfileOptionBuilder.float_operation()        flops = tf.profiler.profile(sess.graph, run_meta=run_meta, cmd='op', options=opts)    opts = tf.profiler.ProfileOptionBuilder.trainable_variables_parameter()        params = tf.profiler.profile(sess.graph, run_meta=run_meta, cmd='op', options=opts)    print("{:,} --- {:,}".format(flops.total_float_ops, params.total_parameters))

希望我能帮上忙!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624692.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号