bp神经网络matlab实例
时间: 2023-09-05 10:13:29 浏览: 104
以下是一个 BP 神经网络的 MATLAB 实例:
假设我们需要训练一个 BP 神经网络来预测一个人的体重,基于他们的身高和年龄。我们有一个包含 1000 个样本的数据集,其中每个样本都具有身高和年龄这两个特征,以及一个对应的目标体重。我们可以将这个问题看作是一个回归问题,因为我们需要预测连续的数值输出。
首先,我们需要将数据集分成训练集和测试集:
```matlab
% Load the dataset
load('weight_dataset.mat');
% Split the dataset into training and testing sets
train_ratio = 0.8;
[trainInd,testInd] = dividerand(size(X,2),train_ratio,1-train_ratio);
X_train = X(:,trainInd);
Y_train = Y(:,trainInd);
X_test = X(:,testInd);
Y_test = Y(:,testInd);
```
然后,我们需要定义 BP 神经网络的结构和超参数:
```matlab
% Define the number of input, hidden, and output units
input_units = size(X_train,1);
hidden_units = 10;
output_units = 1;
% Define the learning rate and number of epochs
learning_rate = 0.01;
num_epochs = 1000;
```
接下来,我们需要初始化 BP 神经网络的权重和偏差:
```matlab
% Initialize the weights and biases
W1 = rand(hidden_units,input_units);
b1 = rand(hidden_units,1);
W2 = rand(output_units,hidden_units);
b2 = rand(output_units,1);
```
然后,我们可以开始训练 BP 神经网络:
```matlab
% Train the neural network
for i = 1:num_epochs
% Forward propagation
A1 = sigmoid(W1*X_train + b1);
A2 = W2*A1 + b2;
% Compute the loss
error = Y_train - A2;
loss = 0.5*mean(error.^2);
% Backward propagation
dA2 = -error;
dW2 = (1/size(X_train,2))*dA2*A1';
db2 = (1/size(X_train,2))*sum(dA2,2);
dA1 = W2'*dA2.*sigmoid_gradient(A1);
dW1 = (1/size(X_train,2))*dA1*X_train';
db1 = (1/size(X_train,2))*sum(dA1,2);
% Update the weights and biases
W2 = W2 - learning_rate*dW2;
b2 = b2 - learning_rate*db2;
W1 = W1 - learning_rate*dW1;
b1 = b1 - learning_rate*db1;
% Print the loss every 100 epochs
if mod(i,100) == 0
fprintf('Epoch %d, loss = %f\n', i, loss);
end
end
```
最后,我们可以使用训练好的 BP 神经网络来进行预测:
```matlab
% Use the neural network to make predictions
A1_test = sigmoid(W1*X_test + b1);
A2_test = W2*A1_test + b2;
% Compute the test loss
error_test = Y_test - A2_test;
loss_test = 0.5*mean(error_test.^2);
fprintf('Test loss = %f\n', loss_test);
```
完整的代码如下所示:
```matlab
% Load the dataset
load('weight_dataset.mat');
% Split the dataset into training and testing sets
train_ratio = 0.8;
[trainInd,testInd] = dividerand(size(X,2),train_ratio,1-train_ratio);
X_train = X(:,trainInd);
Y_train = Y(:,trainInd);
X_test = X(:,testInd);
Y_test = Y(:,testInd);
% Define the number of input, hidden, and output units
input_units = size(X_train,1);
hidden_units = 10;
output_units = 1;
% Define the learning rate and number of epochs
learning_rate = 0.01;
num_epochs = 1000;
% Initialize the weights and biases
W1 = rand(hidden_units,input_units);
b1 = rand(hidden_units,1);
W2 = rand(output_units,hidden_units);
b2 = rand(output_units,1);
% Train the neural network
for i = 1:num_epochs
% Forward propagation
A1 = sigmoid(W1*X_train + b1);
A2 = W2*A1 + b2;
% Compute the loss
error = Y_train - A2;
loss = 0.5*mean(error.^2);
% Backward propagation
dA2 = -error;
dW2 = (1/size(X_train,2))*dA2*A1';
db2 = (1/size(X_train,2))*sum(dA2,2);
dA1 = W2'*dA2.*sigmoid_gradient(A1);
dW1 = (1/size(X_train,2))*dA1*X_train';
db1 = (1/size(X_train,2))*sum(dA1,2);
% Update the weights and biases
W2 = W2 - learning_rate*dW2;
b2 = b2 - learning_rate*db2;
W1 = W1 - learning_rate*dW1;
b1 = b1 - learning_rate*db1;
% Print the loss every 100 epochs
if mod(i,100) == 0
fprintf('Epoch %d, loss = %f\n', i, loss);
end
end
% Use the neural network to make predictions
A1_test = sigmoid(W1*X_test + b1);
A2_test = W2*A1_test + b2;
% Compute the test loss
error_test = Y_test - A2_test;
loss_test = 0.5*mean(error_test.^2);
fprintf('Test loss = %f\n', loss_test);
% Define the sigmoid function and its derivative
function y = sigmoid(x)
y = 1./(1 + exp(-x));
end
function y = sigmoid_gradient(x)
y = sigmoid(x).*(1 - sigmoid(x));
end
```
阅读全文