reverse engineering a simple neural network
Show older comments
Hello, For teaching purposes, I am interested in using matlab to calculate the weights for a simple neural network classification problem, and then doing the classification myself using net1.IW, net1.LW, net1.b, etc. If I put the code below into a nn_bare.m file and run it, y_matlab and y_my are the same the first time, but y_my is then different the next time I run this. any hints for why the behavior is not consistent? I tried disabling adaptation, and adding a random number seed, but this did not help. Many thanks... E
%%data to be classified:
X=[2 7 0 7 4 3 9 1 8 4 10 6 4 5 2 3 5 8 3 4;
10 6 7 10 8 1 3 4 4 1 1 7 10 8 0 0 2 4 6 9];
y=[3 3 1 3 3 1 2 1 2 1 2 3 3 3 1 1 2 2 1 3];
%%for classification, turn labels into matrix format:
T=zeros(max(y),length(y)); for i=1:length(y); T(y(i),i)=1; end
rng('default'); % for reproducible results, as weights are initialized randomly
net1 = patternnet([5 5]);
net1.divideFcn=''; % don't divide data into training, testing, validation.
[net1,tr] = train(net1,X,T);
net1.adaptFcn=''; % don't change network during usage after training
%%use the trained network to classify a new point:
Xtest=[7 2]'
y_matlab=net1(Xtest)
%%Test matlab's classification manually:
a1 = tansig(net1.IW{1,:}*Xtest + net1.b{1});
a2 = tansig(net1.LW{2,1}*a1 + net1.b{2});
y_my = softmax(net1.LW{3,2}*a2 + net1.b{3});
y_my
Output: >> nn_bare
Xtest =
7
2
y_matlab =
0.0000
1.0000
0.0000
y_my =
0.0000
1.0000
0.0000
>> nn_bare
Xtest =
7
2
y_matlab =
0.0000
1.0000
0.0000
y_my =
0.0000
0.9307
0.0693
>>
Accepted Answer
More Answers (0)
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!