Search code examples
matlabgpusoftmaxmatlab-gpu

Is there a GPU implementation multiclass classification function in MATLAB?


I have a multiclass classification task, and I have tried to use 'trainSoftmaxLayer' in Matlab, but it's a CPU implementation version, and is slow. So I tried to read the documentation for a GPU option, like 'trainSoftmaxLayer('useGPU', 'yes')' in traditional neural network, but there isn't any related options.


Solution

  • Finally, the problem is sovled by hacking the source code of trainSoftmaxLayer.m, which is provided by MATLAB. We can write our own GPU-enabled softmax layer like this:

    function [net] = trainClassifier(x, t, use_gpu, showWindow)
    net = network;
    
    % define topology
    net.numInputs = 1;
    net.numLayers = 1;
    net.biasConnect = 1;
    net.inputConnect(1, 1) = 1;
    net.outputConnect = 1;
    
    % set values for labels
    net.name = 'Softmax Classifier with GPU Option';
    net.layers{1}.name = 'Softmax Layer';
    
    % define transfer function
    net.layers{1}.transferFcn = 'softmax';
    
    % set parameters
    net.performFcn = 'crossentropy';
    net.trainFcn = 'trainscg';
    net.trainParam.epochs = 1000;
    net.trainParam.showWindow = showWindow;
    net.divideFcn = 'dividetrain';
    
    if use_gpu == 1
        net = train(net, x, full(t), 'useGPU', 'yes');
    else
        net = train(net, x, full(t));
    end
    
    end