Can't match inputs

1 次查看(过去 30 天)
Kantemir
Kantemir 2023-10-8
回答: Shreshth 2025-3-4
I do a simple code:
net = network(2,3,[1; 0; 0],[1 1; 0 0; 0 0],...
[0 0 0; 1 0 0; 0 1 0],[0 0 1])
net = Neural Network name: 'Custom Neural Network' userdata: (your custom info) dimensions: numInputs: 2 numLayers: 3 numOutputs: 1 numInputDelays: 0 numLayerDelays: 0 numFeedbackDelays: 0 numWeightElements: 0 sampleTime: 1 connections: biasConnect: [1; 0; 0] inputConnect: [1 1; 0 0; 0 0] layerConnect: [0 0 0; 1 0 0; 0 1 0] outputConnect: [0 0 1] subobjects: output: Equivalent to outputs{3} inputs: {2x1 cell array of 2 inputs} layers: {3x1 cell array of 3 layers} outputs: {1x3 cell array of 1 output} biases: {3x1 cell array of 1 bias} inputWeights: {3x2 cell array of 2 weights} layerWeights: {3x3 cell array of 2 weights} functions: adaptFcn: (none) adaptParam: (none) derivFcn: 'defaultderiv' divideFcn: (none) divideParam: (none) divideMode: 'sample' initFcn: 'initlay' performFcn: 'mse' performParam: .regularization, .normalization plotFcns: {} plotParams: {1x0 cell array of 0 params} trainFcn: (none) trainParam: (none) weight and bias values: IW: {3x2 cell} containing 2 input weight matrices LW: {3x3 cell} containing 2 layer weight matrices b: {3x1 cell} containing 1 bias vector methods: adapt: Learn while in continuous use configure: Configure inputs & outputs gensim: Generate Simulink model init: Initialize weights & biases perform: Calculate performance sim: Evaluate network outputs given inputs train: Train network with examples view: View diagram unconfigure: Unconfigure inputs & outputs
net.inputs{1}.size = 2
net = Neural Network name: 'Custom Neural Network' userdata: (your custom info) dimensions: numInputs: 2 numLayers: 3 numOutputs: 1 numInputDelays: 0 numLayerDelays: 0 numFeedbackDelays: 0 numWeightElements: 0 sampleTime: 1 connections: biasConnect: [1; 0; 0] inputConnect: [1 1; 0 0; 0 0] layerConnect: [0 0 0; 1 0 0; 0 1 0] outputConnect: [0 0 1] subobjects: output: Equivalent to outputs{3} inputs: {2x1 cell array of 2 inputs} layers: {3x1 cell array of 3 layers} outputs: {1x3 cell array of 1 output} biases: {3x1 cell array of 1 bias} inputWeights: {3x2 cell array of 2 weights} layerWeights: {3x3 cell array of 2 weights} functions: adaptFcn: (none) adaptParam: (none) derivFcn: 'defaultderiv' divideFcn: (none) divideParam: (none) divideMode: 'sample' initFcn: 'initlay' performFcn: 'mse' performParam: .regularization, .normalization plotFcns: {} plotParams: {1x0 cell array of 0 params} trainFcn: (none) trainParam: (none) weight and bias values: IW: {3x2 cell} containing 2 input weight matrices LW: {3x3 cell} containing 2 layer weight matrices b: {3x1 cell} containing 1 bias vector methods: adapt: Learn while in continuous use configure: Configure inputs & outputs gensim: Generate Simulink model init: Initialize weights & biases perform: Calculate performance sim: Evaluate network outputs given inputs train: Train network with examples view: View diagram unconfigure: Unconfigure inputs & outputs
net.inputs{2}.size = 2
net = Neural Network name: 'Custom Neural Network' userdata: (your custom info) dimensions: numInputs: 2 numLayers: 3 numOutputs: 1 numInputDelays: 0 numLayerDelays: 0 numFeedbackDelays: 0 numWeightElements: 0 sampleTime: 1 connections: biasConnect: [1; 0; 0] inputConnect: [1 1; 0 0; 0 0] layerConnect: [0 0 0; 1 0 0; 0 1 0] outputConnect: [0 0 1] subobjects: output: Equivalent to outputs{3} inputs: {2x1 cell array of 2 inputs} layers: {3x1 cell array of 3 layers} outputs: {1x3 cell array of 1 output} biases: {3x1 cell array of 1 bias} inputWeights: {3x2 cell array of 2 weights} layerWeights: {3x3 cell array of 2 weights} functions: adaptFcn: (none) adaptParam: (none) derivFcn: 'defaultderiv' divideFcn: (none) divideParam: (none) divideMode: 'sample' initFcn: 'initlay' performFcn: 'mse' performParam: .regularization, .normalization plotFcns: {} plotParams: {1x0 cell array of 0 params} trainFcn: (none) trainParam: (none) weight and bias values: IW: {3x2 cell} containing 2 input weight matrices LW: {3x3 cell} containing 2 layer weight matrices b: {3x1 cell} containing 1 bias vector methods: adapt: Learn while in continuous use configure: Configure inputs & outputs gensim: Generate Simulink model init: Initialize weights & biases perform: Calculate performance sim: Evaluate network outputs given inputs train: Train network with examples view: View diagram unconfigure: Unconfigure inputs & outputs
P = [1 1; 1 1]
P = 2×2
1 1 1 1
A = sim(net, P)
Error using network/sim
Number of inputs does not match net.numInputs.
But always get an error: Number of inputs does not match net.numInputs.
I tried P = [1; 1] P = [1 1] P = {[1 1]; [1 1]} and lots of other variants, but none of them seem to work.
I also tried creating nn with 1 input and it works:
net = network(1,3,[1; 0; 0],[1; 0; 0],...
[0 0 0; 1 0 0; 0 1 0],[0 0 1])
net.inputs{1}.size = 2
P = [1; 1]
A = sim(net, P)
but I can't manage 2 inputs. What am I doing wrong? Please help

回答(1 个)

Shreshth
Shreshth 2025-3-4
Hi Kantemir,
According to the MATLAB documentation, when creating a custom network using the "network" function, it is essential to train the network before utilizing it with the "sim" function. The "sim" function is responsible for simulating the trained network's response to input data. The reason behind this requirement is that the "network" function generates a network object with randomly initialized weights and biases. These initial values are not suitable for producing accurate predictions or classifications. Consequently, prior to employing the network for a specific task, it is necessary to train it by adjusting the weights and biases based on a training dataset. The training process typically involves iteratively adjusting the network's weights and biases in response to the error between the desired output and the predicted output. This iterative process enables the network to learn and enhance its performance on the given task. Once the network has undergone proper training, the "sim" function can be used to provide input data and obtain the network's response.
Attempting to use the "sim" function on an untrained network may result in size mismatch errors, as the network's weights and biases have not been adjusted to align with the specific task or dataset. Therefore, it is crucial to train the network prior to utilizing the "sim" function, ensuring that accurate predictions or classifications can be made.
You can refer to the below MathWorks documentation for more information:
On "network" function:
On "sim" function:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Deep Learning Toolbox 的更多信息

产品


版本

R2011a

Community Treasure Hunt

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

Start Hunting!

Translated by