Main Content

loss

Compute loss between predicted states and actual states

Since R2023b

    Description

    example

    L = loss(mpnet,statePred,stateActual) computes loss between the predicted states and the actual states while training the Motion Planning Networks (MPNet). The function calculates the loss value by computing the weighted mean square distance between the actual states and the predicted states. When training the network, the goal is to minimize the loss between the predicted outputs and the actual outputs. You can also use the loss function to check the accuracy of a trained MPNet during testing.

    Note

    To run this function, you will require the Deep Learning Toolbox™.

    Examples

    collapse all

    Load a data file containing a pretrained MPNet and training parameters like loss weights, state bounds, and encoding size into the MATLAB® workspace.

    data = load("mazeMapTrainedMPNET")
    data = struct with fields:
          encodingSize: [9 9]
           lossWeights: [100 100 0]
            mazeParams: {[5]  [1]  'MapSize'  [10 10]  'MapResolution'  [2.5000]}
           stateBounds: [3x2 double]
        trainedNetwork: [1x1 dlnetwork]
    
    

    Configure the mpnetSE2 object to use the pretrained MPNet.

    mpnet = mpnetSE2(Network=data.trainedNetwork,StateBounds=data.stateBounds, ...
                    EncodingSize=data.encodingSize);

    Specify the actual state samples for ground truth.

    stateActual = [4 1 pi/4; 6 1 pi/4];

    Specify the predicted state sample values.

    statePred = [5 2 pi/5; 7 4 pi/5];

    To compute loss between the actual states and predicted states, you must normalize the state space variables x and y to the range [0, 1]. Use the local function preprocessVariables to normalize the state variables.

    stateActual = preprocessVariables(stateActual,mpnet.StateBounds);
    statePred = preprocessVariables(statePred,mpnet.StateBounds);

    Compute loss between the predicted state samples and the actual state samples.

    lossvalue = loss(mpnet,statePred,stateActual)
    lossvalue = 
      1x1 dlarray
    
        0.0662
    
    

    Helper Function

    Use the local function preprocessVariables to normalize the state space variables. The function normalizes the state space variables x and y to the range [0, 1].

    The function also adjust the bounds for the orientation angle of the input state space to lie within a specific range, to avoid NaN values.

    function [output] = preprocessVariables(stateIn,stateBounds)
    output = zeros(4,size(stateIn,1));
    for i = 1:size(stateIn,1)
        state = stateIn(i,:);
        cosTheta = cos(state(:,3));
        sinTheta = sin(state(:,3));
        input = [state(:,1:2) cosTheta sinTheta];
    
        thetaBounds = stateBounds(3,:);
        sinThetaLimits = sort(sin(thetaBounds));
        cosThetaLimits = sort(cos(thetaBounds));
        %% Sin limits
        if thetaBounds(1) < pi/2 && thetaBounds(2) > pi/2
            sinThetaLimits(2) = 1;
        end
        if thetaBounds(1) < -pi/2 && thetaBounds(2) > -pi/2
            sinThetaLimits(1) = -1;
        end
        %% Cos limits
        if thetaBounds(1) < 0 && thetaBounds(2) > 0
            cosThetaLimits(2) = 1;
        end
        positionLimits = stateBounds(1:2,:);
        StateBoundsTransformed = [positionLimits; cosThetaLimits; sinThetaLimits];
        minlimit(1,:) = StateBoundsTransformed(:,1);
        maxlimit(1,:) = StateBoundsTransformed(:,2);
        output(:,i) = (input - minlimit)./(maxlimit - minlimit);
    end
    end

    Input Arguments

    collapse all

    Trained MPNet, specified as an mpnetSE2 object.

    Predicted states, specified as a 4-by-n matrix. n is the number of predicted states. Each row is of the form [x y cos(θ) sin(θ)]. x, y, and θ are the state space variables.

    The state space variables x and y must be normalized to the range [0, 1]. The orientation angle θ must be within the range [–pi, pi]. Units are in radians.

    Data Types: single | double

    Actual states, specified as a 4-by-n matrix of row vectors. n is the total number of states. Each row is of the form [x y cos(θ) sin(θ)]. x, y, and θ are the state space variables.

    The state space variables x and y must be normalized to the range [0, 1]. The orientation angle θ must be within the range [-pi, pi]. Units are in radians.

    Data Types: single | double

    Output Arguments

    collapse all

    Computed loss value, returned as a dlarray (Deep Learning Toolbox) object.

    References

    [1] Qureshi, Ahmed Hussain, Yinglong Miao, Anthony Simeonov, and Michael C. Yip. “Motion Planning Networks: Bridging the Gap Between Learning-Based and Classical Motion Planners.” IEEE Transactions on Robotics 37, no. 1 (February 2021): 48–66. https://doi.org/10.1109/TRO.2020.3006716.

    Extended Capabilities

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    Version History

    Introduced in R2023b

    See Also

    Objects

    Functions