how to train 1000 neural networks using GPU in Matlab simultaneously? - matlab

everyone!
I tried to use
parfor i = 1 : 1000
net{i} = train(net_sample, x, t,'useGPU','yes');
end;
but it failed.
Is there any way to teach them simultaneously?
Is there any code for other programming language to train multiple networks simultaneously?
For simple example,
let's assume that We have network which contains 2 x 2 x 1 neurons and takes 10 input train vectors 5 x 1.

If you don't have more than one GPU, there's just no point in doing this. Every worker in your pool is competing for the same resources, and if your network is anything more than trivially small the GPU will be fully utilized so you can't even get any benefit out of using MPS.

Related

Neuronal networks for Julia - first steps

I would like to use neuronal networks and related concepts with Julia. The idea is to start with Tic-Tac-Toe, then look at more general m,n,k-games. I would like to be flexible for experiments with different networks, size and number of hidden layers etc.
A simple way to represent the board is using an m * n matrix with +1, -1, 0 for “X”, “O”, “empty cell”. Therefore the network should have the m * n input layer, at least one hidden layer and the m * n output layer indicating the probability of the next move per cell.
I had a look at the available resources in https://juliapackages.com/c/ai
Which package do you propose to start with? What about ease of use and performance? (I don’t need a GPU backend)

Conceptual issues on training neural network wih particle swarm optimization

I have a 4 Input and 3 Output Neural network trained by particle swarm optimization (PSO) with Mean square error (MSE) as the fitness function using the IRIS Database provided by MATLAB. The fitness function is evaluated 50 times. The experiment is to classify features. I have a few doubts
(1) Does the PSO iterations/generations = number of times the fitness function is evaluated?
(2) In many papers I have seen the training curve of MSE vs generations being plot. In the picture, the graph (a) on left side is a model similar to NN. It is a 4 input-0 hidden layer-3 output cognitive map. And graph (b) is a NN trained by the same PSO. The purpose of this paper was to show the effectiveness of the new model in (a) over NN.
But they mention that the experiment is conducted say Cycles = 100 times with Generations =300. In that case, the Training curve for (a) and (b) should have been MSE vs Cycles and not MSE vs PSO generations ? For ex, Cycle1 : PSO iteration 1-50 --> Result(Weights_1,Bias_1, MSE_1, Classification Rate_1). Cycle2: PSO iteration 1- 50 -->Result(Weights_2,Bias_2, MSE_2, Classification Rate_2) and so on for 100 Cycles. How come the X axis in (a),(b) is different and what do they mean?
(3) Lastly, for every independent run of the program (Running the m file several times independently, through the console) , I never get the same classification rate (CR) or the same set of weights. Concretely, when I first run the program I get W (Weights) values and CR =100%. When I again run the Matlab code program, I may get CR = 50% and another set of weights!! As shown below for an example,
%Run1 (PSO generaions 1-50)
>>PSO_NN.m
Correlation =
0
Classification rate = 25
FinalWeightsBias =
-0.1156 0.2487 2.2868 0.4460 0.3013 2.5761
%Run2 (PSO generaions 1-50)
>>PSO_NN.m
Correlation =
1
Classification rate = 100
%Run3 (PSO generaions 1-50)
>>PSO_NN.m
Correlation =
-0.1260
Classification rate = 37.5
FinalWeightsBias =
-0.1726 0.3468 0.6298 -0.0373 0.2954 -0.3254
What should be the correct method? So, which weight set should I finally take and how do I say that the network has been trained? I am aware that evolutionary algorithms due to their randomness will never give the same answer, but then how do I ensure that the network has been trained?
Shall be obliged for clarification.
As in most machine learning methods, the number of iterations in PSO is the number of times the solution is updated. In the case of PSO, this is the number of update rounds over all particles. The cost function here is evaluated after every particle is updated, so more than the number of iterations. Approximately, (# cost function calls) = (# iterations) * (# particles).
The graphs here are comparing different classifiers, fuzzy cognitive maps for graph (a) and a neural network for graph (b). So the X-axis displays the relevant measures of learning iterations for each.
Every time you run your NN you initialize it with different random values, so the results are never the same. The fact that the results vary greatly from one run to the next means you have a convergence problem. The first thing to do in this case is to try running more iterations. In general convergence is a rather complicated issue and the solutions vary greatly with applications (and read carefully through the answer and comments Isaac gave you on your other question). If your problem persists after increasing the number of iterations, you can post it as a new question, providing a sample of your data and the actual code you use to construct and train the network.

Matlab Neural Network Advice

I am working currently on a project to optimize heater performance using MATLAB neural network tool, I read the manuals and got the guidance from MATLAB manual.
I have configured the network and tested it, what I need is two points:
1. Am I on the right track? is my network correct? I need an expert advise
2. I need to (Optimize) the performance of the heater, I have defined my function but I don't have a clue how to integrate the network in the optimization of the function.
my network is as follows
3 inputs x1 x2 x3
one out put
load input1
load input2
load input3
x1= importdata('input1.txt'); (similar the other inputs and output)
[x1n,x1min,x1max]=norm_nn(x1); ( I worte my own normalization function)
IN=[x1n x2n x3n]';
OUT=[y1n]';
INTRAIN = IN(:,1:1307);
OUTTRAIN = OUT(:,1:1307);
INTEST =IN(:,1308 : 1634);
OUTTEST = OUT(:,1308:1634);
NETWORKNet1 = newff(IN,OUT,[20 20 20], {'tansig' 'tansig' }, 'trainbr');
net = init (NETWORKNet1);
NETWORKNet1 = trainbr(NETWORKNet1,INTRAIN,OUTTRAIN);
YtestNwt1 = sim(NETWORKNet1,INTEST);
y1testd=denorm_nn7(YtestNet1(1,:),y1min,y1max);
e1=er8(y1testd,y1(1308:1634));
save Net1
I have used (1634 data points and divided it for training (80%) and test (20%))
Here is some advice:
(A) Use feedforwardnet as newff is deprecated
(B) Plot the training, test data and the network result to make it easier to visualize what's going on.
(C) By writing [20 20 20] your network has 3 hidden layers. The vast majority of problems require only 1 hidden layer. Only if all other avenues have been exhausted should you move to multiple hidden layers.
(D) Test the network (ie, the sim command) on the training data first. This is an 'easy' test for a neural network and should be working first before you move on. Then you can test it with the test data (which the network was not trained on). This will show if the network has generalized the shape of the data it is trying to learn.
Validation is also another important factor which helps the network to generalize. If you look at the matlab neural network training window (nntraintool) and click 'performance', one of the graphs should be labelled 'validation'.
Regarding your specific questions:
1. Is my network correct? - difficult to say without seeing the dataset.
2. Optimizing performance of the heater - on a simple level you would have a single output neuron, a number between 0 and 1 which denotes heater performance. The input neurons then contain any other parameters involved.
But now, the network can only predict what the performance will be, given any combination of inputs. It won't be able to tell you which inputs will give you maxmimum output. For only 3 inputs, with low resolution / granularity, you could try an exhaustive / brute force search. Otherwise, look into genetic algorithms to quickly find a good solution.

How to use trained Neural Network in Matlab for classification in a real system

I have trained Feed Forward NN using Matlab Neural Network Toolbox on a dataset containing speech features and accelerometer measurements. Targetset contains two target classes for dataset: 0 and 1. The training, validation and performance are all fine and I have generated code for this network.
Now I need to use this neural network in real-time to recognize pattern when occur and generate 0 or 1 when I test a new dataset against previously trained NN. But when I issue a command:
c = sim(net, j)
Where "j" is a new dataset[24x11]; instead 0 or 1 i get this as an output (I assume I get percent of correct classification but there is no classification result itself):
c =
Columns 1 through 9
0.6274 0.6248 0.9993 0.9991 0.9994 0.9999 0.9998 0.9934 0.9996
Columns 10 through 11
0.9966 0.9963
So is there any command or a way that I can actually see classification results? Any help highly appreciated! Thanks
I'm no matlab user, but from a logical point of view, you are missing an important point:
The input to a Neural Network is a single vector, you are passing a matrix. Thus matlab thinks that you want to classify a bunch of vectors (11 in your case). So the vector that you get is the output activation for every of these 11 vectors.
The output activation is a value between 0 and 1 (I guess you are using the sigmoid), so this is perfectly normal. Your job is to get a threshold that fits your data best. You can get this threshold with cross validation on your training/test data or by just choosing one (0.5?) and see if the results are "good" and modify if needed.
NNs normally convert their output to a value within (0,1) using for example the logistic function. It's not a percentage or probability, just a relative measure of certainty. In any case this means is that you have to manually use a threshold (such as 0.5) to discriminate the two classes. Which threshold is best is tough to find because you must select the optimum trade off between precision and recall.

How to implement Q-learning with a neural network?

I have created a neural network with 2 inputs nodes, 4 hidden nodes and 3 output nodes. The initial weights are random between -1 to 1. I used backpropagation method to update the network with TD error. However, the performance is not good.
I want to know, where the problem might be?
1. Is a bias node necessary?
2. Are eligibility traces necessary?
If anyone can provide me any sample code, I'm very grateful.
Yes, you should include the bias nodes, and yes you should use eligibility traces. The bias nodes just give one additional tunable parameter. Think of the neural network as a "function approximator" as described in Sutton and Barto's book (free online). If the neural network has parameters theta (a vector containing all of the weights in the network), then the Sarsa update is just (using LaTeX notation):
\delta_t = r_t + \gamma*Q(s_{t+1},a_{t+1},\theta_t) - Q(s_t,a_t, \theta_t)
\theta_{t+1} = \theta_t + \alpha*\delta_t*\frac{\partial Q(s,a,\theta)}{\partial \theta}
This is for any function approximator Q(s,a,\theta), which estimates Q(s,a) by tuning its parameters, \theta.
However, I must ask why you're doing this. If you're just trying to get Q learning working really well, then you should use the Fourier Basis instead of a neural network:
http://all.cs.umass.edu/pubs/2011/konidaris_o_t_11.pdf
If you really want to use a neural network for RL, then you should use a natural actor-critic (NAC). NACs follow something called the "natural gradient," which was developed by Amari specifically to speed up learning using neural networks, and it makes a huge difference.
We need more information. What is the problem domain. What are the inputs? What are the outputs?
RL can take a very long time to train and, depending on how you're training, can go from good to great to good to not-so-good during training. Therefore, you should plot the performance of your agent during learning, not just the end result.
You always should use bias nodes. Eligibility traces? Probably not.