Exemplo n.º 1
0
def _create_initializer(init_function_name, model_metadata_dict):
    """Creates initialization function.

    :param init_function_name: See documentation at top of file.
    :param model_metadata_dict: Dictionary returned by
        `cnn.read_model_metadata`.
    :return: init_function: Function (see below).
    """

    backwards_opt.check_init_function(init_function_name)
    training_option_dict = model_metadata_dict[cnn.TRAINING_OPTION_DICT_KEY]

    used_minmax_norm = (
        training_option_dict[trainval_io.NORMALIZATION_TYPE_KEY] ==
        dl_utils.MINMAX_NORMALIZATION_TYPE_STRING)

    if init_function_name == backwards_opt.CONSTANT_INIT_FUNCTION_NAME:
        if used_minmax_norm:
            return backwards_opt.create_constant_initializer(
                (training_option_dict[trainval_io.MAX_NORMALIZED_VALUE_KEY] -
                 training_option_dict[trainval_io.MIN_NORMALIZED_VALUE_KEY]) /
                2)

        return backwards_opt.create_constant_initializer(0.)

    if init_function_name == backwards_opt.UNIFORM_INIT_FUNCTION_NAME:
        if used_minmax_norm:
            return backwards_opt.create_uniform_random_initializer(
                min_value=training_option_dict[
                    trainval_io.MIN_NORMALIZED_VALUE_KEY],
                max_value=training_option_dict[
                    trainval_io.MAX_NORMALIZED_VALUE_KEY])

        return backwards_opt.create_uniform_random_initializer(min_value=-3.,
                                                               max_value=3.)

    if init_function_name == backwards_opt.GAUSSIAN_INIT_FUNCTION_NAME:
        if used_minmax_norm:
            return backwards_opt.create_gaussian_initializer(
                mean=(
                    training_option_dict[trainval_io.MAX_NORMALIZED_VALUE_KEY]
                    -
                    training_option_dict[trainval_io.MIN_NORMALIZED_VALUE_KEY])
                / 2,
                standard_deviation=(
                    training_option_dict[trainval_io.MAX_NORMALIZED_VALUE_KEY]
                    -
                    training_option_dict[trainval_io.MIN_NORMALIZED_VALUE_KEY])
                / 6)

        return backwards_opt.create_gaussian_initializer(mean=0.,
                                                         standard_deviation=1.)

    return backwards_opt.create_climo_initializer(
        model_metadata_dict=model_metadata_dict)
Exemplo n.º 2
0
    def test_create_constant_initializer_5d(self):
        """Ensures correct output from create_constant_initializer.

        In this case, the desired matrix is 5-D.
        """

        this_init_function = backwards_opt.create_constant_initializer(
            constant_value=CONSTANT_VALUE)

        this_matrix = this_init_function(ARRAY_DIMENSIONS_5D)
        self.assertTrue(
            numpy.allclose(this_matrix, CONSTANT_MATRIX_5D, atol=TOLERANCE))
def _create_initializer(init_function_name):
    """Creates initializer function.

    :param init_function_name: See documentation at top of file.
    :return: init_function: Initializer function.
    """

    if init_function_name == backwards_opt.CONSTANT_INIT_FUNCTION_NAME:
        return backwards_opt.create_constant_initializer(0.)

    if init_function_name == backwards_opt.UNIFORM_INIT_FUNCTION_NAME:
        return backwards_opt.create_uniform_random_initializer(min_value=-1.,
                                                               max_value=1.)

    return backwards_opt.create_gaussian_initializer(mean=0.,
                                                     standard_deviation=1.)