package org.deeplearning4j.ui.weights;


import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import org.deeplearning4j.nn.api.Model;
import org.deeplearning4j.optimize.api.IterationListener;
import org.deeplearning4j.ui.providers.ObjectMapperProvider;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 *
 * A histogram iteration listener that
 * updates the weights of the model
 * with a web based ui.
 *
 * @author Adam Gibson
 */
public class HistogramIterationListener implements IterationListener {

    private Client client = ClientBuilder.newClient().register(JacksonJsonProvider.class).register(new ObjectMapperProvider());
    private WebTarget target;
    private int iterations = 1;

    public HistogramIterationListener(int iterations) {
        this.iterations = iterations;
        target = client.target("http://localhost:8080").path("weights").path("update");
    }




    @Override
    public boolean invoked() {
        return false;
    }

    @Override
    public void invoke() {

    }

    @Override
    public void iterationDone(Model model, int iteration) {
        if(iteration % iterations == 0) {
            ModelAndGradient g = new ModelAndGradient();
            g.setGradients(model.gradient().gradientForVariable());
            g.setParameters(model.paramTable());
            g.setScore(model.score());
            Response resp = target.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(Entity.entity(g,MediaType.APPLICATION_JSON));
            System.out.println(resp);
        }


    }
}

@@ -253,16 +253,61 @@
 rovider;
+%0Aimport org.nd4j.linalg.api.ndarray.INDArray;
 %0A%0Aimport
@@ -514,16 +514,70 @@
 esponse;
+%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -1417,117 +1417,786 @@
    M
-odelAndGradient g = new ModelAndGradient();%0A            g.setGradients(model.gradient().gradientForVariable()
+ap%3CString,INDArray%3E grad = model.gradient().gradientForVariable();%0A            Map%3CString,INDArray%3E newGrad = new LinkedHashMap%3C%3E();%0A            for(Map.Entry%3CString,INDArray%3E entry : grad.entrySet() )%7B%0A                newGrad.put(%22param_%22 + entry.getKey(),entry.getValue());    //CSS identifier can't start with digit http://www.w3.org/TR/CSS21/syndata.html#value-def-identifier%0A            %7D%0A%0A            Map%3CString,INDArray%3E params = model.paramTable();%0A            Map%3CString,INDArray%3E newParams = new LinkedHashMap%3C%3E();%0A            for(Map.Entry%3CString,INDArray%3E entry : params.entrySet()) %7B%0A                newParams.put(%22param_%22 + entry.getKey(),entry.getValue());%0A            %7D%0A%0A            ModelAndGradient g = new ModelAndGradient();%0A            g.setGradients(newGrad
 );%0A 
@@ -2222,34 +2222,25 @@
 ameters(
-model.paramTable()
+newParams
 );%0A     
@@ -2269,24 +2269,25 @@
 l.score());%0A
+%0A
             

Fix histogram listener to produce valid CSS identifier