Search in sources :

Example 1 with Random

use of org.nd4j.linalg.api.rng.Random in project deeplearning4j by deeplearning4j.

the class DBOW method inferSequence.

/**
     * This method does training on previously unseen paragraph, and returns inferred vector
     *
     * @param sequence
     * @param nextRandom
     * @param learningRate
     * @return
     */
@Override
public INDArray inferSequence(Sequence<T> sequence, long nextRandom, double learningRate, double minLearningRate, int iterations) {
    AtomicLong nr = new AtomicLong(nextRandom);
    if (sequence.isEmpty())
        return null;
    Random random = Nd4j.getRandomFactory().getNewRandomInstance(configuration.getSeed() * sequence.hashCode(), lookupTable.layerSize() + 1);
    INDArray ret = Nd4j.rand(new int[] { 1, lookupTable.layerSize() }, random).subi(0.5).divi(lookupTable.layerSize());
    for (int iter = 0; iter < iterations; iter++) {
        nr.set(Math.abs(nr.get() * 25214903917L + 11));
        dbow(0, sequence, (int) nr.get() % window, nr, learningRate, true, ret);
        learningRate = ((learningRate - minLearningRate) / (iterations - iter)) + minLearningRate;
    }
    finish();
    return ret;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Random(org.nd4j.linalg.api.rng.Random) INDArray(org.nd4j.linalg.api.ndarray.INDArray)

Example 2 with Random

use of org.nd4j.linalg.api.rng.Random in project deeplearning4j by deeplearning4j.

the class SequenceVectors method fit.

/**
     * Starts training over
     */
public void fit() {
    Properties props = Nd4j.getExecutioner().getEnvironmentInformation();
    if (props.getProperty("backend").equals("CUDA")) {
        if (Nd4j.getAffinityManager().getNumberOfDevices() > 1)
            throw new IllegalStateException("Multi-GPU word2vec/doc2vec isn't available atm");
    //if (!NativeOpsHolder.getInstance().getDeviceNativeOps().isP2PAvailable())
    //throw new IllegalStateException("Running Word2Vec on multi-gpu system requires P2P support between GPUs, which looks to be unavailable on your system.");
    }
    Nd4j.getRandom().setSeed(configuration.getSeed());
    AtomicLong timeSpent = new AtomicLong(0);
    if (!trainElementsVectors && !trainSequenceVectors)
        throw new IllegalStateException("You should define at least one training goal 'trainElementsRepresentation' or 'trainSequenceRepresentation'");
    if (iterator == null)
        throw new IllegalStateException("You can't fit() data without SequenceIterator defined");
    if (resetModel || (lookupTable != null && vocab != null && vocab.numWords() == 0)) {
        // build vocabulary from scratches
        buildVocab();
    }
    WordVectorSerializer.printOutProjectedMemoryUse(vocab.numWords(), configuration.getLayersSize(), configuration.isUseHierarchicSoftmax() && configuration.getNegative() > 0 ? 3 : 2);
    if (vocab == null || lookupTable == null || vocab.numWords() == 0)
        throw new IllegalStateException("You can't fit() model with empty Vocabulary or WeightLookupTable");
    // if model vocab and lookupTable is built externally we basically should check that lookupTable was properly initialized
    if (!resetModel || existingModel != null) {
        lookupTable.resetWeights(false);
    } else {
        // otherwise we reset weights, independent of actual current state of lookup table
        lookupTable.resetWeights(true);
        // if preciseWeights used, we roll over data once again
        if (configuration.isPreciseWeightInit()) {
            log.info("Using precise weights init...");
            iterator.reset();
            while (iterator.hasMoreSequences()) {
                Sequence<T> sequence = iterator.nextSequence();
                // initializing elements, only once
                for (T element : sequence.getElements()) {
                    T realElement = vocab.tokenFor(element.getLabel());
                    if (realElement != null && !realElement.isInit()) {
                        Random rng = Nd4j.getRandomFactory().getNewRandomInstance(configuration.getSeed() * realElement.hashCode(), configuration.getLayersSize() + 1);
                        INDArray randArray = Nd4j.rand(new int[] { 1, configuration.getLayersSize() }, rng).subi(0.5).divi(configuration.getLayersSize());
                        lookupTable.getWeights().getRow(realElement.getIndex()).assign(randArray);
                        realElement.setInit(true);
                    }
                }
                // initializing labels, only once
                for (T label : sequence.getSequenceLabels()) {
                    T realElement = vocab.tokenFor(label.getLabel());
                    if (realElement != null && !realElement.isInit()) {
                        Random rng = Nd4j.getRandomFactory().getNewRandomInstance(configuration.getSeed() * realElement.hashCode(), configuration.getLayersSize() + 1);
                        INDArray randArray = Nd4j.rand(new int[] { 1, configuration.getLayersSize() }, rng).subi(0.5).divi(configuration.getLayersSize());
                        lookupTable.getWeights().getRow(realElement.getIndex()).assign(randArray);
                        realElement.setInit(true);
                    }
                }
            }
            this.iterator.reset();
        }
    }
    initLearners();
    log.info("Starting learning process...");
    timeSpent.set(System.currentTimeMillis());
    if (this.stopWords == null)
        this.stopWords = new ArrayList<>();
    for (int currentEpoch = 1; currentEpoch <= numEpochs; currentEpoch++) {
        final AtomicLong linesCounter = new AtomicLong(0);
        final AtomicLong wordsCounter = new AtomicLong(0);
        AsyncSequencer sequencer = new AsyncSequencer(this.iterator, this.stopWords);
        sequencer.start();
        //final VectorCalculationsThread[] threads = new VectorCalculationsThread[workers];
        final AtomicLong timer = new AtomicLong(System.currentTimeMillis());
        final List<VectorCalculationsThread> threads = new ArrayList<>();
        for (int x = 0; x < workers; x++) {
            threads.add(x, new VectorCalculationsThread(x, currentEpoch, wordsCounter, vocab.totalWordOccurrences(), linesCounter, sequencer, timer));
            threads.get(x).start();
        }
        try {
            sequencer.join();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        for (int x = 0; x < workers; x++) {
            try {
                threads.get(x).join();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        // TODO: fix this to non-exclusive termination
        if (trainElementsVectors && elementsLearningAlgorithm != null && (!trainSequenceVectors || sequenceLearningAlgorithm == null) && elementsLearningAlgorithm.isEarlyTerminationHit()) {
            break;
        }
        if (trainSequenceVectors && sequenceLearningAlgorithm != null && (!trainElementsVectors || elementsLearningAlgorithm == null) && sequenceLearningAlgorithm.isEarlyTerminationHit()) {
            break;
        }
        log.info("Epoch: [" + currentEpoch + "]; Words vectorized so far: [" + wordsCounter.get() + "];  Lines vectorized so far: [" + linesCounter.get() + "]; learningRate: [" + minLearningRate + "]");
        if (eventListeners != null && !eventListeners.isEmpty()) {
            for (VectorsListener listener : eventListeners) {
                if (listener.validateEvent(ListenerEvent.EPOCH, currentEpoch))
                    listener.processEvent(ListenerEvent.EPOCH, this, currentEpoch);
            }
        }
    }
    log.info("Time spent on training: {} ms", System.currentTimeMillis() - timeSpent.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Random(org.nd4j.linalg.api.rng.Random) INDArray(org.nd4j.linalg.api.ndarray.INDArray) VectorsListener(org.deeplearning4j.models.sequencevectors.interfaces.VectorsListener)

Example 3 with Random

use of org.nd4j.linalg.api.rng.Random in project deeplearning4j by deeplearning4j.

the class TestOptimizers method testSphereFnOptHelper.

public void testSphereFnOptHelper(OptimizationAlgorithm oa, int numLineSearchIter, int nDimensions) {
    if (PRINT_OPT_RESULTS)
        System.out.println("---------\n Alg= " + oa + ", nIter= " + numLineSearchIter + ", nDimensions= " + nDimensions);
    NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().maxNumLineSearchIterations(numLineSearchIter).iterations(100).learningRate(1e-2).layer(new RBM.Builder().nIn(1).nOut(1).updater(Updater.SGD).build()).build();
    //Normally done by ParamInitializers, but obviously that isn't done here
    conf.addVariable("W");
    Random rng = new DefaultRandom(12345L);
    org.nd4j.linalg.api.rng.distribution.Distribution dist = new org.nd4j.linalg.api.rng.distribution.impl.UniformDistribution(rng, -10, 10);
    Model m = new SphereFunctionModel(nDimensions, dist, conf);
    m.computeGradientAndScore();
    double scoreBefore = m.score();
    assertTrue(!Double.isNaN(scoreBefore) && !Double.isInfinite(scoreBefore));
    if (PRINT_OPT_RESULTS) {
        System.out.println("Before:");
        System.out.println(scoreBefore);
        System.out.println(m.params());
    }
    ConvexOptimizer opt = getOptimizer(oa, conf, m);
    opt.setupSearchState(m.gradientAndScore());
    opt.optimize();
    m.computeGradientAndScore();
    double scoreAfter = m.score();
    assertTrue(!Double.isNaN(scoreAfter) && !Double.isInfinite(scoreAfter));
    if (PRINT_OPT_RESULTS) {
        System.out.println("After:");
        System.out.println(scoreAfter);
        System.out.println(m.params());
    }
    //Expected behaviour after optimization:
    //(a) score is better (lower) after optimization.
    //(b) Parameters are closer to minimum after optimization (TODO)
    assertTrue("Score did not improve after optimization (b= " + scoreBefore + " ,a= " + scoreAfter + ")", scoreAfter < scoreBefore);
}
Also used : DefaultRandom(org.nd4j.linalg.api.rng.DefaultRandom) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ConvexOptimizer(org.deeplearning4j.optimize.api.ConvexOptimizer) Random(org.nd4j.linalg.api.rng.Random) DefaultRandom(org.nd4j.linalg.api.rng.DefaultRandom) Model(org.deeplearning4j.nn.api.Model)

Example 4 with Random

use of org.nd4j.linalg.api.rng.Random in project nd4j by deeplearning4j.

the class RandomFactory method getRandom.

/**
 * This method returns Random implementation instance associated with calling thread
 *
 * @return object implementing Random interface
 */
public Random getRandom() {
    try {
        if (threadRandom.get() == null) {
            Random t = (Random) randomClass.newInstance();
            if (t.getStatePointer() != null) {
            // TODO: attach this thing to deallocator
            // if it's stateless random - we just don't care then
            }
            threadRandom.set(t);
            return t;
        }
        return threadRandom.get();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Random(org.nd4j.linalg.api.rng.Random)

Example 5 with Random

use of org.nd4j.linalg.api.rng.Random in project nd4j by deeplearning4j.

the class RandomTests method testLogNormal1.

@Test
public void testLogNormal1() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    INDArray z01 = Nd4j.create(1000000);
    JDKRandomGenerator rng = new JDKRandomGenerator();
    rng.setSeed(119);
    org.apache.commons.math3.distribution.LogNormalDistribution dst = new org.apache.commons.math3.distribution.LogNormalDistribution(rng, 0.0, 1.0);
    double[] array = dst.sample(1000000);
    double mean = 0.0;
    for (double e : array) {
        mean += e;
    }
    mean /= array.length;
    LogNormalDistribution distribution01 = new LogNormalDistribution(z01, 0.0, 1.0);
    Nd4j.getExecutioner().exec(distribution01, random1);
    log.info("Java mean: {}; Native mean: {}", mean, z01.meanNumber().doubleValue());
    assertEquals(mean, z01.meanNumber().doubleValue(), 1e-1);
}
Also used : Random(org.nd4j.linalg.api.rng.Random) DefaultRandom(org.nd4j.linalg.api.rng.DefaultRandom) NativeRandom(org.nd4j.rng.NativeRandom) INDArray(org.nd4j.linalg.api.ndarray.INDArray) JDKRandomGenerator(org.apache.commons.math3.random.JDKRandomGenerator) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest) Test(org.junit.Test)

Aggregations

Random (org.nd4j.linalg.api.rng.Random)38 Test (org.junit.Test)31 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)31 DefaultRandom (org.nd4j.linalg.api.rng.DefaultRandom)31 NativeRandom (org.nd4j.rng.NativeRandom)29 INDArray (org.nd4j.linalg.api.ndarray.INDArray)24 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Model (org.deeplearning4j.nn.api.Model)2 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)2 ConvexOptimizer (org.deeplearning4j.optimize.api.ConvexOptimizer)2 JDKRandomGenerator (org.apache.commons.math3.random.JDKRandomGenerator)1 VectorsListener (org.deeplearning4j.models.sequencevectors.interfaces.VectorsListener)1 Ignore (org.junit.Ignore)1 MatchCondition (org.nd4j.linalg.api.ops.impl.accum.MatchCondition)1 Distribution (org.nd4j.linalg.api.rng.distribution.Distribution)1 NormalDistribution (org.nd4j.linalg.api.rng.distribution.impl.NormalDistribution)1 OrthogonalDistribution (org.nd4j.linalg.api.rng.distribution.impl.OrthogonalDistribution)1