/*******************************************************************************
 * DIANNE  - Framework for distributed artificial neural networks
 * Copyright (C) 2015  iMinds - IBCN - UGent
 *
 * This file is part of DIANNE.
 *
 * DIANNE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *     Tim Verbelen, Steven Bohez
 *******************************************************************************/
package be.iminds.iot.dianne.nn.module.activation;

import java.util.UUID;

import be.iminds.iot.dianne.api.nn.module.AbstractModule;
import be.iminds.iot.dianne.tensor.ModuleOps;
import be.iminds.iot.dianne.tensor.TensorOps;

public class Softmax extends AbstractModule {

	private float alpha = 0.0001f;
	
	public Softmax() {
		super();
	}
	
	public Softmax(UUID id) {
		super(id);
	}
	
	@Override
	protected void forward() {
		output = ModuleOps.softmax(output, input);
		
		// this makes sure that you don't end up with zeros and a one, which 
		// gives -Inf in the NLL ... this does add a (small) error though...
		output = TensorOps.add(output, output, alpha);
		output = TensorOps.div(output, output, 1f + alpha*output.size());
	}

	@Override
	protected void backward() {
		gradInput = ModuleOps.softmaxGradIn(gradInput, gradOutput, output);
	}
}

@@ -1203,54 +1203,8 @@
 Ops;
-%0Aimport be.iminds.iot.dianne.tensor.TensorOps;
 %0A%0Apu
@@ -1252,42 +1252,8 @@
  %7B%0A%0A
-%09private float alpha = 0.0001f;%0A%09%0A
 %09pub
@@ -1368,16 +1368,16 @@
 ard() %7B%0A
+
 %09%09output
@@ -1417,271 +1417,8 @@
 t);%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 = TensorOps.add(output, output, alpha);%0A%09%09output = TensorOps.div(output, output, 1f + alpha*output.size());%0A
 %09%7D%0A%0A

fix softmax