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.
Related
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.
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
Neural Networks are mostly used to classify. So, the activation of a neuron in the output layer indicates the class of whatever you are classifying.
Is it possible (and correct) to design a NN to get 3D coordinates? This is, three output neurons with values in ranges, for example [-1000.0, 1000.0], each one.
Yes. You can use a neural network to perform linear regression, and more complicated types of regression, where the output layer has multiple nodes that can be interpreted as a 3-D coordinate (or a much higher-dimensional tuple).
To achieve this in TensorFlow, you would create a final layer with three output neurons, each corresponding to a different dimension of your target coordinates, then minimize the root mean squared error between the current output and the known value for each example.
I am making 8 x 8 tiles of Images and I want to train a RBF Neural Network in Matlab using those tiles as inputs. I understand that I can convert the matrix into a vector and use it. But is there a way to train them as matrices? (to preserve the locality) Or is there any other technique to solve this problem?
There is no way to use a matrix as an input to such a neural network, but anyway this won't change anything:
Assume you have any neural network with an image as input, one hidden layer, and the output layer. There will be one weight from every input pixel to every hidden unit. All weights are initialized randomly and then trained using backpropagation. The development of these weights does not depend on any local information - it only depends on the gradient of the output error with respect to the weight. Having a matrix input will therefore make no difference to having a vector input.
For example, you could make a vector out of the image, shuffle that vector in any way (as long as you do it the same way for all images) and the result would be (more or less, due to the random initialization) the same.
The way to handle local structures in the input data is using convolutional neural networks (CNN).
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