Clear Filters
Clear Filters

Data input and target formatting for Deep Learning Models

69 views (last 30 days)
Isabelle Museck
Isabelle Museck on 1 Jul 2024 at 13:05
Edited: Ruth on 9 Jul 2024 at 10:46
I am trying to train a ML model with data from 10 different trials in batches. Right now the input data is stored in a 1x9 cell array (Features) with each cell containing a 3x1x541 dlarray corresponding to (the 3 accelerometer channels C, Batch, and 541 time steps T) for all 10 trials. The other cell array that contains the correposding continous variable we are trying to predict/output values over the 541 time steps stored in (Predictionvalue). I am getting an error when inputing into my model that: Error using trainnet (line 46)
Dimension format of predictions and target values arguments must match.
Are there any suggestions on how I could fix this or if I am formatting my data inputs/tragets incorrectly?
Thank you so much in advance!
  1 Comment
Catalytic
Catalytic on 2 Jul 2024 at 3:11
Please don't use screen shots to show code. Post the code as text that we can copy, paste, and run.

Sign in to comment.

Answers (7)

Umar
Umar on 2 Jul 2024 at 2:51
Hi Isabelle,
Have you tried to reshape or transpose the arrays to align their dimensions before passing them to the trainnet function because by aligning the dimensions of the predicted values and target values correctly, you can avoid the dimension format error and successfully train your model in Matlab. Let me know if this resolves your problem.
  1 Comment
Isabelle Museck
Isabelle Museck on 2 Jul 2024 at 14:43
Hi there thank you for getting back to me! I am having issues when I try and transpose my dlarray. When I run this code I get the following error:
net = trainnet(XTrain',YTrain',net,"mse",options)
Error using ' (line 17)
TRANSPOSE does not support N-D arrays. Use PAGETRANSPOSE/PAGECTRANSPOSE to transpose pages or PERMUTE to reorder dimensions of N-D arrays.
I then tried to use the pagetranspose/pagectranspose function instead and got the following error:
net = trainnet(pagetranspose(XTrain),pagetranspose(YTrain),net,"mse",options)
Error using dlarray/permute (line 54)
Permutation must be between dimensions that have the same label.
Error in pagetranspose (line 16)
x = permute(x, [2 1 3:ndims(x)]);
Do you have any suggestions on how I can sucessfully transpose a dlarray so that I can get the dimensions to align and my model to run? Thank you so much!

Sign in to comment.


Umar
Umar on 2 Jul 2024 at 15:18
Hi Isabelle,
Try using the permute function to reorder the dimensions. By using the permute function with the appropriate dimension order, you can effectively transpose the dlarray and align the dimensions for training your model without encountering errors.
for more information on this function, please refer to
https://www.mathworks.com/help/matlab/ref/permute.html#
Hope this will help resolve your problem.
  1 Comment
Isabelle Museck
Isabelle Museck on 2 Jul 2024 at 16:40
Thank you ofr your help! I am trying to use the prmute function but am still getting an error. If I have a 9(B) x 540 (T) dlarray and a 3(C) × 9(B) × 540(T) dlarray how would I use this function to get their dimensions to align? Right now I am trying to reorder the 3(C) × 9(B) × 540(T) dlarray in XTrain and it is giving me an error:
Features = permute(XTrain, [3 540 9])
Error using permute
ORDER contains an invalid permutation index.
Error in dlarray/permute (line 24)
permute(1, perm);

Sign in to comment.


Umar
Umar on 2 Jul 2024 at 17:35
Hi Isabelle,
Sorry to hear that you are still having issues. I read your comments, If I have a 9(B) x 540 (T) dlarray and a 3(C) × 9(B) × 540(T) dlarray how would I use this function to get their dimensions to align? Right now I am trying to reorder the 3(C) × 9(B) × 540(T) dlarray in XTrain and it is giving me an error.
In order to resolve this problem, provide a valid permutation index that matches the dimensions of the dlarray. In this case, to reorder the dimensions of the dlarray [3(C) x 9(B) x 540(T)] to [9(B) x 540(T) x 3(C)], you should use the permutation index [2 3 1] instead of [3 540 9].
Here is the corrected code snippet:
Features = permute(XTrain, [2 3 1]);
By using the correct permutation index [2 3 1], you will reorder the dimensions of the dlarray as intended without encountering the error.
  1 Comment
Isabelle Museck
Isabelle Museck on 2 Jul 2024 at 19:59
Hi Umar, I thruly apprectiate all your help. I just tried the corrected code snipet you suggested and am still reciving an error:
Features = permute(XTrain, [2 3 1]);
Error using dlarray/permute (line 54)
Permutation must be between dimensions that have the same label.
Do you have any suggestions as to why this may be? Also what what order do you suggest I rearrage my dlarray to in order for it to alighn with the 9(B) x 540 (T) dlarray output for my ML model?

Sign in to comment.


Umar
Umar on 2 Jul 2024 at 20:27
Hi Isabelle,
I am so sorry to find out that are still having issues with this problem. So, you mentioned this time that Do you have any suggestions as to why this may be? Also what what order do you suggest I rearrage my dlarray to in order for it to alighn with the 9(B) x 540 (T) dlarray output for my ML model?
In order to resolve this issue, I am going to use the dlarray function to create dlarray objects with specific dimension labels. Here is an example of how you can create a dlarray with custom dimension labels:
XTrain = randn(9, 540, 100); % Example data
XTrainDL = dlarray(XTrain, 'SSB'); % Create dlarray with custom dimension labels
So, in the above code snippet, 'SSB' represents the labels for the dimensions of the dlarray (Spatial, Spatial, Batch). You can adjust the labels according to your specific data structure.Once the dimension labels are consistent,then you can proceed with the permutation. To align the dlarray with a 9(B) x 540(T) output format for your machine learning model, you can permute the dimensions as follows as mentioned earlier.
Features = permute(XTrainDL, [3 1 2]); % Permute dimensions to align with 9 x 540 format
Now, the dimensions are rearranged to match the desired output format. The first dimension of the dlarray becomes the third dimension after permutation, the second dimension becomes the first, and the third dimension becomes the second.
Wish you good luck.
  1 Comment
Isabelle Museck
Isabelle Museck on 5 Jul 2024 at 16:23
Hi Umar,
Thank you for getting back to me. I did acutually create a dlarray objects with specific dimension labels 'CTB' specifying the 3 channels, 541 time steps, and 9 batches as shown here; however I am still getting this error. Do you know why this is or how I could fix it and get it to run properly?
XTrain= dlarray(cat(3,Xtrain{:}),'CTB')
YTrain= dlarray(cat(1,Ytrain{:}),'BT')
Features = permute(XTrain, [3 2 1]);
Error using dlarray/permute (line 54)
Permutation must be between dimensions that have the same label.

Sign in to comment.


Umar
Umar on 5 Jul 2024 at 16:32
Hi Isabelle,
The 'CTB' label in the dlarray XTrain indicates that the dimensions are Channel, Time, and Batch, respectively. When you attempt to permute XTrain using [3 2 1], you are trying to change the order of dimensions to Batch, Time, and Channel. However, this permutation is not allowed because the dimensions must maintain their original labels. To fix this issue and perform the permutation correctly, you need to ensure that the permutation maintains the integrity of the dimension labels. In this case, you should permute the dimensions within the same label. For example, to swap the Time and Channel dimensions, you can use [1 3 2]:
Features = permute(XTrain, [1 3 2]);
By using [1 3 2], you are rearranging the dimensions within the 'CTB' label, swapping the Channel and Time dimensions while keeping the Batch dimension unchanged. This adjustment aligns with the dimension labels of the dlarray and allows the permutation to be executed without errors.
Please remember, when working with dlarray objects in MATLAB, always consider the dimension labels and ensure that any operations involving permutation or reshaping maintain the consistency of these labels to avoid errors like the one you encountered.
  1 Comment
Isabelle Museck
Isabelle Museck on 5 Jul 2024 at 18:08
I am so sorry but I am not quite sure what you mean by permute the dimensions within the same label? I just tried this as you suggested and am still getting an error:
XTrain= dlarray(cat(3,Xtrain{:}),'CTB')
YTrain= dlarray(cat(1,Ytrain{:}),'BT')
Features = permute(XTrain,[1 3 2])
Error using dlarray/permute (line 54)
Permutation must be between dimensions that have the same label.
The code only runs when the dimensions are set to [1 2 3] which is the same dimensions as it is was before so it is not rearraging anything. Do you know why this is or how to fix my code above for it to run properly and rearge the dimnesions?

Sign in to comment.


Umar
Umar on 5 Jul 2024 at 21:20

Hi Isabelle,

After going through permute.m file, I wanted to find out what is this error your code, Error using dlarray/permute (line 54) is referring to,

53 if ~isequal(dims(obj), origDims) 54 error(message('deep:permute:ChangeDims')); 55 end

So, this error message 'deep:permute:ChangeDims' typically occurs when there is an issue with the dimensions specified in the permute function in MATLAB. To illustrate this fix and provide an example with printed results, let's consider a sample scenario with random data:

>> % Sample Data XTrain = rand(4, 3, 2); % Creating a random 3D array disp('Original XTrain:'); disp(XTrain);

% Permute Dimensions Features = permute(XTrain, [1 3 2]); disp('Permuted Features:'); disp(Features);

When you run this code snippet, you should see the original XTrain array followed by the permuted Features array in the MATLAB command window. Please see attached results.

This demonstrates how the permute function can be used correctly to rearrange the dimensions of a 3D array without encountering the 'deep:permute:ChangeDims' error. By following these steps and ensuring that the dimensions are correctly specified in the permute function, you can avoid the error message and successfully manipulate the dimensions of your data arrays in MATLAB.


Ruth
Ruth on 9 Jul 2024 at 10:45
Edited: Ruth on 9 Jul 2024 at 10:46
Hi Isabelle,
The error is because your target data and the predictions are not in the same format, so the network can't calculate the loss or metrics.
The issue is most likely with the TargetDataFormats value, the format should include a channel ("C") dimension. Without seeing your data, it's difficult to know which way round the formats should be but if you have a 10x1 cell array that contains a 1x541 array, the TargetDataFormats should be "CT". Since there is one channel (the prediction) across 541 timesteps. The data format only cares about what's inside the cell so you don't need to include the "B" dimension (the batch is the number of elements in the cell).
It is generally easier to change the TargetDataFormats than change the dimensions of the data. Have a look at how the data is formatted in this example, the inputs and targets are both "TC", though since this is the default for trainnet, it is not specified in trainingOptions: https://uk.mathworks.com/help/deeplearning/ug/sequence-to-sequence-regression-using-deep-learning.html
Changing the TargetDataFormats to match your target data should resolve the issue.

Community Treasure Hunt

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

Start Hunting!