Search in sources :

Example 1 with SequenceIterator

use of org.deeplearning4j.models.sequencevectors.interfaces.SequenceIterator in project deeplearning4j by deeplearning4j.

the class VocabConstructorTest method testCounter1.

@Test
public void testCounter1() throws Exception {
    VocabCache<VocabWord> vocabCache = new AbstractCache.Builder<VocabWord>().build();
    final List<VocabWord> words = new ArrayList<>();
    words.add(new VocabWord(1, "word"));
    words.add(new VocabWord(2, "test"));
    words.add(new VocabWord(1, "here"));
    Iterable<Sequence<VocabWord>> iterable = new Iterable<Sequence<VocabWord>>() {

        @Override
        public Iterator<Sequence<VocabWord>> iterator() {
            return new Iterator<Sequence<VocabWord>>() {

                private AtomicBoolean switcher = new AtomicBoolean(true);

                @Override
                public boolean hasNext() {
                    return switcher.getAndSet(false);
                }

                @Override
                public Sequence<VocabWord> next() {
                    Sequence<VocabWord> sequence = new Sequence<>(words);
                    return sequence;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
    SequenceIterator<VocabWord> sequenceIterator = new AbstractSequenceIterator.Builder<>(iterable).build();
    VocabConstructor<VocabWord> constructor = new VocabConstructor.Builder<VocabWord>().addSource(sequenceIterator, 0).useAdaGrad(false).setTargetVocabCache(vocabCache).build();
    constructor.buildJointVocabulary(false, true);
    assertEquals(3, vocabCache.numWords());
    assertEquals(1, vocabCache.wordFrequency("test"));
}
Also used : VocabWord(org.deeplearning4j.models.word2vec.VocabWord) Sequence(org.deeplearning4j.models.sequencevectors.sequence.Sequence) AbstractCache(org.deeplearning4j.models.word2vec.wordstore.inmemory.AbstractCache) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractSequenceIterator(org.deeplearning4j.models.sequencevectors.iterators.AbstractSequenceIterator) FileLabelAwareIterator(org.deeplearning4j.text.documentiterator.FileLabelAwareIterator) SentenceIterator(org.deeplearning4j.text.sentenceiterator.SentenceIterator) AbstractSequenceIterator(org.deeplearning4j.models.sequencevectors.iterators.AbstractSequenceIterator) BasicLineIterator(org.deeplearning4j.text.sentenceiterator.BasicLineIterator) SequenceIterator(org.deeplearning4j.models.sequencevectors.interfaces.SequenceIterator) Test(org.junit.Test)

Example 2 with SequenceIterator

use of org.deeplearning4j.models.sequencevectors.interfaces.SequenceIterator in project deeplearning4j by deeplearning4j.

the class VocabConstructorTest method testCounter2.

@Test
public void testCounter2() throws Exception {
    VocabCache<VocabWord> vocabCache = new AbstractCache.Builder<VocabWord>().build();
    final List<VocabWord> words = new ArrayList<>();
    words.add(new VocabWord(1, "word"));
    words.add(new VocabWord(0, "test"));
    words.add(new VocabWord(1, "here"));
    Iterable<Sequence<VocabWord>> iterable = new Iterable<Sequence<VocabWord>>() {

        @Override
        public Iterator<Sequence<VocabWord>> iterator() {
            return new Iterator<Sequence<VocabWord>>() {

                private AtomicBoolean switcher = new AtomicBoolean(true);

                @Override
                public boolean hasNext() {
                    return switcher.getAndSet(false);
                }

                @Override
                public Sequence<VocabWord> next() {
                    Sequence<VocabWord> sequence = new Sequence<>(words);
                    return sequence;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
    SequenceIterator<VocabWord> sequenceIterator = new AbstractSequenceIterator.Builder<>(iterable).build();
    VocabConstructor<VocabWord> constructor = new VocabConstructor.Builder<VocabWord>().addSource(sequenceIterator, 0).useAdaGrad(false).setTargetVocabCache(vocabCache).build();
    constructor.buildJointVocabulary(false, true);
    assertEquals(3, vocabCache.numWords());
    assertEquals(1, vocabCache.wordFrequency("test"));
}
Also used : VocabWord(org.deeplearning4j.models.word2vec.VocabWord) Sequence(org.deeplearning4j.models.sequencevectors.sequence.Sequence) AbstractCache(org.deeplearning4j.models.word2vec.wordstore.inmemory.AbstractCache) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractSequenceIterator(org.deeplearning4j.models.sequencevectors.iterators.AbstractSequenceIterator) FileLabelAwareIterator(org.deeplearning4j.text.documentiterator.FileLabelAwareIterator) SentenceIterator(org.deeplearning4j.text.sentenceiterator.SentenceIterator) AbstractSequenceIterator(org.deeplearning4j.models.sequencevectors.iterators.AbstractSequenceIterator) BasicLineIterator(org.deeplearning4j.text.sentenceiterator.BasicLineIterator) SequenceIterator(org.deeplearning4j.models.sequencevectors.interfaces.SequenceIterator) Test(org.junit.Test)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 SequenceIterator (org.deeplearning4j.models.sequencevectors.interfaces.SequenceIterator)2 AbstractSequenceIterator (org.deeplearning4j.models.sequencevectors.iterators.AbstractSequenceIterator)2 Sequence (org.deeplearning4j.models.sequencevectors.sequence.Sequence)2 VocabWord (org.deeplearning4j.models.word2vec.VocabWord)2 AbstractCache (org.deeplearning4j.models.word2vec.wordstore.inmemory.AbstractCache)2 FileLabelAwareIterator (org.deeplearning4j.text.documentiterator.FileLabelAwareIterator)2 BasicLineIterator (org.deeplearning4j.text.sentenceiterator.BasicLineIterator)2 SentenceIterator (org.deeplearning4j.text.sentenceiterator.SentenceIterator)2 Test (org.junit.Test)2