Clear Filters
Clear Filters

How to use a self-made loss function for a simple Neural Network in Matlab?

24 views (last 30 days)
I have been using
net = feedforwardnet(10) %or
net = fitnet(10)
for my regression problem in Matlab. I am using simple networks with 1 or 2 layers and ReLU activation function (net.transferFcn = 'poslin').
But now, I have to use a self-made custom loss functions instead of 'mse' (mean squared error). Could you please let me know how can I do this.
I have found the following document regarding using custom layers and loss functions:https://www.mathworks.com/help/deeplearning/ug/define-custom-regression-output-layer.html
But this is regarding to complex Neural Networks like CNN. I could not understand how to simplify this for a normal deep neural network.

Answers (2)

yanqi liu
yanqi liu on 30 Dec 2021
yes,sir,may be it same on:https://ww2.mathworks.cn/matlabcentral/answers/1618945-how-to-use-a-self-made-loss-function-for-a-simple-neural-net
net=newff([0,1],[5,1],{'tansig','logsig'},'traingd')
Warning: NEWFF used in an obsolete way.
See help for NEWFF to update calls to the new argument list. net = Neural Network name: 'Custom Neural Network' userdata: (your custom info) dimensions: numInputs: 1 numLayers: 2 numOutputs: 1 numInputDelays: 0 numLayerDelays: 0 numFeedbackDelays: 0 numWeightElements: 16 sampleTime: 1 connections: biasConnect: [1; 1] inputConnect: [1; 0] layerConnect: [0 0; 1 0] outputConnect: [0 1] subobjects: input: Equivalent to inputs{1} output: Equivalent to outputs{2} inputs: {1x1 cell array of 1 input} layers: {2x1 cell array of 2 layers} outputs: {1x2 cell array of 1 output} biases: {2x1 cell array of 2 biases} inputWeights: {2x1 cell array of 1 weight} layerWeights: {2x2 cell array of 1 weight} functions: adaptFcn: 'adaptwb' adaptParam: (none) derivFcn: 'defaultderiv' divideFcn: (none) divideParam: (none) divideMode: 'sample' initFcn: 'initlay' performFcn: 'mse' performParam: .regularization, .normalization plotFcns: {'plotperform', 'plottrainstate', 'plotregression'} plotParams: {1x3 cell array of 3 params} trainFcn: 'traingd' trainParam: .showWindow, .showCommandLine, .show, .epochs, .time, .goal, .min_grad, .max_fail, .lr weight and bias values: IW: {2x1 cell} containing 1 input weight matrix LW: {2x2 cell} containing 1 layer weight matrix b: {2x1 cell} containing 2 bias vectors methods: adapt: Learn while in continuous use configure: Configure inputs & outputs gensim: Generate Simulink model init: Initialize weights & biases perform: Calculate performance sim: Evaluate network outputs given inputs train: Train network with examples view: View diagram unconfigure: Unconfigure inputs & outputs
net.performFcn
ans = 'mse'
we can find the default is mse

pathakunta
pathakunta on 26 Jan 2024
I have been using net = feedforwardnet(10) %or net = fitnet(10) for my regression problem in Matlab. I am using simple networks with 1 or 2 layers and ReLU activation function (net.transferFcn = 'poslin'). But now, I have to use a self-made custom loss functions instead of 'mse' (mean squared error). Could you please let me know how can I do this. I have found the following document regarding using custom layers and loss functions:https://www.mathworks.com/help/deeplearning/ug/define-custom-regression-output-layer.html But this is regarding to complex Neural Networks like CNN. I could not understand how to simplify this for a normal deep neural network.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!