我一直在使用 Tensorflow 的 TFLite 研究量化。据我了解,可以量化我的模型权重(以便使用更少的 4 倍内存来存储它们),但这并不一定意味着模型不会将其转换回浮点数来运行它。我还了解到,要仅使用 int 运行我的模型,我需要设置以下参数:
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
Run Code Online (Sandbox Code Playgroud)
我想知道tf.lite.Interpreter设置了这些参数的加载模型与未设置这些参数的加载模型之间有什么区别。我试图对此进行调查.get_tensor_details(),但没有发现任何差异。
在编写仅使用整数运算的算法时,我注意到Python没有利用它.
所以我尝试了以下代码来查看"显式"声明效果
import time
repeat = 1000000
start = time.time()
x = 0
for i in range(repeat):
x += 1
no_type_time = time.time() - start
start = time.time()
y = int(0)
for i in range(repeat):
y += 1
int_time = time.time() - start
print('{} - No type'.format(no_type_time))
print('{} - Int'.format(int_time))
Run Code Online (Sandbox Code Playgroud)
代码输出如下:
0.0692429542542 - No type
0.0545210838318 - Int
Run Code Online (Sandbox Code Playgroud)
我认为它与Python是一种动态类型语言有关.但是当我试图找出变量的类型时,使用type(x)和type(y)都输出int.这很奇怪,因为我还使用x = float(0)运行了一些测试,结果非常接近没有类型"声明"的测试.
我想知道它为什么会发生,如果可能的话,可以从Python文档中获得一些解释它的参考.