首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow不能量化整形函数

Tensorflow不能量化整形函数
EN

Stack Overflow用户
提问于 2020-09-04 08:21:18
回答 1查看 626关注 0票数 2

我将训练我的模型量化意识。但是,当我使用它时,tensorflow_model_optimization不能量化tf.reshape函数,并抛出一个错误。

'2.4.0-dev20200903'

  • python
  1. tensorflow版本: 3.6.9

守则:

代码语言:javascript
复制
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '3'
from tensorflow.keras.applications import VGG16
import tensorflow_model_optimization as tfmot
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
quantize_model = tfmot.quantization.keras.quantize_model
inputs = keras.Input(shape=(784,))
# img_inputs = keras.Input(shape=(32, 32, 3))

dense = layers.Dense(64, activation="relu")
x = dense(inputs)
x = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(10)(x)
outputs = tf.reshape(outputs, [-1, 2, 5])
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")

# keras.utils.plot_model(model, "my_first_model.png")


q_aware_model = quantize_model(model)

以及产出:

代码语言:javascript
复制
Traceback (most recent call last):

  File "<ipython-input-39-af601b78c010>", line 14, in <module>
    q_aware_model = quantize_model(model)

  File "/home/essys/.local/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py", line 137, in quantize_model
    annotated_model = quantize_annotate_model(to_quantize)

  File "/home/essys/.local/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py", line 210, in quantize_annotate_model
    to_annotate, input_tensors=None, clone_function=_add_quant_wrapper)
...

  File "/home/essys/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py", line 667, in wrapper
    raise e.ag_error_metadata.to_exception(e)

TypeError: in user code:


    TypeError: tf__call() got an unexpected keyword argument 'shape'

如果有人知道,请帮忙?

EN

回答 1

Stack Overflow用户

发布于 2020-09-10 06:51:05

背后的原因是因为您的层目前还不支持QAT。如果你想量化它,你必须自己用quantize_annotate_layer写你的量化,然后通过quantize_scope传递它,然后用quantize_apply应用到你的模型中,如这里描述的:https://www.tensorflow.org/model_optimization/guide/quantization/training_comprehensive_guide?hl=en#quantize_custom_keras_layer

我已经以batch_norm_layer为例在here中创建了一个

对于QAT层,Tensorflow 2.x是不完整的,请考虑在操作符后面添加FakeQuant来使用tf1.x。

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

https://stackoverflow.com/questions/63737440

复制
相关文章

相似问题

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