matlab - 在 MATLAB 中训练简单神经网络时出现错误

标签 matlab machine-learning neural-network

我在 MATLAB 中构建了简单的神经网络,如下代码所示,输入和输出数据的维度为 128x1,我将输入数据划分为 21x1 进行验证和 107 用于训练:

clear all; clc; clear;  

bits = randi([0 1], 1 , 128);                 %Original generated data 
data_temp = bi2de(reshape(bits,128/2,2));   
data_mod = (1/sqrt(2))*qammod(data_temp , 4);  

inputs = [real(data_mod); imag(data_mod)];      %the input data of input layer (128 x 1); 
outputs = bits.';                               %output data from output layer (128 x 1); 

inputs_len = 128;                    %length of input data 
outputs_len = 128;                   %length of output data 

IN_V1 = inputs(end-20:end);                     %Test validation input vector
OUT_V1 = outputs(end-20:end);                   %Test Validation output Vector 

Train_seq_in = inputs(22 : end);               %Training inpur Data
Train_seq_out = outputs(22:end);               %Training output Data 

%build Neural network
options1 = trainingOptions('adam','MaxEpochs',2000,'InitialLearnRate',0.001,'MiniBatchSize',10,'Shuffle','every','L2Regularization',0,'Plots','training-progress','ValidationData',{IN_V1,OUT_V1})
layers =[sequenceInputLayer(107) fullyConnectedLayer(1024) reluLayer() fullyConnectedLayer(outputs_len) regressionLayer];

%train network 
 Net1 = trainNetwork(Train_seq_in,Train_seq_out,layers,options1);                  %Training the Network real

当我运行上面的代码时,出现错误:

Error using trainNetwork (line 165)
The validation sequences are of feature dimension 21 but the input layer expects sequences of feature
dimension 107.

Error in quetion (line 24)
 Net1 = trainNetwork(Train_seq_in,Train_seq_out,layers,options1);                  %Training the Network
 real

但是我认为这是不合逻辑的,因为验证数据应该小于测试数据!!

我尝试将验证数据更改为与输入数据相同,如下所示:

%build Neural network
options1 = trainingOptions('adam','MaxEpochs',2000,'InitialLearnRate',0.001,'MiniBatchSize',10,'Shuffle','every','L2Regularization',0,'Plots','training-progress','ValidationData',{IN_V1,OUT_V1})
layers =[sequenceInputLayer(21) fullyConnectedLayer(1024) reluLayer() fullyConnectedLayer(outputs_len) regressionLayer];

%train network 
 Net1 = trainNetwork(IN_V1,IN_V1,layers,options1);                  %Training the Network real

但在这种情况下,我会收到另一个奇怪的错误,如下所示:

Error using trainNetwork (line 165)
To RESHAPE the number of elements must not change.

Error in quetion (line 24)
 Net1 = trainNetwork(IN_V1,IN_V1,layers,options1);                  %Training the Network real

Caused by:
    Error using reshape
    To RESHAPE the number of elements must not change.

请问有人可以帮我解决这个问题吗?

谢谢

最佳答案

对于第二个错误: 使用 trainNetwork 时出错(第 165 行) 要 RESHAPE,元素数量不得更改。

Error in quetion (line 24)
 Net1 = trainNetwork(IN_V1,IN_V1,layers,options1);                  %Training the Network real

Caused by:
    Error using reshape
    To RESHAPE the number of elements must not change. 

当您修改输入层的大小时,您还应该修改输出层的大小,因此您应该替换此行:

layers =[sequenceInputLayer(21) fullyConnectedLayer(1024) reluLayer() fullyConnectedLayer(outputs_len) regressionLayer];

通过这一行:

layers =[sequenceInputLayer(21) fullyConnectedLayer(1024) reluLayer() fullyConnectedLayer(21) regressionLayer];

关于matlab - 在 MATLAB 中训练简单神经网络时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58424370/

相关文章:

python - Keras 3d 张量的前 k 个分类精度问题

python - 是否可以在 keras 中实现动态类权重?

python - 了解 PyTorch 中的反向传播

python - 如何确定 CNN 模型的准确性?

matlab - 在 MATLAB 中使用 if 语句组合另外两个矩阵来创建新矩阵

matlab - Dicom:Matlab 与 ImageJ 灰度级

python - Keras 使用什么损失函数?

python - scikit 学习 : custom classifier compatible with GridSearchCV

matlab - 具有两个非平凡约束的随机二元矩阵

matlab - MATLAB 中的函数矩阵向量化