Custom weights and training of Neural Network in MATLAB - matlab

Consider this Neural Network:
My Neural Network
the numbers which are on connections in this Neural Network, show the connection weights and numbers on each node (neuron) are thresholds of hard-lim transfer function.
Now here are my two questions:
First: for implementing this neural network I used below code, but I couldn't specify connection weights. How can I specify them?
net=feedforwardnet([2,4]);
net.numInputs=2;
net.inputConnect=[1 1;0 0;0 0];
net.inputs{1}.size=4;
net.inputs{2}.size=4;
net.layers{3}.size=2
view(net)
Second: (x1,x2) is the coordinate of input dots and this Neural Network specify if this dot belong to s1 or s2. For example:
(3,3)∈s2
(0,1)∈s2
(1,3)∈s1
(4,1)∈s1
now how should I train this neural network for this kind of inputs and targets?
(I use MATLAB R2014a and am a little new in MATLAB Neural Network, so please be kind)
Any suggestion will help so much. Thanks in advance.

Related

Supervised Neural Networks

I am reading a lot of articles about neural networks and I found very different information. I understand that the supervised neural network can be also regression and classification. In both cases I can use the sigmoid function but what is the difference?
A single-layer neural network is essentially the same thing as linear regression. That's because of how neural networks work: Each input gets weighted with a weight factor to produce an output, and the weight factors are iteratively chosen in such a way that the error (the discrepancy between the outputs produced by the model and the correct output that should be produced for a given input) is minimised. Linear regression does the same thing. But in a neural network, you can stack several of such layers on top of each other.
Classification is a potential, but by far not the only, use case for neural networks. Conversely, there are classification algorithms that don't use neural networks (e.g. K-nearest neighbours). The sigmoid function is often used as an activation function for the last layer in a classifier neural network.

what can you say about two exactly same neural networks after training on same dataset?

Supposed you have two convolutional neural networks implemented in matlab and composed by these layers:
imageInputLayer
ConvolutionalLayer
maxPoolinglayer
relulayer
softmaxlayer
fullyconnectedlayer
classification layer
Both of these networks have exactly same architecture.
I apply the same method of training for 2 networks with same hyperparameters.
Both of these networks have exactly same weights in their corresponding layers.
That is, both of these networks are a replica of each other.
Both of these networks are trained using exactly same training set and validation set without shuffle.
I am wondering:
Will the scores (training error and validation error) and trained weights be different for both?
Does it depend upon the method for training?
In short: Yes to both - because inital weights are usually initated using random numbers.
A tad less short: A neural network is simply an algorithm, if there is no noise (i.e. randomness) introduced in any function on the way, 2 networks will end up being completely the same.

How to derive the model equation from the probabilistic neural network?

Suppose I am using the MATLAB probabilistic neural network example for a classification problem, as given (click here)
The weights and the bias for the network can be determined as follows;
weights = net.LW
biases = net.b
My question is, how do I get an equations describing the model. I am a beginner in neural network, any detailed explanation/sample code would be most helpful.
Thanks!

How do i take a trained neural network and implement in another system?

I have trained a feedforward neural network in Matlab. Now I have to implement this neural network in C language (or simulate the model in Matlab using mathematical equations, without using direct functions). How do I do that? I know that I have to take the weights and bias and activation function. What else is required?
There is no point in representing it as a mathematical function because it won't save you any computations.
Indeed all you need is the weights, biases, activation and your architecture. I'm assuming it is a simple feedforward network as you said, you need to implement some kind of matrix multiplication and addition in C. Also, you'll need to implement the activation function. After that, you're ready to go. Your feed forward NN is ready to be implemented. If the C code will not be used for training, it won't be necessary to implement the backpropagation algorithm in C.
A feedforward layer would be implemented as follows:
Output = Activation_function(Input * weights + bias)
Where,
Input: (1 x number_of_input_parameters_for_this_layer)
Weights: (number_of_input_parameters_for_this_layer x number_of_neurons_for_this_layer)
Bias: (1 x number_of_neurons_for_this_layer)
Output: (1 x number_of_neurons_for_this_layer)
The output of a layer is the input to the next layer.
After some days of searching, I have found the following webpage to be very useful http://ufldl.stanford.edu/tutorial/supervised/MultiLayerNeuralNetworks/
The picture below shows a simple feedforward neural network. Picture taken from the above website.
In this figure, the circles denote the inputs to the network. The circles labeled “+1” are called bias units, and correspond to the intercept term. The leftmost layer of the network is called the input layer, and the rightmost layer the output layer (which, in this example, has only one node). The middle layer of nodes is called the hidden layer, because its values are not observed in the training set. In this example, the neural network has 3 input units (not counting the bias unit), 3 hidden units, and 1 output unit.
The mathematical equations representing this feedforward network are
This neural network has parameters (W,b)=(W(l),b(l),W(2),b(2)), where we write Wij(l) to denote the parameter (or weight) associated with the connection between unit j in layer l, and unit i in layer l+1. (Note the order of the indices.) Also, bi(l) is the bias associated with unit i in layer l+1.
So, from the trained model, as Mido mentioned in his answer, we have to take the input weight matrix which is W(1), the layer weight matrix which is W(2), biases, hidden layer transfer function and output layer transfer function. After this, use the above equations to estimate the output hW,b(x). A popular transfer function used for a regression problem is tan-sigmoid transfer function in the hidden layer and linear transfer function in the output layer.
Those who use Matlab, these links are highly useful
try to simulate neural network in Matlab by myself
Neural network in MATLAB
Programming a Basic Neural Network from scratch in MATLAB

How to get the exact weights and threshold used by a Neural net (Matlab Neural Network)

After training and testing a neural net on Matlab, I got a satisfactory Net-output.
The problem I am facing now is how to get the weights/bias distributed by the network, as well as the threshold, as I intend to use them on a different program.
I just need a guide on how to retrieve these values from the network
Thanks for your suggestions.
The weights are saved in the network class. The values are contained in
net.IW
net.LW
net.b
where net.IW contains the input weight values, net.LW contains the layer weight values and net.b contains the bias values.
To help you with the implementation of the neural network, you could use genFunction to create a MATLAB function for your neural network.