mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

How can i derive weights from a model?

Open diggerdu opened this issue 10 years ago • 7 comments

this is my code:

param = mymodel.get_params()
print param
print type(param["fc1_weight"].asnumpy())

but:

Traceback (most recent call last):
  File "dunet_debug.py", line 58, in <module>
    print type(param["fc1_weight"].asnumpy())
KeyError: 'fc1_weight'

diggerdu avatar Jul 05 '16 12:07 diggerdu

what's the results from print param?

piiswrong avatar Jul 05 '16 18:07 piiswrong

{'optimizer': 'sgd', 'initializer': <mxnet.initializer.Uniform object at 0x7f7d2bbb5310>, 'aux_params': {}, 'symbol': <mxnet.symbol.Symbol object at 0x7f7d2bbd92d0>, 'ctx': [cpu(0)], 'epoch_size': None, 'numpy_batch_size': 128, 'num_epoch': 200, 'begin_epoch': 0, 'allow_extra_params': False, 'arg_params': {'fc2_weight': <mxnet.ndarray.NDArray object at 0x7f7d2bbd9610>, 'fc1_weight': <mxnet.ndarray.NDArray object at 0x7f7d2bbd9490>, 'fc3_bias': <mxnet.ndarray.NDArray object at 0x7f7d2bbd96d0>, 'fc3_weight': <mxnet.ndarray.NDArray object at 0x7f7d2bbd9710>, 'fc2_bias': <mxnet.ndarray.NDArray object at 0x7f7d2bbd9750>, 'fc1_bias': <mxnet.ndarray.NDArray object at 0x7f7d2bbd95d0>}} @piiswrong Thanks

diggerdu avatar Jul 06 '16 07:07 diggerdu

@diggerdu you can try mymodel.get_params()['arg_params']['fc1_weight'].asnumpy()

aigaolc avatar Jul 06 '16 10:07 aigaolc

@Orange15 as before

diggerdu avatar Jul 06 '16 12:07 diggerdu

@diggerdu The inner dictionary has the key fc1_weight and you should be able to access it via param["arg_params"]["fc1_weight"].

sxjscience avatar Jul 06 '16 12:07 sxjscience

@sxjscience 谢谢你,终于好了。 我也叫Xing Jian(行健)

diggerdu avatar Jul 06 '16 12:07 diggerdu

In the latest version 0.10.0, params = model.get_params() weights = model.get_params()[0]['name of the layer'].asnumpy()

sookinoby avatar Aug 16 '17 22:08 sookinoby