clearCache
Syntax
Description
clearCache(
clears the trace cache of
the accfun
)AcceleratedFunction
object accfun
Examples
Clear Cache of Accelerated Function
Load the dlnetwork
object and class names from the MAT file dlnetDigits.mat
.
s = load("dlnetDigits.mat");
net = s.net;
classNames = s.classNames;
Accelerate the model loss function modelLoss
listed at the end of the example.
fun = @modelLoss; 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
accfun = AcceleratedFunction with properties: Function: @modelLoss Enabled: 1 CacheSize: 50 HitRate: 0 Occupancy: 0 CheckMode: 'none' CheckTolerance: 1.0000e-04
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"); X = dlarray(X,"SSCB"); T = categorical(classNames(randi(10,[128 1]))); T = onehotencode(T,2)'; T = dlarray(T,"CB"); [loss,gradients,state] = dlfeval(accfun,net,X,T);
View the Occupancy
property of the accelerated function. Because the function has been evaluated, the cache is nonempty.
accfun.Occupancy
ans = 2
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
ans = 0
Model Loss Function
The modelLoss
function takes a dlnetwork
object net
, a mini-batch of input data X
with corresponding target labels T
and returns the loss, the gradients of the loss with respect to the learnable parameters in net
, and the network state. To compute the gradients, use the dlgradient
function.
function [loss,gradients,state] = modelLoss(net,X,T) [Y,state] = forward(net,X); loss = crossentropy(Y,T); gradients = dlgradient(loss,net.Learnables); end
Input Arguments
accfun
— Accelerated function
AcceleratedFunction
object
Accelerated function, specified as an AcceleratedFunction
object.
Version History
Introduced in R2021a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)