读取和存储 讨论区

dense实际做的事情是 y = x w^T

1赞

load进checkpoint继续训练就可以了,dataloader不需要改,每个epoch会用完一次。但可能需要的是你如果调了learning rate,可能要改到重新开始的那个epoch

1赞

谢谢,我来试试!

不支持dict类型序列化,教程有误

mydict = {“x”: “1”, “y”: “2”}
filename = "test2.params"
nd.save(filename, mydict)
mydict = {“x”: “1”, “y”: “2”}
filename = "test2.params"
nd.save(filename, mydict)

TypeError Traceback (most recent call last)
in ()
1 mydict = {“x”: “1”, “y”: “2”}
2 filename = “test2.params”
----> 3 nd.save(filename, mydict)

~/miniconda3/envs/gluon/lib/python3.6/site-packages/mxnet/ndarray/utils.py in save(fname, data)
220 if any(not isinstance(k, string_types) for k in str_keys) or
221 any(not isinstance(v, NDArray) for v in nd_vals):
–> 222 raise TypeError(‘save only accept dict str->NDArray or list of NDArray’)
223 keys = c_str_array(str_keys)
224 handles = c_handle_array(nd_vals)

TypeError: save only accept dict str->NDArray or list of NDArray

dict的元素需要是ndarray才可以

Traceback (most recent call last):
File “E:\ProgramData\Anaconda3\lib\site-packages\mxnet\gluon\block.py”, line 348, in save_params
self.collect_params().save(filename, strip_prefix=self.prefix)
File “E:\ProgramData\Anaconda3\lib\site-packages\mxnet\gluon\parameter.py”, line 873, in save
strip_prefix, param.name, strip_prefix))
ValueError: Prefix ‘mlp0_’ is to be striped before saving, but Parameter’s name ‘dense0_weight’ does not start with ‘mlp0_’. this may be due to your Block shares parameters from other Blocks or you forgot to use ‘with name_scope()’ when creating child blocks. For more info on naming, please see http://mxnet.incubator.apache.org/tutorials/basic/naming.html

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “D:/Study/CODE/ML/gluon/DL_Calc/Serialization.py”, line 44, in
net.save_params(filename)
File “E:\ProgramData\Anaconda3\lib\site-packages\mxnet\gluon\block.py”, line 351, in save_params
‘save_parameters may resolve this error.’%e.message)
AttributeError: ‘ValueError’ object has no attribute ‘message’

想请教这个错误如何解决?

from mxnet import nd
from mxnet.gluon import nn

class MLP(nn.Block):
def init(self, **kwargs):
super(MLP, self).init(**kwargs)
self.hidden = nn.Dense(256, activation=‘relu’)
self.output = nn.Dense(10)
def forward(self, x):
return self.output(self.hidden(x))

net = MLP()
net.initialize()

x = nd.random.uniform(shape=(2, 20))
y = net(x)

print(net.collect_params(), ‘\n’)
net.save_parameters(‘mlp.params’)

filename = “…/data/mlp.params”
net.save_parameters(filename)

net2 = MLP()
net2.load_params(‘mlp.params’)

y2 = net2(x)
y2 == y

程序是教程中的例子

1赞

我也遇到了,改用net.save_parameters()就Ok了

请问这些例题中的文件保存到哪个目录了啊?
找到这些文件啦,和程序在一个目录。

怎么样可以保存优化器的状态呢

这个symbol是相当于model structure吧?