1## Copyright (C) 2007 Michel D. Schmid <michaelschmid@users.sourceforge.net> 2## 3## 4## This program is free software; you can redistribute it and/or modify it 5## under the terms of the GNU General Public License as published by 6## the Free Software Foundation; either version 2, or (at your option) 7## any later version. 8## 9## This program is distributed in the hope that it will be useful, but 10## WITHOUT ANY WARRANTY; without even the implied warranty of 11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12## General Public License for more details. 13## 14## You should have received a copy of the GNU General Public License 15## along with this program; see the file COPYING. If not, see 16## <http://www.gnu.org/licenses/>. 17 18 19## author: msd 20 21 22## load data 23 24mData = load("mData.txt","mData"); 25mData = mData.mData; 26[nRows, nColumns] = size(mData); 27# this file contains 13 columns. The first 12 columns are the inputs 28# the last column is the output 29# remove column 4, 8 and 12 30# 89 rows 31 32## first permute the whole matrix in row wise 33## this won't be used right now for debug and test purpose 34# order = randperm(nRows); 35# mData(order,:) = mData; 36 37mOutput = mData(:,end); 38mInput = mData(:,1:end-1); 39mInput(:,[4 8 12]) = []; # delete column 4, 8 and 12 40 41## now prepare data 42mInput = mInput'; 43mOutput = mOutput'; 44%mOutput = [mOutput; mOutput*4]; 45# now split the data matrix in 3 pieces, train data, test data and validate data 46# the proportion should be about 1/2 train, 1/3 test and 1/6 validate data 47# in this neural network we have 12 weights, for each weight at least 3 train sets.. 48# (that's a rule of thumb like 1/2, 1/3 and 1/6) 49# 1/2 of 89 = 44.5; let's take 44 for training 50nTrainSets = floor(nRows/2); 51# now the rest of the sets are again 100% 52# ==> 2/3 for test sets and 1/3 for validate sets 53nTestSets = (nRows-nTrainSets)/3*2; 54nValiSets = nRows-nTrainSets-nTestSets; 55 56mValiInput = mInput(:,1:nValiSets); 57mValliOutput = mOutput(:,1:nValiSets); 58mInput(:,1:nValiSets) = []; 59mOutput(:,1:nValiSets) = []; 60mTestInput = mInput(:,1:nTestSets); 61mTestOutput = mOutput(:,1:nTestSets); 62mInput(:,1:nTestSets) = []; 63mOutput(:,1:nTestSets) = []; 64mTrainInput = mInput(:,1:nTrainSets); 65mTrainOutput = mOutput(:,1:nTrainSets); 66 67[mTrainInputN,cMeanInput,cStdInput] = prestd(mTrainInput);# standardize inputs 68 69## comments: there is no reason to standardize the outputs because we have only 70# one output ... 71 72# define the max and min inputs for each row 73mMinMaxElements = min_max(mTrainInputN); % input matrix with (R x 2)... 74 75## define network 76nHiddenNeurons = 2; 77nOutputNeurons = 1; 78 79MLPnet = newff(mMinMaxElements,[nHiddenNeurons nOutputNeurons],{"logsig","purelin"},"trainlm","learngdm","mse"); 80## for test purpose, define weights by hand 81MLPnet.IW{1,1}(1,:) = 0.5; 82MLPnet.IW{1,1}(2,:) = 1.5; 83MLPnet.LW{2,1}(:) = 0.5; 84MLPnet.b{1,1}(1,:) = 0.5; 85MLPnet.b{1,1}(2,:) = 1.5; 86MLPnet.b{2,1}(:) = 0.5; 87 88saveMLPStruct(MLPnet,"MLP3test.txt"); 89#disp("network structure saved, press any key to continue...") 90#pause 91 92## define validation data new, for matlab compatibility 93VV.P = mValiInput; 94VV.T = mValliOutput; 95 96## standardize also the validate data 97VV.P = trastd(VV.P,cMeanInput,cStdInput); 98 99#[net,tr,out,E] = train(MLPnet,mInputN,mOutput,[],[],VV); 100MLPnet.trainParam.show = NaN; 101[net] = train(MLPnet,mTrainInputN,mTrainOutput,[],[],VV); 102# saveMLPStruct(net,"MLP3testNachTraining.txt"); 103# disp("network structure saved, press any key to continue...") 104# pause 105# % the names in matlab help, see "train" in help for more informations 106# tr.perf(max(tr.epoch)+1); 107# 108# % make preparations for net test and test MLPnet 109# % standardise input & output test data 110[mTestInputN] = trastd(mTestInput,cMeanInput,cStdInput); 111# % [mTestOutputN] = trastd(mTestOutput,cMeanOutput,cStdOutput); 112# % define unused parameters to get E-variable of simulation 113# Pi = zeros(6,0); 114# Ai = zeros(6,0); 115# % simulate net 116[simOut] = sim(net,mTestInputN);#%,Pi,Ai,mTestOutput);