// BP algorithm. cpp: defines the entry point of the console application. <Br/> // </P> <p> # include "stdafx. H "<br/> # include" iostream "<br/> # include <string. h> <br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <math. h> <br/> # include <time. h> </P> <p> # define N4 // column, n-dimensional input data <br/> # define m214 // row, M sample records <br/> # define traincount 150 // number of training samples, 75% of the total number of samples <br/> # define testcount 64 // Number of test samples, 25% of the total samples </P> <p> file * Sample = fopen ("sample.txt", "R "); // file where the sample data is located <br/> file * traindata = fopen ("Traindata.txt", "W +"); // file of the training data <br/> file * test = fopen ("test.txt", "W + "); // file where the test data is located <br/> file * expectation = fopen ("expectation.txt", "W + "); // file of the data to be output <br/> file * testactual = fopen ("testactual.txt", "W + "); // actual value of the data to be predicted in the test set <br/> file * MINMAX = fopen ("minmax.txt", "W + "); // maximum and minimum values </P> <p> float a [m] [N] = {0}; // sample data, n-dimensional, initialization is 0 <br/> float train [traincount] [N] = {0 }; // samples used for training <br/> float test [testcount] [N] = {0 }; // Sample </P> <p> float MAX [N]; // maximum value of each dimension data, not initialized <br/> float Min [N]; // minimum value of each dimension data, not initialized </P> <p> float weight [m] [N]; // weight </P> <p> void readsample () // read sample data <br/> {<br/> If (sample = 0) <br/>{< br/> printf ("sample data not found"); <br/> return; <br/>}< br/> for (INT I = 0; I <m; I ++) <br/>{< br/> for (Int J = 0; j <n; j ++) <br/>{< br/> fscanf (sample, "% F", & A [I] [J]); <br/>}< br/> printf ("the sample data has been read/N "); <br/>}</P> <p> void findminmax () // find the maximum and minimum values <br />{< Br/> for (INT I = 0; I <n; I ++) // For each dimension <br/>{< br/> Min [I] = A [0] [I]; // assume that the minimum value is the first data <br/> MAX [I] = A [0] [I]; // assume that the maximum value is the first data. </P> <p> for (Int J = 0; j <m; j ++) // For each record <br/>{< br/> If (MAX [I] <A [J] [I]) // note the subscript, changed <br/> {<br/> MAX [I] = A [J] [I]; <br/>}< br/> If (Min [I]> A [J] [I]) <br/>{< br/> Min [I] = A [J] [I]; <br/>}< br/> fprintf (MINMAX, "/n dimension % d data, minimum value % F, maximum value % F", I, Min [I], Max [I]); <br/>}< br/> printf ("the maximum and minimum values are searched for/N"); <br/>}< /P> <p> void reflect () // Sample Data normalization <br/> {<br/> for (INT I = 0; I <n; I ++) // every one-dimensional data needs to be normalized <br/>{< br/> for (Int J = 0; j <m; j ++) // each record <br/>{< br/> A [J] [I] = (a [J] [I]-min [I]) /(MAX [I]-min [I]); <br/>}< br/> printf ("Sample Data normalization completed/N "); <br/>}</P> <p> void settrain () // sets the training sample, randomly generate 75% of the total sample capacity <br/>{< br/> srand (unsigned) Time (null )); // generate a random number with the current time as the seed </P> <p> for (Int J = 0; j <n; j ++) // For each one-dimensional data, all training samples are required <br/>{< br/> for (INT I = 0; I <tra Incount; I ++) <br/>{< br/> int r = rand () % m; // generates a random number between 0 and 99. Note: It may be repeated! <Br/> train [I] [J] = A [r] [J]; <br/> // printf ("random number is % d/N", R ); <br/>}< br/> printf ("training sample extraction completed/N "); <br/>}</P> <p> void savetrain () // Save the training set to traindata.txt <br/>{< br/> If (traindata = 0) <br/>{< br/> printf ("sample data file not found"); <br/> return; <br/>}< br/> for (INT I = 0; I <traincount; I ++) <br/>{< br/> for (Int J = 0; j <n; j ++) <br/>{< br/> fprintf (traindata, "% F", train [I] [J]); <br/>}< br/> fprintf (traindata, "/N"); <br/>}< br/> PR INTF ("sample data written/N"); <br/>}</P> <p> void saveexpectation () // Save the expected output data to expectation.txt <br/>{< br/> If (expectation = 0) <br/>{< br/> printf ("sample data file not found "); <br/> return; <br/>}< br/> for (INT I = 0; I <traincount; I ++) <br/>{< br/> fprintf (expectation, "% F/N", train [I] [N-1]); // The last dimension is the dissolved oxygen data <br/>}< br/> printf ("Expected output data write completion/N "); <br/>}< br/> void settest () // set the test sample to generate 25% of the total sample capacity randomly <br/>{< br/> srand (unsigned) time (null); // generate A random number with the current time as the seed </P> <p> for (Int J = 0; j <n; j ++) // For each dimension of data, all training samples are required <br/>{< br/> for (INT I = 0; I <testcount; I ++) <br/>{< br/> int r = rand () % m; // generates a random number between 0 and 99. Note: It may be repeated! <Br/> test [I] [J] = A [r] [J]; <br/> // printf ("random number is % d/N", R ); <br/>}< br/> printf ("training sample extraction completed/N "); <br/>}</P> <p> void savetest () // save test set to test.txt <br/>{< br/> If (test = 0) <br/>{< br/> printf ("test data file not found"); <br/> return; <br/>}< br/> for (INT I = 0; I <testcount; I ++) <br/>{< br/> for (Int J = 0; j <n; j ++) <br/>{< br/> fprintf (test, "% F", test [I] [J]); <br/>}< br/> fprintf (test, "/N"); <br/>}< br/> printf ("Test Data Writing completed/N "); <br/>}</P> <p> void savetestactual () // save test concentration values to testactual.txt <br/>{< br/> If (testactual = 0) <br/>{< br/> printf ("file of parameters to be tested in test data not found"); <br/> return; <br/>}< br/> for (INT I = 0; I <testcount; I ++) <br/>{< br/> fprintf (testactual, "% F/N", test [I] [N-1]); // do oxygen in the last column <br/>}< br/> printf ("the file of the parameter to be tested in the test data has been written/N "); <br/>}</P> <p> void outputsample () // output sample data <br/>{< br/> printf ("sample data is as follows: /n "); <br/> for (INT I = 0; I <m; I ++) <br/> {<br/> for (Int J = 0; j <n; j ++) <br/> {<br/> printf ("% F,", a [I] [J]); <br/>}< br/> printf ("/N"); <br/>}</P> <p> void outputtrain () // output training sample <br/>{< br/> printf ("training data:/N"); <br/> for (INT I = 0; I <traincount; I ++) <br/>{< br/> for (Int J = 0; j <n; j ++) <br/>{< br/> printf ("% F,", train [I] [J]); <br/>}< br/> printf ("/N"); <br/>}</P> <p> void outputtest () // output test sample <br/>{< br/> printf ("test data:/N"); <br/> for (INT I = 0; I <testcount; I ++) <br/> {<br/> for (Int J = 0; j <n; j ++) <br/>{< br/> printf ("% F,", test [I] [J]); <br/>}< br/> printf ("/N"); <br/>}< br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> readsample (); // read the sample <br/> findminmax (); // find the maximum and minimum values of each dimension <br/> reflect (); // normalization </P> <p> settrain (); // randomly select 75% samples as the training set <br/> savetrain (); // Save the training set </P> <p> saveexpectation (); // Save the expected output data </P> <p> settest (); // 25% samples are randomly selected as the test set <br/> savetest (); // Save the test set <br/> savetestactual (); // Save the actual value to be compared <br/> system ("pause"); <br/> return 0; <br/>}
% Import data, and then run the following code
% Enter a three-dimensional training vector. Normalized. Traindata.txt. Import Data
P = traindata (:,:);
P = P'; % transpose
% Output 1-dimensional expected output vector. Normalized. Expectation.txt
T = expectation (:);
T = T'; % transpose
% The value range of the input vector is [0, 1]. Remember that it is a four-dimensional
Threshold = [0 1; 0 1; 0 1; 0 1];
% New BP network, number of hidden layer neurons 9 (adjusted to a large value or a small value based on network performance) 2 * n + 1, n = 4
% Number of output neurons 1,
% Intermediate layer neuron Transfer Function tansig (s tangent function)
% Output-layer neuron Transfer Function logsig (S-type logarithm function)
% Training function traingdx (gradient descent method learning, and the learning rate is adaptive)
Net = newff (threshold, [7 1], {'tansig ', 'logsig'}, 'traingdx ');
% Network training times
Net. trainparam. epochs = 1000;
% Training target
Net. trainparam. Goal = 0.001;
% Start training
Net = train (net, P, T );
Export to test.txt, and then run the following code:
% Followed by the training part.
P_test = test (:,:);
P_test = p_test ';
Y = SIM (net, p_test );
% Add the following statement and save the result to C: // result.txt. It still needs to be processed. Of course, it is much better than direct replication.
% Csvwrite ('C: // result.txt ', Y)
% Z Note: Save the result to your working directory.
Export volume to result.txt and run the following code:
% R = result (:);
% Y = Result = r
Export R to excel
You can also simulate curves in MATLAB. Source Code:
Import testactual.txt,
Comparison with result.txt
Note transpose
% Assume the length is 64
Z = test (:, 4); % actual value
X = 1: 1: 64;
Plot (X, Y, 'B'); % predicted value
Hold on
Plot (x, z, 'R'); % actual value