How to use different dimensions for neural network?

8 views (last 30 days)
I'm trying to write a neural network that calculates the cross product of 2 vectors in which elements are between -1 and 1.
So as Xtrain it gets an array with 3x2 matrices or basically 2 vectors and as Ytrain it's an array of vectors. So dimensions are 3,2,1000 and 3,1000. Here is my code:
Xtrain = zeros(3,2,1000);
Ytrain = zeros(3,1000);
for c = 1:1000
v1 = -1 + (1+1)*rand(3,1);
v2 = -1 + (1+1)*rand(3,1);
C = cross(v1,v2);
m = [v1, v2];
Xtrain(:,:,c) = m;
Ytrain(:,c) = C;
end
net = feedforwardnet([3 2]);
net.layers{1}.transferFcn = 'tansig';
net.layers{2}.transferFcn = 'purelin';
net = train(net, Xtrain, Ytrain);
However Matlab gives 'Inputs X is not two-dimensional.' error. I wonder what is the best solution here? One of my ideas is to convert Xtrain into an array of cells, so the cell would consist of 2 vectors but as far as I understand it's an antipattern.

Accepted Answer

Sam Chak
Sam Chak on 23 Jun 2022
Documentation says that Network inputs must be specified as an InputSize-by-BatchSize matrix. Is this approach acceptable?
Xtrain = zeros(6,5);
Ytrain = zeros(3,5);
for c = 1:5
v1 = -1 + (1+1)*rand(3,1);
v2 = -1 + (1+1)*rand(3,1);
C = cross(v1,v2);
m = [v1; v2];
Xtrain(:,c) = m;
Ytrain(:,c) = C;
end
net = feedforwardnet([3 2]);
net.layers{1}.transferFcn = 'tansig';
net.layers{2}.transferFcn = 'purelin';
net = train(net, Xtrain, Ytrain);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!