ANN code encountered an error on line 340 when trying to create training module
8 次查看(过去 30 天)
显示 更早的评论
%input and output
x = [1000 0 0.42 2 60;
750 250 0.42 2 60;
750 250 0.42 2 28;
750 250 0.42 2 7;
500 500 0.42 2 60;
500 500 0.42 2 28;
500 500 0.42 2 7;
250 750 0.42 2 60;
250 750 0.42 2 28;
250 750 0.42 2 7;
0 1000 0.42 2 60;
0 1000 0.42 2 28;
0 1000 0.42 2 7];
y = [80;
77;
72.5;
72;
63;
59;
53;
45;
35;
22;
20;
16;
15];
inputs = x; % 输入数据矩阵,每行包含 5 个特征
outputs = y; % 目标数据矩阵,每行包含 1 个单一参数
for i = 1:13 % read 13 times 读入 13 次输入数据
fprintf('Enter input %d:\n', i);
inputs(i, :) = input(''); % 读入 5 个参数
fprintf('Enter output %d:\n', i);
outputs(i, :) = input(''); % 读入 1 个单一参数+
end
% create ANN 创建神经网络模型
numInputs = 5; % 输入特征数
numHiddenLayers = 2; % 隐藏层数量
numNeurons = 5; % 每个隐藏层中神经元数量
numOutputs = 1; % 输出单一参数数
net = feedforwardnet(numNeurons, 'trainlm'); % 创建网络模型
net.numInputs = numInputs; % 设置输入层神经元数量
net.numLayers = numHiddenLayers + 1; % 设置总层数
net.numOutputs = numOutputs; % 设置输出层神经元数量
% 设置训练参数
net.trainParam.epochs = 1000; % 迭代次数
net.trainParam.lr = 0.01; % 学习率
net.trainParam.goal = 1e-5; % 目标误差
net.trainParam.showWindow = true; % 显示训练窗口
% 训练神经网络
[net, tr] = train(net, inputs', outputs');
% 储存模型
save('ANN_model.mat', 'net');
% 加载已储存的模型
load('ANN_model.mat');
% 使用测试数据进行预测
testInput = input('1000 0 0.42 2 28');
testOutput = net(testInput');
fprintf('Predicted output: %f\n', testOutput);
The code encountered an error on line 340 when trying to create training module::
Error using network/train (line 340)
Number of inputs does not match net.numInputs. But I can't find any not match?
0 个评论
回答(1 个)
Eshan Patel
2023-4-26
Hi,
I understand that you are trying to train a shallow neural network using the 'train' function.
The code you have provided results in an error due ot the number of inputs actually being 1. Here, the number of inputs 'net.numInputs' defines how many sets of vectors the network receives as input. The size of each input (i.e., the number of elements in each input vector) is determined by the input size (net.inputs{i}.size).
I also noticed that your code sets 'net.numOutputs' to 1. This is a read-only property, and should no be set manually in your code. It is always equal to the number of 1s in 'net.outputConnect'.
You can refer to this page for more information about the neural network object properties:
Instead, you could use something like this to achieve what you are trying to do:
%using a simple sample dataset
[x, y] = simplefit_dataset;
x = reshape(x, [2 94/2]);
y = y(1:47);
%set neural network properties
numHiddenLayers = 2;
numNeurons = 5;
%create the neural network
net = feedforwardnet(numNeurons, 'trainlm');
%confugure the neural network and set more properties
net = configure(net, x, y);
net.numLayers = numHiddenLayers + 1;
net.trainParam.epochs = 1000;
net.trainParam.lr = 0.01;
net.trainParam.goal = 1e-5;
net.trainParam.showWindow = true;
%perform training using the neural network
[net, tr] = train(net, x, y);
This should let you train the neural network without any errors. Hope this helps!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 定义浅层神经网络架构 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!