Simple denoising autoencoder for 1D data in Matlab

1.9k views Asked by At

I'm trying to set up a simple denoising autoencoder with Matlab for 1D data. As currently there is no specialised input layer for 1D data the imageInputLayer() function has to be used:

function net = DenoisingAutoencoder(data)
[N, n] = size(data);

%setting up input
X = zeros([n 1 1 N]);
for i = 1:n
    for j = 1:N
        X(i, 1, 1, j) = data(j,i);
    end
end

% noisy X : 1/10th of elements are set to 0
Xnoisy = X;
mask1 = (mod(randi(10, size(X)), 7) ~= 0); 
Xnoisy = Xnoisy .* mask1;

layers = [imageInputLayer([n 1 1]) fullyConnectedLayer(n) regressionLayer()];

opts = trainingOptions('sgdm');
net = trainNetwork(X, Xnoisy, layers, opts);

However, the code fails with this error message:

The output size [1 1 n] of the last layer doesn't match the response size [n 1 1].

Any thoughts on how should the input / layers should be reconfigured? If the fullyConnectedLayer is left out then the code runs fine, but obviously then I'm left without the hidden layer.

1

There are 1 answers

0
vikakise On

The target output should be a matrix, not a 4D tensor.

Here's a working version of the previous code:

function DenoisingAutoencoder(data)
[N, n] = size(data);
X = data;
Xoriginal = data;
Xout = data';

% corrupting the input
zeroMask = (mod(randi(100, size(X)), 99) ~= 0); 
X = X + randn(size(X))*0.05; 
X = X .* zeroMask;

X4D = reshape(X, [1 n 1 N]);

layers = [imageInputLayer([1 n]) fullyConnectedLayer(n) regressionLayer()];

opts = trainingOptions('sgdm');

net = trainNetwork(X4D, Xout, layers, opts);
R = predict(net, X4D)';