How we can use vectors in Deep Learning custom training loop?
6 次查看(过去 30 天)
显示 更早的评论
Hi every one.
I am trying to train a CNN with my own optimizer through costum training loop.
[loss,gradient]= dlfeval(@modelgradient,dlnet, Xtrian,YTrain)
myFun = @(dlnet,gradient,loss)myOptimizer(dlnet,gradient,loss,...)
dlnet = dlupdate(myFun,dlnet,gradient,loss)
My optimizer needs w (current parameter vector), g (its corresponding gradient vector), f (its corresponding loss value) and… as inputs. This optimizer needs many computations with w, g, f inside to give w = w + p, p is a optimal vector that my optimizer has to compute it by which I can update my w.
I need something by which I can convert the parameters and gradient of dl format to vectors for those computations inside of my optimizer, then to use above syntax I need to convert vector to dl formats required in loop and in my optimizer as well. This back and forth is necessary for my job for using training loop. Can you help to find functions in the toolbox to do these jobs (vector to table (because gradient and dlnet’s parameters are tables with dlarray cells) and vice versa), or any other solutions?
0 个评论
回答(1 个)
Amanpreetsingh Arora
2020-11-12
Weight parameters of the dlnetwork object can be accessed from the "Learnables" property of the object. Both the "gradient" and this "Learnables" property will return a table with variables "Layer", "Parameter" and "Value". You can access the weight parameter and its corresponding gradient by indexing into the table in a loop as follows.
for i=1:size(dlnet.Learnables,1)
w=dlnet.Learnables{i,"Value"}{1,1};
layerName = dlnet.Learnables{i,"Layer"};
paramName = dlnet.Learnables{i,"Parameter"};
g=gradient{gradient.Layer==layerName & gradient.Parameter==paramName,"Value"}{1,1}
end
For more information on indexing into table, refer to the following documentation.
"w" and "g" will be dlarray objects in the above code snippet. Converting this to double array might be unnecessary as many operations/functions that support double array also support dlarray objects. Refer to the following documentation for a list of functions that support dlarray.
3 个评论
Amanpreetsingh Arora
2020-11-12
编辑:Amanpreetsingh Arora
2020-11-12
"dlupdate" will call "myOptimizer" with weights and gradient of each layer individually. So, the inputs to "myOptimizer" (and even "sgdFunction" in the example) are of type dlarray and "myOptimizer" will be called several times in one iteration with each layer's parameters. Inside "myOptimizer", you won't need table indexing if it is called using "dlupdate". Refer to the following documentation for more information on "dlupdate".
The recommended approach for working with dlarray, as mentioned in the answer, is to perform direct operation on it and not convert to double array. However, if you convert dlarray to double array using "extractdata", you can convert the results of the computation back to dlarray by passing it to "dlarray" function.
For example, to convert a double array X
dlX=dlarray(X);
Refer to the following documentation to know more about "dlarray".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Custom Training Loops 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!