package com.github.neuralnetworks.calculation.neuronfunctions;

import com.amd.aparapi.Kernel;
import com.github.neuralnetworks.tensor.Matrix;
import com.github.neuralnetworks.tensor.Tensor;
import com.github.neuralnetworks.util.Environment;

/**
 * Softmax activation function
 */
public class SoftmaxFunction extends Kernel implements TensorFunction {

    private static final long serialVersionUID = 1L;

    private float[] values;
    private int startIndex;
    private int nextRowStep;
    private int nextColumnStep;
    private int rows;

    @Override
    public void value(Tensor inputOutput) {
	Matrix io = (Matrix) inputOutput;

	this.values = io.getElements();
	this.startIndex = io.getStartIndex();
	this.nextRowStep = io.getRowElementsDistance();
	this.nextColumnStep = io.getColumnElementsDistance();
	this.rows = io.getRows();

	Environment.getInstance().getExecutionStrategy().execute(this, io.getColumns());
    }

    @Override
    public void run() {
	float sum = 0;
	int start = startIndex;
	int r = rows;
	int nr = nextRowStep;
	int nc = nextColumnStep;
	int id = getGlobalId();

	for (int i = 0; i < r; i++) {
	    sum += values[start + i * nr + id * nc];
	}

	for (int i = 0; i < r; i++) {
	    values[start + i * nr + id * nc] /= sum;
	}
    }
}

@@ -1125,24 +1125,110 @@
 %3C r; i++) %7B%0A
+            values%5Bstart + i * nr + id * nc%5D = exp(values%5Bstart + i * nr + id * nc%5D);%0A
 %09    sum += 
@@ -1260,16 +1260,17 @@
 d * nc%5D;
+%09
 %0A%09%7D%0A%0A%09fo

Update SoftmaxFunction.java