ANN module can't do any prediction

5 次查看(过去 30 天)
impp
impp 2023-7-24
评论: impp 2023-7-26
% 某个水泥有5个制造参数,一共有13组数据如下矩阵:
data = [1 60; 1 28; 1 7; 0.75 60; 0.75 28; 0.75 7; 0.5 60; 0.5 28; 0.5 7; 0.25 60; 0.25 28; 0.25 7; 0 60; 0 28; 0 7];
% 该水泥压力结果如下矩阵:
pressure = [80;79;75;77;72.5; 72;63;59;53;45;35;22;20;16;15];
% 数据标准化
[dataNorm, mu, sigma] = zscore(data);
%新的神经网络模型
net = feedforwardnet([2,2]); % 3层隐藏层,第一层1个神经元,第二层2个神经元,第三层3个神经元
net.trainFcn = 'trainscg'; % 使用Levenberg-Marquardt算法
net.performFcn = 'mse'; % 使用均方误差作为性能函数
net.trainParam.epochs = 1000; % 设置迭代次数为1000次
net.trainParam.goal = 1e-6; % 设置误差目标值
net.divideFcn = 'dividerand'; % 随机划分训练集、验证集和测试集
net.divideParam.trainRatio = 1;
net.performParam.regularization = 0.1; % 设置正则化项系数
% 训练神经网络模型
[net, tr] = train(net, dataNorm', pressure', 'useParallel', 'no'); % 添加'useParallel'参数
% 预测结果
pressurePred = net(dataNorm')';
% 结果可视化
plot(pressure,'o');
hold on;
plot(pressurePred,'*');
legend('Actual','Predicted');
xlabel('Sample');
ylabel('Pressure');
title('Actual vs Predicted Pressure');
% 性能评估
mse = mean((pressure-pressurePred).^2);
r2 = corrcoef(pressure,pressurePred)^2;
% 测试数据
test_inputs = [ 1 60; 0.5 28];
% 归一化测试数据
test_inputs_norm = (test_inputs - mu) ./ sigma;
% 使用训练好的模型进行预测
test_outputs_norm = net(test_inputs_norm')';
test_outputs = test_outputs_norm .* std(pressure) + mean(pressure);
% 显示结果
disp("Test:");
disp(test_outputs);
I had prepared an ANN module for training database, and start with a make up matrix. Testing result showed it is workable. But when I put 2 groups of input for prediction, the output is over 1000, which the maxium output of the training data is 80. And no matter how I change the training data input, the prediction outputs are around 1k-3k. Are there any bugs in my code?

回答(1 个)

Angelo Yeo
Angelo Yeo 2023-7-24
Your output was not normalized when you trained the neural network. You do not have to multiply standard deviation of pressure and add mean at line 46.
% There are 5 manufacturing parameters for a certain cement, and there are 13 sets of data in the following matrix:
data = [1 60; 1 28; 1 7; 0.75 60; 0.75 28; 0.75 7; 0.5 60; 0.5 28; 0.5 7; 0.25 60; 0.25 28; 0.25 7; 0 60; 0 28; 0 7];
% The pressure results of the cement are as follows:
pressure = [80;79;75;77;72.5; 72;63;59;53;45;35;22;20;16;15];
% Data normalization
[dataNorm, mu, sigma] = zscore(data);
% New neural network model
net = feedforwardnet([2,2]); % 3 layers of hidden units, the first layer has 1 neuron, the second layer has 2 neurons, and the third layer has 3 neurons
net.trainFcn = 'trainscg'; % Use the Levenberg-Marquardt algorithm for training
net.performFcn = 'mse'; % Use mean squared error as the performance function
net.trainParam.epochs = 1000; % Set the number of epochs to 1000
net.trainParam.goal = 1e-6; % Set the error goal
net.divideFcn = 'dividerand'; % Randomly divide the data into training, validation, and test sets
net.divideParam.trainRatio = 1;
net.performParam.regularization = 0.1; % Set the regularization coefficient
% Train the neural network model
[net, tr] = train(net, dataNorm', pressure', 'useParallel', 'no'); % Add 'useParallel' parameter
% Predict the results
pressurePred = net(dataNorm')';
% Visualize the results
plot(pressure,'o');
hold on;
plot(pressurePred,'*');
legend('Actual','Predicted');
xlabel('Sample');
ylabel('Pressure');
title('Actual vs Predicted Pressure');
% Performance evaluation
mse = mean((pressure-pressurePred).^2);
r2 = corrcoef(pressure,pressurePred)^2;
% Test data
test_inputs = [ 1 60; 0.5 28];
% Normalize the test data
test_inputs_norm = (test_inputs - mu) ./ sigma;
% Use the trained model for prediction
test_outputs_norm = net(test_inputs_norm')';
% test_outputs = test_outputs_norm .* std(pressure) + mean(pressure);
% Display the results
disp("Test:");
disp(test_outputs_norm);

类别

Help CenterFile Exchange 中查找有关 Pattern Recognition and Classification 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by