TensorFlow报错AttributeError: type object ‘TFLiteConverterV2‘ has no attribute ‘from

编程入门 行业动态 更新时间:2024-10-09 17:28:25

TensorFlow<a href=https://www.elefans.com/category/jswz/34/1771188.html style=报错AttributeError: type object ‘TFLiteConverterV2‘ has no attribute ‘from"/>

TensorFlow报错AttributeError: type object ‘TFLiteConverterV2‘ has no attribute ‘from

文章目录

  • 问题描述
  • 解决方案
  • 参考文献

问题描述

pip list | grep tensorflowtensorflow-gpu==2.3.0

使用 TensorFlow2 转换 Keras 模型为 TensorFlow Lite 模型时报错 AttributeError: type object 'TFLiteConverterV2' has no attribute 'from_keras_model_file'

import numpy as np
import tensorflow as tfxs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 0.0, 3.0, 5.0, 7.0], dtype=float)
model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])
modelpile(optimizer='sgd', loss='mean_squared_error')
model.fit(xs, ys, epochs=500)
print(model.predict([10.0]))keras_file = 'linear.h5'
tf.keras.models.save_model(model, keras_file)
converter = tf.lite.TFLiteConverter.from_keras_model_file(model)
tflite_model = converter.convert()
with open('linear.tflite', 'wb') as f:f.write(tflite_model)




解决方案

  1. 加载 Keras 模型 tf.keras.models.load_model()
  2. from_keras_model_file() 改成 from_keras_model()
import numpy as np
import tensorflow as tfxs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 0.0, 3.0, 5.0, 7.0], dtype=float)
model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])
modelpile(optimizer='sgd', loss='mean_squared_error')
model.fit(xs, ys, epochs=500)
print(model.predict([10.0]))keras_file = 'linear.h5'
tf.keras.models.save_model(model, keras_file)
model = tf.keras.models.load_model(keras_file)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open('linear.tflite', 'wb') as f:f.write(tflite_model)




参考文献

  1. TensorFlow Lite 转换器
  2. tf.lite.TFLiteConverter
  3. AttributeError: type object ‘TFLiteConverterV2’ has no attribute ‘from_keras_model_file’

更多推荐

TensorFlow报错AttributeError: type object ‘TFLiteConverterV2‘ has no attribute ‘fr

本文发布于:2024-02-07 05:32:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753414.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:报错   AttributeError   TensorFlow   type   attribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!