Table for neural network regression

Illustration
Shin-yee-lu - 2021-06-22T14:23:24+00:00
Question: Table for neural network regression

I made a table for neural network regression, regNet = trainNetwork(trData,'Percent',layers,options), and gor the following error Error using trainNetwork (line 165) Invalid training data table for regression. Predictors must be in the first column of the table, as a cell array of image paths or images.   The Table, .txt file, I made has two variables, a string and a double, The first two rows looks like this. I thought they look exactly as in the tutorial .   fileNames,Percent C:\Users\181620\SCF\iMEC\Data\regData\s7_x8y28_x8500y2800_1_1.png,68.6 C:\Users\181620\SCF\iMEC\Data\regData\s7_x8y28_x8500y2800_1_2.png,68.6

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

The error arises because the format of your table does not match the requirements for regression tasks using trainNetwork. Specifically:

  1. The first column must be a cell array of image paths or actual image data (as MATLAB arrays).
  2. The second column must contain numeric values corresponding to the regression targets.

Steps to Resolve

Correcting Your Table

Ensure your table is formatted properly. If your table contains image paths and numeric regression targets, do the following:

  1. Load the Table in MATLAB: If you read your .txt file into a table, ensure the fileNames column is recognized as a cell array of strings:

 

tbl = readtable('yourFile.txt');
tbl.fileNames = cellstr(tbl.fileNames); % Ensure the first column is a cell array

Check the Table: Verify that the table has the correct structure:

disp(tbl);
% It should look like:
% fileNames                               Percent
% {'C:\path\to\image1.webp'}                68.6
% {'C:\path\to\image2.webp'}                68.6

Using the Table with trainNetwork

The first column (fileNames) should contain image paths, and the second column (Percent) should contain numeric regression targets. Pass the table to trainNetwork as follows:

 

regNet = trainNetwork(tbl, 'Percent', layers, options);

Here:

  • 'Percent' specifies the column containing the regression target.
  • The first column, fileNames, is implicitly recognized as the input data.

Common Issues and Fixes

  1. File Paths Not Found: Ensure that the file paths in fileNames are correct and accessible from your system. Use exist(path, 'file') to verify each path:

 

assert(all(cellfun(@(x) exist(x, 'file') == 2, tbl.fileNames)), 'Some file paths are invalid.');
  1. Image Formats: Verify that all images have a consistent format (e.g., .webp, .jpg) and are readable by MATLAB.

  2. Table Column Order: The predictors must be in the first column, and the response variable must be in the specified target column (in this case, 'Percent').

  3. Table Data Types: The fileNames column must be a cell array of strings, and the Percent column must be numeric.


Example

Here’s an example of creating and using a table for neural network regression:

% Example data
fileNames = {'C:\path\to\image1.webp'; 'C:\path\to\image2.webp'};
percent = [68.6; 72.1];

% Create table
tbl = table(fileNames, percent, 'VariableNames', {'fileNames', 'Percent'});

% Define a simple network
layers = [
    imageInputLayer([32 32 3])
    fullyConnectedLayer(1)
    regressionLayer
];
options = trainingOptions('sgdm', 'MaxEpochs', 5, 'Plots', 'training-progress');

% Train network
regNet = trainNetwork(tbl, 'Percent', layers, options);

This will train the network using the table data. Ensure your table format matches this structure.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!