无法在数据块中保存keras模型
Unable to save keras model in databricks
我正在保存keras模型
model.save('model.h5')
在数据块中,但模型没有保存,
我也试过在 /tmp/model.h5
中保存 here
但模型没有保存。
保存单元格执行但当我加载模型时它显示没有 model.h5
文件可用。
当我这样做时dbfs_model_path = 'dbfs:/FileStore/models/model.h5' dbutils.fs.cp('file:/tmp/model.h5', dbfs_model_path)
或尝试加载模型
tf.keras.models.load_model("file:/tmp/model.h5")
我收到错误消息 java.io.FileNotFoundException: File file:/tmp/model.h5 does not exist
问题在于 Keras 设计为仅处理本地文件,因此它不理解 URI,例如 dbfs:/
或 file:/
。所以你需要使用本地路径进行保存和加载操作,然后复制文件 to/from DBFS(不幸的是 /dbfs
不能很好地与 Keras 一起工作,因为它的工作方式)。
下面的代码工作得很好。请注意,dbfs:/
或 file:/
仅用于调用 dbutils.fs
命令 - Keras 东西使用本地文件的名称。
- 创建模型并在本地保存为
/tmp/model-full.h5
:
from tensorflow.keras.applications import InceptionV3
model = InceptionV3(weights="imagenet")
model.save('/tmp/model-full.h5')
- 将数据复制到 DBFS 为
dbfs:/tmp/model-full.h5
并检查它:
dbutils.fs.cp("file:/tmp/model-full.h5", "dbfs:/tmp/model-full.h5")
display(dbutils.fs.ls("/tmp/model-full.h5"))
- 从 DBFS 复制文件为
/tmp/model-full2.h5
并加载它:
dbutils.fs.cp("dbfs:/tmp/model-full.h5", "file:/tmp/model-full2.h5")
from tensorflow import keras
model2 = keras.models.load_model("/tmp/model-full2.h5")
我正在保存keras模型
model.save('model.h5')
在数据块中,但模型没有保存,
我也试过在 /tmp/model.h5
中保存 here
但模型没有保存。
保存单元格执行但当我加载模型时它显示没有 model.h5
文件可用。
当我这样做时dbfs_model_path = 'dbfs:/FileStore/models/model.h5' dbutils.fs.cp('file:/tmp/model.h5', dbfs_model_path)
或尝试加载模型
tf.keras.models.load_model("file:/tmp/model.h5")
我收到错误消息 java.io.FileNotFoundException: File file:/tmp/model.h5 does not exist
问题在于 Keras 设计为仅处理本地文件,因此它不理解 URI,例如 dbfs:/
或 file:/
。所以你需要使用本地路径进行保存和加载操作,然后复制文件 to/from DBFS(不幸的是 /dbfs
不能很好地与 Keras 一起工作,因为它的工作方式)。
下面的代码工作得很好。请注意,dbfs:/
或 file:/
仅用于调用 dbutils.fs
命令 - Keras 东西使用本地文件的名称。
- 创建模型并在本地保存为
/tmp/model-full.h5
:
from tensorflow.keras.applications import InceptionV3
model = InceptionV3(weights="imagenet")
model.save('/tmp/model-full.h5')
- 将数据复制到 DBFS 为
dbfs:/tmp/model-full.h5
并检查它:
dbutils.fs.cp("file:/tmp/model-full.h5", "dbfs:/tmp/model-full.h5")
display(dbutils.fs.ls("/tmp/model-full.h5"))
- 从 DBFS 复制文件为
/tmp/model-full2.h5
并加载它:
dbutils.fs.cp("dbfs:/tmp/model-full.h5", "file:/tmp/model-full2.h5")
from tensorflow import keras
model2 = keras.models.load_model("/tmp/model-full2.h5")