当前位置: 首页>>代码示例>>Python>>正文


Python fields.ArrayField方法代码示例

本文整理汇总了Python中allennlp.data.fields.ArrayField方法的典型用法代码示例。如果您正苦于以下问题:Python fields.ArrayField方法的具体用法?Python fields.ArrayField怎么用?Python fields.ArrayField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在allennlp.data.fields的用法示例。


在下文中一共展示了fields.ArrayField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_padding_handles_list_fields

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
def test_padding_handles_list_fields(self):
        array1 = ArrayField(numpy.ones([2, 3]))
        array2 = ArrayField(numpy.ones([1, 5]))
        empty_array = array1.empty_field()
        list_field = ListField([array1, array2, empty_array])

        returned_tensor = (
            list_field.as_tensor(list_field.get_padding_lengths()).detach().cpu().numpy()
        )
        correct_tensor = numpy.array(
            [
                [[1.0, 1.0, 1.0, 0.0, 0.0], [1.0, 1.0, 1.0, 0.0, 0.0]],
                [[1.0, 1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0, 0.0]],
                [[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]],
            ]
        )
        numpy.testing.assert_array_equal(returned_tensor, correct_tensor) 
开发者ID:allenai,项目名称:allennlp,代码行数:19,代码来源:array_field_test.py

示例2: test_padding_handles_list_fields_with_padding_values

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
def test_padding_handles_list_fields_with_padding_values(self):
        array1 = ArrayField(numpy.ones([2, 3]), padding_value=-1)
        array2 = ArrayField(numpy.ones([1, 5]), padding_value=-1)
        empty_array = array1.empty_field()
        list_field = ListField([array1, array2, empty_array])

        returned_tensor = (
            list_field.as_tensor(list_field.get_padding_lengths()).detach().cpu().numpy()
        )
        correct_tensor = numpy.array(
            [
                [[1.0, 1.0, 1.0, -1.0, -1.0], [1.0, 1.0, 1.0, -1.0, -1.0]],
                [[1.0, 1.0, 1.0, 1.0, 1.0], [-1.0, -1.0, -1.0, -1.0, -1.0]],
                [[-1.0, -1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, -1.0, -1.0, -1.0]],
            ]
        )
        numpy.testing.assert_array_equal(returned_tensor, correct_tensor) 
开发者ID:allenai,项目名称:allennlp,代码行数:19,代码来源:array_field_test.py

示例3: test_alternative_dtypes

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
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 
开发者ID:allenai,项目名称:allennlp,代码行数:26,代码来源:array_field_test.py

示例4: text_to_instance

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
def text_to_instance(self, vec: str = None) -> Instance:  # type: ignore
        """
        Parameters
        ----------
        text : ``str``, required.
            The text to classify
        label ``str``, optional, (default = None).
            The label for this text.

        Returns
        -------
        An ``Instance`` containing the following fields:
            tokens : ``TextField``
                The tokens in the sentence or phrase.
            label : ``LabelField``
                The label label of the sentence or phrase.
        """
        # pylint: disable=arguments-differ
        fields: Dict[str, Field] = {}
        fields['tokens'] = ArrayField(vec)
        return Instance(fields) 
开发者ID:allenai,项目名称:vampire,代码行数:23,代码来源:vampire_reader.py

示例5: text_to_instance

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
def text_to_instance(self, state: np.ndarray, action: int = None) -> Instance:  # type: ignore
        """
        """
        # pylint: disable=arguments-differ
        fields: Dict[str, Field] = {}
        fields["states"] = ArrayField(state)
        if action is not None:
            fields["actions"] = LabelField(action, skip_indexing=True)
        return Instance(fields) 
开发者ID:ConvLab,项目名称:ConvLab,代码行数:11,代码来源:dataset_reader.py

示例6: test_get_padding_lengths_correctly_returns_ordered_shape

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
def test_get_padding_lengths_correctly_returns_ordered_shape(self):
        shape = [3, 4, 5, 6]
        array = numpy.zeros(shape)
        array_field = ArrayField(array)
        lengths = array_field.get_padding_lengths()
        for i in range(len(lengths)):
            assert lengths["dimension_{}".format(i)] == shape[i] 
开发者ID:allenai,项目名称:allennlp,代码行数:9,代码来源:array_field_test.py

示例7: test_as_tensor_handles_larger_padding_dimensions

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
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.0) 
开发者ID:allenai,项目名称:allennlp,代码行数:12,代码来源:array_field_test.py

示例8: test_printing_doesnt_crash

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
def test_printing_doesnt_crash(self):
        array = ArrayField(numpy.ones([2, 3]), padding_value=-1)
        print(array) 
开发者ID:allenai,项目名称:allennlp,代码行数:5,代码来源:array_field_test.py

示例9: test_as_tensor_with_scalar_keeps_dtype

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
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 
开发者ID:allenai,项目名称:allennlp,代码行数:6,代码来源:array_field_test.py

示例10: test_len_works_with_scalar

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
def test_len_works_with_scalar(self):
        array = ArrayField(numpy.asarray(42))
        assert len(array) == 1 
开发者ID:allenai,项目名称:allennlp,代码行数:5,代码来源:array_field_test.py

示例11: test_eq

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
def test_eq(self):
        array1 = ArrayField(numpy.asarray([1, 1, 1]))
        array2 = ArrayField(numpy.asarray([[1, 1, 1], [1, 1, 1]]))
        array3 = ArrayField(numpy.asarray([1, 1, 2]))
        array4 = ArrayField(numpy.asarray([1, 1, 1]))
        assert array1 != array2
        assert array1 != array3
        assert array1 == array4 
开发者ID:allenai,项目名称:allennlp,代码行数:10,代码来源:array_field_test.py

示例12: test_get_padding_lengths_correctly_returns_ordered_shape

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
def test_get_padding_lengths_correctly_returns_ordered_shape(self):
        shape = [3, 4, 5, 6]
        array = numpy.zeros(shape)
        array_field = ArrayField(array)
        lengths = array_field.get_padding_lengths()
        for i in range(len(lengths)):
            assert lengths[u"dimension_{}".format(i)] == shape[i] 
开发者ID:plasticityai,项目名称:magnitude,代码行数:9,代码来源:array_field_test.py

示例13: test_as_tensor_handles_larger_padding_dimensions

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
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({u"dimension_0": 5, u"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.) 
开发者ID:plasticityai,项目名称:magnitude,代码行数:10,代码来源:array_field_test.py

示例14: test_padding_handles_list_fields

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
def test_padding_handles_list_fields(self):
        array1 = ArrayField(numpy.ones([2, 3]))
        array2 = ArrayField(numpy.ones([1, 5]))
        empty_array = array1.empty_field()
        list_field = ListField([array1, array2, empty_array])

        returned_tensor = list_field.as_tensor(list_field.get_padding_lengths()).detach().cpu().numpy()
        correct_tensor = numpy.array([[[1., 1., 1., 0., 0.],
                                       [1., 1., 1., 0., 0.]],
                                      [[1., 1., 1., 1., 1.],
                                       [0., 0., 0., 0., 0.]],
                                      [[0., 0., 0., 0., 0.],
                                       [0., 0., 0., 0., 0.]]])
        numpy.testing.assert_array_equal(returned_tensor, correct_tensor) 
开发者ID:plasticityai,项目名称:magnitude,代码行数:16,代码来源:array_field_test.py

示例15: test_padding_handles_list_fields_with_padding_values

# 需要导入模块: from allennlp.data import fields [as 别名]
# 或者: from allennlp.data.fields import ArrayField [as 别名]
def test_padding_handles_list_fields_with_padding_values(self):
        array1 = ArrayField(numpy.ones([2, 3]), padding_value=-1)
        array2 = ArrayField(numpy.ones([1, 5]), padding_value=-1)
        empty_array = array1.empty_field()
        list_field = ListField([array1, array2, empty_array])

        returned_tensor = list_field.as_tensor(list_field.get_padding_lengths()).detach().cpu().numpy()
        correct_tensor = numpy.array([[[1., 1., 1., -1., -1.],
                                       [1., 1., 1., -1., -1.]],
                                      [[1., 1., 1., 1., 1.],
                                       [-1., -1., -1., -1., -1.]],
                                      [[-1., -1., -1., -1., -1.],
                                       [-1., -1., -1., -1., -1.]]])
        numpy.testing.assert_array_equal(returned_tensor, correct_tensor) 
开发者ID:plasticityai,项目名称:magnitude,代码行数:16,代码来源:array_field_test.py


注:本文中的allennlp.data.fields.ArrayField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。