How to set the encoder transfer function in autoencoder

8 views (last 30 days)
I want to set the encoder transfer function by myself. But I do not how to do it. Can the encoder transfer function be changed by myself

Answers (2)

SANA
SANA on 16 Nov 2018
First you make an autoencoder and generate its function, the code for generating the function of autoencoder is:
autoenc = trainAutoencoder(Data, 300,...
'MaxEpochs', 100,...
'L2WeightRegularization', 0.001,...
'SparsityRegularization', 4,...
'SparsityProportion', 0.05,...
'ScaleData', true);
generateFunction(autoenc)
The generated function open in MATLAB editor with the name of neural_function, I renamed it my_autoencoder and the transfer function is mentioned there, so you can edit it as you wish, code is below:
function [y1] = my_encoder(x1)
%NEURAL_FUNCTION neural network simulation function.
%
% Generated by Neural Network Toolbox function genFunction, 15-Nov-2018 15:50:26.
%
% [y1] = neural_function(x1) takes these arguments:
% x = 5088xQ matrix, input #1
% and returns:
% y = 5088xQ matrix, output #1
% where Q is the number of samples.
%#ok<*RPMT0>
% ===== NEURAL NETWORK CONSTANTS =====
% Input 1
x1_step1.xoffset = [-10;-11;-11;-12;-1]
x1_step1.gain = [0.090;0.9090;0.9090;0.90909;0.0769]
x1_step1.ymin = 0;
% Layer 1
b1 = [-0.115;0.768;0.7066;0.5009303396;0.1249019302]
IW1_1 = [-0.0947 0.7320 0.3146 0.494636173 -0.0951171]
b2 = [3.5409901027442902688;3.6635759144424437928]
LW2_1 = [-1.1707436273955371675 1.5786236406994880177 -1]
% Output 1
y1_step1.ymin = 0;
y1_step1.gain = [0.0909090909090909;0.07]
y1_step1.xoffset = [-10;-11;-11;-12;-12]
% ===== SIMULATION ========
% Dimensions
Q = size(x1,2); % samples
% Input 1
xp1 = mapminmax_apply(x1,x1_step1);
% Layer 1
a1 = logsig_apply(repmat(b1,1,Q) + IW1_1*xp1);
% Layer 2
a2 = logsig_apply(repmat(b2,1,Q) + LW2_1*a1);
% Output 1
y1 = mapminmax_reverse(a2,y1_step1);
end
% ===== MODULE FUNCTIONS ========
% Map Minimum and Maximum Input Processing Function
function y = mapminmax_apply(x,settings)
y = bsxfun(@minus,x,settings.xoffset);
y = bsxfun(@times,y,settings.gain);
y = bsxfun(@plus,y,settings.ymin);
end
% **********************************************
% ************ Enhance encoder here ************
% Sigmoid Positive Transfer Function
function a = logsig_apply(n,~)
a = 1 ./ (1 + exp(-n));
end
% ************ Enhance encoder here ************
% **********************************************
% Map Minimum and Maximum Output Reverse-Processing Function
function x = mapminmax_reverse(y,settings)
x = bsxfun(@minus,y,settings.ymin);
x = bsxfun(@rdivide,x,settings.gain);
x = bsxfun(@plus,x,settings.xoffset);
end
You can change decoder function as well. Enjoy !!!
  1 Comment
huilin zhu
huilin zhu on 18 Nov 2018
Thanks very much.But do you mean I have to edit a autoencoder before I edit the transfer function, and then I have to train my autoencoder again?

Sign in to comment.


Frédéric BERTHOMMIER
Frédéric BERTHOMMIER on 28 Sep 2023
Edited: Frédéric BERTHOMMIER on 28 Sep 2023
I changed the decoder transfer function to recover a PCA equivalent which I checked::
autoenc = trainAutoencoder(Data,4,'MaxEpochs',5000,'DecoderTransferFunction','purelin');
This substitutes to the generation of a .m function as proposed before for obtaining a PCA equivalent. Note that the encoder transfer function cannot be modified.

Community Treasure Hunt

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

Start Hunting!