use of org.deeplearning4j.nn.graph.ComputationGraph in project deeplearning4j by deeplearning4j.
the class RemoteFlowIterationListener method buildModelInfo.
protected ModelInfo buildModelInfo(Model model) {
ModelInfo modelInfo = new ModelInfo();
if (model instanceof ComputationGraph) {
ComputationGraph graph = (ComputationGraph) model;
/*
we assume that graph starts on input. every layer connected to input - is on y1
every layer connected to y1, is on y2 etc.
*/
List<String> inputs = graph.getConfiguration().getNetworkInputs();
// now we need to add inputs as y0 nodes
int x = 0;
for (String input : inputs) {
GraphVertex vertex = graph.getVertex(input);
INDArray gInput = vertex.getInputs()[0];
long tadLength = Shape.getTADLength(gInput.shape(), ArrayUtil.range(1, gInput.rank()));
long numSamples = gInput.lengthLong() / tadLength;
StringBuilder builder = new StringBuilder();
builder.append("Vertex name: ").append(input).append("<br/>");
builder.append("Model input").append("<br/>");
builder.append("Input size: ").append(tadLength).append("<br/>");
builder.append("Batch size: ").append(numSamples).append("<br/>");
LayerInfo info = new LayerInfo();
info.setId(0);
info.setName(input);
info.setY(0);
info.setX(x);
info.setLayerType(INPUT);
info.setDescription(new Description());
info.getDescription().setMainLine("Model input");
info.getDescription().setText(builder.toString());
modelInfo.addLayer(info);
x++;
}
GraphVertex[] vertices = graph.getVertices();
// filling grid in LTR/TTB direction
List<String> needle = new ArrayList<>();
// we assume that max row can't be higher then total number of vertices
for (int y = 1; y < vertices.length; y++) {
if (needle.isEmpty())
needle.addAll(inputs);
/*
for each grid row we look for nodes, that are connected to previous layer
*/
List<LayerInfo> layersForGridY = flattenToY(modelInfo, vertices, needle, y);
needle.clear();
for (LayerInfo layerInfo : layersForGridY) {
needle.add(layerInfo.getName());
}
if (needle.isEmpty())
break;
}
} else if (model instanceof MultiLayerNetwork) {
MultiLayerNetwork network = (MultiLayerNetwork) model;
// manually adding input layer
INDArray input = model.input();
long tadLength = Shape.getTADLength(input.shape(), ArrayUtil.range(1, input.rank()));
long numSamples = input.lengthLong() / tadLength;
StringBuilder builder = new StringBuilder();
builder.append("Model input").append("<br/>");
builder.append("Input size: ").append(tadLength).append("<br/>");
builder.append("Batch size: ").append(numSamples).append("<br/>");
LayerInfo info = new LayerInfo();
info.setId(0);
info.setName("Input");
info.setY(0);
info.setX(0);
info.setLayerType(INPUT);
info.setDescription(new Description());
info.getDescription().setMainLine("Model input");
info.getDescription().setText(builder.toString());
info.addConnection(0, 1);
modelInfo.addLayer(info);
// entry 0 is reserved for inputs
int y = 1;
// for MLN x value is always 0
final int x = 0;
for (Layer layer : network.getLayers()) {
LayerInfo layerInfo = getLayerInfo(layer, x, y, y);
// since it's MLN, we know connections in advance as curLayer + 1
layerInfo.addConnection(x, y + 1);
modelInfo.addLayer(layerInfo);
y++;
}
LayerInfo layerInfo = modelInfo.getLayerInfoByCoords(x, y - 1);
layerInfo.dropConnections();
}
// find layers without connections, and mark them as output layers
for (LayerInfo layerInfo : modelInfo.getLayers()) {
if (layerInfo.getConnections().size() == 0)
layerInfo.setLayerType("OUTPUT");
}
// now we apply colors to distinct layer types
AtomicInteger cnt = new AtomicInteger(0);
for (String layerType : modelInfo.getLayerTypes()) {
String curColor = colors.get(cnt.getAndIncrement());
if (cnt.get() >= colors.size())
cnt.set(0);
for (LayerInfo layerInfo : modelInfo.getLayersByType(layerType)) {
if (layerType.equals(INPUT)) {
layerInfo.setColor("#99ff66");
} else if (layerType.equals("OUTPUT")) {
layerInfo.setColor("#e6e6e6");
} else {
layerInfo.setColor(curColor);
}
}
}
return modelInfo;
}
use of org.deeplearning4j.nn.graph.ComputationGraph in project deeplearning4j by deeplearning4j.
the class RemoteFlowIterationListener method buildModelState.
protected void buildModelState(Model model) {
// first we update performance state
long timeSpent = currTime - lastTime;
float timeSec = timeSpent / 1000f;
INDArray input = model.input();
long tadLength = Shape.getTADLength(input.shape(), ArrayUtil.range(1, input.rank()));
long numSamples = input.lengthLong() / tadLength;
modelState.addPerformanceSamples(numSamples / timeSec);
modelState.addPerformanceBatches(1 / timeSec);
modelState.setIterationTime(timeSpent);
// now model score
modelState.addScore((float) model.score());
modelState.setScore((float) model.score());
modelState.setTrainingTime(parseTime(System.currentTimeMillis() - initTime));
// and now update model params/gradients
Map<String, Map> newGrad = new LinkedHashMap<>();
Map<String, Map> newParams = new LinkedHashMap<>();
Map<String, INDArray> params = model.paramTable();
Layer[] layers = null;
if (model instanceof MultiLayerNetwork) {
layers = ((MultiLayerNetwork) model).getLayers();
} else if (model instanceof ComputationGraph) {
layers = ((ComputationGraph) model).getLayers();
}
List<Double> lrs = new ArrayList<>();
if (layers != null) {
for (Layer layer : layers) {
lrs.add(layer.conf().getLayer().getLearningRate());
}
modelState.setLearningRates(lrs);
}
Map<Integer, LayerParams> layerParamsMap = new LinkedHashMap<>();
for (Map.Entry<String, INDArray> entry : params.entrySet()) {
String param = entry.getKey();
if (!Character.isDigit(param.charAt(0)))
continue;
int layer = Integer.parseInt(param.replaceAll("\\_.*$", ""));
String key = param.replaceAll("^.*?_", "").toLowerCase();
if (!layerParamsMap.containsKey(layer))
layerParamsMap.put(layer, new LayerParams());
HistogramBin histogram = new HistogramBin.Builder(entry.getValue().dup()).setBinCount(14).setRounding(6).build();
// TODO: something better would be nice to have here
if (key.equalsIgnoreCase("w")) {
layerParamsMap.get(layer).setW(histogram.getData());
} else if (key.equalsIgnoreCase("rw")) {
layerParamsMap.get(layer).setRW(histogram.getData());
} else if (key.equalsIgnoreCase("rwf")) {
layerParamsMap.get(layer).setRWF(histogram.getData());
} else if (key.equalsIgnoreCase("b")) {
layerParamsMap.get(layer).setB(histogram.getData());
}
}
modelState.setLayerParams(layerParamsMap);
}
use of org.deeplearning4j.nn.graph.ComputationGraph in project deeplearning4j by deeplearning4j.
the class FlowIterationListener method buildModelInfo.
protected ModelInfo buildModelInfo(Model model) {
ModelInfo modelInfo = new ModelInfo();
if (model instanceof ComputationGraph) {
ComputationGraph graph = (ComputationGraph) model;
/*
we assume that graph starts on input. every layer connected to input - is on y1
every layer connected to y1, is on y2 etc.
*/
List<String> inputs = graph.getConfiguration().getNetworkInputs();
// now we need to add inputs as y0 nodes
int x = 0;
for (String input : inputs) {
GraphVertex vertex = graph.getVertex(input);
long numSamples;
long tadLength;
if (vertex.getInputs() == null || vertex.getInputs().length == 0) {
numSamples = 0;
tadLength = 0;
} else {
INDArray gInput = vertex.getInputs()[0];
tadLength = Shape.getTADLength(gInput.shape(), ArrayUtil.range(1, gInput.rank()));
numSamples = gInput.lengthLong() / tadLength;
}
StringBuilder builder = new StringBuilder();
builder.append("Vertex name: ").append(input).append("<br/>");
builder.append("Model input").append("<br/>");
builder.append("Input size: ").append(tadLength).append("<br/>");
builder.append("Batch size: ").append(numSamples).append("<br/>");
LayerInfo info = new LayerInfo();
info.setId(0);
info.setName(input);
info.setY(0);
info.setX(x);
info.setLayerType(INPUT);
info.setDescription(new Description());
info.getDescription().setMainLine("Model input");
info.getDescription().setText(builder.toString());
modelInfo.addLayer(info);
x++;
}
GraphVertex[] vertices = graph.getVertices();
// filling grid in LTR/TTB direction
List<String> needle = new ArrayList<>();
// we assume that max row can't be higher then total number of vertices
for (int y = 1; y < vertices.length; y++) {
if (needle.isEmpty())
needle.addAll(inputs);
/*
for each grid row we look for nodes, that are connected to previous layer
*/
List<LayerInfo> layersForGridY = flattenToY(modelInfo, vertices, needle, y);
needle.clear();
for (LayerInfo layerInfo : layersForGridY) {
needle.add(layerInfo.getName());
}
if (needle.isEmpty())
break;
}
} else if (model instanceof MultiLayerNetwork) {
MultiLayerNetwork network = (MultiLayerNetwork) model;
// manually adding input layer
INDArray input = model.input();
long tadLength = Shape.getTADLength(input.shape(), ArrayUtil.range(1, input.rank()));
long numSamples = input.lengthLong() / tadLength;
StringBuilder builder = new StringBuilder();
builder.append("Model input").append("<br/>");
builder.append("Input size: ").append(tadLength).append("<br/>");
builder.append("Batch size: ").append(numSamples).append("<br/>");
LayerInfo info = new LayerInfo();
info.setId(0);
info.setName("Input");
info.setY(0);
info.setX(0);
info.setLayerType(INPUT);
info.setDescription(new Description());
info.getDescription().setMainLine("Model input");
info.getDescription().setText(builder.toString());
info.addConnection(0, 1);
modelInfo.addLayer(info);
// entry 0 is reserved for inputs
int y = 1;
// for MLN x value is always 0
final int x = 0;
for (Layer layer : network.getLayers()) {
LayerInfo layerInfo = getLayerInfo(layer, x, y, y);
// since it's MLN, we know connections in advance as curLayer + 1
layerInfo.addConnection(x, y + 1);
modelInfo.addLayer(layerInfo);
y++;
}
LayerInfo layerInfo = modelInfo.getLayerInfoByCoords(x, y - 1);
layerInfo.dropConnections();
}
// find layers without connections, and mark them as output layers
for (LayerInfo layerInfo : modelInfo.getLayers()) {
if (layerInfo.getConnections().size() == 0)
layerInfo.setLayerType("OUTPUT");
}
// now we apply colors to distinct layer types
AtomicInteger cnt = new AtomicInteger(0);
for (String layerType : modelInfo.getLayerTypes()) {
String curColor = colors.get(cnt.getAndIncrement());
if (cnt.get() >= colors.size())
cnt.set(0);
for (LayerInfo layerInfo : modelInfo.getLayersByType(layerType)) {
if (layerType.equals(INPUT)) {
layerInfo.setColor("#99ff66");
} else if (layerType.equals("OUTPUT")) {
layerInfo.setColor("#e6e6e6");
} else {
layerInfo.setColor(curColor);
}
}
}
return modelInfo;
}
use of org.deeplearning4j.nn.graph.ComputationGraph in project deeplearning4j by deeplearning4j.
the class FlowIterationListener method buildModelState.
protected void buildModelState(Model model) {
// first we update performance state
long timeSpent = currTime - lastTime;
float timeSec = timeSpent / 1000f;
INDArray input = model.input();
long tadLength = Shape.getTADLength(input.shape(), ArrayUtil.range(1, input.rank()));
long numSamples = input.lengthLong() / tadLength;
modelState.addPerformanceSamples(numSamples / timeSec);
modelState.addPerformanceBatches(1 / timeSec);
modelState.setIterationTime(timeSpent);
// now model score
modelState.addScore((float) model.score());
modelState.setScore((float) model.score());
modelState.setTrainingTime(parseTime(System.currentTimeMillis() - initTime));
// and now update model params/gradients
Map<String, Map> newGrad = new LinkedHashMap<>();
Map<String, Map> newParams = new LinkedHashMap<>();
Map<String, INDArray> params = model.paramTable();
Layer[] layers = null;
if (model instanceof MultiLayerNetwork) {
layers = ((MultiLayerNetwork) model).getLayers();
} else if (model instanceof ComputationGraph) {
layers = ((ComputationGraph) model).getLayers();
}
List<Double> lrs = new ArrayList<>();
if (layers != null) {
for (Layer layer : layers) {
lrs.add(layer.conf().getLayer().getLearningRate());
}
modelState.setLearningRates(lrs);
}
Map<Integer, LayerParams> layerParamsMap = new LinkedHashMap<>();
for (Map.Entry<String, INDArray> entry : params.entrySet()) {
String param = entry.getKey();
if (!Character.isDigit(param.charAt(0)))
continue;
int layer = Integer.parseInt(param.replaceAll("\\_.*$", ""));
String key = param.replaceAll("^.*?_", "").toLowerCase();
if (!layerParamsMap.containsKey(layer))
layerParamsMap.put(layer, new LayerParams());
HistogramBin histogram = new HistogramBin.Builder(entry.getValue().dup()).setBinCount(14).setRounding(6).build();
// TODO: something better would be nice to have here
if (key.equalsIgnoreCase("w")) {
layerParamsMap.get(layer).setW(histogram.getData());
} else if (key.equalsIgnoreCase("rw")) {
layerParamsMap.get(layer).setRW(histogram.getData());
} else if (key.equalsIgnoreCase("rwf")) {
layerParamsMap.get(layer).setRWF(histogram.getData());
} else if (key.equalsIgnoreCase("b")) {
layerParamsMap.get(layer).setB(histogram.getData());
}
}
modelState.setLayerParams(layerParamsMap);
}
use of org.deeplearning4j.nn.graph.ComputationGraph in project deeplearning4j by deeplearning4j.
the class TestRenders method testHistogramComputationGraphUnderscoresInName.
@Test
public void testHistogramComputationGraphUnderscoresInName() throws Exception {
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).graphBuilder().addInputs("input").setInputTypes(InputType.convolutional(1, 28, 28)).addLayer("cnn_1", new ConvolutionLayer.Builder(2, 2).stride(2, 2).nIn(1).nOut(3).build(), "input").addLayer("cnn_2", new ConvolutionLayer.Builder(4, 4).stride(2, 2).padding(1, 1).nIn(1).nOut(3).build(), "input").addLayer("max_1", new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2).build(), "cnn_1", "cnn_2").addLayer("output", new OutputLayer.Builder().nIn(7 * 7 * 6).nOut(10).build(), "max_1").setOutputs("output").pretrain(false).backprop(true).build();
ComputationGraph graph = new ComputationGraph(conf);
graph.init();
graph.setListeners(new HistogramIterationListener(1), new ScoreIterationListener(1));
DataSetIterator mnist = new MnistDataSetIterator(32, 640, false, true, false, 12345);
graph.fit(mnist);
}
Aggregations