priya_ws asked . 2021-07-19

How can I train a neural network such that the output of

How can I train a neural network such that the output of the network satisfies a certain constraint function in the Neural Network Toolbox 5.0 (R2006a)?

I would like to train a neural nework so that the outputs satisfy certain constraints captured by a certain function.

neural , net , train , optimize , constrain , constrained

Expert Answer

John Williams answered . 2024-12-21 19:53:23

If the target data for training meets the criteria of your constraint function 'F' then the network will naturally tend to meet that criteria without any special effort.
 
It is recommended that the target data be edited if necessary so that it meets the desired criteria, and then the network can be trained normally.
 
However if that is not possible, you can create a custom performance function (in Neural Network Toolbox 5.0 (R2006a)) which optimizes a cost function of the outputs, in addition to mean squared error.
 
This cost function must be a smooth function and returns only '0' or positive values. It should return larger values as the desired criterion is not met and smaller values as the criterion is met. If the desired cost function is a discontinuous function such as the following:
 
    cost = F(y)>0,

then a continuous version of this would need to be created,

   cost = -1 ./ (1 + exp(10*F(y))
The example continous cost function above returns 0 for F(y)>0, 1 for F(y)<0, and crosses F(y)==0 at 0.5.
 
 
Attached is a custom transfer function 'performance1.m' (based on the template file 'template_performance.m') which includes subfunctions (COST_FUNCTION and DCOST_FUNCTION) for calculating cost and derivative of cost at the end.
 
The sum of sin(y)+1 is used for the cost function. You can edit these two subfunctions to implement a performance function with any cost function desired.
 
To use the function,you must set the networks performance function accordingly before training using the statement:
 
 
    net.performFcn = 'performance1';

 You should be aware that this will result in training a network that will attempt to fit both the target data and the cost function, but may not perfectly fit either, especially in cases where the target data violates the cost function.
function out1=performance1(varargin)
%PERFORMANCE Template performance function with cost function.
%
%  WARNING - Future versions of the toolbox may require you to update
%  custom functions.
%
%  Directions for Customizing
%
%    1. Make a copy of this function with a new name
%    2. Edit your new function according to the code comments marked ***
%    3. Type HELP NNPERFORMANCE to see a list of other performance functions.
%
%  Syntax
%
%    perf = template_performance(E,Y,X,FP)
%    dPerf_dy = template_performance('dy',E,Y,X,perf,FP);
%    dPerf_dx = template_performance('dx',E,Y,X,perf,FP);
%    info = template_performance(code)
%
%  Description
%
%    TEMPLATE_PERFORMANCE(E,Y,X,PP) takes E and optional function parameters,
%      E - Matrix or cell array of error vectors.
%      Y - Matrix or cell array of output vectors. (ignored).
%      X  - Vector of all weight and bias values (ignored).
%      FP - Function parameters (ignored).
%     and returns the mean squared error.
%
%    TEMPLATE_PERFORMANCE('dy',E,Y,X,PERF,FP) returns derivative of PERF with respect to Y.
%    TEMPLATE_PERFORMANCE('dx',E,Y,X,PERF,FP) returns derivative of PERF with respect to X.
%    TEMPLATE_PERFORMANCE('name') returns the name of this function.
%    TEMPLATE_PERFORMANCE('pnames') returns the name of this function.
%    TEMPLATE_PERFORMANCE('pdefaults') returns the default function parameters.
%
%  Network Use
%
%    To prepare a custom network to be trained with TEMPLATE_PERFORMANCE set
%    NET.performFcn to 'template_performance'.  This will automatically set
%    NET.performParam to the default functions parameters.

% Copyright 1992-2005 The MathWorks, Inc.

fn = mfilename;
boiler_perform

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Name
function n = name

% *** CUSTOMIZE HERE
% *** Define this functions human readable name
n = 'Template';
% ***

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Parameter Defaults
function fp = param_defaults()
fp = struct;

% *** CUSTOMIZE HERE
% *** Defined this functions parameters here
% ***

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Parameter Names
function names = param_names

% *** CUSTOMIZE HERE
% *** Defined human readable names for this functions parameters, if any
names = {'Param One', 'Param Two'};
% ***

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Parameter Check
function err = param_check(fp)
err = [];

% *** CUSTOMIZE HERE
% *** Return an error string if any function parameter is not defined properly.
if (fp.param1 < -1000)
   err = 'Argument One is less than -1000';
elseif (fp.param2 == 20)
  err = 'Argument Two is 20';
end
% ***

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Apply Performance Function
function perf = performance(e,y,x,fp)

dontcareindices = find(~finite(e));
numdontcares = length(dontcareindices);
e(dontcareindices) = 0;

% *** CUSTOMIZE HERE
% *** Calculate scaler performance from error matrix E, output matrix Y, and
% *** the network weight and bias values vector X.
% *** Zero should indicate perfect performance, with more positive values
% *** indication worse performance.
% *** Don't care elements should effectively be ignored.
numerator = sum(sum(e.^2));
numElements = prod(size(e)) - numdontcares;
if (numElements == 0)
  perf = 0;
else
  mean_square_error = numerator / numElements;
end
perf = mean_square_error + cost_function(y);
% ***

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Derivative of Perf w/respect to Y
function d = derivative_dperf_dy(e,y,x,perf,fp)

dontcareindices = find(~finite(e));
numdontcares = length(dontcareindices);
e(dontcareindices) = 0;

% *** CUSTOMIZE HERE
% *** Calculate derivative of performance with respect to outputs Y
% *** This should include contributions of both error and any direct effect
% *** of outputs.
numElements = prod(size(e)) - length(dontcares);
if (numElements == 0)
  dmse = zeros(size(e));
else
  dmse = e * (2/numElements);
end
d = dmse + dcost_function(y);
% ***

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Derivative of Perf w/respect to X
function d = derivative_dperf_dx(t,y,x,perf,fp)

n = length(x);

% *** CUSTOMIZE HERE
% *** Calculate the Nx1 derivative of performance with respect to the networks
% *** weights and biases.
d = zeros(n,1);
% ***
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Function F
function cost = cost_function(y)

% CUSTOMIZE HERE
% Replace with your own output cost function
% Needs to be continuous and return only 0 or positive values
cost = sum(sin(y)+1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Function F
function dcost = dcost_function(y)

% CUSTOMIZE HERE
% Replace with your own derivative of output cost function
dcost = cos(y);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.