コード例 #1
0
    def test_alternative_dtypes(self):
        shape = [3, 4, 5, 6]
        array = numpy.zeros(shape)

        # Setting dtype to numpy.int64 should produce a torch.LongTensor when field is converted to
        # a tensor
        array_field1 = ArrayField(array, dtype=numpy.int64)
        returned_tensor1 = array_field1.as_tensor(
            array_field1.get_padding_lengths())
        assert returned_tensor1.dtype == torch.int64

        # Setting dtype to numpy.uint8 should produce a torch.ByteTensor when field is converted to
        # a tensor
        array_field2 = ArrayField(array, dtype=numpy.uint8)
        returned_tensor2 = array_field2.as_tensor(
            array_field2.get_padding_lengths())
        assert returned_tensor2.dtype == torch.uint8

        # Padding should not affect dtype
        padding_lengths = {
            "dimension_" + str(i): 10
            for i, _ in enumerate(shape)
        }
        padded_tensor = array_field2.as_tensor(padding_lengths)
        assert padded_tensor.dtype == torch.uint8

        # Empty fields should have the same dtype
        empty_field = array_field2.empty_field()
        assert empty_field.dtype == array_field2.dtype
コード例 #2
0
ファイル: array_field_test.py プロジェクト: pyknife/allennlp
    def test_as_tensor_handles_larger_padding_dimensions(self):
        shape = [3, 4]
        array = numpy.ones(shape)
        array_field = ArrayField(array)

        padded_tensor = array_field.as_tensor({"dimension_0": 5, "dimension_1": 6}).detach().cpu().numpy()
        numpy.testing.assert_array_equal(padded_tensor[:3, :4], array)
        numpy.testing.assert_array_equal(padded_tensor[3:, 4:], 0.)
コード例 #3
0
    def test_as_tensor_handles_larger_padding_dimensions(self):
        shape = [3, 4]
        array = numpy.ones(shape)
        array_field = ArrayField(array)

        padded_tensor = array_field.as_tensor({"dimension_0": 5, "dimension_1": 6}).detach().cpu().numpy()
        numpy.testing.assert_array_equal(padded_tensor[:3, :4], array)
        numpy.testing.assert_array_equal(padded_tensor[3:, 4:], 0.)
コード例 #4
0
 def test_as_tensor_works_with_scalar(self):
     array = ArrayField(numpy.asarray(42))
     returned_tensor = array.as_tensor(array.get_padding_lengths())
     current_tensor = numpy.asarray(42)
     numpy.testing.assert_array_equal(returned_tensor, current_tensor)
コード例 #5
0
 def test_as_tensor_with_scalar_keeps_dtype(self):
     array = ArrayField(numpy.asarray(42, dtype=numpy.float32))
     returned_tensor = array.as_tensor(array.get_padding_lengths())
     assert returned_tensor.dtype == torch.float32
コード例 #6
0
ファイル: array_field_test.py プロジェクト: apmoore1/allennlp
 def test_as_tensor_works_with_scalar(self):
     array = ArrayField(numpy.asarray(42))
     returned_tensor = array.as_tensor(array.get_padding_lengths())
     current_tensor = numpy.asarray(42)
     numpy.testing.assert_array_equal(returned_tensor, current_tensor)