matlab - 在 MATLAB 中使用自动编码器进行函数逼近

标签 matlab machine-learning neural-network autoencoder

我有一个简单的非线性函数 y=x.^2,其中 x 和 y 是 n 维向量,平方是分量平方。我想使用 Matlab 中的自动编码器用低维向量来近似 y 。问题是即使低维空间设置为 n-1,我的重建 y 也会变形。我的训练数据看起来像 this这是一个典型的result从低维空间重构。我的Matlab代码如下。

%% Training data
inputSize=100;
hiddenSize1 = 80;

epo=1000;
dataNum=6000;
rng(123);
y=rand(2,dataNum);
xTrain=zeros(inputSize,dataNum);
for i=1:dataNum
    xTrain(:,i)=linspace(y(1,i),y(2,i),inputSize).^2;
end

%scaling the data to [-1,1]
for i=1:inputSize
    meanX=0.5; %mean(xTrain(i,:));
    sd=max(xTrain(i,:))-min(xTrain(i,:));
    xTrain(i,:) = (xTrain(i,:)- meanX)./sd;
end

%% Training the first Autoencoder

% Create the network. 
autoenc1 = feedforwardnet(hiddenSize1);
autoenc1.trainFcn = 'trainscg';
autoenc1.trainParam.epochs = epo;

% Do not use process functions at the input or output
autoenc1.inputs{1}.processFcns = {};
autoenc1.outputs{2}.processFcns = {};

% Set the transfer function for both layers to the logistic sigmoid
autoenc1.layers{1}.transferFcn = 'tansig';
autoenc1.layers{2}.transferFcn = 'tansig';

% Use all of the data for training
autoenc1.divideFcn = 'dividetrain';
autoenc1.performFcn = 'mae';
%% Train the autoencoder
autoenc1 = train(autoenc1,xTrain,xTrain);
%%
% Create an empty network
autoEncoder = network;

% Set the number of inputs and layers
autoEncoder.numInputs = 1;
autoEncoder.numlayers = 1;

% Connect the 1st (and only) layer to the 1st input, and also connect the
% 1st layer to the output
autoEncoder.inputConnect(1,1) = 1;
autoEncoder.outputConnect = 1;

% Add a connection for a bias term to the first layer
autoEncoder.biasConnect = 1;

% Set the size of the input and the 1st layer
autoEncoder.inputs{1}.size = inputSize;
autoEncoder.layers{1}.size = hiddenSize1;

% Use the logistic sigmoid transfer function for the first layer
autoEncoder.layers{1}.transferFcn = 'tansig';

% Copy the weights and biases from the first layer of the trained
% autoencoder to this network
autoEncoder.IW{1,1} = autoenc1.IW{1,1};
autoEncoder.b{1,1} = autoenc1.b{1,1};


%%
% generate the features
feat1 = autoEncoder(xTrain);

%%
% Create an empty network
autoDecoder = network;

% Set the number of inputs and layers
autoDecoder.numInputs = 1;
autoDecoder.numlayers = 1;

% Connect the 1st (and only) layer to the 1st input, and also connect the
% 1st layer to the output
autoDecoder.inputConnect(1,1) = 1;
autoDecoder.outputConnect(1) = 1;

% Add a connection for a bias term to the first layer
autoDecoder.biasConnect(1) = 1;

% Set the size of the input and the 1st layer
autoDecoder.inputs{1}.size = hiddenSize1;
autoDecoder.layers{1}.size = inputSize;

% Use the logistic sigmoid transfer function for the first layer
autoDecoder.layers{1}.transferFcn = 'tansig';

% Copy the weights and biases from the first layer of the trained
% autoencoder to this network

autoDecoder.IW{1,1} = autoenc1.LW{2,1};
autoDecoder.b{1,1} = autoenc1.b{2,1};

%% Reconstruction
desired=xTrain(:,50);
input=feat1(:,50);
output = autoDecoder(input);

figure
plot(output)
hold on
plot(desired,'r')

最佳答案

我不是 Matlab 用户,但你的代码让我认为你有一个标准的浅层自动编码器。您无法真正使用单个自动编码器来近似非线性,因为它不会比纯线性 PCA 重建更优化(如果您需要,我可以提供更详细的数学推理,尽管这不是 math.stackexchange) 。您需要构建一个深度网络,通过多层线性变换来近似非线性。然后,自动编码器是一个不好选择的模型(今天几乎没有人在实践中使用它们),当你有去噪自动编码器时,它倾向于通过尝试从其噪声版本重建先验来学习更重要的表示。尝试构建深度去噪自动编码器。 This video引入了去噪自动编码器的概念。该类(class)还有一个关于深度降噪自动编码器的视频。

关于matlab - 在 MATLAB 中使用自动编码器进行函数逼近,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34703372/

相关文章:

matlab - 获取矩阵中行的平均值减去 a 值并将其放入另一个矩阵

matlab - 解决最佳控制问题,ode45与fmincon

matlab - 多维数组的滑动最大窗口及其平均值

matlab - 如何使用Excel VBA通​​过matlab在Excel中制作页眉和页脚

python - Udacity 深度学习卷积神经网络 - TensorFlow

python - Wasserstein 损失可以是负数吗?

math - 使用之前的数据来预测下一步(算法+统计)

python - Tensorflow 形状不匹配

python - 无法训练只有一个隐藏层的神经网络

python - 如何运行以张量为范围的循环? (在 tensorflow 中)