Customized neural network Matlab - matlab

I am struggling to create a customized neural network in MatLab. I've made a sketch of my intended neural network.
To explain better how the network should work:
Two input parameters (features) connected to the first hidden layer with two neurons (exactly equal to the number of input parameters)
Each input parameter is connected to one neuron only.
No bias in the first hidden layer.
All the neurons in the first hidden layer are connected to the neurons in the second layer. There is a bias term in the second layer.
The neurons from the second hidden layer is connected to one output.
For simplicity, I did not show the projection functions in the plots.
Could somebody help me with creating this (probably) simple customized network?
I appreciate your help.

You want a feedforwardnet, in your example you have one layer of 3 neurons and an output layer but no bias on the neurons. For this you'll need to set up your network and change the net.biasConnect element
net = feedforwardnet(3);
net.biasConnect(1) = false;
view(net)
Once you train the network the rest will come into place.

Related

Why do we need layers in artificial neural network?

I am quite new to artificial neural network, and what I cannot understand is why we need the concept of layer.
Isn't is enough to connect each neuron to some other neurons creating a kind of web more then a layered based structure?
For example for solving the XOR we usually need at least 3 layers, 1 input with 2 neurons, 1+ hidden layer(s) with some neurons and 1 output layer with 1 neuron.
Couldn't we create a network with 2 input neurons (we need them) and 1 output connected by a web of other neurons?
Example of what I mean
The term 'layer' is different than you might think. There is always a 'web' of neurons. A layer just denotes a group of neurons.
If I want to connect layer X with layer Y, this means I am connecting all neurons from layer X to all neurons from layer Y. But not always! You could also connect each neuron from layer X to just one neuron in layer Y. There are lots of different connection techniques.
But layers aren't required! It just makes the coding (and explanation) process a whole lot easier. Instead of connecting all neurons one by one, you can connect them in layers. It's far easier to say "layer A and B are connected" than "neuron 1,2,3,4,5 are all connected with neurons 6,7,8,9".
If you are interested in 'layerless' networks, please take a look at Liquid State Machines:
(the neurons might look to be layered, but they aren't!)
PS: I develop a Javascript neural network library, and I have created an onlinedemo in which a neural network evolves to an XOR gate - without layers, just starting with input and output. View it here.. Your example picture is exactly what kind of networks you could develop with this library.

CNN feed forward or back propagtion model

Is convolutional neural network (CNN) a feed forward model or back propagation model. I get this confusion by comparing the blog of DR.Yann and Wikipedia definition of CNN.
A convolutional neural net is a structured neural net where the first several layers are sparsely connected in order to process information (usually visual).
A feed forward network is defined as having no cycles contained within it. If it has cycles, it is a recurrent neural network. For example, imagine a three layer net where layer 1 is the input layer and layer 3 the output layer. A feed forward network would be structured by layer 1 taking inputs, feeding them to layer 2, layer 2 feeds to layer 3, and layer 3 outputs. A recurrent neural net would take inputs at layer 1, feed to layer 2, but then layer two might feed to both layer 1 and layer 3. Since the "lower" layer feeds its outputs into a "higher" layer, it creates a cycle inside the neural net.
Back propagation, however, is the method by which a neural net is trained. It doesn't have much to do with the structure of the net, but rather implies how input weights are updated.
When training a feed forward net, the info is passed into the net, and the resulting classification is compared to the known training sample. If the net's classification is incorrect, the weights are adjusted backward through the net in the direction that would give it the correct classification. This is the backward propagation portion of the training.
So a CNN is a feed-forward network, but is trained through back-propagation.
In short,
CNN is feed forward Neural Network.
Backward propagation is a technique that is used for training neural network.
Similar to tswei's answer but perhaps more concise.
A convolutional Neural Network is a feed forward nn architecture that uses multiple sets of weights (filters) that "slide" or convolve across the input-space to analyze distance-pixel relationship opposed to individual node activations.
Backward propagation is a method to train neural networks by "back propagating" the error from the output layer to the input layer (including hidden layers).

Can a convolutional neural network be built with perceptrons?

I was reading this interesting article on convolutional neural networks. It showed this image, explaining that for every receptive field of 5x5 pixels/neurons, a value for a hidden value is calculated.
We can think of max-pooling as a way for the network to ask whether a given feature is found anywhere in a region of the image. It then throws away the exact positional information.
So max-pooling is applied.
With multiple convolutional layers, it looks something like this:
But my question is, this whole architecture could be build with perceptrons, right?
For every convolutional layer, one perceptron is needed, with layers:
input_size = 5x5;
hidden_size = 10; e.g.
output_size = 1;
Then for every receptive field in the original image, the 5x5 area is inputted into a perceptron to output the value of a neuron in the hidden layer. So basically doing this for every receptive field:
So the same perceptron is used 24x24 amount of times to construct the hidden layer, because:
is that we're going to use the same weights and bias for each of the 24×24 hidden neurons.
And this works for the hidden layer to the pooling layer as well, input_size = 2x2; output_size = 1;. And in the case of a max-pool layer, it's just a max() function on an array.
and then finally:
The final layer of connections in the network is a fully-connected
layer. That is, this layer connects every neuron from the max-pooled
layer to every one of the 10 output neurons.
which is a perceptron again.
So my final architecture looks like this:
-> 1 perceptron for every convolutional layer/feature map
-> run this perceptron for every receptive field to create feature map
-> 1 perceptron for every pooling layer
-> run this perceptron for every field in the feature map to create a pooling layer
-> finally input the values of the pooling layer in a regular ALL to ALL perceptron
Or am I overseeing something? Or is this already how they are programmed?
The answer very much depends on what exactly you call a Perceptron. Common options are:
Complete architecture. Then no, simply because it's by definition a different NN.
A model of a single neuron, specifically y = 1 if (w.x + b) > 0 else 0, where x is the input of the neuron, w and b are its trainable parameters and w.b denotes the dot product. Then yes, you can force a bunch of these perceptrons to share weights and call it a CNN. You'll find variants of this idea being used in binary neural networks.
A training algorithm, typically associated with the Perceptron architecture. This would make no sense to the question, because the learning algorithm is in principle orthogonal to the architecture. Though you cannot really use the Perceptron algorithm for anything with hidden layers, which would suggest no as the answer in this case.
Loss function associated with the original Perceptron. This notion of Peceptron is orthogonal to the problem at hand, you're loss function with a CNN is given by whatever you try to do with your whole model. You can eventually use it, but it is non-differentiable, so good luck :-)
A sidenote rant: You can see people refer to feed-forward, fully-connected NNs with hidden layers as "Multilayer Perceptrons" (MLPs). This is a misnomer, there are no Perceptrons in MLPs, see e.g. this discussion on Wikipedia -- unless you go explore some really weird ideas. It would make sense call these networks as Multilayer Linear Logistic Regression, because that's what they used to be composed of. Up till like 6 years ago.

Disconnect some input-hidden layer connections in MLP neural network in MATLAB

I am using Neural Network (NN) wizard in MATLAB for some implementations. Also i can use code-based version of NN in MATLAB which is available after construction NN by wizard (It is clear!).
When we provide our NN with MATLAB, it is a fully connected input-hidden layer. For example, if you have 4 inputs in the input layer and 2 neurons in the hidden layer, we have fully connected relation between 4 inputs and 2 neurons in hidden layer. I am going to manipulate this connections. For example, disconnect 3rd input connection to 1st neuron and 2nd input connection to 2nd neuron in hidden layer. How is it possible by the MATLAB?
Thank you in advance for any guidance.
I read completely documentation of NN in MATLAB. With the following command we can access to each connection and change their weights and bias so that the desired connection gets off duty!
For a NN with one hidden layer:
Network.IW{1,1} = The matrix of Input weights to Hidden layer.
Network.LW{2,1} = The matrix of Hidden layer weights to Output layer.
Network.b{1,1} = The matrix of bias between Input to Hidden layer.
Network.b{2,1} = The matrix of bias between Hidden layer to Output.
Then, we can set 0 to those connections (both weights and bias) between Input and Hidden layer as our desired. With this type of configuration, we can re-construct the neural network infrastructure.
If you want to randomize this switching off and on, of some nodes, you can also use the dropoutLayer in Matlab. This works best for deep NNs.
https://in.mathworks.com/help/deeplearning/ref/nnet.cnn.layer.dropoutlayer.html

Matlab neural network simulate up to hidden layer

I have trained a 3-layer (input, hidden and output) feedforward neural network in Matlab. After training, I would like to simulate the trained network with an input test vector and obtain the response of the neurons of the hidden layer (not the final output layer). How can I go about doing this?
Additionally, after training a neural network, is it possible to "cut away" the final output layer and make the current hidden layer as the new output layer (for any future use)?
Extra-info: I'm building an autoencoder network.
The trained weights for a trained network are available in the net.LW property. You can use these weights to get the hidden layer outputs
From Matlab Documentation
nnproperty.net_LW
Neural network LW property.
NET.LW
This property defines the weight matrices of weights going to layers
from other layers. It is always an Nl x Nl cell array, where Nl is the
number of network layers (net.numLayers).
The weight matrix for the weight going to the ith layer from the jth
layer (or a null matrix []) is located at net.LW{i,j} if
net.layerConnect(i,j) is 1 (or 0).
The weight matrix has as many rows as the size of the layer it goes to
(net.layers{i}.size). It has as many columns as the product of the size
of the layer it comes from with the number of delays associated with the
weight:
net.layers{j}.size * length(net.layerWeights{i,j}.delays)
Addition to using input and layer weights and biases, you may add a output connect from desired layer (after training the network). I found it possible and easy but I didn't exam the correctness of it.