matlab BP神经网络

clear

clc

% 准备数据

inputs = rand(10, 100); % 100组输入,每组10个特征

outputs = rand(1, 100); % 100组输出,每组1个输出值

% 将数据分成训练集和测试集

trainRatio = 0.8;

valRatio = 0.1;

testRatio = 0.1;

trainInd, valInd, testInd\] = dividerand(100, trainRatio, valRatio, testRatio); X_train = inputs(:, trainInd); y_train = outputs(:, trainInd); X_test = inputs(:, testInd); y_test = outputs(:, testInd); % 创建前馈神经网络 hiddenLayerSize = 10; net = feedforwardnet(hiddenLayerSize); % 设置训练、验证和测试数据 net.divideParam.trainRatio = 0.8; net.divideParam.valRatio = 0.1; net.divideParam.testRatio = 0.1; % 训练神经网络 \[net, tr\] = train(net, X_train, y_train); % 查看训练结果 plotperform(tr); % 进行预测 y_pred = net(X_test); % 计算误差 errors = y_pred - y_test; % 输出误差 for i = 1:length(y_test) fprintf('Test sample %d:\\n', i); fprintf('Predicted output: %.4f\\n', y_pred(i)); fprintf('Actual output: %.4f\\n', y_test(i)); fprintf('Error: %.4f\\n\\n', errors(i)); end % 计算均方误差(MSE) mse = mean(errors.\^2); fprintf('Mean Squared Error: %.4f\\n', mse);

相关推荐
然哥依旧7 小时前
【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)
算法·支持向量机·matlab·cnn
jllllyuz17 小时前
基于MATLAB的锂电池物理对象建模实现
开发语言·matlab
Evand J18 小时前
【MATLAB例程】无人机三维路径规划|A*,RRT(快速随机树算法), APF(人工势场法)算法对比|可自定义起终点、障碍物坐标。附下载链接
算法·matlab·无人机·astar·路径规划·rrt·apf
cici1587419 小时前
基于MATLAB的四旋翼无人机三维动态仿真实例
开发语言·matlab·无人机
小文数模1 天前
2026年美赛数学建模C题完整参考论文(含模型和代码)
python·数学建模·matlab
小文数模2 天前
2026美赛数学建模D题完整参考论文(含模型建立求解、代码等)
python·数学建模·matlab
Wh-Constelltion2 天前
【PQ分解法潮流计算(matlab版)】
算法·matlab
小文数模2 天前
2026美赛数学建模F题完整参考论文(含模型建立求解、代码等)
python·数学建模·matlab
小郭团队2 天前
2_6_五段式SVPWM(经典算法+DPWM2)算法理论与MATLAB实现详解
算法·matlab·硬件架构·arm·dsp开发
guygg882 天前
梯度投影法(GPSR)MATLAB源代码实现与解析
开发语言·matlab