ludwig-ai--ludwig
593b94c120
pytest / Unit Tests (push) Has been cancelled
pytest / Integration (integration_tests_a) (push) Has been cancelled
pytest / Integration (integration_tests_b) (push) Has been cancelled
pytest / Integration (integration_tests_c) (push) Has been cancelled
pytest / Integration (integration_tests_d) (push) Has been cancelled
pytest / Integration (integration_tests_e) (push) Has been cancelled
pytest / Integration (integration_tests_f) (push) Has been cancelled
pytest / Integration (integration_tests_g) (push) Has been cancelled
pytest / Integration (integration_tests_h) (push) Has been cancelled
pytest / Integration (integration_tests_i) (push) Has been cancelled
pytest / Integration (integration_tests_j) (push) Has been cancelled
pytest / Distributed (distributed_a) (push) Has been cancelled
pytest / Distributed (distributed_b) (push) Has been cancelled
pytest / Distributed (distributed_c) (push) Has been cancelled
pytest / Distributed (distributed_d) (push) Has been cancelled
pytest / Distributed (distributed_e) (push) Has been cancelled
pytest / Distributed (distributed_f) (push) Has been cancelled
pytest / Minimal Install (push) Has been cancelled
pytest / Event File (push) Has been cancelled
pytest (slow) / py-slow (push) Has been cancelled
Publish JSON Schema / publish-schema (push) Has been cancelled
46 行
1.9 KiB
Python
46 行
1.9 KiB
Python
# Copyright (c) 2023 Predibase, Inc., 2019 Uber Technologies, Inc.
|
|
#
|
|
# 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.
|
|
# ==============================================================================
|
|
|
|
import torch
|
|
|
|
from ludwig.constants import TYPE
|
|
from ludwig.utils.misc_utils import get_from_registry
|
|
from ludwig.utils.torch_utils import initializer_registry
|
|
|
|
|
|
def _create_and_init(init_fn, init_kwargs, *args, **kwargs):
|
|
t = torch.empty(*args, **kwargs)
|
|
init_fn(t, **init_kwargs)
|
|
return t
|
|
|
|
|
|
def get_initializer(parameters):
|
|
if parameters is None:
|
|
return lambda *args, **kwargs: _create_and_init(initializer_registry[parameters], {}, *args, **kwargs)
|
|
elif isinstance(parameters, str):
|
|
initializer_fun = get_from_registry(parameters, initializer_registry)
|
|
return lambda *args, **kwargs: _create_and_init(initializer_fun, {}, *args, **kwargs)
|
|
elif isinstance(parameters, dict):
|
|
initializer_fun = get_from_registry(parameters[TYPE], initializer_registry)
|
|
init_kwargs = parameters.copy()
|
|
del init_kwargs[TYPE]
|
|
return lambda *args, **kwargs: _create_and_init(initializer_fun, init_kwargs, *args, **kwargs)
|
|
else:
|
|
raise ValueError(
|
|
f"Initializers parameters should be either strings or dictionaries, "
|
|
f"but the provided parameters are a {type(parameters)}. "
|
|
f"Parameters values: {parameters}"
|
|
)
|