Getting NaN values in neural network weight matrices
7 views (last 30 days)
Show older comments
Tulasi Ram Tammu
on 23 Oct 2016
Commented: Tulasi Ram Tammu
on 24 Oct 2016
I am trying to develop a feedforward NN in MATLAB. I have a dataset of 12 inputs and 1 output with 46998 samples. I have some NaN values in last rows of Matrix, because some inputs are accelerations & velocities which are 1 & 2 steps less respectively than displacements. With this current data set I am getting w1_grad & w2_grad as NaN matrices. I tried to remove them using
Heave_dataset(isnan(Heave_dataset))=[];
but my dataset is getting converted into a column matrix of (1*610964).
can anyone help me with this ?
clc;
clear all;
close all;
mkdir('Results//'); %Directory for Storing Results
%%Configurations/Parameters
load 'Heave_dataset'
% Heave_dataset(isnan(Heave_dataset))=[];
nbrOfNeuronsInEachHiddenLayer = 24;
nbrOfOutUnits = 1;
unipolarBipolarSelector = -1; %0 for Unipolar, -1 for Bipolar
learningRate = 0.08;
nbrOfEpochs_max = 50000;
%%Read Data
Input = Heave_dataset(:, 1:length(Heave_dataset(1,:))-1);
TargetClasses = Heave_dataset(:, length(Heave_dataset(1,:)));
%%Calculate Number of Input and Output NodesActivations
nbrOfInputNodes = length(Input(1,:)); %=Dimention of Any Input Samples
nbrOfLayers = 2 + length(nbrOfNeuronsInEachHiddenLayer);
nbrOfNodesPerLayer = [nbrOfInputNodes nbrOfNeuronsInEachHiddenLayer nbrOfOutUnits];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Forward Pass %%%%%%%%%%%
%%Adding the Bias to Input layer
Input = [ones(length(Input(:,1)),1) Input];
%%Weights leading from input layer to hidden layer is w1
w1 = rand(nbrOfNeuronsInEachHiddenLayer,(nbrOfInputNodes+1));
%%Input & output of hidde layer
hiddenlayer_input = Input*w1';
hiddenlayer_output = -1 + 2./(1 + exp(-(hiddenlayer_input)));
%%Adding the Bias to hidden layer
hiddenlayer_output = [ones(length(hiddenlayer_output(:,1)),1) hiddenlayer_output];
%%Weights leading from input layer to hidden layer is w1
w2 = rand(nbrOfOutUnits,(nbrOfNeuronsInEachHiddenLayer+1));
%%Input & output of hidde layer
outerlayer_input = hiddenlayer_output*w2';
outerlayer_output = outerlayer_input;
%%Error Calculation
TotalError = 0.5*(TargetClasses-outerlayer_output).^2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Backward Pass %%%%%%%%%%%
d3 = outerlayer_output - TargetClasses;
d2 = (d3*w2).*hiddenlayer_output.*(1-hiddenlayer_output);
d2 = d2(:,2:end);
D1 = d2' * Input;
D2 = d3' * hiddenlayer_output;
w1_grad = D1/46998 + learningRate*[zeros(size(w1,1),1) w1(:,2:end)]/46998;
w2_grad = D2/46998 + learningRate*[zeros(size(w2,1),1) w2(:,2:end)]/46998;
0 Comments
Accepted Answer
Greg Heath
on 24 Oct 2016
Remove all input and corresponding output vectors if EITHER OR BOTH contain NaNs.
Hope this helps
Thank you for formally accepting my answer
Greg
More Answers (1)
Teja Muppirala
on 24 Oct 2016
Edited: Teja Muppirala
on 24 Oct 2016
Heave_dataset = rmmissing(Heave_dataset);
Or you can do it like this.
Heave_dataset(any(isnan(Heave_dataset),2),:)=[];
You might want to make this two lines, just to be more readable.
missingRows = any(isnan(Heave_dataset),2);
Heave_dataset(missingRows,:) = [];
The way you have written now, it removes the NaN values, but not the rows, so it has to turn the matrix into a long vector of all the values.
See Also
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!