I am trying to adapt example provided here https://www.mathworks.com/help/releases/R2017a/nnet/examples/training-a-deep-neural-network-for-digit-classification.html Except that I want to replace softnet = trainSoftmaxLayer(feat2,tTrain,'MaxEpochs',400); With something similar to this options = trainingOptions('sgdm','MaxEpochs',20,...'InitialLearnRate',0.0001); routputlayer = regressionLayer('Name','routput'); trainedROL = trainNetwork(feat2,yTrain,routputlayer,options); However, I receive the following error: Error using trainNetwork>iAssertXAndYHaveSameNumberOfObservations (line 604) X and Y must have the same number of observations. Error in trainNetwork>iParseInput (line 336) iAssertXAndYHaveSameNumberOfObservations( X, Y ); Error in trainNetwork (line 68) [layers, opts, X, Y] = iParseInput(varargin{:}); Error in test (line 176) trainedROL = trainNetwork(feat2,tTrain,routputlayer,options); How can I modify the algorithm to do regression rather than classification?
Kshitij Singh answered .
2025-11-20
%%Layers
layers = [ imageInputLayer([1 50])
fullyConnectedLayer(10)
regressionLayer() ];
%%Manipulate feature data
% Reshape to image format ([H x W x C x N])
feat2ImageFormat = reshape( feat2, [1 50 1 5000] );
%%Train
trainReg = trainNetwork( feat2ImageFormat, tTrain', layers, trainingOptions('sgdm') );