Main Content

dljacobian

Jacobian matrix deep learning operation

Since R2024b

    Description

    The Jacobian deep learning operation returns the Jacobian matrix for neural network and model function outputs with respect to the specified input data and operation dimension.

    jac = dljacobian(u,x,dim) returns the Jacobian matrix for the neural network outputs u with respect to the data x for the specified operation dimension.

    example

    jac = dljacobian(u,x,dim,EnableHigherDerivatives=tf) also specifies whether to enable higher derivatives by tracing the backward pass.

    Examples

    collapse all

    Create a neural network.

    inputSize = [16 16 3];
    numOutputChannels = 5;
    
    layers = [
        imageInputLayer(inputSize)
        convolution2dLayer(3,64)
        reluLayer
        fullyConnectedLayer(numOutputChannels)
        softmaxLayer];
    
    net = dlnetwork(layers);

    Load the training data. For the purposes of this example, generate some random data.

    numObservations = 128;
    X = rand([inputSize numObservations]);
    X = dlarray(X,"SSCB");
    
    T = rand([numOutputChannels numObservations]);
    T = dlarray(T,"CB");

    Define a model loss function that takes the network and data as input and returns the loss, gradients of the loss with respect to the learnable parameters, and the Jacobian of the predictions with respect to the input data.

    function [loss,gradients,jac] = modelLoss(net,X,T)
    
    Y = forward(net,X);
    loss = l1loss(Y,T);
    
    X = stripdims(X);
    Y = stripdims(Y);
    
    jac = dljacobian(Y,X,1);
    gradients = dlgradient(loss,net.Learnables);
    
    end

    Evaluate the model loss function using the dlfeval function.

    [loss,gradients,jac] = dlfeval(@modelLoss,net,X,T);

    View the size of the Jacobian.

    size(jac)
    ans = 1×5
    
         5    16    16     3   128
    
    

    Input Arguments

    collapse all

    Input, specified as a traced dlarray matrix.

    When the software evaluates a function with automatic differentiation enabled, the software traces the input dlarray objects. These are some contexts where the software traces dlarray objects:

    • Inside loss functions that the trainnet function evaluates

    • Inside forward functions that custom layers evaluate

    • Inside model and model loss functions that the dlfeval function evaluates

    The sizes of the dimensions not specified by the dim argument must match.

    Input, specified as a traced dlarray object.

    When the software evaluates a function with automatic differentiation enabled, the software traces the input dlarray objects. These are some contexts where the software traces dlarray objects:

    • Inside loss functions that the trainnet function evaluates

    • Inside forward functions that custom layers evaluate

    • Inside model and model loss functions that the dlfeval function evaluates

    The sizes of the dimensions not specified by the dim argument must match.

    Operation dimension of u, specified as a positive integer.

    The dljacobian function treats the remaining dimensions of the data as independent batch dimensions.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Flag to enable higher-order derivatives, specified as one of the following:

    • true — Enable higher-order derivatives. Trace the backward pass so that the returned gradients can be used in further computations for subsequent calls to functions that compute derivatives using automatic differentiation (for example, dlgradient, dljacobian, dldivergence, and dllaplacian).

    • false — Disable higher-order derivatives. Do not trace the backward pass. Use this option when you need to compute first-order derivatives only as this is usually quicker and requires less memory.

    Output Arguments

    collapse all

    Jacobian matrix, returned as an unformatted dlarray object.

    The layout of jac depends on dim and the size of u.

    If size(u,dim) == 1, then jac is a matrix, and:

    • If dim is 1, then jac(j,k) corresponds to u(j,k)=ukx(j,k) and uk corresponds to u(:,k).

    • If dim is 2, then jac(k,j) corresponds to (u)(k,j)=ukx(k,j) and uk corresponds to u(k,:).

    Otherwise, if size(u,dim) > 1, then jac is a 3-D array, and:

    • If dim is 1, then jac(i,j,k) corresponds to (u)(i,j,k)=u(k,i)x(j,k).

    • If dim is 2, then jac(i,k,j) corresponds to (u)(i,k,j)=u(k,i)x(k,j).

    Version History

    Introduced in R2024b