首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TensorFlow:有什么方法来衡量一个模型的失败吗?

TensorFlow:有什么方法来衡量一个模型的失败吗?
EN

Stack Overflow用户
提问于 2017-07-13 16:05:46
回答 2查看 26K关注 0票数 26

我能得到的最接近的例子是在这个问题上:https://github.com/tensorflow/tensorflow/issues/899

有了这个最低限度的可复制代码:

代码语言:javascript
复制
import tensorflow as tf
import tensorflow.python.framework.ops as ops 
g = tf.Graph()
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]
for op in g.get_operations():
  flops = ops.get_stats_for_node_def(g, op.node_def, 'flops').value
  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

但是,返回的失败始终为零。是否有一种方法可以具体地测量触发器,特别是使用PB文件?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-29 20:23:19

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

代码语言:javascript
复制
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,如下所示:

代码语言:javascript
复制
import tensorflow as tf
import keras.backend as K
from keras.applications.mobilenet import MobileNet

run_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))

希望我能帮上忙!

票数 23
EN

Stack Overflow用户

发布于 2019-11-30 07:25:38

上述方法不再适用于TF2.0,因为分析器方法已被废弃,并在compat.v1下移动。看来这个特性还需要实现。

下面是关于Github的一个问题:https://github.com/tensorflow/tensorflow/issues/32809

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45085938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档