如何将TensorFlow Keras模型保存为JSON文件

时间:2019-12-20 05:26:39

标签: tensorflow keras

如何使用TensorFlow在Colab中制作的Keras模型将其与网站集成

我使用了这段代码const saveResult = await model.save('localstorage://my-model-1'),但出现编译错误,为
const saveResult = await model.save('localstorage://my-model-1') ^ SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:0)

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