package be.iminds.iot.dianne.nn.module.activation;

import java.util.UUID;

import be.iminds.iot.dianne.nn.module.AbstractModule;
import be.iminds.iot.dianne.tensor.TensorFactory;

public class Softmax extends AbstractModule {

	public Softmax(TensorFactory factory) {
		super(factory);
	}
	
	public Softmax(TensorFactory factory, UUID id) {
		super(factory, id);
	}
	
	@Override
	protected void forward() {
		output = factory.getTensorMath().softmax(output, input);
	}

	@Override
	protected void backward() {
		float sum = factory.getTensorMath().sum(factory.getTensorMath().cmul(null, gradOutput, output));
		
		gradInput = factory.getTensorMath().sub(gradInput, gradOutput, sum);
		gradInput = factory.getTensorMath().cmul(gradInput, output, gradInput);
	}
}

@@ -221,16 +221,50 @@
 dule %7B%0A%0A
+%09private float alpha = 0.0001f;%0A%09%0A
 %09public 
@@ -494,16 +494,307 @@
 input);%0A
+%09%09%0A%09%09// this makes sure that you don't end up with zeros and a one, which %0A%09%09// gives -Inf in the NLL ... this does add a (small) error though...%0A%09%09output = factory.getTensorMath().add(output, output, alpha);%0A%09%09output = factory.getTensorMath().div(output, output, 1f + alpha*output.size());%0A
 %09%7D%0A%0A%09@Ov

add small term in softmax to avoid resulting to zeros and a one