How can I use "Epoch" as a variable?

Hi,
one option in the ADAM-Training options is to use
'OutputFcn', 'Epoch' ...
right?
Matlab won't recognize 'OutputFcn' as a option in my code....
I mainly need the "Epoch" as a variable, so I can use it as a input variable in one of my layers.
What is the name of the variable "Epoch" within the "trainNetwork" function?

2 Comments

Eli, that was rude of you to erase all your posts. There's a chance that no one will answer you any more because of that.
(Answers Dev) Restored edit

Sign in to comment.

 Accepted Answer

OutputFcn is defined as requiring a function handle or cell array of function handles.
For backwards compatibility, the name of a function might possibly be permitted, but in that case the function name would be searched within the base workspace, so it would need to refer to a "top-level" function -- either a built-in function or else a function that has a .m file named after it. For example if Epoch.m existed then possibly 'Epoch' might be accepted. But considering that ADAM is relatively recent, they might possibly not have bothered to program in support for function names (which have been discouraged in such contexts for over 20 years.) We recommend against using the names of functions.
If Epoch is the name of a variable that is storing a function handle, then use it without quotes, like 'OutputFcn', Epoch
If Epoch is the name of a function, then use @ with it, like 'OutputFcn', @Epoch
I mainly need the "Epoch" as a variable
OutputFcn does not support naming variables to be output, it only supports functions.
You could use 'OutputFcn', @(info)info.Epoch ... but the result is that the code will stop running on the first epoch:
To stop training early, make your output function return 1 (true). If any output function returns 1 (true), then training finishes and trainNetwork returns the latest network
(The reference to "true" tends to imply that really it tests for non-zero to terminate.)
so I can use it as a input variable in one of my layers.
OutputFcn does not permit you to import a computed value into a layer. OutputFcn just runs, and is expected to display something to the user, or plot something, or save a file or append to a shared (or global) variable; the only thing returned by an OutputFcn is whether to stop execution or not.

5 Comments

Use a shared variable initialized to 1. Then use an OutputFcn that increments the variable by 1 each time. Do whatever is appropriate with the variable in your function.
You do not initialize the shared variable within a layer. You want the Epoch count, which is not calculated by layers.
I must admit that I am having difficulty thinking of a situation in which the epoch count would be used in a layer, except possibly in a situation similar to Simulated Annealing in which the "learning rate" is adjusted on a schedule that is related to the number of iterations.
function train_ADAM
%stuff goes here
SharedEpochCount = 1; %shared variable
layers = [...
functionLayer(@SomethingAboutSharedEpoch);
...
]
options = trainingOptions('adam', 'OutputFcn', @AdjustSharedEpoch);
function stop = AdjustSharedEpoch(status)
SharedEpochCount = status.Epoch + 1;
stop = 0;
end
function output = SomethingAboutSharedEpoch(input1, input2, etc)
CurrentEpoch = SharedEpochCount;
stuff
end
end
You want to do something with the Epoch. You would do that something inside SomethingAboutSharedEpoch. I have no idea what you were hoping to do with the epoch number inside your layers.
Please remind us which MATLAB version are you using.
If you try
options = trainingOptions("adam", 'OutputFcn', @fprintf)
then is that rejected by your MATLAB version ?

Sign in to comment.

More Answers (0)

Asked:

Eli
on 26 Sep 2022

Commented:

on 15 Nov 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!