首页 > 解决方案 > 如何将 TensorFlow keras 模型保存为 .js 文件

问题描述

如何以.js(TensorFlow js)格式保存TensorFlow-Keras模型

标签: tensorflowkerastensorflow.js

解决方案


安装tensorflowjs包后尝试此代码,您可能还需要安装ipython包为

pip install tensorflowjs pip install -U ipython

import tensorflowjs as tfjs
tfjs_target_dir = "./tfjs"
def train():
   model = keras.Sequential([
   keras.layers.Flatten(input_shape=(190, 190)),
   keras.layers.Dense(80, activation='relu'),
   keras.layers.Dense(2, activation='softmax')
  ])   # for example
   model.compile(optimizer='adam',
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy']) # for example
   model.fit(train_data_images, train_data_labels, epochs=15) # for example
   tfjs.converters.save_keras_model(model, tfjs_target_dir)`

推荐阅读