Use a custom activation function in neural network toolbox? [Solved]

조회 수: 11 (최근 30일)
Emmanuel Calvet
Emmanuel Calvet 2016년 11월 9일
댓글: Christoph Aistleitner 2022년 2월 21일
Hi everyone,
I've seen that it is possible to create our own transfer function here : https://www.mathworks.com/help/nnet/ug/custom-neural-network-helper-functions.html
So I did what is said :
1) edit hardlim,
2) copy paste in another 'function' file, change the name "mytransfer", save
3) put it in my working directory
% I'm just willing to have a hardlim function with output values between [-1, 1]
Of course, I'm able to compute my function in another file like that :
x = -3:0.1:3;
y = mytransfer(x);
But I can't use it in my neural network :
net=newff([-1 1; -1 1], [2 1], {'mytransfer','mytransfer'}, 'trainlm');% code
it gives me an error, looks like I should create that name ? how to do so ?
Error using feval
Undefined function or variable 'mytransfer.name'.
Error in nnModuleInfo (line 7)
info.name = feval([f '.name']);
Error in nnet7.transfer_fcn (line 6)
info = nnModuleInfo(fcn);
Thanks for helping :)
[EDIT]
Ok I found a more precise answer to my question here, the documentation is a bit fuzzy :
https://www.mathworks.com/matlabcentral/answers/56137-how-to-use-a-custom-transfer-function-in-neural-net-training
So I did what Bob said :
I invented a transfer called "bobfer". To implement it, go into .../toolbox/nnet/nnet/nntransfer.
Copy tansig.m and the folder +tansig to a directory on your path.
Give each a new name, such as bobfer.m and +bobfer.
Modify bobfer.m at the last line so it calls the bobfer.apply method:
a = bobfer.apply(n);
Now, in +bobfer, modify apply.m. This is pretty easy; it's only a few lines. This is where your transfer function goes.
Let me know if this helps...
Bob
  댓글 수: 2
Christoph Aistleitner
Christoph Aistleitner 2022년 2월 21일
Works also in R2021a. For simulink-implementation you need to create a new transferfunction block.
Christoph Aistleitner
Christoph Aistleitner 2022년 2월 21일
In order to create a new transferfunction block, edit the simulink library. Copy paste a existing activation function and break the links. Then replace the logic and rename the block.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

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