predicting crossproduct and magnitude of a vector
2 views (last 30 days)
Show older comments
I do programming exercises and need to answer if it’s possible to predict the magnitude of a vector in using neural networks. Here we assume that any element of a vector is a real number between -10 and 10. For this exo I'm supposed to use feedforwardnet function.Here is my attempt to code it:
arr = {};
vals = [];
for c = 1:10
arr{c} = -10 + (10+10)*rand(3,1);
vals(c) = norm(arr{c});
end
A = cell2mat(arr);
net = feedforwardnet([3 2]);
net.layers{1}.transferFcn = 'tansig';
net.layers{2}.transferFcn = 'purelin';
net = train(net, A,vals);
view(net)
arr2 = {};
vals2 = [];
for c = 1:10
arr2{c} = -10 + (10+10)*rand(3,1);
vals2(c) = norm(arr2{c});
end
A2 = cell2mat(arr2);
y2 = cell2mat(net(arr2));
err = immse(y2, vals2);
Probably I messed up with number of layers and functions. Also I don't have enough base in machine learning to answer if this task is possible for NN, so all ideas about it are welcome. And I wonder if it would be possible to predict a cross product of 2 vectors in ?
0 Comments
Accepted Answer
Chunru
on 23 Jun 2022
Multiple-layer feedforward network is a universal approximater (with some nonlinear transfer functions). So you can predict the vector norm.
ntrain = 100000; % need more train data
Xtrain = rand(3, ntrain)*20-10; % Train input
Ttrain = sqrt(sum(Xtrain.^2)); % Train target
net = feedforwardnet([3 2]);
net.layers{1}.transferFcn = 'tansig';
net.layers{2}.transferFcn = 'purelin';
net = train(net, Xtrain, Ttrain);
ntest = 1000; % test data
Xtest = rand(3, ntest)*20-10; % test input
Ttest= sqrt(sum(Xtest.^2)); % test target
Ytest = net(Xtest);
err = std(Ytest-Ttest)
2 Comments
Chunru
on 24 Jun 2022
As universal approximator, it is also possible to predict the cross duct.
ntrain = 100000; % need more train data
% inputs: 1-3 for vector1 and 4-6 for vector2
Xtrain = rand(6, ntrain)*20-10; % Train input for vector1 and vector 2
Ttrain = cross(Xtrain(1:3, :), Xtrain(4:6, :)); % Train target (output size is 3)
net = feedforwardnet([3 2]);
net.layers{1}.transferFcn = 'tansig';
net.layers{2}.transferFcn = 'purelin';
More Answers (0)
See Also
Categories
Find more on Define Shallow Neural Network Architectures 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!