How to train a sequence to classification network on GPU

Illustration
Tony Marino - 2023-12-07T22:15:29+00:00
Question: How to train a sequence to classification network on GPU

I have a sequence to classification network that I can successfully train on a CPU using the trainNetwork function. However, when I set the ExecutionEnvironment to GPU it takes the same amount of time as on the CPU, even though it says that it's running on the GPU. I'm assuming that's because the input/output data arrays are not on the GPU. When I try to move the arrays to the GPU (using gpuArray) the input array moves but I get an error on the output array because it's a categorical array and gpuArray only supports numeric and logical arrays.   Is there any way to move a categorical array to the GPU so that I can get trainNetwork to actually run at full speed on the GPU? Or is there another way to get a sequence to classification network to train on a GPU?

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

Training on a GPU should indeed be faster, but the issue with categorical arrays can be tricky. Here's a solution:

Convert Categorical Array to Numeric Array

You can convert the categorical array to a numeric array using the grp2idx function. This way, you can move the data to the GPU without errors.

Here's an example:

matlab
% Assuming 'labels' is your categorical array
numericLabels = grp2idx(labels);
numericLabelsGPU = gpuArray(numericLabels);

Train Network on GPU

After converting the labels, you can proceed to train your network on the GPU:

matlab
% Define training options
options = trainingOptions('adam', ...
    'ExecutionEnvironment', 'auto', ... % Automatically handle data movement
    'MiniBatchSize', 64, ...
    'MaxEpochs', 10, ...
    'InitialLearnRate', 0.001);

% Train the network
net = trainNetwork(X, numericLabelsGPU, layers, options);

By setting the ExecutionEnvironment to 'auto', MATLAB will handle the data movement and computations on the GPU for you.

Summary

  1. Convert Categorical to Numeric: Use grp2idx to convert categorical labels to numeric labels.

  2. Set ExecutionEnvironment to 'auto': Let MATLAB handle data movement.

  3. Train Network: Use trainNetwork with the converted data.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!