其他分享
首页 > 其他分享> > Conver a Keras model to tensorflow lite model

Conver a Keras model to tensorflow lite model

作者:互联网

This is a copy from offical web site https://tensorflow.google.cn/lit/convert

he following example shows how to convert a Keras model into a TensorFlow Lite model.

import tensorflow as tf

# Create a model using high-level tf.keras.* APIs
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(units=1, input_shape=[1]),
    tf.keras.layers.Dense(units=16, activation='relu'),
    tf.keras.layers.Dense(units=1)
])
model.compile(optimizer='sgd', loss='mean_squared_error') # compile the model
model.fit(x=[-1, 0, 1], y=[-3, -1, 1], epochs=5) # train the model
# (to generate a SavedModel) tf.saved_model.save(model, "saved_model_keras_dir")

# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

# Save the model.
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

标签:layers,Dense,keras,tflite,tf,lite,tensorflow,model
来源: https://www.cnblogs.com/clblacksmith/p/15150996.html