神经网络的存储与加载
ModelSerializer(
模型序列化器)是一个处理加载和保存模型的类。通过链接显示的示例中保存模型有两种方法。第一个例子保存了一个正常的多层网络,第二个例子保存了一个计算图。
下面是一个基本示例,其中包含使用ModelSerializer类保存计算图的代码,以及使用ModelSerializer保存使用MultiLayer配置构建的神经网络的示例。
RNG 种子
如果你的模型使用概率(即,DropOut/DropConnect),那么单独保存它并在恢复模型之后应用它可能是有意义的;即:
Nd4j.getRandom().setSeed(12345);
ModelSerializer.restoreMultiLayerNetwork(modelFile);

这将保证会话/JVM之间相等的结果。
模型序列化器
ModelSerializer(模型序列化器)
适用于保存/恢复神经网络模型的实用类
writeModel
public static void writeModel(@NonNull Model model, @NonNull File file, boolean saveUpdater) throws IOException

把一个模型写入文件
- 参数 model 要写入的模型
- 参数 file 要写入的文件
- 参数 saveUpdater 是否保存更新器
- 抛出 IOException
writeModel
public static void writeModel(@NonNull Model model, @NonNull File file, boolean saveUpdater,DataNormalization dataNormalization) throws IOException

把一个模型写入文件
-
参数 model 要写入的模型
-
参数 file 要写入的文件
-
参数 saveUpdater 是否保存更新器
-
参数 dataNormalization 数据归一化
-
抛出 IOException
writeModel
public static void writeModel(@NonNull Model model, @NonNull String path, boolean saveUpdater) throws IOException

把一个模型写入文件路径
- 参数 model 要写入的模型
- 参数 file 要写入的文件路径
- 参数 saveUpdater 是否保存更新器
- 抛出 IOException
writeModel
public static void writeModel(@NonNull Model model, @NonNull OutputStream stream, boolean saveUpdater)
throws IOException

把一个文件写入到输出流
- 参数 model 要写入的模型
- 参数 stream 要写入的输入流
- 参数 saveUpdater 是否保存更新器
- 抛出 IOException
writeModel
public static void writeModel(@NonNull Model model, @NonNull OutputStream stream, boolean saveUpdater,DataNormalization dataNormalization)
throws IOException

把一个文件写入到输出流
-
参数 model 要写入的模型
-
参数 stream 要写入的输入流
-
参数 saveUpdater 是否保存更新器
-
参数 dataNormalization 要被保存的归一化器 (可能为空)
-
抛出 IOException
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull File file) throws IOException

从文件中加载多层网络
- 参数 file 要从中加载的文
- 返回加载的多层网络
- 抛出 IOException
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull File file, boolean loadUpdater)
throws IOException

从文件中加载多层网络
- 参数 file 要从中加载的文
- 参数loadUpdater是否加载更新器
- 返回加载的多层网络
- 抛出 IOException
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull InputStream is, boolean loadUpdater)
throws IOException

从输入流加载多层网络
注:输入流被完全读取并通过该方法关闭。因此,不能重新使用输入流。
-
参数 is 要从中加载的输入流
-
参数loadUpdater是否加载更新器
-
返回加载的多层网络
-
抛出 IOException
-
查看 #restoreMultiLayerNetworkAndNormalizer(InputStream, boolean)
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull InputStream is) throws IOException

从输入流加载多层网络
注:输入流被完全读取并通过该方法关闭。因此,不能重新使用输入流。
- 参数 is 要从中加载的输入流
- 参数loadUpdater是否加载更新器
- 返回加载的多层网络
- 抛出 IOException
- 查看 #restoreMultiLayerNetworkAndNormalizer(InputStream, boolean)
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull String path) throws IOException

从文件路径中加载多层网络
- 参数 path 要从中加载的文路径
- 返回加载的多层网络
- 抛出 IOException
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull String path, boolean loadUpdater)
throws IOException

从文件路径中加载多层网络
-
参数 path 要从中加载的文路径
-
参数loadUpdater是否加载更新器
-
返回加载的多层网络
-
抛出 IOException
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull String path) throws IOException

从一个文件路径中加载计算图
- 参数 path 要从中加载的文路径
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull String path, boolean loadUpdater)
throws IOException

从文件路径中加载计算图
- 参数 path 要从中加载的文件路径
- 参数loadUpdater是否加载更新器
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull InputStream is, boolean loadUpdater)
throws IOException

从输入流中加载计算图
- 参数is 要从中加载的输入流
- 参数loadUpdater是否加载更新器
- 抛出 IOException
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull InputStream is) throws IOException

从输入流中加载计算图
- 参数is 要从中加载的输入流
- 抛出 IOException
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull File file) throws IOException

从文件中加载计算图
- 参数 file 要从中加载的文件
- 抛出 IOException
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull File file, boolean loadUpdater) throws IOException

从文件中加载计算图
- 参数 file 要从中加载的文件
- 参数loadUpdater是否加载更新器
- 抛出 IOException
taskByModel
public static Task taskByModel(Model model)

addNormalizerToModel
public static void addNormalizerToModel(File f, Normalizer<?> normalizer)

此方法将归一化器追加到给定的持久化模型中。
请注意:文件应该是ModelSerializer早期保存的模型文件。
- 参数 f 文件
- 参数 normalizer 归一化器
addObjectToFile
public static void addObjectToFile(@NonNull File f, @NonNull String key, @NonNull Object o)

使用Java对象序列化将对象添加到(已经存在的)模型文件中。对象可以恢复 使用 {- link #getObjectFromFile(File, String)}
- 参数 f 将对象添加到的文件
- 参数 key 存储对象的键
- 参数 o 使用Java列化存储的对象
翻译:风一样的男子

如果您觉得我的文章给了您帮助,请为我买一杯饮料吧!以下是我的支付宝,意思一下我将非常感激!

网友评论