如何将 TensorFlow keras 模型保存为 .js 文件

How to save TensorFlow keras model as .js file

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

安装 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)`