Browse Source

add testing code in stack embedding

tags/v0.4.10
xuyige 5 years ago
parent
commit
65f92855ba
1 changed files with 20 additions and 0 deletions
  1. +20
    -0
      test/embeddings/test_stack_embeddings.py

+ 20
- 0
test/embeddings/test_stack_embeddings.py View File

@@ -0,0 +1,20 @@
import unittest

import torch

from fastNLP import Vocabulary, DataSet, Instance
from fastNLP.embeddings import LSTMCharEmbedding, CNNCharEmbedding, StackEmbedding


class TestCharEmbed(unittest.TestCase):
def test_case_1(self):
ds = DataSet([Instance(words=['hello', 'world']), Instance(words=['hello', 'Jack'])])
vocab = Vocabulary().from_dataset(ds, field_name='words')
self.assertEqual(len(vocab), 5)
cnn_embed = CNNCharEmbedding(vocab, embed_size=60)
lstm_embed = LSTMCharEmbedding(vocab, embed_size=70)
embed = StackEmbedding([cnn_embed, lstm_embed])
x = torch.LongTensor([[2, 1, 0], [4, 3, 4]])
y = embed(x)
self.assertEqual(tuple(y.size()), (2, 3, 130))


Loading…
Cancel
Save