Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TensorFlow:有没有一种方法可以测量模型的失败率?_Python_Tensorflow - Fatal编程技术网

Python TensorFlow:有没有一种方法可以测量模型的失败率?

Python TensorFlow:有没有一种方法可以测量模型的失败率?,python,tensorflow,Python,Tensorflow,我能找到的最接近的例子是本期: 使用此最小可复制代码: 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) #

我能找到的最接近的例子是本期:

使用此最小可复制代码:

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文件?

有点晚了,但它可能会在将来帮助一些访问者。对于您的示例,我成功地测试了以下代码段:

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)
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))
也可以将探查器与
Keras
结合使用,如以下代码片段所示:

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

我希望我能帮忙

我想以Tobias Schnek的回答为基础,同时回答最初的问题:如何从
pb
文件中获取FLOP

使用TensorFlow 1.6.0运行Tobias answer中的第一段代码

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)

    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('TF stats gives',flops.total_float_ops)
我们得到以下输出:

Flops should be ~ 7200
TF stats gives 8288
那么,为什么我们得到的是
8288
,而不是预期的结果
7200=2*25*16*9
[a]?答案是张量
A
B
的初始化方式。使用高斯分布初始化会导致一些失败。通过更改
A
B
的定义

    A = tf.Variable(initial_value=tf.zeros([25, 16]))
    B = tf.Variable(initial_value=tf.zeros([16, 9]))
给出预期的输出
7200

通常,在其他方案中,使用高斯分布初始化网络变量。大多数情况下,我们对初始化失败不感兴趣,因为它们在初始化过程中只发生过一次,在训练或推理过程中不会发生。那么,如果不考虑初始化触发器,如何获得触发器的确切数目呢

使用
pb
冻结图形。从
pb
文件计算触发器实际上是OP的用例

以下代码段说明了这一点:

import tensorflow as tf
from tensorflow.python.framework import graph_util

def load_pb(pb):
    with tf.gfile.GFile(pb, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name='')
        return graph

# ***** (1) Create Graph *****
g = tf.Graph()
sess = tf.Session(graph=g)
with g.as_default():
    A = tf.Variable(initial_value=tf.random_normal([25, 16]))
    B = tf.Variable(initial_value=tf.random_normal([16, 9]))
    C = tf.matmul(A, B, name='output')
    sess.run(tf.global_variables_initializer())
    flops = tf.profiler.profile(g, options = tf.profiler.ProfileOptionBuilder.float_operation())
    print('FLOP before freezing', flops.total_float_ops)
# *****************************        

# ***** (2) freeze graph *****
output_graph_def = graph_util.convert_variables_to_constants(sess, g.as_graph_def(), ['output'])

with tf.gfile.GFile('graph.pb', "wb") as f:
    f.write(output_graph_def.SerializeToString())
# *****************************


# ***** (3) Load frozen graph *****
g2 = load_pb('./graph.pb')
with g2.as_default():
    flops = tf.profiler.profile(g2, options = tf.profiler.ProfileOptionBuilder.float_operation())
    print('FLOP after freezing', flops.total_float_ops)
输出

FLOP before freezing 8288
FLOP after freezing 7200


[a] 通常矩阵乘法的失败是乘积AB的mq(2p-1),其中
a[m,p]
B[p,q]
,但由于某种原因,TensorFlow返回2mpq。已打开一个文件以了解原因。

上述方法不再适用于TF2.0,因为探查器方法已被弃用并移动到
compat.v1
下。这项功能似乎还需要实现

以下是Github上的一个问题: 发布了答案。它已被mod删除,因此无法恢复。但它确实解决了问题,而且比其他答案更好。所以我在这里重复一遍


您可以使用下面的pip包获得一些基本信息,如模型的内存需求、参数数量、触发器等

它将输出如下内容

模型剖面 价值 单位 选定的GPU ['0', '1'] GPU ID 失败次数 0.30932349055999997 BFLOPs GPU内存需求 7.4066760912537575 国标 模型参数 138.357544 一百万 模型权重所需的内存 527.7921447753906 兆字节
第一个代码段输出的
触发器应该是~7200
,而
tfstats给出的是8288
。为什么会有这种差异?我根据这个答案来解释它。它就像一个符咒。但是有没有办法阻止
tf.profiler.profile
登录到控制台?冻结图形确实有效!!谢谢你的回答!!您好,谢谢您提供的详细信息!对于上面的解决方案,我还有一个问题:我收到了带有二维输入张量的冻结图(.pb),其中形状没有完全定义(例如,[?,3]),因此您的代码在冻结0 12次操作后给了我
FLOP,因为形状不完整,没有flops统计。
有没有办法“假设”一下没有重新进行训练过程的特定形状?关于如何在TF2.0上进行训练的任何线索?这个版本怎么样?注意:从源代码来看,这个库似乎没有实现所有的公共层。因此,输出可能是非常错误的。由于它还很年轻,作者可能会在将来解决这个问题。