Exemple #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)
Exemple #2
0
    def test_create_climo_initializer_2d(self):
        """Ensures correct output from create_climo_initializer.

        In this case, the desired matrix is 2-D (invalid).
        """

        this_metadata_dict = {
            cnn.TRAINING_OPTION_DICT_KEY: TRAINING_OPTION_DICT_3D,
            cnn.CONV_2D3D_KEY: False
        }

        this_init_function = backwards_opt.create_climo_initializer(
            model_metadata_dict=this_metadata_dict,
            test_mode=True,
            radar_normalization_table=RADAR_NORMALIZATION_TABLE,
            sounding_normalization_table=SOUNDING_NORMALIZATION_TABLE)

        this_matrix = this_init_function(ARRAY_DIMENSIONS_2D)
        self.assertTrue(this_matrix is None)
Exemple #3
0
    def test_create_climo_initializer_5d(self):
        """Ensures correct output from create_climo_initializer.

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

        this_metadata_dict = {
            cnn.TRAINING_OPTION_DICT_KEY: TRAINING_OPTION_DICT_3D,
            cnn.CONV_2D3D_KEY: False
        }

        this_init_function = backwards_opt.create_climo_initializer(
            model_metadata_dict=this_metadata_dict,
            test_mode=True,
            radar_normalization_table=RADAR_NORMALIZATION_TABLE,
            sounding_normalization_table=SOUNDING_NORMALIZATION_TABLE)

        this_matrix = this_init_function(RADAR_DIMENSIONS_5D)
        self.assertTrue(
            numpy.allclose(this_matrix, INIT_RADAR_MATRIX_5D, atol=TOLERANCE))