Use a custom activation function in neural network toolbox? [Solved]
11 views (last 30 days)
Show older comments
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 Comments
Christoph Aistleitner
on 21 Feb 2022
Works also in R2021a. For simulink-implementation you need to create a new transferfunction block.
Christoph Aistleitner
on 21 Feb 2022
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.
Answers (0)
See Also
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!