本文整理汇总了Python中allennlp.nn.InitializerApplicator.from_params方法的典型用法代码示例。如果您正苦于以下问题:Python InitializerApplicator.from_params方法的具体用法?Python InitializerApplicator.from_params怎么用?Python InitializerApplicator.from_params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类allennlp.nn.InitializerApplicator
的用法示例。
在下文中一共展示了InitializerApplicator.from_params方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_params
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def from_params(cls, vocab: Vocabulary, params: Params) -> 'CrfTagger':
embedder_params = params.pop("text_field_embedder")
text_field_embedder = TextFieldEmbedder.from_params(vocab, embedder_params)
encoder = Seq2SeqEncoder.from_params(params.pop("encoder"))
label_namespace = params.pop("label_namespace", "labels")
constraint_type = params.pop("constraint_type", None)
dropout = params.pop("dropout", None)
include_start_end_transitions = params.pop("include_start_end_transitions", True)
initializer = InitializerApplicator.from_params(params.pop('initializer', []))
regularizer = RegularizerApplicator.from_params(params.pop('regularizer', []))
params.assert_empty(cls.__name__)
return cls(vocab=vocab,
text_field_embedder=text_field_embedder,
encoder=encoder,
label_namespace=label_namespace,
constraint_type=constraint_type,
dropout=dropout,
include_start_end_transitions=include_start_end_transitions,
initializer=initializer,
regularizer=regularizer)
示例2: test_union_of_castable_types
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_union_of_castable_types(self):
class IntFloat(FromParams):
def __init__(self, a: Union[int, float]) -> None:
self.a = a
class FloatInt(FromParams):
def __init__(self, a: Union[float, int]) -> None:
self.a = a
float_param_str = '{"a": 1.0}'
int_param_str = '{"a": 1}'
import json
for expected_type, param_str in [(int, int_param_str), (float, float_param_str)]:
for cls in [IntFloat, FloatInt]:
c = cls.from_params(Params(json.loads(param_str)))
assert type(c.a) == expected_type
示例3: test_transferring_of_modules_ensures_type_consistency
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_transferring_of_modules_ensures_type_consistency(self):
model_archive = str(
self.FIXTURES_ROOT / "basic_classifier" / "serialization" / "model.tar.gz"
)
trained_model = load_archive(model_archive).model
config_file = str(self.FIXTURES_ROOT / "basic_classifier" / "experiment_seq2seq.jsonnet")
model_params = Params.from_file(config_file).pop("model").as_dict(quiet=True)
# Override only text_field_embedder and make it load Seq2SeqEncoder
model_params["text_field_embedder"] = {
"_pretrained": {
"archive_file": model_archive,
"module_path": "_seq2seq_encoder._module",
}
}
with pytest.raises(ConfigurationError):
Model.from_params(vocab=trained_model.vocab, params=Params(model_params))
示例4: test_lazy_construction_can_happen_multiple_times
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_lazy_construction_can_happen_multiple_times(self):
test_string = "this is a test"
extra_string = "extra string"
class ConstructedObject(FromParams):
def __init__(self, string: str, extra: str):
self.string = string
self.extra = extra
class Testing(FromParams):
def __init__(self, lazy_object: Lazy[ConstructedObject]):
first_time = lazy_object.construct(extra=extra_string)
second_time = lazy_object.construct(extra=extra_string)
assert first_time.string == test_string
assert first_time.extra == extra_string
assert second_time.string == test_string
assert second_time.extra == extra_string
Testing.from_params(Params({"lazy_object": {"string": test_string}}))
示例5: test_from_params_string
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_from_params_string(self):
Initializer.from_params(params="eye")
示例6: test_from_params_none
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_from_params_none(self):
Initializer.from_params(params=None)
示例7: test_regex_matches_are_initialized_correctly
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_regex_matches_are_initialized_correctly(self):
class Net(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear_1_with_funky_name = torch.nn.Linear(5, 10)
self.linear_2 = torch.nn.Linear(10, 5)
self.conv = torch.nn.Conv1d(5, 5, 5)
def forward(self, inputs):
pass
# Make sure we handle regexes properly
json_params = """{"initializer": {"regexes": [
["conv", {"type": "constant", "val": 5}],
["funky_na.*bi", {"type": "constant", "val": 7}]
]}}
"""
params = Params(json.loads(_jsonnet.evaluate_snippet("", json_params)))
initializers = InitializerApplicator.from_params(params=params["initializer"])
model = Net()
initializers(model)
for parameter in model.conv.parameters():
assert torch.equal(parameter.data, torch.ones(parameter.size()) * 5)
parameter = model.linear_1_with_funky_name.bias
assert torch.equal(parameter.data, torch.ones(parameter.size()) * 7)
示例8: test_regex_match_prevention_prevents_and_overrides
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_regex_match_prevention_prevents_and_overrides(self):
class Net(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear_1 = torch.nn.Linear(5, 10)
self.linear_2 = torch.nn.Linear(10, 5)
# typical actual usage: modules loaded from allenlp.model.load(..)
self.linear_3_transfer = torch.nn.Linear(5, 10)
self.linear_4_transfer = torch.nn.Linear(10, 5)
self.pretrained_conv = torch.nn.Conv1d(5, 5, 5)
def forward(self, inputs):
pass
json_params = """{"initializer": {
"regexes": [
[".*linear.*", {"type": "constant", "val": 10}],
[".*conv.*", {"type": "constant", "val": 10}]
],
"prevent_regexes": [".*_transfer.*", ".*pretrained.*"]
}}
"""
params = Params(json.loads(_jsonnet.evaluate_snippet("", json_params)))
initializers = InitializerApplicator.from_params(params=params["initializer"])
model = Net()
initializers(model)
for module in [model.linear_1, model.linear_2]:
for parameter in module.parameters():
assert torch.equal(parameter.data, torch.ones(parameter.size()) * 10)
transfered_modules = [
model.linear_3_transfer,
model.linear_4_transfer,
model.pretrained_conv,
]
for module in transfered_modules:
for parameter in module.parameters():
assert not torch.equal(parameter.data, torch.ones(parameter.size()) * 10)
示例9: test_from_params
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_from_params(self):
my_class = MyClass.from_params(Params({"my_int": 10}), my_bool=True)
assert isinstance(my_class, MyClass)
assert my_class.my_int == 10
assert my_class.my_bool
示例10: test_no_constructor
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_no_constructor(self):
params = Params({"type": "just_spaces"})
Tokenizer.from_params(params)
示例11: test_union
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_union(self):
class A(FromParams):
def __init__(self, a: Union[int, List[int]]) -> None:
self.a = a
class B(FromParams):
def __init__(self, b: Union[A, List[A]]) -> None:
# Really you would want to be sure that `self.b` has a consistent type, but for
# this test we'll ignore that.
self.b = b
params = Params({"a": 3})
a = A.from_params(params)
assert a.a == 3
params = Params({"a": [3, 4, 5]})
a = A.from_params(params)
assert a.a == [3, 4, 5]
params = Params({"b": {"a": 3}})
b = B.from_params(params)
assert isinstance(b.b, A)
assert b.b.a == 3
params = Params({"b": [{"a": 3}, {"a": [4, 5]}]})
b = B.from_params(params)
assert isinstance(b.b, list)
assert b.b[0].a == 3
assert b.b[1].a == [4, 5]
示例12: test_crazy_nested_union
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_crazy_nested_union(self):
class A(FromParams):
def __init__(self, a: Union[int, List[int]]) -> None:
self.a = a
class B(FromParams):
def __init__(self, b: Union[A, List[A]]) -> None:
# Really you would want to be sure that `self.b` has a consistent type, but for
# this test we'll ignore that.
self.b = b
class C(FromParams):
def __init__(self, c: Union[A, B, Dict[str, A]]) -> None:
# Really you would want to be sure that `self.c` has a consistent type, but for
# this test we'll ignore that.
self.c = c
# This is a contrived, ugly example (why would you want to duplicate names in a nested
# structure like this??), but it demonstrates a potential bug when dealing with mutatable
# parameters. If you're not careful about keeping the parameters un-mutated in two
# separate places, you'll end up with a B, or with a dict that's missing the 'b' key.
params = Params({"c": {"a": {"a": 3}, "b": {"a": [4, 5]}}})
c = C.from_params(params)
assert isinstance(c.c, dict)
assert c.c["a"].a == 3
assert c.c["b"].a == [4, 5]
示例13: test_dict
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_dict(self):
from allennlp.common.registrable import Registrable
class A(Registrable):
pass
@A.register("b")
class B(A):
def __init__(self, size: int) -> None:
self.size = size
class C(Registrable):
pass
@C.register("d")
class D(C):
def __init__(self, items: Dict[str, A]) -> None:
self.items = items
params = Params(
{
"type": "d",
"items": {"first": {"type": "b", "size": 1}, "second": {"type": "b", "size": 2}},
}
)
d = C.from_params(params)
assert isinstance(d.items, dict)
assert len(d.items) == 2
assert all(isinstance(key, str) for key in d.items.keys())
assert all(isinstance(value, B) for value in d.items.values())
assert d.items["first"].size == 1
assert d.items["second"].size == 2
示例14: test_dict_not_params
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_dict_not_params(self):
class A(FromParams):
def __init__(self, counts: Dict[str, int]) -> None:
self.counts = counts
params = Params({"counts": {"a": 10, "b": 20}})
a = A.from_params(params)
assert isinstance(a.counts, dict)
assert not isinstance(a.counts, Params)
示例15: test_list
# 需要导入模块: from allennlp.nn import InitializerApplicator [as 别名]
# 或者: from allennlp.nn.InitializerApplicator import from_params [as 别名]
def test_list(self):
from allennlp.common.registrable import Registrable
class A(Registrable):
pass
@A.register("b")
class B(A):
def __init__(self, size: int) -> None:
self.size = size
class C(Registrable):
pass
@C.register("d")
class D(C):
def __init__(self, items: List[A]) -> None:
self.items = items
params = Params(
{"type": "d", "items": [{"type": "b", "size": 1}, {"type": "b", "size": 2}]}
)
d = C.from_params(params)
assert isinstance(d.items, list)
assert len(d.items) == 2
assert all(isinstance(item, B) for item in d.items)
assert d.items[0].size == 1
assert d.items[1].size == 2