Cntk: How do I update the parameters?

Created on 10 May 2018  路  6Comments  路  Source: microsoft/CNTK

I am currently implementing Center loss using CTNK.
But is there a way to update the parameters for the center update?

I already tried the "C.assign" function, but I can not update on dynamic axes!

Most helpful comment

C.assign only assigns to constants or parameters, so you may need to use unpack_batch to convert batch axis to static axis. Besides, note that you need to add the assign node to computation graph by doing something like:

a1 = C.assign(...)
loss = loss + 0*a1

All 6 comments

C.assign only assigns to constants or parameters, so you may need to use unpack_batch to convert batch axis to static axis. Besides, note that you need to add the assign node to computation graph by doing something like:

a1 = C.assign(...)
loss = loss + 0*a1

We have updated the parameter 'unpack_batch' input_variable to 'parameter' using 'C.assign'.
However, if the result is evaluated, the result is not displayed. Is it normal?

What do you mean by "displayed"?


import cntk as C
import numpy as np

data = np.array([[1,2,3]], np.float32)

param = C.Parameter((3))
inputs = C.input_variable((3))
un_inputs = C.unpack_batch(inputs)

compute = un_inputs + 3
param =C.assign(param, compute)

Error!

print(param.eval(data))

When I run the above code, I get the following error message.

Traceback (most recent call last):
File "", line 1, in
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\cntk\ops\functions.py", line 708, in eval
_, output_map = self.forward(arguments, outputs, device=device, as_numpy=as_numpy)
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\cntk\internal\swig_helper.py", line 69, in wrapper
result = f(args, *kwds)
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\cntk\ops\functions.py", line 842, in forward
keep_for_backward)
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\cntk\cntk_py.py", line 1962, in _forward
return _cntk_py.Function__forward(self, *args)
ValueError: AssignNode: All inputs should have same sample layout.

[CALL STACK]

std::enable_shared_from_this:: shared_from_this
- std::enable_shared_from_this:: shared_from_this (x3)
- CNTK::Internal:: UseSparseGradientAggregationInDataParallelSGD
- std::enable_shared_from_this:: shared_from_this
- CNTK::Internal:: UseSparseGradientAggregationInDataParallelSGD
- CNTK::Function:: Forward
- PyInit__cntk_py (x2)
- PyCFunction_Call
- PyEval_GetFuncDesc
- PyEval_EvalFrameEx (x2)
- PyEval_GetFuncDesc (x2)

This is because your param has different shape than the value assigned to it. Change to param = C.parameter(data.shape) fixed the issue. NOTE that in your input data, the batch size is 2, and to have consistent param shape the batch size cannot be changed.

Wow! The code works normally. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jsundh picture jsundh  路  16Comments

robinhad picture robinhad  路  61Comments

Arminea picture Arminea  路  19Comments

nikosdim1 picture nikosdim1  路  17Comments

StevenGann picture StevenGann  路  125Comments