Matlab's neural network (1) BP network

I just started learning neural networks recently, and I feel that it is no problem to get in touch with shallow understanding at the beginning, but deep understanding still needs continuous practice. BP is a feed-forward operation, but the error can be fed back; according to the gradient descent method to continuously adjust the weight (easy to fall into a local minimum), until the final data can meet the demand; you can look at the newff function Usage matlab explains newff (feedforward and backpropagation network)_xiaotao_1's blog-CSDN blog_matlab newffz

I feel that this explanation is very good, and I need to understand the parameters inside;

Matlab writing method of BP neural network:

1. Import data (import .mat data, import with load function)

2. Randomly generate a training set and a test set (use the randperm function to scramble the order of the data, and then divide the total data into a training set and a test set)

3. Data normalization (use the mapminmax function for data normalization, mapminmax('apply',n,x), n is the test data, x is the data feature of the training set)

4. Create a BP neural network (newff function, newff(p_train,t_train,9), 9 is the number of hidden layers)

5. Set training parameters

6. Train the network (use the train function for training)

7. Simulation test (simulation test with sim function)

8. Data denormalization (mapminmax('reverse', t_sim, ps_output) for denormalization)

Then perform performance evaluation

9. Relative error

10. Coefficient of determination

11. Results comparison

Then you can perform the operation

% 1. 导入数据
load spectra_data.mat%用load函数

% 2. 随机产生训练集和测试集
temp = randperm(size(NIR,1));%randperm函数为打乱总体数据的顺序
% 训练集——50个样本
P_train = NIR(temp(1:50),:)';%根据newff函数里面的参数要求,所以要让训练集和测试集的输入数据和输出数据的列相同,所以用’
T_train = octane(temp(1:50),:)';%因为要让训练集和测试集的输入数据和输出数据的列相同,所以用’
% 测试集——10个样本
P_test = NIR(temp(51:end),:)';
T_test = octane(temp(51:end),:)';
N = size(P_test,2);

%% III. 数据归一化
[p_train, ps_input] = mapminmax(P_train,0,1);%用mapminmax函数进行归一化
p_test = mapminmax('apply',P_test,ps_input);%用P_train归一化得到的数据特征将P_test的数据进行相同性质的归一化;

[t_train, ps_output] = mapminmax(T_train,0,1);%对训练集的输出数据进行归一化

%% IV. BP神经网络创建、训练及仿真测试
% 1. 创建网络
net = newff(p_train,t_train,9);%创建BP网络

% 2. 设置训练参数
net.trainParam.epochs = 1000;
net.trainParam.goal = 1e-3;
net.trainParam.lr = 0.01;

% 3. 训练网络
net = train(net,p_train,t_train);

% 4. 仿真测试
t_sim = sim(net,p_test);%用sim函数进行仿真测试

% 5. 数据反归一化
T_sim = mapminmax('reverse',t_sim,ps_output);%ps_output是T_train的归一化数据特征

Advantages and disadvantages of BP neural network:

advantage:

1. Self-learning and adaptive ability

2. Nonlinear mapping capability

3. Have a certain fault tolerance

4. Apply learning outcomes to new knowledge

shortcoming:

1. Easy to fall into local minimum

2. Algorithm convergence speed is slow

3. The choice of network structure, if it is too large, it is easy to overfit, and if it is too small, it is easy to fail to converge

4. Too much reliance on typical samples, requiring a large number of typical samples

Guess you like

Origin blog.csdn.net/new_EAGLE/article/details/125927128