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);

相关推荐
吃好睡好便好21 小时前
MATLAB仿真框图2
开发语言·matlab·仿真·simulink
AAIshangyanxiu2 天前
涡度通量碳观测数据 MATLAB 全流程解析与敏感性定量分析、观测数据质控、插补及 footprint 建模技术
开发语言·matlab·涡度通量·生态碳汇·生态碳汇模拟
吃好睡好便好2 天前
MATLAB仿真框图
开发语言·matlab·仿真·simulink
netccdn3 天前
Hough变换检测直线(Matlab)
开发语言·计算机视觉·matlab
DFT计算杂谈4 天前
ADV. SCI. 反铁磁体MnTe2中的Plaid-like自旋劈裂与手性磁振子
matlab
机器学习之心5 天前
可拓理论+云模型:一套量化高校科研创新能力的MATLAB实现方案
matlab·云模型·可拓理论
bu_shuo5 天前
MATLAB中的meshgrid和ndgrid
开发语言·matlab
青春不败 177-3266-05206 天前
MATLAB在生态环境数据处理与分析
matlab·生态学·遥感·生态系统服务·数据处理·环境科学
WangYan20226 天前
MATLAB/Meta分析/R语言/贝叶斯方法/piecewiseSEM结构方程模型在生态环境数据处理与分析中的应用
matlab·meta分析·生态环境·结构方程模型·贝叶斯方法
熊猫_豆豆6 天前
MATLAB山地地形仿真与最高点到最低点最短路径
开发语言·matlab·dijkstra·最短路径