Why newff() does not work properly when I use its arguments! - matlab

My data-set includes 29 inputs and 6 outputs. When I use
net = newff(minmax(Pl),[14 12 8 6]);
to build my feed forward MLP network and train it by
net.trainParam.epochs=50;
net=train(net,Pl,Tl);
the network can not learn my data-set and its error does not decrease below 0.7, but when I use arguments of newff function like this:
net=newff(minmax(Pl),[14 12 8 6],{'tansig' 'tansig' 'tansig' 'purelin'},'trainlm');
the error is decreased very fast and it comes below 0.0001! The unusual note is that when I use the previous code using only one layer including 2 neurons:
net=newff(minmax(Pl),[2 6],{'tansig' 'purelin'},'trainlm');
the error is decreased below 0.2 again and it is doubtful!
Please give me some tips and help me to know what is difference between:
net = newff(minmax(Pl),[14 12 8 6]);
and
net=newff(minmax(Pl),[14 12 8 myANN.m],{'tansig' 'tansig' 'tansig' 'purelin'},'trainlm');
?

I think that the second argument to NEWFF (link requires login) is supposed to be the target vectors, not the size of hidden layers (which is the third argument).
Note that the default transfer function for hidden layers is tansig and for output layer is purelin, and the default training algorithm is trainlm.
Finally, remember that if you want to get reproducible results, you have to manually reset the random number generator to a fixed state at the beginning of each run.

Related

Training simple artifical neural network to solve XOR sometimes doesn't work

I use NN with:
1 input layer (2 neurons),
1 hidden layer (2 neurons),
1 output layer (1 neuron).
I train it using feed forward and backpropagation algorithm. I also initialize weights randomly from range [-1, 1] (also tried [0, 1], but it doesn't change anything actually). And often (like 4/5 times) everything is trained properly (for inputs
[00, 01, 10, 11]
it outputs
[~0.1, ~0.9, ~0.9, ~0.1]
, respectively), but the remaining 1/5 times it outputs like
[~0.5, ~0.6, ~0.4, ~0.1]
(by ~number I mean a value around the number e.g. ~0.1 may be either 0.098 or 0.132 or similiar)
It doesn't matter if I train it for, let's say, 20 seconds or 10 minutes, it's still the same.
I'm pretty sure it's because of the randomly intialized weights, but I'm not sure how to fix that.
How shall I initialize the weights for this problem as well as for others (if it causes the problem)?
How do you do that? Have you any idea what causes this problem or you need some code? Thanks in advance.

Nvidia DIGITS - more than top 5 predicted classes

I am using nvidia-digits to train a CNN and get predictions. However the .../classify_one.json returns top 5 predictions by default. I need a larger n, how can I modify my request param to get the top n for example?
I couldn't find anything in DIGITS documentation.
Try modifying the accuracy_param of your top "Accuracy" layer of the model's prototxt file. You should have something like
accuracy_param {
top_k: 5
}
Change the top_k: 5 to the number you wish to get, e.g., top_k: 10 for getting top 10 classes.

What would be a good neural net model for pick 3 lotteries?

This is just for fun to see if neural network predictions increase my odds of getting pick 3 lotteries correct.
Right now i just have a simple model of 30 input units, 30 hidden units, and 30 output units.
30 because if the pick 3 result was something like 124, i would make so that all my inputs are 0's except input[1] = 1 (because i assign 0 to 9 for the first digit), input[12] = 1 (because i assign 10 to 19 for the middle digit), input[24] = 1 (because i assign 20 to 29 for the last digit). I just do that so that my inputs are able store placement of digits.
i am training it so that if enter inputs for one draw, it gives me outputs for the next draw.
Do you know of a better model (if you have had experience with neural networks that dealt with pick3 lotteries)?

How to find values of the input data in plotsommaphits

I have used SOM tool box in MATLAB or iris data set. following example and using the plotsomhits i can see that how many data values are in each neuron of my neuron grid of the SOM . However I wish to know actual data values which are grouped in every neuron of the given SOM configuration .Is there any way to do it. this is the example I used.
net = selforgmap([8 8]);
view(net)
[net,tr] = train(net,x);
nntraintool
plotsomhits(net,x)
not that hard. plotsomhits just plots "fancily" the results of the simulation of the net.
so if you simulate it and add the "hits" you have the result!
basicaly:
hits=sum(sim(net,x)');
In your net case this was my results, that coincide with the numbers in the plotsomehits
hits= 6 5 0 7 15 7 4 0 8 20 3 3 9 3 0 8 6 3 11 4 5 5 7 10 1
PD: you can learn a lot in this amazing SO answer:
MATLAB: help needed with Self-Organizing Map (SOM) clustering
You need to convert vector to indices first and then you can see what input values a neuron correspond to.
>> input_neuron_mapping = vec2ind(net(x))';
Now, look into the neuron's inputs.
For example, you want to see neuron input values for neuron 2.
>> neuron_2_input_indices = find(input_neuron_mapping == 2)
>> neuron_2_input_values = x(neuron_2_input_indices)
It will display all the input values from your data.
Read here for more details:
https://bioinformaticsreview.com/20220603/how-to-get-input-values-from-som-sample-hits-plot-in-matlab/

Matlab neural networks - bad results

I've got a problem with implementing multilayered perceptron with Matlab Neural Networks Toolkit.
I try to implement neural network which will recognize single character stored as binary image(size 40x50).
Image is transformed into a binary vector. The output is encoded in 6bits. I use simple newff function in that way (with 30 perceptrons in hidden layer):
net = newff(P, [30, 6], {'tansig' 'tansig'}, 'traingd', 'learngdm', 'mse');
Then I train my network with a dozen of characters in 3 different fonts, with following train parameters:
net.trainParam.epochs=1000000;
net.trainParam.goal = 0.00001;
net.traxinParam.lr = 0.01;
After training net recognized all characters from training sets correctly but...
It cannot recognize more then twice characters from another fonts.
How could I improve that simple network?
you can try to add random elastic distortion to your training set (in order to expand it, and making it more "generalizable").
You can see the details on this nice article from Microsoft Research :
http://research.microsoft.com/pubs/68920/icdar03.pdf
You have a very large number of input variables (2,000, if I understand your description). My first suggestion is to reduce this number if possible. Some possible techniques include: subsampling the input variables or calculating informative features (such as row and column total, which would reduce the input vector to 90 = 40 + 50)
Also, your output is coded as 6 bits, which provides 32 possible combined values, so I assume that you are using these to represent 26 letters? If so, then you may fare better with another output representation. Consider that various letters which look nothing alike will, for instance, share the value of 1 on bit 1, complicating the mapping from inputs to outputs. An output representation with 1 bit for each class would simplify things.
You could use patternnet instead of newff, this creates a network more suitable for pattern recognition. As target function use a 26-elements vector with 1 in the right letter's position (0 elsewhere). The output of the recognition will be a vector of 26 real values between 0 and 1, with the recognized letter with the highest value.
Make sure to use data from all fonts for the training.
Give as input all data sets, train will automatically divide them into train-validation-test sets according to the specified percentages:
net.divideParam.trainRatio = .70;
net.divideParam.valRatio = .15;
net.divideParam.testRatio = .15;
(choose you own percentages).
Then test using only the test set, you can find their indices into
[net, tr] = train(net,inputs,targets);
tr.testInd