Clear Filters
Clear Filters

Hi, everyone! dlgradient throws an error message as Undefined function 'dlgradient' for input arguments of type 'cell'.

4 views (last 30 days)
I am new to NN tools and functions in MatLab, so probably there is a simple mistake I am doing. I have the the code that follows. I had this code running, but then I spotted few bugs in other parts of the code, that I fixed, probably something is linked to dlgradient. I created the weights and biases arrays by myself, not using any off-the-shelf tools of Matlab. The code below are a part of a class where I try to manage all NN computations. Thank you all in advance.
Alim
function [totalLoss, result] = trainingByBatches(obj,data,coef)
totalLoss = 0;
NoOfRecords = length(data(1,:));
BatchSize = floor(NoOfRecords*obj.batchSize);
NoOfBatches = floor(NoOfRecords/BatchSize);
params = coef; % cells of 6 arrays. There are two hidden layers 1 input and 1 output so 3 arrays of weight and 3 vectors for biases
for i=0:NoOfBatches-1
batch=data(:,i*BatchSize+1:(i+1)*BatchSize); %slide through data to extract batches
times = obj.t(i*BatchSize+1:(i+1)*BatchSize); %time coordinates for each data array
batch = [times;batch]; %combine time coordinates and data itself for decreasing # of parameters to pass
ll = [0;obj.l]; %space coordinates for each data record in batch
batch = [ll, batch]; %combine space coordinates with data for reducing number of parameters to pass
[grad, loss] = dlfeval(@train,obj,batch,params); %wraping in dlfeval
for jj = 1:obj.NL-1 %gradient descent workflow obj.LR - learning rate
params{1,jj} = params{1,jj} - obj.LR * grad{1,jj};
params{2,jj} = params{2,jj} - obj.LR * grad{2,jj};
end
totalLoss = totalLoss + loss;
end
result = params;
end
function [grads, loss] = train(obj, data, params) %a fuction to find gradient from
l = data(2:end,1); %x coordinates - extracting space coordinates
times = data(1,2:end); %time coordinates - extracting time coordinates
batch = data(2:end,2:end); % temperature values - extracting the data itself
curLayer = [batch'; l']; %input information for NN
output = NN(params,curLayer); %this function just does forward pass of input values through NN
loss = piDiffEqn(batch,output,obj.l,times); % this is a loss function that returns a double
grads = dlgradient(loss,params); % HERE I HAVE ERROR MESSAGE
end
  2 Comments
Dyuman Joshi
Dyuman Joshi on 22 Nov 2023
You should check what the syntax and the data type of inputs is expected to be for dlgradient. Go through the linked documentation page.
Alim
Alim on 22 Nov 2023
Thank you. I assume that the problem is something you mentioned, but the code worked with the parameters as it is now. I am looking through the documentation. thank you

Sign in to comment.

Accepted Answer

Alim
Alim on 23 Nov 2023
Problem solved. batch variable in the code above needs to be wrapped into dlarray.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!