|
Java example source code file (FlowIterationListener.java)
The FlowIterationListener.java Java example source codepackage org.deeplearning4j.ui.flow; import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; import lombok.NonNull; import org.deeplearning4j.berkeley.Pair; import org.deeplearning4j.nn.api.Layer; import org.deeplearning4j.nn.api.Model; import org.deeplearning4j.nn.conf.layers.BaseOutputLayer; import org.deeplearning4j.nn.conf.layers.FeedForwardLayer; import org.deeplearning4j.nn.graph.ComputationGraph; import org.deeplearning4j.nn.graph.vertex.GraphVertex; import org.deeplearning4j.nn.graph.vertex.VertexIndices; import org.deeplearning4j.nn.layers.recurrent.BaseRecurrentLayer; import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; import org.deeplearning4j.optimize.api.IterationListener; import org.deeplearning4j.ui.UiConnectionInfo; import org.deeplearning4j.ui.UiServer; import org.deeplearning4j.ui.UiUtils; import org.deeplearning4j.ui.flow.beans.Description; import org.deeplearning4j.ui.flow.beans.LayerInfo; import org.deeplearning4j.ui.flow.beans.ModelInfo; import org.deeplearning4j.ui.providers.ObjectMapperProvider; import org.glassfish.jersey.filter.LoggingFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; import javax.ws.rs.core.UriBuilder; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; /** * This IterationListener is suited for general model performance/architecture overview * * PLEASE NOTE: WORK IN PROGRESS, DO NOT USE IT UNLESS YOU HAVE TO * @author raver119@gmail.com */ public class FlowIterationListener implements IterationListener { // TODO: basic auth should be considered here as well private String remoteAddr; private int remotePort; private String login; private String password; private int frequency = 1; private boolean firstIteration = true; private String path; private UiConnectionInfo connectionInfo; private static final List<String> colors = Collections.unmodifiableList(Arrays.asList("#9966ff", "#ff9933", "#ffff99", "#3366ff", "#0099cc", "#669999", "#66ffff")); private Client client = ClientBuilder.newClient().register(JacksonJsonProvider.class).register(new ObjectMapperProvider()); private WebTarget target; private static Logger log = LoggerFactory.getLogger(FlowIterationListener.class); /** * Creates IterationListener and keeps it detached from any UiServer instances */ protected FlowIterationListener() { // please keep this constructor protected } /** * Creates IterationListener and attaches it local UiServer instance * * @param frequency update frequency */ public FlowIterationListener(int frequency) { this("localhost", 0, frequency); } /** * Creates IterationListener and attaches it to specified remote UiServer instance * * @param address remote UiServer address * @param port remote UiServer port * @param frequency update frequency */ public FlowIterationListener(@NonNull String address, int port, int frequency) { this.remoteAddr = address; this.remotePort = port; this.frequency = frequency; UiConnectionInfo info = null; if (address.equals("localhost") || address.equals("127.0.0.1") || address.isEmpty()) { try { this.remoteAddr = "localhost"; this.remotePort = UiServer.getInstance().getPort(); info = UiServer.getInstance().getConnectionInfo(); } catch (Exception e) { throw new RuntimeException(e); } } setup(info); } /** * Creates IterationListener and attaches it to specified remote UiServer instance * * @param login Login for HTTP Basic auth * @param password Password for HTTP Basic auth * @param address remote UiServer address * @param port remote UiServer port * @param frequency update frequency */ public FlowIterationListener(@NonNull String login, @NonNull String password, @NonNull String address, int port, int frequency) { this(address, port, frequency); this.connectionInfo.setLogin(login); this.connectionInfo.setPassword(password); this.login = login; this.password = password; } public FlowIterationListener(@NonNull UiConnectionInfo connectionInfo, int frequency) { setup(connectionInfo); } private void setup(@NonNull UiConnectionInfo connectionInfo) { // TODO: add auth option this.connectionInfo = connectionInfo; java.util.logging.Logger logger = java.util.logging.Logger.getGlobal(); login = null; password = null; // client.register(new LoggingFilter(logger, true)); if (login == null || password == null) target = client.target(connectionInfo.getFirstPart()).path(connectionInfo.getSecondPart("flow")).path("state").queryParam("sid", connectionInfo.getSessionId()); this.path = connectionInfo.getFullAddress("flow"); log.info("Flow UI address: " + this.path); } /** * Get if listener invoked */ @Override public boolean invoked() { return false; } /** * Change invoke to true */ @Override public void invoke() { } /** * Event listener for each iteration * * @param model the model iterating * @param iteration the iteration */ @Override public synchronized void iterationDone(Model model, int iteration) { if (iteration % frequency == 0) { /* Basic plan: 1. We should detect, if that's CompGraph or MultilayerNetwork. However the actual difference will be limited to number of non-linear connections. 2. Network structure should be converted to JSON 3. Params for each node should be packed to JSON as well 4. For specific cases (like CNN) binary data should be wrapped into base64 5. For arrays/params gzip could be used (to be investigated) ...... Later, on client side, this JSON should be parsed and rendered. So, proper object structure to be considered. */ // On first pass we just build list of layers. However, for MultiLayerNetwork first pass is the last pass, since we know connections in advance ModelInfo info = buildModelInfo(model); // add info about inputs /* as soon as model info is built, we need to define color scheme based on number of unique nodes */ // send ModelInfo to UiServer Response resp = target.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(Entity.entity(info, MediaType.APPLICATION_JSON)); // log.info("ModelInfo:" + Entity.entity(info, MediaType.APPLICATION_JSON)); log.debug("Response: " + resp); /* TODO: it would be nice to send updates of nodes as well */ if(firstIteration){ try { UiUtils.tryOpenBrowser(path, log); } catch (Exception e) { ; } firstIteration = false; } } } /** * This method returns all Layers connected to the currentInput * * @param vertices * @param currentInput * @param currentY * @return */ protected List<LayerInfo> flattenToY(ModelInfo model, GraphVertex[] vertices, List Other Java examples (source code examples)Here is a short list of links related to this Java FlowIterationListener.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.