forked from keras-team/keras-nlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_word_deletion_test.py
87 lines (69 loc) · 2.97 KB
/
random_word_deletion_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright 2022 The KerasNLP 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
#
# https://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.
"""Tests for Random Word Deletion Layer."""
import tensorflow as tf
from keras_nlp.layers import random_word_deletion
class RandomDeletionTest(tf.test.TestCase):
def test_shape_with_scalar(self):
augmenter = random_word_deletion.RandomWordDeletion(
probability=0.5, max_deletions=3
)
input = ["Running Around"]
output = augmenter(input)
self.assertAllEqual(output.shape, tf.convert_to_tensor(input).shape)
def test_get_config_and_from_config(self):
augmenter = random_word_deletion.RandomWordDeletion(
probability=0.5, max_deletions=3
)
expected_config_subset = {"probability": 0.5, "max_deletions": 3}
config = augmenter.get_config()
self.assertEqual(config, {**config, **expected_config_subset})
restored_augmenter = (
random_word_deletion.RandomWordDeletion.from_config(
config,
)
)
self.assertEqual(
restored_augmenter.get_config(),
{**config, **expected_config_subset},
)
def test_augment_first_batch_second(self):
tf.random.get_global_generator().reset_from_seed(30)
tf.random.set_seed(30)
augmenter = random_word_deletion.RandomWordDeletion(
probability=0.5, max_deletions=3
)
ds = tf.data.Dataset.from_tensor_slices(
["samurai or ninja", "keras is good", "tensorflow is a library"]
)
ds = ds.map(augmenter)
ds = ds.apply(tf.data.experimental.dense_to_ragged_batch(3))
output = ds.take(1).get_single_element()
exp_output = [b"samurai", b"is good", b"tensorflow a library"]
for i in range(output.shape[0]):
self.assertAllEqual(output[i], exp_output[i])
def test_batch_first_augment_second(self):
tf.random.get_global_generator().reset_from_seed(30)
tf.random.set_seed(30)
augmenter = random_word_deletion.RandomWordDeletion(
probability=0.5, max_deletions=3
)
ds = tf.data.Dataset.from_tensor_slices(
["samurai or ninja", "keras is good", "tensorflow is a library"]
)
ds = ds.batch(3).map(augmenter)
output = ds.take(1).get_single_element()
exp_output = [b"samurai", b"is good", b"tensorflow"]
for i in range(output.shape[0]):
self.assertAllEqual(output[i], exp_output[i])