Java Code Examples for org.nd4j.common.primitives.Pair#setSecond()
The following examples show how to use
org.nd4j.common.primitives.Pair#setSecond() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CentersHolder.java From deeplearning4j with Apache License 2.0 | 6 votes |
public synchronized Pair<Double, Long> getCenterByMinDistance(Point point, Distance distanceFunction) { if (distances == null) distances = Nd4j.create(centers.dataType(), centers.rows()); if (argMin == null) argMin = Nd4j.createUninitialized(DataType.LONG, new long[0]); if (op == null) { op = ClusterUtils.createDistanceFunctionOp(distanceFunction, centers, point.getArray(), 1); imin = new ArgMin(distances, argMin); op.setZ(distances); } op.setY(point.getArray()); Nd4j.getExecutioner().exec(op); Nd4j.getExecutioner().exec(imin); Pair<Double, Long> result = new Pair<>(); result.setFirst(distances.getDouble(argMin.getLong(0))); result.setSecond(argMin.getLong(0)); return result; }
Example 2
Source File: LSTM.java From deeplearning4j with Apache License 2.0 | 5 votes |
private Pair<Gradient, INDArray> backpropGradientHelper(final INDArray epsilon, final boolean truncatedBPTT, final int tbpttBackwardLength, LayerWorkspaceMgr workspaceMgr) { assertInputSet(true); final INDArray inputWeights = getParamWithNoise(LSTMParamInitializer.INPUT_WEIGHT_KEY, true, workspaceMgr); final INDArray recurrentWeights = getParamWithNoise(LSTMParamInitializer.RECURRENT_WEIGHT_KEY, true, workspaceMgr); //Shape: [hiddenLayerSize,4*hiddenLayerSize+3]; order: [wI,wF,wO,wG,wFF,wOO,wGG] //First: Do forward pass to get gate activations, zs etc. FwdPassReturn fwdPass; if (truncatedBPTT) { fwdPass = activateHelper(true, stateMap.get(STATE_KEY_PREV_ACTIVATION), stateMap.get(STATE_KEY_PREV_MEMCELL), true, workspaceMgr); //Store last time step of output activations and memory cell state in tBpttStateMap tBpttStateMap.put(STATE_KEY_PREV_ACTIVATION, fwdPass.lastAct.detach()); tBpttStateMap.put(STATE_KEY_PREV_MEMCELL, fwdPass.lastMemCell.detach()); } else { fwdPass = activateHelper(true, null, null, true, workspaceMgr); } fwdPass.fwdPassOutput = permuteIfNWC(fwdPass.fwdPassOutput); Pair<Gradient,INDArray> p = LSTMHelpers.backpropGradientHelper(this, this.conf, this.layerConf().getGateActivationFn(), permuteIfNWC(this.input), recurrentWeights, inputWeights, permuteIfNWC(epsilon), truncatedBPTT, tbpttBackwardLength, fwdPass, true, LSTMParamInitializer.INPUT_WEIGHT_KEY, LSTMParamInitializer.RECURRENT_WEIGHT_KEY, LSTMParamInitializer.BIAS_KEY, gradientViews, null, false, helper, workspaceMgr, layerConf().isHelperAllowFallback()); weightNoiseParams.clear(); p.setSecond(permuteIfNWC(backpropDropOutIfPresent(p.getSecond()))); return p; }
Example 3
Source File: GravesLSTM.java From deeplearning4j with Apache License 2.0 | 5 votes |
private Pair<Gradient, INDArray> backpropGradientHelper(final INDArray epsilon, final boolean truncatedBPTT, final int tbpttBackwardLength, LayerWorkspaceMgr workspaceMgr) { assertInputSet(true); final INDArray inputWeights = getParamWithNoise(GravesLSTMParamInitializer.INPUT_WEIGHT_KEY, true, workspaceMgr); final INDArray recurrentWeights = getParamWithNoise(GravesLSTMParamInitializer.RECURRENT_WEIGHT_KEY, true, workspaceMgr); //Shape: [hiddenLayerSize,4*hiddenLayerSize+3]; order: [wI,wF,wO,wG,wFF,wOO,wGG] //First: Do forward pass to get gate activations, zs etc. FwdPassReturn fwdPass; if (truncatedBPTT) { fwdPass = activateHelper(true, stateMap.get(STATE_KEY_PREV_ACTIVATION), stateMap.get(STATE_KEY_PREV_MEMCELL), true, workspaceMgr); //Store last time step of output activations and memory cell state in tBpttStateMap tBpttStateMap.put(STATE_KEY_PREV_ACTIVATION, fwdPass.lastAct.detach()); tBpttStateMap.put(STATE_KEY_PREV_MEMCELL, fwdPass.lastMemCell.detach()); } else { fwdPass = activateHelper(true, null, null, true, workspaceMgr); } fwdPass.fwdPassOutput = permuteIfNWC(fwdPass.fwdPassOutput); Pair<Gradient, INDArray> p = LSTMHelpers.backpropGradientHelper(this, this.conf, this.layerConf().getGateActivationFn(), permuteIfNWC(this.input), recurrentWeights, inputWeights, permuteIfNWC(epsilon), truncatedBPTT, tbpttBackwardLength, fwdPass, true, GravesLSTMParamInitializer.INPUT_WEIGHT_KEY, GravesLSTMParamInitializer.RECURRENT_WEIGHT_KEY, GravesLSTMParamInitializer.BIAS_KEY, gradientViews, maskArray, true, null, workspaceMgr, layerConf().isHelperAllowFallback()); weightNoiseParams.clear(); p.setSecond(permuteIfNWC(backpropDropOutIfPresent(p.getSecond()))); return p; }
Example 4
Source File: TimeDistributedLayer.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public Pair<Gradient, INDArray> backpropGradient(INDArray epsilon, LayerWorkspaceMgr workspaceMgr) { INDArray reshapedEps = reshape(epsilon); Pair<Gradient, INDArray> p = underlying.backpropGradient(reshapedEps, workspaceMgr); INDArray reverted = revertReshape(p.getSecond(), epsilon.size(0)); reverted = workspaceMgr.dup(ArrayType.ACTIVATION_GRAD, reverted); p.setSecond(reverted); return p; }
Example 5
Source File: LayerVertex.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public Pair<Gradient, INDArray[]> doBackward(boolean tbptt, LayerWorkspaceMgr workspaceMgr) { if (!canDoBackward()) { if(inputs == null || inputs[0] == null){ throw new IllegalStateException("Cannot do backward pass: inputs not set. Layer: \"" + vertexName + "\" (idx " + vertexIndex + "), numInputs: " + getNumInputArrays()); } else { throw new IllegalStateException("Cannot do backward pass: all epsilons not set. Layer \"" + vertexName + "\" (idx " + vertexIndex + "), numInputs :" + getNumInputArrays() + "; numOutputs: " + getNumOutputConnections()); } } //Edge case: output layer - never did forward pass hence layer.setInput was never called... if(!setLayerInput){ applyPreprocessorAndSetInput(workspaceMgr); } Pair<Gradient, INDArray> pair; if (tbptt && layer instanceof RecurrentLayer) { //Truncated BPTT for recurrent layers pair = ((RecurrentLayer) layer).tbpttBackpropGradient(epsilon, graph.getConfiguration().getTbpttBackLength(), workspaceMgr); } else { //Normal backprop pair = layer.backpropGradient(epsilon, workspaceMgr); //epsTotal may be null for OutputLayers } if (layerPreProcessor != null) { INDArray eps = pair.getSecond(); eps = layerPreProcessor.backprop(eps, graph.batchSize(), workspaceMgr); pair.setSecond(eps); } //Layers always have single activations input -> always have single epsilon output during backprop return new Pair<>(pair.getFirst(), new INDArray[] {pair.getSecond()}); }
Example 6
Source File: FastText.java From deeplearning4j with Apache License 2.0 | 5 votes |
public Pair<String, Float> predictProbability(String text) { assertModelLoaded(); JFastText.ProbLabel predictedProbLabel = fastTextImpl.predictProba(text); Pair<String,Float> retVal = new Pair<>(); retVal.setFirst(predictedProbLabel.label); retVal.setSecond(predictedProbLabel.logProb); return retVal; }
Example 7
Source File: HyperRect.java From deeplearning4j with Apache License 2.0 | 5 votes |
public static Pair<float[],float[]> point(INDArray vector) { Pair<float[],float[]> ret = new Pair<>(); float[] curr = new float[(int)vector.length()]; for (int i = 0; i < vector.length(); i++) { curr[i] = vector.getFloat(i); } ret.setFirst(curr); ret.setSecond(curr); return ret; }
Example 8
Source File: MultiLayerNetwork.java From deeplearning4j with Apache License 2.0 | 4 votes |
private Pair<Gradient,INDArray> calculateGradientsHelper(INDArray features, INDArray label, INDArray fMask, INDArray labelMask){ setInput(features); setLabels(label); setLayerMaskArrays(fMask, labelMask); LayerWorkspaceMgr mgr; if(layerWiseConfigurations.getTrainingWorkspaceMode() == WorkspaceMode.NONE){ mgr = LayerWorkspaceMgr.noWorkspaces(); } else { mgr = LayerWorkspaceMgr.builder() .with(ArrayType.INPUT, WS_ALL_LAYERS_ACT, WS_ALL_LAYERS_ACT_CONFIG) .with(ArrayType.ACTIVATIONS, WS_ALL_LAYERS_ACT, WS_ALL_LAYERS_ACT_CONFIG) .with(ArrayType.FF_WORKING_MEM, WS_LAYER_WORKING_MEM, WS_LAYER_WORKING_MEM_CONFIG) .with(ArrayType.BP_WORKING_MEM, WS_LAYER_WORKING_MEM, WS_LAYER_WORKING_MEM_CONFIG) .with(ArrayType.RNN_FF_LOOP_WORKING_MEM, WS_RNN_LOOP_WORKING_MEM, WS_RNN_LOOP_WORKING_MEM_CONFIG) .with(ArrayType.RNN_BP_LOOP_WORKING_MEM, WS_RNN_LOOP_WORKING_MEM, WS_RNN_LOOP_WORKING_MEM_CONFIG) .build(); if(layerWiseConfigurations.getCacheMode() != null){ //For now: store cache mode activations in activations workspace mgr.setWorkspace(ArrayType.FF_CACHE, WS_ALL_LAYERS_ACT, WS_ALL_LAYERS_ACT_CONFIG); } } mgr.setHelperWorkspacePointers(helperWorkspaces); //Calculate activations (which are stored in each layer, and used in backprop) try(MemoryWorkspace ws = mgr.notifyScopeEntered(ArrayType.ACTIVATIONS)) { //First: do a feed-forward through the network //Note that we don't actually need to do the full forward pass through the output layer right now; but we do // need the input to the output layer to be set (such that backprop can be done) List<INDArray> activations = ffToLayerActivationsInWs(layers.length - 2, FwdPassType.STANDARD, false, input, mask, fMask); if (!trainingListeners.isEmpty()) { //TODO: We possibly do want output layer activations in some cases here... for (TrainingListener tl : trainingListeners) { tl.onForwardPass(this, activations); } } INDArray inputToOutputLayer = activations.get(activations.size() - 1); if (layerWiseConfigurations.getInputPreProcess(layers.length - 1) != null) { inputToOutputLayer = layerWiseConfigurations.getInputPreProcess(layers.length - 1) .preProcess(inputToOutputLayer, getInputMiniBatchSize(), mgr); //Validate activations location } getOutputLayer().setInput(inputToOutputLayer, mgr); Pair<Gradient,INDArray> p = calcBackpropGradients(null, true, false, true); if(p.getSecond() != null){ p.setSecond( p.getSecond().detach()); } return p; } }
Example 9
Source File: SameDiffLoss.java From deeplearning4j with Apache License 2.0 | 3 votes |
/** * Compute both the score (loss function value) and gradient. This is equivalent to calling {@link #computeScore(INDArray, INDArray, IActivation, INDArray, boolean)} * and {@link #computeGradient(INDArray, INDArray, IActivation, INDArray)} individually * * @param labels Label/expected output * @param preOutput Output of the model (neural network) * @param activationFn Activation function that should be applied to preOutput * @param mask Mask array; may be null * @param average Whether the score should be averaged (divided by number of rows in labels/output) or not * @return The score (loss function value) and gradient */ @Override public Pair<Double, INDArray> computeGradientAndScore(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask, boolean average) { Pair<Double, INDArray> GradientAndScore = new Pair<>(); GradientAndScore.setFirst(this.computeScore(labels, preOutput, activationFn, mask, average)); GradientAndScore.setSecond(this.computeGradient(labels, preOutput, activationFn, mask)); return GradientAndScore; }