public int numParams(NeuralNetConfiguration conf) { FeedForwardLayer layerConf = (FeedForwardLayer) conf.getLayer(); return super.numParams(conf) + numUsers * layerConf.getNOut(); // plus another user weight matrix }
public Map<String, INDArray> getGradientsFromFlattened(NeuralNetConfiguration conf, INDArray gradientView) { Map<String, INDArray> out = super.getGradientsFromFlattened(conf, gradientView); FeedForwardLayer layerConf = (FeedForwardLayer) conf.getLayer(); int nIn = layerConf.getNIn(); int nOut = layerConf.getNOut(); int nWeightParams = nIn * nOut; int nUserWeightParams = numUsers * nOut; INDArray userWeightGradientView = gradientView.get(NDArrayIndex.point(0), NDArrayIndex.interval(nWeightParams + nOut, nWeightParams + nOut + nUserWeightParams)) .reshape('f', numUsers, nOut); out.put(USER_WEIGHT_KEY, userWeightGradientView); return out; } }
public Map<String, INDArray> init(NeuralNetConfiguration conf, INDArray paramsView, boolean initializeParams) { Map<String, INDArray> params = super.init(conf, paramsView, initializeParams); FeedForwardLayer layerConf = (FeedForwardLayer) conf.getLayer(); int nIn = layerConf.getNIn(); int nOut = layerConf.getNOut(); int nWeightParams = nIn * nOut; int nUserWeightParams = numUsers * nOut; INDArray userWeightView = paramsView.get(NDArrayIndex.point(0), NDArrayIndex.interval(nWeightParams + nOut, nWeightParams + nOut + nUserWeightParams)); params.put(USER_WEIGHT_KEY, this.createUserWeightMatrix(conf, userWeightView, initializeParams)); conf.addVariable(USER_WEIGHT_KEY); return params; }
protected INDArray createUserWeightMatrix(NeuralNetConfiguration conf, INDArray weightParamView, boolean initializeParameters) { FeedForwardLayer layerConf = (FeedForwardLayer) conf.getLayer(); if (initializeParameters) { Distribution dist = Distributions.createDistribution(layerConf.getDist()); return createWeightMatrix(numUsers, layerConf.getNOut(), layerConf.getWeightInit(), dist, weightParamView, true); } else { return createWeightMatrix(numUsers, layerConf.getNOut(), null, null, weightParamView, false); } }
if (conf.isUseDropConnect() && training && conf.getLayer().getDropOut() > 0) { W = Dropout.applyDropConnect(this, CDAEParamInitializer.WEIGHT_KEY);
@Override public MemoryReport getMemoryReport(InputType... inputTypes) { //TODO preprocessor memory return layerConf.getLayer().getMemoryReport(inputTypes[0]); } }
/** * @param conf the configuration to get * the number of kernels from * @return the number of kernels/filters to apply */ public static int numFeatureMap(NeuralNetConfiguration conf) { return ((org.deeplearning4j.nn.conf.layers.ConvolutionLayer) conf.getLayer()).getNOut(); }
public Map<String, INDArray> paramTable(boolean backpropParamsOnly) { //Get all parameters from all layers Map<String, INDArray> allParams = new LinkedHashMap<>(); for (Layer layer : layers) { Map<String, INDArray> paramMap = layer.paramTable(backpropParamsOnly); for (Map.Entry<String, INDArray> entry : paramMap.entrySet()) { String newKey = layer.conf().getLayer().getLayerName() + "_" + entry.getKey(); allParams.put(newKey, entry.getValue()); } } return allParams; }
public SubsamplingLayer(NeuralNetConfiguration conf) { super(conf); initializeHelper(); this.convolutionMode = ((org.deeplearning4j.nn.conf.layers.SubsamplingLayer) conf.getLayer()).getConvolutionMode(); }
@Override public int numParams(NeuralNetConfiguration conf) { org.deeplearning4j.nn.conf.layers.BasePretrainNetwork layerConf = (org.deeplearning4j.nn.conf.layers.BasePretrainNetwork) conf.getLayer(); return super.numParams(conf) + layerConf.getNIn(); }
private INDArray createBeta(NeuralNetConfiguration conf, INDArray betaView, boolean initializeParams) { BatchNormalization layer = (BatchNormalization) conf.getLayer(); if (initializeParams) betaView.assign(layer.getBeta()); return betaView; }
@Override public int numParams(NeuralNetConfiguration conf) { org.deeplearning4j.nn.conf.layers.FeedForwardLayer layerConf = (org.deeplearning4j.nn.conf.layers.FeedForwardLayer) conf.getLayer(); int nIn = layerConf.getNIn(); int nOut = layerConf.getNOut(); // also equal to numClasses return nIn * nOut + nOut + nIn * nOut; //weights + bias + embeddings }
@Override public org.deeplearning4j.nn.graph.vertex.GraphVertex instantiate(ComputationGraph graph, String name, int idx, INDArray paramsView, boolean initializeParams) { //Now, we need to work out if this vertex is an output vertex or not... boolean isOutput = graph.getConfiguration().getNetworkOutputs().contains(name); org.deeplearning4j.nn.api.Layer layer = layerConf.getLayer().instantiate(layerConf, null, idx, paramsView, initializeParams); return new org.deeplearning4j.nn.graph.vertex.impl.LayerVertex(graph, name, idx, layer, preProcessor, isOutput); }
/** * Set the state of the RNN layer, for use in {@link #rnnTimeStep(INDArray...)} * * @param layer The number/index of the layer. * @param state The state to set the specified layer to */ public void rnnSetPreviousState(int layer, Map<String, INDArray> state) { rnnSetPreviousState(layers[layer].conf().getLayer().getLayerName(), state); }
public boolean isPretrainUpdaterBlock() { //All in block should be the same layer, and all be pretrain params ParamState vs = layersAndVariablesInBlock.get(0); return vs.getLayer().conf().getLayer().isPretrainParam(vs.getParamName()); }
public void init() { if (gradientUpdater == null) { ParamState varState = layersAndVariablesInBlock.get(0); String varName = varState.getParamName(); gradientUpdater = varState.getLayer().conf().getLayer().getIUpdaterByParam(varName).instantiate(updaterView, updaterViewRequiresInitialization); //UpdaterUtils.getGradientUpdater(varState.getLayer(), varState.getParamName()); } }
/** * Fit the model * * @param examples the examples to classify (one example in each row) * @param labels the labels for each example (the number of labels must match */ @Override public void fit(INDArray examples, int[] labels) { org.deeplearning4j.nn.conf.layers.OutputLayer layerConf = (org.deeplearning4j.nn.conf.layers.OutputLayer) getOutputLayer().conf().getLayer(); fit(examples, FeatureUtil.toOutcomeMatrix(labels, layerConf.getNOut())); }
@Override public Map<String, INDArray> init(NeuralNetConfiguration conf, INDArray paramsView, boolean initializeParams) { FrozenLayer fl = (FrozenLayer) conf.getLayer(); Layer innerLayer = fl.getLayer(); ParamInitializer initializer = innerLayer.initializer(); conf.setLayer(innerLayer); Map<String, INDArray> m = initializer.init(conf, paramsView, initializeParams); conf.setLayer(fl); return m; }
@Override public Map<String, INDArray> getGradientsFromFlattened(NeuralNetConfiguration conf, INDArray gradientView) { FrozenLayer fl = (FrozenLayer) conf.getLayer(); Layer innerLayer = fl.getLayer(); ParamInitializer initializer = innerLayer.initializer(); conf.setLayer(innerLayer); Map<String, INDArray> m = initializer.getGradientsFromFlattened(conf, gradientView); conf.setLayer(fl); return m; } }
@Override public void setBackpropGradientsViewArray(INDArray gradients) { if (this.params != null && gradients.length() != numParams()) throw new IllegalArgumentException("Invalid input: expect gradients array of length " + numParams(true) + ", got array of length " + gradients.length() + " - " + layerId()); this.gradientsFlattened = gradients; this.gradientViews = conf.getLayer().initializer().getGradientsFromFlattened(conf, gradients); }