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

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import be.iminds.iot.dianne.api.nn.learn.Processor;
import be.iminds.iot.dianne.tensor.Tensor;

/**
 * Additional learning techniques like Momentum can be implemented as a Processor decorator
 */
public class MomentumProcessor extends AbstractProcessor {

	private final Processor decorated;
	private final float momentum;
	
	private Map<UUID, Tensor> previousDelta = new HashMap<UUID, Tensor>();
	
	public MomentumProcessor( AbstractProcessor p, float momentum ) {
		super(p.factory, p.nn, p.logger);
		this.decorated = p;
		
		this.momentum = momentum;
	}
	
	@Override
	public float processNext() {
		float error = decorated.processNext();
		
		// add momentum
		nn.getTrainables().entrySet().stream().forEach(e -> {
			Tensor prev = previousDelta.get(e.getKey());
			if(prev!=null){
				Tensor deltaParams = e.getValue().getDeltaParameters();
				factory.getTensorMath().add(deltaParams, deltaParams , momentum, prev);
			}
		});
		
		// copy to previousGrad
		nn.getTrainables().entrySet().stream().forEach(e -> {
			Tensor prev = previousDelta.get(e.getKey());
			Tensor deltaParams = e.getValue().getDeltaParameters();
			deltaParams.copyInto(prev);
			previousDelta.put(e.getKey(), deltaParams);
		});
		
		return error;
	}

}

@@ -1495,29 +1495,24 @@
 Tensor%3E 
-previousDelta
+velocity
  = new H
@@ -1867,36 +1867,28 @@
 %09Tensor 
-pre
 v = 
-previousDelta
+velocity
 .get(e.g
@@ -1903,19 +1903,16 @@
 ;%0A%09%09%09if(
-pre
 v!=null)
@@ -2042,19 +2042,16 @@
 mentum, 
-pre
 v);%0A%09%09%09%7D
@@ -2077,20 +2077,16 @@
  to 
-previousGrad
+velocity
 %0A%09%09n
@@ -2152,28 +2152,20 @@
 sor 
-pre
 v = 
-previousDelta
+velocity
 .get
@@ -2244,52 +2244,16 @@
 %0A%09%09%09
-deltaParams.copyInto(prev);%0A%09%09%09previousDelta
+velocity
 .put
@@ -2276,16 +2276,28 @@
 taParams
+.copyInto(v)
 );%0A%09%09%7D);

fix momentum to explicitly state velocity