How to use a self-made loss function for a simple Neural Net ?

3 次查看(过去 30 天)
I have been using
net = feedforwardnet(10) %or
net = fitnet(10)
for my regression problem. 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.
Thanks!

回答(1 个)

yanqi liu
yanqi liu 2021-12-29
编辑:yanqi liu 2021-12-29
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'
for more information,please check
  2 个评论
Neelabh Jyoti Saharia
Hi sir,
I can see in line 141 that the performFcn is still 'mse'.
I want to replace 'mse' with my self-made loss function.
Can I get a template of the default mse.m file used by Matlab?
yanqi liu
yanqi liu 2021-12-31
yes,sir,just as
\toolbox\nnet\nnet\nnperformance
format,we can make the same functions,such as
then we use
clc; clear all; close all;
warning off all
net=newff([0,1],[5,1],{'tansig','logsig'},'traingd');
net.performFcn
net.performFcn = 'self_made_loss_function';
net.performFcn
can get result
ans =
'mse'
ans =
'self_made_loss_function'
>>

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Deep Learning Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by