<commit_before># coding=utf-8
# Copyright 2020 The Trax Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Lint as: python3
"""Tests for RNNs."""

from absl.testing import absltest
from absl.testing import parameterized
import numpy as np

from trax import fastmath
from trax import shapes
from trax.models import rnn


@parameterized.named_parameters(
    ('_' + b.value, b) for b in (fastmath.Backend.JAX, fastmath.Backend.TFNP))
class RNNTest(parameterized.TestCase):

  def test_rnnlm_forward_shape(self, backend):
    with fastmath.use_backend(backend):
      model = rnn.RNNLM(vocab_size=20, d_model=16)
      x = np.ones((3, 28)).astype(np.int32)
      _, _ = model.init(shapes.signature(x))
      y = model(x)
      self.assertEqual(y.shape, (3, 28, 20))

  def test_grulm_forward_shape(self, backend):
    with fastmath.use_backend(backend):
      model = rnn.GRULM(vocab_size=20, d_model=16)
      x = np.ones((3, 28)).astype(np.int32)
      _, _ = model.init(shapes.signature(x))
      y = model(x)
      self.assertEqual(y.shape, (3, 28, 20))


if __name__ == '__main__':
  absltest.main()<commit_msg>Added unit test for LSTMSeq2SeqAttn in the RNN module<commit_after># coding=utf-8
# Copyright 2020 The Trax Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Lint as: python3
"""Tests for RNNs."""

from absl.testing import absltest
from absl.testing import parameterized
import numpy as np

from trax import fastmath
from trax import shapes
from trax.models import rnn


@parameterized.named_parameters(
    ('_' + b.value, b) for b in (fastmath.Backend.JAX, fastmath.Backend.TFNP))
class RNNTest(parameterized.TestCase):

  def test_rnnlm_forward_shape(self, backend):
    with fastmath.use_backend(backend):
      model = rnn.RNNLM(vocab_size=20, d_model=16)
      x = np.ones((3, 28)).astype(np.int32)
      _, _ = model.init(shapes.signature(x))
      y = model(x)
      self.assertEqual(y.shape, (3, 28, 20))

  def test_grulm_forward_shape(self, backend):
    with fastmath.use_backend(backend):
      model = rnn.GRULM(vocab_size=20, d_model=16)
      x = np.ones((3, 28)).astype(np.int32)
      _, _ = model.init(shapes.signature(x))
      y = model(x)
      self.assertEqual(y.shape, (3, 28, 20))

  def test_lstmseq2seqattn_forward_shape(self, backend):
    with fastmath.use_backend(backend):
      model = rnn.LSTMSeq2SeqAttn(
        input_vocab_size=20,
        target_vocab_size=20,
        d_model=16
      )
      x = np.ones((3, 28)).astype(np.int32)
      _, _ = model.init([shapes.signature(x), shapes.signature(x)])
      ys = model([x, x])
      self.assertEqual([y.shape for y in ys], [(3, 28, 20), (3, 28)])


if __name__ == '__main__':
  absltest.main()
{'fraction_non_alphanumeric': 0.11359956826767405, 'fraction_numerical': 0.02644360496492175, 'mean_word_length': 3.770913770913771, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7624683', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1228, 'n_words': 365}