如何加载经过训练的MXnet模型? [英] How to load a trained MXnet model?

查看:205
本文介绍了如何加载经过训练的MXnet模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用MXnet训练了网络,但是不确定如何保存和加载参数以供以后使用。首先,我定义并训练网络:

I have trained a network using MXnet, but am not sure how I can save and load the parameters for later use. First I define and train the network:

    dataIn = mx.sym.var('data')
    fc1 = mx.symbol.FullyConnected(data=dataIn, num_hidden=100)
    act1 = mx.sym.Activation(data=fc1, act_type="relu")
    fc2 = mx.symbol.FullyConnected(data=act1, num_hidden=50)
    act2 = mx.sym.Activation(data=fc2, act_type="relu")
    fc3 = mx.symbol.FullyConnected(data=act2, num_hidden=25)
    act3 = mx.sym.Activation(data=fc3, act_type="relu")
    fc4 = mx.symbol.FullyConnected(data=act3, num_hidden=10)
    act4 = mx.sym.Activation(data=fc4, act_type="relu")
    fc5 = mx.symbol.FullyConnected(data=act4, num_hidden=2)
    lenet = mx.sym.SoftmaxOutput(data=fc5, name='softmax',normalization = 'batch')


# create iterator around training and validation data
train_iter = mx.io.NDArrayIter(data=data[:ntrain], label = phen[:ntrain],batch_size=batch_size, shuffle=True)
val_iter = mx.io.NDArrayIter(data=data[ntrain:], label=phen[ntrain:], batch_size=batch_size)

# create a trainable module on GPU 0
lenet_model = mx.mod.Module(symbol=lenet, context=mx.gpu())
# train with the same
lenet_model.fit(train_iter,
                eval_data=val_iter,
                optimizer='adam',
                optimizer_params={'learning_rate':0.00001},
                eval_metric='f1',
                batch_end_callback = mx.callback.Speedometer(batch_size, 10),
                num_epoch=1000)

此模型在测试集上的性能很好,所以我想保留它。接下来,我保存网络布局和参数化:

This model performs well on the test set, so I want to keep it. Next, I save the network layout and the parameterization:

lenet.save('./testNet_symbol.mxnet')
lenet_model.save_params('./testNet_module.mxnet')

全部我在加载网络时可以找到的文档似乎已在训练例程中实现了保存功能,以在每个时期结束时保存网络参数。在训练过程中我没有设置这些检查点。其他方法使用mx.model.FeedForward类,这似乎不合适。还有其他方法可以从.json文件加载网络,这是我保存功能所没有的。培训结束后,如何保存/加载网络?

All the documentation I can find on loading the network seem to have implemented the save function within the training routine, to save the network parameters at the end of each epoch. I haven't set these checkpoints during the training process Other methods use the mx.model.FeedForward class, which doesn't seem appropriate. Still other methods load the network from a .json file, which I don't have as a result of my save functions. How can I save/load a network after it's already finished training?

推荐答案

您只需执行以下操作即可保存:

You just have to do this instead to save:

lenet_model.save_checkpoint('lenet', num_epoch, save_optimizer_states=True)

如果状态标志设置为True,则将创建3个文件:

This would create 3 files if the states flag is set to True else 2 files:

.params (权重),
.json(符号),
.states

.params (weights), .json (symbol), .states

要加载的

lenet_model = mx.mod.Module.load(prefix,epoch)
lenet_model.bind(for_training=False, data_shapes=[('data', (1,3,224,224))])

这篇关于如何加载经过训练的MXnet模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆