机器学习

DL4J中文文档/模型/模型持久化

2018-11-08  本文已影响23人  hello风一样的男子

神经网络的存储与加载

ModelSerializer(模型序列化器)是一个处理加载和保存模型的类。通过链接显示的示例中保存模型有两种方法。第一个例子保存了一个正常的多层网络,第二个例子保存了一个计算图。

下面是一个基本示例,其中包含使用ModelSerializer类保存计算图的代码,以及使用ModelSerializer保存使用MultiLayer配置构建的神经网络的示例。

RNG 种子

如果你的模型使用概率(即,DropOut/DropConnect),那么单独保存它并在恢复模型之后应用它可能是有意义的;即:

 Nd4j.getRandom().setSeed(12345);
 ModelSerializer.restoreMultiLayerNetwork(modelFile);

image.gif

这将保证会话/JVM之间相等的结果。

模型序列化器


ModelSerializer(模型序列化器)

[源码]

适用于保存/恢复神经网络模型的实用类

writeModel

public static void writeModel(@NonNull Model model, @NonNull File file, boolean saveUpdater) throws IOException 

image.gif

把一个模型写入文件

writeModel

public static void writeModel(@NonNull Model model, @NonNull File file, boolean saveUpdater,DataNormalization dataNormalization) throws IOException 

image.gif

把一个模型写入文件

writeModel

public static void writeModel(@NonNull Model model, @NonNull String path, boolean saveUpdater) throws IOException 

image.gif

把一个模型写入文件路径

writeModel

public static void writeModel(@NonNull Model model, @NonNull OutputStream stream, boolean saveUpdater)
            throws IOException 

image.gif

把一个文件写入到输出流

writeModel

public static void writeModel(@NonNull Model model, @NonNull OutputStream stream, boolean saveUpdater,DataNormalization dataNormalization)
            throws IOException 

image.gif

把一个文件写入到输出流

restoreMultiLayerNetwork

public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull File file) throws IOException 

image.gif

从文件中加载多层网络

restoreMultiLayerNetwork

public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull File file, boolean loadUpdater)
            throws IOException 

image.gif

从文件中加载多层网络

restoreMultiLayerNetwork

public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull InputStream is, boolean loadUpdater)
            throws IOException 

image.gif

从输入流加载多层网络

注:输入流被完全读取并通过该方法关闭。因此,不能重新使用输入流。

restoreMultiLayerNetwork

public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull InputStream is) throws IOException 

image.gif

从输入流加载多层网络

注:输入流被完全读取并通过该方法关闭。因此,不能重新使用输入流。

restoreMultiLayerNetwork

public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull String path) throws IOException 

image.gif

从文件路径中加载多层网络

restoreMultiLayerNetwork

public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull String path, boolean loadUpdater)
            throws IOException 

image.gif

从文件路径中加载多层网络

restoreComputationGraph

public static ComputationGraph restoreComputationGraph(@NonNull String path) throws IOException 

image.gif

从一个文件路径中加载计算图

restoreComputationGraph

public static ComputationGraph restoreComputationGraph(@NonNull String path, boolean loadUpdater)
            throws IOException 

image.gif

从文件路径中加载计算图

restoreComputationGraph

public static ComputationGraph restoreComputationGraph(@NonNull InputStream is, boolean loadUpdater)
            throws IOException 

image.gif

从输入流中加载计算图

restoreComputationGraph

public static ComputationGraph restoreComputationGraph(@NonNull InputStream is) throws IOException 

image.gif

从输入流中加载计算图

restoreComputationGraph

public static ComputationGraph restoreComputationGraph(@NonNull File file) throws IOException 

image.gif

从文件中加载计算图

restoreComputationGraph

public static ComputationGraph restoreComputationGraph(@NonNull File file, boolean loadUpdater) throws IOException 

image.gif

从文件中加载计算图

taskByModel

public static Task taskByModel(Model model) 

image.gif

addNormalizerToModel

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

image.gif

此方法将归一化器追加到给定的持久化模型中。

请注意:文件应该是ModelSerializer早期保存的模型文件。

addObjectToFile

public static void addObjectToFile(@NonNull File f, @NonNull String key, @NonNull Object o)

image.gif

使用Java对象序列化将对象添加到(已经存在的)模型文件中。对象可以恢复 使用 {- link #getObjectFromFile(File, String)}

翻译:风一样的男子

image

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

image
上一篇 下一篇

猜你喜欢

热点阅读