Skip to content

Instantly share code, notes, and snippets.

@hayunjong83
Created November 9, 2021 09:10
Show Gist options
  • Save hayunjong83/6390f325d709b5ba5dc935e0c179b079 to your computer and use it in GitHub Desktop.
Save hayunjong83/6390f325d709b5ba5dc935e0c179b079 to your computer and use it in GitHub Desktop.
udacity TensorFlow Lite Chap.2-3 example code
import tensorflow as tf
# Store data for x and y
x = [-1, 0, 1, 2, 3, 4]
y = [-3,-1, 1, 3, 5, 7]
# Create a simple Keras model
model = tf.keras.models.Sequential(
[tf.keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(x, y, epochs=500)
import pathlib
# Export the SavedModel
export_dir = '/tmp/saved_model'
tf.saved_model.save(model, export_dir)
# Convert the model
converter = tf.lite.TFLiteConverter.from_saved_model(export_dir)
tflite_model = converter.convert()
# Save the model
tflite_model_file = pathlib.Path('/tmp/foo.tflite')
tflite_model_file.write_bytes(tflite_model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment