/*******************************************************************************
 * 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.join;

import java.util.UUID;

import be.iminds.iot.dianne.tensor.Tensor;
import be.iminds.iot.dianne.tensor.TensorOps;


public class Accumulate extends Join {

	public Accumulate() {
		super();
	}
	
	public Accumulate(UUID id) {
		super(id);
	}
	
	@Override
	protected void forward() {
		// accumulate inputs
		if(output==null){
			output = new Tensor(inputs.values().stream().filter(t -> t !=null).findFirst().get().dims());
		}
		output.fill(0.0f);
		for(Tensor t : inputs.values()){
			if(t!=null)
				output = TensorOps.add(output, output, t);
		}
	}

	@Override
	protected void backward() {
		// forward same error to all
		// TODO copy needed?
		for(UUID id : gradInputs.keySet()){
			gradInputs.put(id, gradOutput);
		}
	}

}

@@ -1692,31 +1692,8 @@
 all%0A
-%09%09// TODO copy needed?%0A
 %09%09fo
@@ -1726,16 +1726,105 @@
 Set())%7B%0A
+%09%09%09Tensor gradInput = gradInputs.get(id);%0A%09%09%09gradInput = gradOutput.copyInto(gradInput);%0A
 %09%09%09gradI
@@ -1841,19 +1841,18 @@
 id, grad
-Out
+In
 put);%0A%09%09

copy gradOut to all prevs in Accumulate