Пример #1
0
    def __init__(
        self,
        prev_layer,
        model_fn,
        layer_args=None,
        name='estimator_layer',
    ):
        super(EstimatorLayer, self).__init__(prev_layer=prev_layer,
                                             layer_args=layer_args,
                                             name=name)

        logging.info("EstimatorLayer %s: %s" % (self.name, model_fn))

        if model_fn is None:
            raise ValueError('model fn is None')

        logging.warning(
            "This API will be removed, please use LambdaLayer instead.")

        with tf.variable_scope(name) as vs:
            self.outputs = model_fn(self.inputs, **self.layer_args)
            variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES,
                                          scope=vs.name)

        self._add_layers(self.outputs)
        self._add_params(variables)
Пример #2
0
    def __init__(
        self,
        prev_layer,
        keras_layer,
        keras_args=None,
        name='keras_layer',
    ):

        super(KerasLayer, self).__init__(prev_layer=prev_layer, name=name)

        logging.info("KerasLayer %s: %s" % (name, keras_layer))

        self.inputs = prev_layer.outputs

        if prev_layer is None:
            raise ValueError("layer is None")
        if keras_args is None:
            keras_args = {}

        logging.warning(
            "This API will be removed, please use LambdaLayer instead.")

        with tf.variable_scope(name) as vs:
            self.outputs = keras_layer(self.inputs, **keras_args)
            variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES,
                                          scope=vs.name)

        self.all_layers.append(self.outputs)
        self.all_params.extend(variables)
Пример #3
0
    def __init__(
            self,
            prev_layer,
            model_fn,
            args=None,
            name='estimator_layer',
    ):
        super(EstimatorLayer, self).__init__(prev_layer=prev_layer, name=name)
        logging.info("EstimatorLayer %s: %s" % (name, model_fn))

        self.inputs = prev_layer.outputs

        if model_fn is None:
            raise ValueError('model fn is None')
        if args is None:
            args = {}

        logging.warning("This API will be removed, please use LambdaLayer instead.")

        with tf.variable_scope(name) as vs:
            self.outputs = model_fn(self.inputs, **args)
            variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name)
        # self.all_layers = list(layer.all_layers)
        # self.all_params = list(layer.all_params)
        # self.all_drop = dict(layer.all_drop)
        self.all_layers.append(self.outputs)
        self.all_params.extend(variables)
Пример #4
0
def rename_kwargs(kwargs, aliases, end_support_version, func_name):

    for alias, new in aliases.items():

        if alias in kwargs:

            if new in kwargs:
                raise TypeError('{}() received both {} and {}'.format(func_name, alias, new))

            warnings.warn('{}() - {} is deprecated; use {}'.format(func_name, alias, new), DeprecationWarning)
            logging.warning(
                "DeprecationWarning: {}(): "
                "`{}` argument is deprecated and will be removed in version {}, "
                "please change for `{}.`".format(func_name, alias, end_support_version, new)
            )
            kwargs[new] = kwargs.pop(alias)
Пример #5
0
    def __init__(
            self,
            prev_layer,
            keras_layer,
            keras_args=None,
            name='keras_layer',
    ):

        super(KerasLayer, self).__init__(prev_layer=prev_layer, keras_args=keras_args, name=name)

        logging.info("KerasLayer %s: %s" % (self.name, keras_layer))

        logging.warning("This API will be removed, please use LambdaLayer instead.")

        with tf.variable_scope(name) as vs:
            self.outputs = keras_layer(self.inputs, **self.keras_args)
            variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name)

        self._add_layers(self.outputs)
        self._add_params(variables)
Пример #6
0
    def deprecated_wrapper(wrapped, instance, args, kwargs):

        _validate_deprecation_args(date, instructions)

        if _PRINT_DEPRECATION_WARNINGS:

            class_or_func_name = decorator_utils.get_qualified_name(wrapped)

            if class_or_func_name not in _PRINTED_WARNING:
                if warn_once:
                    _PRINTED_WARNING[class_or_func_name] = True

                logging.warning(
                    'From %s: %s (from %s) is deprecated and will be removed %s.\n'
                    'Instructions for updating: %s\n' % (
                        _call_location(), class_or_func_name, wrapped.__module__, 'in a future version'
                        if date is None else ('after %s' % date), instructions
                    )
                )

        return wrapped(*args, **kwargs)
Пример #7
0
def set_name_reuse(enable=True):
    logging.warning(
        'this method is DEPRECATED and has no effect, please remove it from your code.'
    )
Пример #8
0
def clear_layers_name():
    logging.warning(
        'this method is DEPRECATED and has no effect, please remove it from your code.'
    )
Пример #9
0
    def __init__(
        self,
        prev_layer,
        n_filter=32,
        filter_size=(3, 3),
        strides=(1, 1),
        act=None,
        padding='SAME',
        use_gemm=False,
        W_init=tf.truncated_normal_initializer(stddev=0.02),
        b_init=tf.constant_initializer(value=0.0),
        W_init_args=None,
        b_init_args=None,
        use_cudnn_on_gpu=None,
        data_format=None,
        # act=None,
        # shape=(5, 5, 1, 100),
        # strides=(1, 1, 1, 1),
        # padding='SAME',
        # W_init=tf.truncated_normal_initializer(stddev=0.02),
        # b_init=tf.constant_initializer(value=0.0),
        # W_init_args=None,
        # b_init_args=None,
        # use_cudnn_on_gpu=None,
        # data_format=None,
        name='ternary_cnn2d',
    ):
        super(TernaryConv2d, self).__init__(prev_layer=prev_layer,
                                            act=act,
                                            W_init_args=W_init_args,
                                            b_init_args=b_init_args,
                                            name=name)

        logging.info(
            "TernaryConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s"
            %
            (name, n_filter, str(filter_size), str(strides), padding,
             self.act.__name__ if self.act is not None else '- No Activation'))

        if len(strides) != 2:
            raise ValueError("len(strides) should be 2.")

        if use_gemm:
            raise Exception(
                "TODO. The current version use tf.matmul for inferencing.")

        try:
            pre_channel = int(prev_layer.outputs.get_shape()[-1])
        except Exception:  # if pre_channel is ?, it happens when using Spatial Transformer Net
            pre_channel = 1
            logging.warning("unknow input channels, set to 1")

        shape = (filter_size[0], filter_size[1], pre_channel, n_filter)
        strides = (1, strides[0], strides[1], 1)

        with tf.variable_scope(name):

            W = tf.get_variable(name='W_conv2d',
                                shape=shape,
                                initializer=W_init,
                                dtype=LayersConfig.tf_dtype,
                                **self.W_init_args)

            alpha = _compute_alpha(W)

            W = _ternary_operation(W)
            W = tf.multiply(alpha, W)

            self.outputs = tf.nn.conv2d(self.inputs,
                                        W,
                                        strides=strides,
                                        padding=padding,
                                        use_cudnn_on_gpu=use_cudnn_on_gpu,
                                        data_format=data_format)

            if b_init:

                b = tf.get_variable(name='b_conv2d',
                                    shape=(shape[-1]),
                                    initializer=b_init,
                                    dtype=LayersConfig.tf_dtype,
                                    **self.b_init_args)

                self.outputs = tf.nn.bias_add(self.outputs, b, name='bias_add')

            self.outputs = self._apply_activation(self.outputs)

        self._add_layers(self.outputs)

        if b_init:
            self._add_params([W, b])
        else:
            self._add_params(W)
Пример #10
0
def moses_multi_bleu(hypotheses, references, lowercase=False):
    """Calculate the bleu score for hypotheses and references
    using the MOSES ulti-bleu.perl script.

    Parameters
    ------------
    hypotheses : numpy.array.string
        A numpy array of strings where each string is a single example.
    references : numpy.array.string
        A numpy array of strings where each string is a single example.
    lowercase : boolean
        If True, pass the "-lc" flag to the multi-bleu script

    Examples
    ---------
    >>> hypotheses = ["a bird is flying on the sky"]
    >>> references = ["two birds are flying on the sky", "a bird is on the top of the tree", "an airplane is on the sky",]
    >>> score = tl.nlp.moses_multi_bleu(hypotheses, references)

    Returns
    --------
    float
        The BLEU score

    References
    ----------
    - `Google/seq2seq/metric/bleu <https://github.com/google/seq2seq>`__

    """
    if np.size(hypotheses) == 0:
        return np.float32(0.0)

    # Get MOSES multi-bleu script
    try:
        multi_bleu_path, _ = urllib.request.urlretrieve(
            "https://raw.githubusercontent.com/moses-smt/mosesdecoder/"
            "master/scripts/generic/multi-bleu.perl")
        os.chmod(multi_bleu_path, 0o755)
    except Exception:  # pylint: disable=W0702
        logging.info("Unable to fetch multi-bleu.perl script, using local.")
        metrics_dir = os.path.dirname(os.path.realpath(__file__))
        bin_dir = os.path.abspath(os.path.join(metrics_dir, "..", "..", "bin"))
        multi_bleu_path = os.path.join(bin_dir, "tools/multi-bleu.perl")

    # Dump hypotheses and references to tempfiles
    hypothesis_file = tempfile.NamedTemporaryFile()
    hypothesis_file.write("\n".join(hypotheses).encode("utf-8"))
    hypothesis_file.write(b"\n")
    hypothesis_file.flush()
    reference_file = tempfile.NamedTemporaryFile()
    reference_file.write("\n".join(references).encode("utf-8"))
    reference_file.write(b"\n")
    reference_file.flush()

    # Calculate BLEU using multi-bleu script
    with open(hypothesis_file.name, "r") as read_pred:
        bleu_cmd = [multi_bleu_path]
        if lowercase:
            bleu_cmd += ["-lc"]
        bleu_cmd += [reference_file.name]
        try:
            bleu_out = subprocess.check_output(bleu_cmd,
                                               stdin=read_pred,
                                               stderr=subprocess.STDOUT)
            bleu_out = bleu_out.decode("utf-8")
            bleu_score = re.search(r"BLEU = (.+?),", bleu_out).group(1)
            bleu_score = float(bleu_score)
        except subprocess.CalledProcessError as error:
            if error.output is not None:
                logging.warning(
                    "multi-bleu.perl script returned non-zero exit code")
                logging.warning(error.output)
            bleu_score = np.float32(0.0)

    # Close temp files
    hypothesis_file.close()
    reference_file.close()

    return np.float32(bleu_score)