Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Shin-yee-lu asked . 2021-06-22

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

regresion , neural network , deep learning , matlab

Expert Answer

Kshitij Singh answered . 2025-03-28 18:17:02

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.png'}                68.6
% {'C:\path\to\image2.png'}                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., .png, .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.png'; 'C:\path\to\image2.png'};
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

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.