%% Clear Cache of Accelerated Function
% Load the dlnetwork object and class names from the MAT file dlnetDigits.mat.

s = load("dlnetDigits.mat");
dlnet = s.dlnet;
classNames = s.classNames;

% Accelerate the model gradients function modelGradients listed at the end of the example.

fun = @modelGradients;
accfun = dlaccelerate(fun);

% Clear any previously cached traces of the accelerated function using the clearCache function.

clearCache(accfun)

% View the properties of the accelerated function. Because the cache is empty, the Occupancy property is 0.

accfun

% The returned AcceleratedFunction object stores the traces of underlying function calls and reuses the cached result when the same input pattern reoccurs. To use the accelerated function in a custom training loop, replace calls to the model gradients function with calls to the accelerated function. You can invoke the accelerated function as you would invoke the underlying function. Note that the accelerated function is not a function handle.
% Evaluate the accelerated model gradients function with random data using the dlfeval function.

X = rand(28,28,1,128,'single');
dlX = dlarray(X,'SSCB');

T = categorical(classNames(randi(10,[128 1])));
T = onehotencode(T,2)';
dlT = dlarray(T,'CB');

[gradients,state,loss] = dlfeval(accfun,dlnet,dlX,dlT);

% View the Occupancy property of the accelerated function. Because the function has been evaluated, the cache is nonempty.

accfun.Occupancy

% Clear the cache using the clearCache function.

clearCache(accfun)

% View the Occupancy property of the accelerated function. Because the cache has been cleared, the cache is empty.

accfun.Occupancy

%
%% Model Gradients Function
% The modelGradients function takes a dlnetwork object dlnet, a mini-batch of input data dlX with corresponding target labels dlT and returns the gradients of the loss with respect to the learnable parameters in dlnet, the network state, and the loss. To compute the gradients, use the dlgradient function.

function [gradients,state,loss] = modelGradients(dlnet,dlX,flT)

[dlYPred,state] = forward(dlnet,dlX);
loss = crossentropy(dlYPred,flT);
gradients = dlgradient(loss,dlnet.Learnables);

end

% Copyright 2020 The MathWorks, Inc.