Spaces:
Runtime error
Runtime error
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. | |
# | |
# 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. | |
# encoding=utf-8 | |
"""Tests for sentence prediction labels.""" | |
import functools | |
from absl.testing import parameterized | |
import tensorflow as tf, tf_keras | |
from official.nlp.modeling.ops import segment_extractor | |
class NextSentencePredictionTest(parameterized.TestCase, tf.test.TestCase): | |
def testNextSentencePrediction(self, | |
sentences, | |
expected_segment, | |
expected_labels, | |
random_threshold=0.5, | |
test_description=""): | |
sentences = tf.ragged.constant(sentences) | |
# Set seed and rig the shuffle function to a deterministic reverse function | |
# instead. This is so that we have consistent and deterministic results. | |
extracted_segment, actual_labels = ( | |
segment_extractor.get_next_sentence_labels( | |
sentences, | |
random_threshold, | |
random_fn=functools.partial( | |
tf.random.stateless_uniform, seed=(2, 3)))) | |
self.assertAllEqual(expected_segment, extracted_segment) | |
self.assertAllEqual(expected_labels, actual_labels) | |
class SentenceOrderLabelsTest(parameterized.TestCase, tf.test.TestCase): | |
def testSentenceOrderPrediction(self, | |
sentences, | |
expected_segment, | |
expected_labels, | |
random_threshold=0.5, | |
random_next_threshold=0.5, | |
test_description=""): | |
sentences = tf.ragged.constant(sentences) | |
# Set seed and rig the shuffle function to a deterministic reverse function | |
# instead. This is so that we have consistent and deterministic results. | |
extracted_segment, actual_labels = ( | |
segment_extractor.get_sentence_order_labels( | |
sentences, | |
random_threshold=random_threshold, | |
random_next_threshold=random_next_threshold, | |
random_fn=functools.partial( | |
tf.random.stateless_uniform, seed=(2, 3)))) | |
self.assertAllEqual(expected_segment, extracted_segment) | |
self.assertAllEqual(expected_labels, actual_labels) | |
if __name__ == "__main__": | |
tf.test.main() | |