Conversion to double from gpuArray is not possible.

I am training a CNN with some modification in classification layer. I have called a function in loss layer but when i start training the netwrok i get the folowing error:
Error using 'forwardLoss' in Layer ClassificationLayer. The function threw an error and could not be executed.
Caused by:
The following error occurred converting from gpuArray to double:
Conversion to double from gpuArray is not possible.
How to resolve this issue.?

Answers (1)

Raza,
My guess is that forwardLoss takes one or more input arguments, one of which is being passed a gpuArray instead of a double. Before calling forwardLoss, you can gather the data from the GPU to the CPU as such:
% GPU array "ga"
ga = ...
% Copy the data from the GPU to the CPU into "a"
a = gather(ga);
% Call forwardLoss fcn
forwardLoss(a,...);

3 Comments

Below is the code of classification layer, as you suggested I tried but its again generating error
classdef LossLayer< nnet.layer.ClassificationLayer
properties
end
methods
function layer = LossLayer(name)
% layer = ClassificationLayer(classWeights) creates a
% Set layer name.
if nargin == 2
layer.Name = name;
end
% Set layer description
layer.Description = 'Loss Fucntion';
end
% %%%%%%%%%GPU array "ga"
addpath ('D:\Ditsnave Transform')
ga=WeightMap(T(:,:,1));
W = gather(ga);
%%%%%%%%%%%%%%%%
function loss = forwardLoss(layer, Y, T)
% loss = forwardLoss(layer, Y, T) returns the weighted cross
% entropy loss between the predictions Y and the training
% targets T.
N = size(Y, 4) * size(Y, 1) * size(Y, 2);
Y = squeeze(Y);
T = squeeze(T);
loss_i = ( layer.W.*T .* log(nnet.internal.cnn.util.boundAwayFromZero(Y)));
loss = -sum( sum( sum( sum(loss_i, 3).*(1./N), 1), 2));
end
function dLdY = backwardLoss(layer, Y, T)
% dLdX = backwardLoss(layer, Y, T) returns the derivatives of
% the weighted cross entropy loss with respect to the
% predictions Y.
N = size(Y, 4) * size(Y, 1) * size(Y, 2);
Y = squeeze(Y);
T = squeeze(T);
dLdY= (-(T./nnet.internal.cnn.util.boundAwayFromZero(Y))).*(1./N);
end
end
end
The specific error that you're receiving typically occurs when you try to assign gpuArray data into a double array, like this:
doubleData = zeros(1,10);
doubleData(3) = gpuArray(7);
The code you posted doesn't appear to do that in any way that I can see... You might want to try running with dbstop if all error to see exactly where the problem is showing up.
Sometimes this sort of thing can be fixed by using the 'like' syntax to build output arrays, like so:
myArray = gpuArray(7); % perhaps this is a function input on the GPU
outputData = zeros(1, 10, 'like', myArray); % outputData is on the GPU if myArray is on the GPU
outputData(3) = myArray;
Thank you Edric Ellis, although the code is still not executable but 'like' and 'dbstop' helped alot to understand the process.

Sign in to comment.

Asked:

on 1 Sep 2020

Commented:

on 3 Sep 2020

Community Treasure Hunt

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

Start Hunting!