将 Keras 模型转换为 TensorFlow Lite 模型

来源——官网
import tensorflow as tf

# Create a model using high-level tf.keras.* APIs

## model = tf.keras.models.Sequential([网络结构])  #描述各层网络
model = tf.keras.models.Sequential([

## tf.keras.layers.Dense() 全连接层作用 全连接层在整个网络卷积神经网络中起到“特征提取器”的作用
    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()方法用于在配置训练方法时,告知训练时用的优化器、损失函数和准确率评测标准
## model.compile(optimizer = 优化器,
  ##                      loss = 损失函数,
  ##                     metrics = ["准确率”])
  
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.
## TFLiteConverter.from_keras_model():用来转换 tf.keras 模型
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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值