Python TFLite转换器:为keras模型实现随机标准,但不为纯TensorFlow模型实现

Python TFLite转换器:为keras模型实现随机标准,但不为纯TensorFlow模型实现,python,python-3.x,tensorflow,keras,tensorflow-lite,Python,Python 3.x,Tensorflow,Keras,Tensorflow Lite,任务 我有两个模型,应该是等效的。第一个是用keras建造的,第二个是用tensorflow建造的。两种变分自动编码器在其模型中使用tf.random.normal方法。而且,它们产生了类似的结果。每件事都通过夜间构建(1.15)进行测试 当我试图将它们转换成一个带有训练后量化的tensorflow lite模型时,会出现混淆。我对两个模型使用相同的命令: converter = tf.compat.v1.lite.TFLiteConverter... # from respective sav

任务

我有两个模型,应该是等效的。第一个是用keras建造的,第二个是用tensorflow建造的。两种变分自动编码器在其模型中使用
tf.random.normal
方法。而且,它们产生了类似的结果。每件事都通过夜间构建(1.15)进行测试

当我试图将它们转换成一个带有训练后量化的tensorflow lite模型时,会出现混淆。我对两个模型使用相同的命令:

converter = tf.compat.v1.lite.TFLiteConverter... # from respective save file
converter.representative_dataset = representative_dataset_gen
converter.optimizations = [tf.lite.Optimize.DEFAULT]

tflite_model = converter.convert()
open('vae.tflite', 'wb').write(tflite_model)
错误

对于keras模型,一切进展顺利,我最终得到了一个工作正常的tflite模型。但是,如果我尝试使用tensorflow版本执行此操作,我会遇到一个错误,指出未实现
RandomStandardNormal

该标准不支持模型中的某些运算符 TensorFlow Lite运行时。如果这些是本机TensorFlow运算符,则 可以通过传递 --启用\u选择\u tf\u ops,或者通过设置target\u ops=TFLITE\u内置,在调用时选择\u tf\u ops tf.lite.TFLiteConverter()。否则,如果你有一个习惯 对于它们的实现,您可以使用禁用此错误 --allow_custom_ops,或者在调用tf.lite.TFLiteConverter()时设置allow_custom_ops=True。以下是您正在使用的内置运算符列表 使用:添加、扩展、完全连接、泄漏、日志、MUL。这是一份清单 需要自定义实现的运算符的数目: 这是正常的

问题

这对我来说没有意义,因为显然这已经在与keras合作。keras知道我必须明确告诉我的tensorflow模型的一些事情吗

型号

张量流

fc_layer = tf.compat.v1.layers.dense

# inputs have shape (90,)
x = tf.identity(inputs, name='x')

# encoder
outputs = fc_layer(x, 40, activation=leaky_relu)

self.z_mu = fc_layer(outputs, 10, activation=None)
self.z_sigma = fc_layer(outputs, 10, activation=softplus)

# latent space
eps = tf.random.normal(shape=tf.shape((10,)), mean=0, stddev=1, dtype=tf.float32)
outputs = self.z_mu + eps * self.z_sigma

# decoder
outputs = fc_layer(outputs, 40, activation=leaky_relu)

# prediction
x_decoded = fc_layer(outputs, 90, activation=None)
凯拉斯


!!pip安装tensorflow==1.15

import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model_file('model.h5') 
converter.allow_custom_ops = True
tfmodel = converter.convert() 
open ("model.tflite" , "wb") .write(tfmodel)

我为此打开了一个github版本,它提供了所有测试代码:
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model_file('model.h5') 
converter.allow_custom_ops = True
tfmodel = converter.convert() 
open ("model.tflite" , "wb") .write(tfmodel)